fixed get ipv4 (w/ netmask) and setup alpine repo and fixed mutliples commands
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Mathieu Broillet 2023-06-20 15:17:17 +02:00
parent a5b09f45d5
commit 3b95f5551d
No known key found for this signature in database
GPG Key ID: 7D4F25BC50A0AA32
2 changed files with 28 additions and 19 deletions

View File

@ -0,0 +1,5 @@
cat <<EOF >/etc/apk/repositories
https://dl-cdn.alpinelinux.org/alpine/edge/main
https://dl-cdn.alpinelinux.org/alpine/edge/community
https://dl-cdn.alpinelinux.org/alpine/edge/testing
EOF

View File

@ -29,6 +29,7 @@ class LXC(LinuxMachine):
self.network = network self.network = network
self.bridge = network["bridge"] self.bridge = network["bridge"]
self.ipv4 = network["ipv4"] self.ipv4 = network["ipv4"]
self.ipv4_netmask = 0
self.ipv6 = network["ipv6"] self.ipv6 = network["ipv6"]
self.mac = network["mac"] self.mac = network["mac"]
self.gateway4 = network["gateway4"] self.gateway4 = network["gateway4"]
@ -179,21 +180,23 @@ class LXC(LinuxMachine):
if self.ipv4 == "dhcp" or self.ipv4 == "auto": if self.ipv4 == "dhcp" or self.ipv4 == "auto":
if self.is_running(): if self.is_running():
if self.has_program("ip", use_ssh=use_ssh): if self.has_program("ip", use_ssh=use_ssh):
if netmask: self.ipv4 = self.run_command(
ip = self.run_command(
"""ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 """,
use_ssh=use_ssh)
return ip
ip = self.run_command(
"""ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'""", """ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'""",
use_ssh=use_ssh) use_ssh=use_ssh)
return ip
elif self.has_program("ifconfig", use_ssh=use_ssh): self.ipv4_netmask = self.run_command(
return self.run_command(command="ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'", """ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 """,
use_ssh=use_ssh) use_ssh=use_ssh)
# elif self.has_program("ifconfig", use_ssh=use_ssh):
# return self.run_command(command="ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'",
# use_ssh=use_ssh)
else:
return self.ipv4
if netmask:
return self.ipv4_netmask
else:
return self.ipv4 return self.ipv4
def get_ipv6(self): def get_ipv6(self):
@ -303,7 +306,6 @@ class LXC(LinuxMachine):
Start LXC Start LXC
""" """
self.pve.start_lxc(self.lxc_id) self.pve.start_lxc(self.lxc_id)
self.ipv4 = self.get_ipv4()
def stop(self): def stop(self):
""" """
@ -335,10 +337,16 @@ class LXC(LinuxMachine):
if not self.has_program("bash", use_ssh=False): if not self.has_program("bash", use_ssh=False):
self.install_package("bash", use_ssh=False) self.install_package("bash", use_ssh=False)
# Install and configure OpenSSH on LXC
logging.info("Setting up SSH for LXC") logging.info("Setting up SSH for LXC")
lxc_utils.run_protected_script(lxc=self, script_path="protected/scripts/install-config-ssh.sh") lxc_utils.run_protected_script(lxc=self, script_path="protected/scripts/install-config-ssh.sh")
self.pve.run_command(f"ssh-keygen -f '/root/.ssh/known_hosts' -R {self.get_ipv4(use_ssh=False)}") self.pve.run_command(f"ssh-keygen -f '/root/.ssh/known_hosts' -R {self.get_ipv4(use_ssh=False)}")
# Add main, community and testing repo for Alpine
if "alpine" in self.os_name:
logging.info("Setting up Alpine repositories for LXC")
lxc_utils.run_protected_script(lxc=self, script_path="protected/scripts/setup-repo-alpine.sh")
def run_creation(self): def run_creation(self):
""" """
Run the creations checks and steps Run the creations checks and steps
@ -387,11 +395,7 @@ class LXC(LinuxMachine):
# logging.debug(f"Running command {command} on LXC {self.lxc_id}") # logging.debug(f"Running command {command} on LXC {self.lxc_id}")
if type(command) == list: if type(command) == list:
for cmd in command: command = ' && '.join(command)
self.run_command(command=cmd, return_status_code=return_status_code,
exception_on_exit=exception_on_exit,
exception_on_empty_stdout=exception_on_empty_stdout,
working_directory=working_directory, use_ssh=use_ssh)
if working_directory: if working_directory:
command = f"cd {working_directory} && {command}" command = f"cd {working_directory} && {command}"
@ -399,7 +403,7 @@ class LXC(LinuxMachine):
# Using pct exec works every time but is 8x slower than using ssh # Using pct exec works every time but is 8x slower than using ssh
if use_ssh: if use_ssh:
return self.pve.run_command( return self.pve.run_command(
command=f"ssh -o StrictHostKeyChecking=no root@{self.get_ipv4()} -- {command}", command=f"ssh -o StrictHostKeyChecking=no root@{self.get_ipv4()} -- \"{command}\"",
return_status_code=return_status_code, return_status_code=return_status_code,
exception_on_exit=exception_on_exit, exception_on_exit=exception_on_exit,
exception_on_empty_stdout=exception_on_empty_stdout) exception_on_empty_stdout=exception_on_empty_stdout)