diff --git a/installer/opengnsys_git_installer.py b/installer/opengnsys_git_installer.py index 62ec7a9..9b4339c 100755 --- a/installer/opengnsys_git_installer.py +++ b/installer/opengnsys_git_installer.py @@ -248,10 +248,6 @@ class OpengnsysGitInstaller: """Ignorar requisito de clave de ssh para el instalador""" self.ignoresshkey = value - def set_usesshkey(self, value): - """Usar clave de ssh especificada""" - self.usesshkey = value - def set_basepath(self, value): """Establece ruta base de OpenGnsys Valor por defecto: /opt/opengnsys @@ -478,38 +474,33 @@ class OpengnsysGitInstaller: self.__logger.debug("Using provided initrd file %s", initrd_file) client_initrd_path = initrd_file - if self.usesshkey: - with open(self.usesshkey, 'r') as f: - public_key = f.read().strip() + self.__logger.debug("Extracting key from %s", client_initrd_path) + if os.path.isfile(client_initrd_path): + #os.makedirs(temp_dir, exist_ok=True) + #os.chdir(self.temp_dir.name) + self.__logger.debug("Uncompressing %s", client_initrd_path) + public_key = None + with libarchive.file_reader(client_initrd_path) as initrd: + for file in initrd: + self.__logger.debug("File: %s", file) + + pathname = file.pathname; + if pathname.startswith("./"): + pathname = pathname[2:] + + if pathname in self.key_paths_dict: + self.__logger.info("Found key %s, extracting", pathname) + + data = bytearray() + for block in file.get_blocks(): + data = data + block + public_key = data.decode('utf-8').strip() + + break else: - self.__logger.debug("Extracting key from %s", client_initrd_path) - - if os.path.isfile(client_initrd_path): - #os.makedirs(temp_dir, exist_ok=True) - #os.chdir(self.temp_dir.name) - self.__logger.debug("Uncompressing %s", client_initrd_path) - public_key = None - with libarchive.file_reader(client_initrd_path) as initrd: - for file in initrd: - self.__logger.debug("File: %s", file) - - pathname = file.pathname; - if pathname.startswith("./"): - pathname = pathname[2:] - - if pathname in self.key_paths_dict: - self.__logger.info("Found key %s, extracting", pathname) - - data = bytearray() - for block in file.get_blocks(): - data = data + block - public_key = data.decode('utf-8').strip() - - break - else: - print(f"Failed to find initrd at {client_initrd_path}") - exit(2) + print(f"Failed to find initrd at {client_initrd_path}") + exit(2) if not public_key: self.__logger.warning("Failed to find a SSH key") @@ -961,7 +952,7 @@ if __name__ == '__main__': parser.add_argument('--testmode', action='store_true', help="Modo de prueba") parser.add_argument('--ignoresshkey', action='store_true', help="Ignorar clave de SSH") - parser.add_argument('--usesshkey', type=str, help="Usar clave SSH especificada") + parser.add_argument('--use-ssh-key', metavar="FILE", type=str, help="Add the SSH key from the specified file") parser.add_argument('--test-createuser', action='store_true') parser.add_argument('--extract-ssh-key', action='store_true', help="Extract SSH key from oglive squashfs") parser.add_argument('--set-ssh-key', action='store_true', help="Read SSH key from oglive squashfs and set it in Forgejo") @@ -993,7 +984,6 @@ if __name__ == '__main__': installer = OpengnsysGitInstaller() installer.set_testmode(args.testmode) installer.set_ignoresshkey(args.ignoresshkey) - installer.set_usesshkey(args.usesshkey) logger.debug("Inicio de instalación") @@ -1012,6 +1002,13 @@ if __name__ == '__main__': print(f"{key}") elif args.set_ssh_key: installer.add_ssh_key_from_squashfs(oglive_num=args.oglive, squashfs_file=args.squashfs_file, oglive_file = args.oglive_file or args.oglive_url) + elif args.use_ssh_key: + with open(args.use_ssh_key, 'r', encoding='utf-8') as ssh_key_file: + ssh_key_data = ssh_key_file.read().strip() + (keytype, keydata, description) = ssh_key_data.split(" ", 2) + + installer.add_forgejo_sshkey(f"{keytype} {keydata}", description) + elif args.set_ssh_key_in_initrd: installer.set_ssh_key_in_initrd() elif args.get_image_paths: