Compare commits
4 Commits
a659323779
...
3b95f5551d
Author | SHA1 | Date | |
---|---|---|---|
3b95f5551d | |||
a5b09f45d5 | |||
6d3d9b3fbc | |||
4ec9c86b2c |
5
protected_resources/scripts/setup-repo-alpine.sh
Normal file
5
protected_resources/scripts/setup-repo-alpine.sh
Normal 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
|
@ -60,6 +60,9 @@ def are_all_conditions_met(lxc: LXC):
|
||||
for condition_type in step["conditions"]:
|
||||
result = check_conditions(c_type=condition_type, parent=step["conditions"], lxc=lxc, result=result)
|
||||
|
||||
if len(result) == 0:
|
||||
return False
|
||||
|
||||
return all(result)
|
||||
|
||||
|
||||
@ -152,27 +155,27 @@ def run_steps(lxc: LXC):
|
||||
case "script":
|
||||
lxc_utils.run_script_step_parser(lxc, step)
|
||||
case "file_create":
|
||||
lxc.create_file(step["path"], optional(step["permission"], 644))
|
||||
lxc.create_file(step["path"], step.get("permission", 644))
|
||||
case "file_copy":
|
||||
lxc.pve.copy_file_to_lxc(lxc, get_path(lxc, step["path"]), step["destination"])
|
||||
case "folder":
|
||||
lxc.create_directory(step["path"], optional(step["permission"], 755))
|
||||
lxc.create_directory(step["path"], step.get("permission", 755))
|
||||
case "folder_copy":
|
||||
lxc.pve.copy_folder_to_lxc(lxc, get_path(lxc, step["path"]), step["destination"])
|
||||
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)
|
||||
case "docker":
|
||||
lxc.run_docker_command(step["container"], step["command"])
|
||||
case "docker_compose":
|
||||
lxc.run_docker_compose_command(command=step["command"],
|
||||
working_directory=optional(step["working_directory"], None))
|
||||
working_directory=step.get("working_directory"))
|
||||
case "git":
|
||||
lxc.run_command(command=f"git clone {step['url']} {step['destination']}", return_status_code=True)
|
||||
case "download":
|
||||
lxc.download_file(step["url"], step["destination"])
|
||||
case "unzip":
|
||||
lxc.unzip_file(step["path"], optional(step["destination"], None))
|
||||
lxc.unzip_file(step["path"], step.get("destination"))
|
||||
case "install-package":
|
||||
lxc.install_package(step["package"])
|
||||
case "remove-package":
|
||||
|
@ -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,21 +180,23 @@ 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)}'",
|
||||
self.ipv4_netmask = self.run_command(
|
||||
"""ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 """,
|
||||
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
|
||||
|
||||
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)
|
||||
|
@ -244,9 +244,13 @@ class LinuxMachine():
|
||||
"""
|
||||
|
||||
if type(package) is list:
|
||||
packages = []
|
||||
for p in package:
|
||||
self.run_command(f"{utils.get_install_package_command(self.get_os_name())} {package}",
|
||||
return_status_code=True, use_ssh=use_ssh)
|
||||
packages.append(p)
|
||||
|
||||
self.run_command(
|
||||
f"{utils.get_install_package_command(self.get_os_name())} {' '.join(packages)}",
|
||||
return_status_code=True)
|
||||
else:
|
||||
self.run_command(f"{utils.get_install_package_command(self.get_os_name())} {package}",
|
||||
return_status_code=True, use_ssh=use_ssh)
|
||||
|
Loading…
Reference in New Issue
Block a user