Get keys from squashfs instead of initrd to work with current oglive packaging

fixes2
Vadim vtroshchinskiy 2024-11-26 16:04:05 +01:00
parent 47ccba0adf
commit dfeccb104d
1 changed files with 74 additions and 9 deletions

View File

@ -294,7 +294,65 @@ class OpengnsysGitInstaller:
raise TimeoutError("Timed out waiting for connection!")
def _extract_ssh_key(self):
def add_ssh_key_from_squashfs(self, oglive_num = None):
if oglive_num is None:
self.__logger.info("Using default oglive")
oglive_num = self.oglive.get_default()
else:
self.__logger.info("Using oglive %i", oglive_num)
oglive_client = self.oglive.get_clients()[str(oglive_num)]
self.__logger.info("Oglive is %s", oglive_client)
keys = installer.extract_ssh_keys(oglive_num = oglive_num)
for k in keys:
timestamp = '{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now())
installer.add_forgejo_sshkey(k, f"Key for {oglive_client} ({timestamp})")
def extract_ssh_keys(self, oglive_num = None):
public_keys = []
squashfs = "ogclient.sqfs"
tftp_dir = os.path.join(self.base_path, "tftpboot")
if oglive_num is None:
self.__logger.info("Reading from default oglive")
oglive_num = self.oglive.get_default()
else:
self.__logger.info("Reading from oglive %i", oglive_num)
oglive_client = self.oglive.get_clients()[str(oglive_num)]
self.__logger.info("Oglive is %s", oglive_client)
client_squashfs_path = os.path.join(tftp_dir, oglive_client, squashfs)
self.__logger.info("Mounting %s", client_squashfs_path)
mount_tempdir = tempfile.TemporaryDirectory()
ssh_keys_dir = os.path.join(mount_tempdir.name, "root", ".ssh")
subprocess.run(["mount", client_squashfs_path, mount_tempdir.name], check=True)
for file in os.listdir(ssh_keys_dir):
full_path = os.path.join(ssh_keys_dir, file)
if file.endswith(".pub"):
self.__logger.info("Found public key: %s", full_path)
with open(full_path, "r", encoding="utf-8") as keyfile:
keydata = keyfile.read().strip()
public_keys = public_keys + [keydata]
subprocess.run(["umount", mount_tempdir.name], check=True)
return public_keys
def _extract_ssh_key_from_initrd(self):
public_key=""
INITRD = "oginitrd.img"
@ -337,7 +395,7 @@ class OpengnsysGitInstaller:
return public_key
def set_ssh_key(self, client_num = None):
def set_ssh_key_in_initrd(self, client_num = None):
INITRD = "oginitrd.img"
tftp_dir = os.path.join(self.base_path, "tftpboot")
@ -658,7 +716,7 @@ class OpengnsysGitInstaller:
token_file.write(token)
ssh_key = self._extract_ssh_key()
ssh_key = self._extract_ssh_key_from_initrd()
self.add_forgejo_sshkey(ssh_key, "Default key")
@ -691,7 +749,7 @@ class OpengnsysGitInstaller:
with open(os.path.join(self.base_path, "etc", "ogGitApiToken.cfg"), "r", encoding='utf-8') as token_file:
token = token_file.read().strip()
self.__logger.info("Adding SSH key to Forgejo: %s", pubkey)
self.__logger.info("Adding SSH key to Forgejo: %s (%s)", pubkey, description)
r = requests.post(
f"http://localhost:{self.forgejo_port}/api/v1/user/keys",
@ -768,8 +826,11 @@ if __name__ == '__main__':
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('--test-createuser', action='store_true')
parser.add_argument('--extract-ssh-key', action='store_true', help="Extract SSH key from oglive")
parser.add_argument('--set-ssh-key', action='store_true', help="Configure SSH key in oglive")
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")
parser.add_argument('--extract-ssh-key-from-initrd', action='store_true', help="Extract SSH key from oglive initrd (obsolete)")
parser.add_argument('--set-ssh-key-in-initrd', action='store_true', help="Configure SSH key in oglive (obsolete)")
parser.add_argument('--oglive', type=int, metavar='NUM', help = "Do SSH key manipulation on this oglive")
parser.add_argument('--quiet', action='store_true', help="Quiet console output")
parser.add_argument("-v", "--verbose", action="store_true", help = "Verbose console output")
@ -799,11 +860,15 @@ if __name__ == '__main__':
elif args.test_createuser:
installer.set_ssh_user_group("oggit2", "oggit2")
elif args.extract_ssh_key:
key = installer._extract_ssh_key()
keys = installer.extract_ssh_keys(oglive_num = args.oglive)
print(f"{keys}")
elif args.extract_ssh_key_from_initrd:
key = installer._extract_ssh_key_from_initrd()
print(f"{key}")
elif args.set_ssh_key:
installer.set_ssh_key()
installer.add_ssh_key_from_squashfs(oglive_num=args.oglive)
elif args.set_ssh_key_in_initrd:
installer.set_ssh_key_in_initrd()
else:
installer.install()
installer.install_forgejo()