diff --git a/resources/config.json b/resources/config.json index c0fb520..9d82390 100644 --- a/resources/config.json +++ b/resources/config.json @@ -6,6 +6,5 @@ "local": false }, "settings": { - "setAuthorizedHostsSSH": true } } \ No newline at end of file diff --git a/resources/lxc/100/config.json b/resources/lxc/100/config.json index 11b2e3f..3d3ae43 100644 --- a/resources/lxc/100/config.json +++ b/resources/lxc/100/config.json @@ -37,9 +37,6 @@ { "type": "script", "path": "/global/install-docker.sh" - }, - { - "type": "" } ] }, diff --git a/src/main.py b/src/main.py index f931c27..881a637 100644 --- a/src/main.py +++ b/src/main.py @@ -25,4 +25,4 @@ def run(): logging.info(f"Loading LXC {lxc.lxc_id}") lxc.create() lxc.start() - lxc.check_creation_conditions() + lxc.run_creation() diff --git a/src/utils/lxc_utils.py b/src/utils/lxc_utils.py index 344d6ac..39858e6 100644 --- a/src/utils/lxc_utils.py +++ b/src/utils/lxc_utils.py @@ -234,6 +234,14 @@ class LXC: Get IPv4 :return: ipv4 """ + if self.ipv4 == "dhcp": + if self.is_running(): + if self.has_program("ip"): + return self.run_command( + command="ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print \$2}' | cut -f1 -d'/'") + elif self.has_program("ifconfig"): + return self.run_command(command="ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'") + return self.ipv4 def get_ipv6(self): @@ -367,11 +375,7 @@ class LXC: logging.info(f"Creating LXC {self.lxc_id}") proxmox_utils.run_command_on_pve(command=self.get_pct_command(create=True), warn_exit_status=True) - if proxmox_utils.get_config()['settings']['setAuthorizedHostsSSH']: - pve_public_key = proxmox_utils.get_ssh_public_key() - self.run_command(f"echo '{pve_public_key}' >> /root/.ssh/authorized_keys") - - def creation(self): + def run_creation(self): """ Run the creations checks and steps """ @@ -411,25 +415,25 @@ class LXC: """ return lxc_commands_utils.run_script(self, {"lxc_path": script_path}) - def run_command(self, command, warn_exit_status=False, only_code=False, working_directory=None): + def run_command(self, command, ssh=False, warn_exit_status=False, only_code=False, working_directory=None): """ Run command on LXC :param command: command to run :return: command output """ - logging.debug(f"Running command {command} on LXC {self.lxc_id}") + # logging.debug(f"Running command {command} on LXC {self.lxc_id}") if working_directory: command = f"cd {working_directory} && {command}" - if only_code: + if ssh: + return proxmox_utils.run_command_ssh(command=command, host=self.get_ipv4(), username="root", port=22, + warn_exit_status=warn_exit_status, only_code=only_code) + else: return proxmox_utils.run_command_on_pve(command=f"pct exec {self.lxc_id} -- {command}", warn_exit_status=warn_exit_status, only_code=only_code) - return proxmox_utils.run_command_on_pve(command=f"pct exec {self.lxc_id} -- {command}", - warn_exit_status=warn_exit_status) - def get_pct_command(self, create=True): """ Get pct command to create/edit LXC @@ -450,6 +454,7 @@ class LXC: f"--storage {self.storage} " \ f"--unprivileged {not self.privileged} " \ f"--rootfs volume={self.storage}:{self.disk},size={self.disk} " \ + f"--ssh-public-keys /root/.ssh/id_rsa.pub " \ f"--unprivileged {not self.privileged}" else: # Update command @@ -464,7 +469,6 @@ class LXC: # TODO: add gateway4 # f"ip6={self.ipv6},gw6={self.gateway6},trunks={self.vlan} " \ # TODO - # f"-ssh-public-keys {self.ssh}" # TODO return pct_command def get_tteck_env_variables(self): @@ -492,7 +496,7 @@ class LXC: "NS": "", "MAC": self.mac, "VLAN": self.vlan, - "SSH": self.ssh, + # "SSH": self.ssh, "VERB": "no" } diff --git a/src/utils/proxmox_utils.py b/src/utils/proxmox_utils.py index 0c64076..113880f 100644 --- a/src/utils/proxmox_utils.py +++ b/src/utils/proxmox_utils.py @@ -81,7 +81,7 @@ def run_command_on_pve(command, warn_exit_status=False, only_code=False, local=F # Check if PVE is local or remote if config['pve']['local'] or local: # Run command and return output (not as bytes) - logging.debug(f"Running command on PVE (locally): \n{command}") + logging.debug(f"Running command on PVE (locally): {command}") command = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8") @@ -101,10 +101,10 @@ def run_command_on_pve(command, warn_exit_status=False, only_code=False, local=F port = config['pve']['port'] # Run command on PVE via SSH and return output - logging.debug(f"Running command on PVE (ssh): \n{command}") + logging.debug(f"Running command on PVE (ssh): {command}") # catch errors code - command = subprocess.run(f"ssh {username}@{host} -p {port} \"{command}\"", shell=True, + command = subprocess.run(f'ssh {username}@{host} -p {port} "{command}"', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8") if command.returncode != 0 and warn_exit_status: @@ -121,6 +121,24 @@ def run_command_locally(command, warn_exit_status=False, only_code=False): run_command_on_pve(command=command, warn_exit_status=warn_exit_status, only_code=only_code, local=True) +def run_command_ssh(command, host, username, port=22, warn_exit_status=False, only_code=False): + # Run command on PVE via SSH and return output + logging.debug(f"Running command on host {host} (ssh): {command}") + + # catch errors code + command = subprocess.run(f'ssh {username}@{host} -p {port} "{command}"', shell=True, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8") + + if command.returncode != 0 and warn_exit_status: + logging.error(f"Error while running command on PVE: \n{command.stderr}") + raise Exception(f"Error while running command on PVE: \n{command.stderr}") + + if only_code: + return command.returncode + + return command.stdout.rstrip() + + def get_install_package_command(distribution): """ Get the install package without interaction command based on the distribution.