Compare commits

...

4 Commits

4 changed files with 42 additions and 26 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

@ -60,6 +60,9 @@ def are_all_conditions_met(lxc: LXC):
for condition_type in step["conditions"]: for condition_type in step["conditions"]:
result = check_conditions(c_type=condition_type, parent=step["conditions"], lxc=lxc, result=result) result = check_conditions(c_type=condition_type, parent=step["conditions"], lxc=lxc, result=result)
if len(result) == 0:
return False
return all(result) return all(result)
@ -152,27 +155,27 @@ def run_steps(lxc: LXC):
case "script": case "script":
lxc_utils.run_script_step_parser(lxc, step) lxc_utils.run_script_step_parser(lxc, step)
case "file_create": case "file_create":
lxc.create_file(step["path"], optional(step["permission"], 644)) lxc.create_file(step["path"], step.get("permission", 644))
case "file_copy": case "file_copy":
lxc.pve.copy_file_to_lxc(lxc, get_path(lxc, step["path"]), step["destination"]) lxc.pve.copy_file_to_lxc(lxc, get_path(lxc, step["path"]), step["destination"])
case "folder": case "folder":
lxc.create_directory(step["path"], optional(step["permission"], 755)) lxc.create_directory(step["path"], step.get("permission", 755))
case "folder_copy": case "folder_copy":
lxc.pve.copy_folder_to_lxc(lxc, get_path(lxc, step["path"]), step["destination"]) lxc.pve.copy_folder_to_lxc(lxc, get_path(lxc, step["path"]), step["destination"])
case "command": case "command":
lxc.run_command(command=step["command"], working_directory=optional(step["working_directory"], None), lxc.run_command(command=step["command"], working_directory=step.get("working_directory"),
return_status_code=True) return_status_code=True)
case "docker": case "docker":
lxc.run_docker_command(step["container"], step["command"]) lxc.run_docker_command(step["container"], step["command"])
case "docker_compose": case "docker_compose":
lxc.run_docker_compose_command(command=step["command"], lxc.run_docker_compose_command(command=step["command"],
working_directory=optional(step["working_directory"], None)) working_directory=step.get("working_directory"))
case "git": case "git":
lxc.run_command(command=f"git clone {step['url']} {step['destination']}", return_status_code=True) lxc.run_command(command=f"git clone {step['url']} {step['destination']}", return_status_code=True)
case "download": case "download":
lxc.download_file(step["url"], step["destination"]) lxc.download_file(step["url"], step["destination"])
case "unzip": case "unzip":
lxc.unzip_file(step["path"], optional(step["destination"], None)) lxc.unzip_file(step["path"], step.get("destination"))
case "install-package": case "install-package":
lxc.install_package(step["package"]) lxc.install_package(step["package"])
case "remove-package": case "remove-package":

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,22 +180,24 @@ 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)
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): 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)

View File

@ -244,9 +244,13 @@ class LinuxMachine():
""" """
if type(package) is list: if type(package) is list:
packages = []
for p in package: for p in package:
self.run_command(f"{utils.get_install_package_command(self.get_os_name())} {package}", packages.append(p)
return_status_code=True, use_ssh=use_ssh)
self.run_command(
f"{utils.get_install_package_command(self.get_os_name())} {' '.join(packages)}",
return_status_code=True)
else: else:
self.run_command(f"{utils.get_install_package_command(self.get_os_name())} {package}", self.run_command(f"{utils.get_install_package_command(self.get_os_name())} {package}",
return_status_code=True, use_ssh=use_ssh) return_status_code=True, use_ssh=use_ssh)