From 3b95f5551da600d02932d4df9ad524bf01acc1fb Mon Sep 17 00:00:00 2001 From: Mathieu Broillet Date: Tue, 20 Jun 2023 15:17:17 +0200 Subject: [PATCH] fixed get ipv4 (w/ netmask) and setup alpine repo and fixed mutliples commands --- .../scripts/setup-repo-alpine.sh | 5 +++ src/lxc/lxc.py | 42 ++++++++++--------- 2 files changed, 28 insertions(+), 19 deletions(-) create mode 100644 protected_resources/scripts/setup-repo-alpine.sh diff --git a/protected_resources/scripts/setup-repo-alpine.sh b/protected_resources/scripts/setup-repo-alpine.sh new file mode 100644 index 0000000..26419e4 --- /dev/null +++ b/protected_resources/scripts/setup-repo-alpine.sh @@ -0,0 +1,5 @@ +cat </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 \ No newline at end of file diff --git a/src/lxc/lxc.py b/src/lxc/lxc.py index 5a9119c..c6ab6eb 100644 --- a/src/lxc/lxc.py +++ b/src/lxc/lxc.py @@ -29,6 +29,7 @@ class LXC(LinuxMachine): self.network = network self.bridge = network["bridge"] self.ipv4 = network["ipv4"] + self.ipv4_netmask = 0 self.ipv6 = network["ipv6"] self.mac = network["mac"] self.gateway4 = network["gateway4"] @@ -179,22 +180,24 @@ class LXC(LinuxMachine): if self.ipv4 == "dhcp" or self.ipv4 == "auto": if self.is_running(): if self.has_program("ip", use_ssh=use_ssh): - if netmask: - 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( + self.ipv4 = self.run_command( """ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'""", use_ssh=use_ssh) - return ip - 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) + self.ipv4_netmask = self.run_command( + """ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 """, + use_ssh=use_ssh) - return self.ipv4 + # 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 def get_ipv6(self): """ @@ -303,7 +306,6 @@ class LXC(LinuxMachine): Start LXC """ self.pve.start_lxc(self.lxc_id) - self.ipv4 = self.get_ipv4() def stop(self): """ @@ -335,10 +337,16 @@ class LXC(LinuxMachine): if not self.has_program("bash", use_ssh=False): self.install_package("bash", use_ssh=False) + # Install and configure OpenSSH on LXC logging.info("Setting up SSH for LXC") 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)}") + # 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): """ Run the creations checks and steps @@ -387,11 +395,7 @@ class LXC(LinuxMachine): # logging.debug(f"Running command {command} on LXC {self.lxc_id}") if type(command) == list: - for cmd in 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) + command = ' && '.join(command) if working_directory: 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 if use_ssh: 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, exception_on_exit=exception_on_exit, exception_on_empty_stdout=exception_on_empty_stdout)