[5b1449d] | 1 | #!/usr/bin/env python3 |
---|
| 2 | |
---|
| 3 | import os |
---|
| 4 | import subprocess |
---|
| 5 | |
---|
| 6 | #/** |
---|
| 7 | # @file mountrepo.py |
---|
| 8 | # @brief Script para montar el repositorio de datos remoto. |
---|
| 9 | #*/ |
---|
| 10 | |
---|
| 11 | OGIMG = os.getenv('OGIMG', '/opt/opengnsys/images') |
---|
| 12 | ROOTREPO = os.getenv('ROOTREPO', os.getenv('ROOTSERVER')) |
---|
| 13 | ogactiveadmin = os.getenv('ogactiveadmin') |
---|
| 14 | ogprotocol = os.getenv('ogprotocol', 'smb') |
---|
| 15 | ogunit = os.getenv('ogunit', '') |
---|
| 16 | ogstatus = os.getenv('ogstatus') |
---|
| 17 | SERVER = os.getenv('SERVER') |
---|
| 18 | OGCAC = os.getenv('OGCAC') |
---|
| 19 | MSG_MOUNTREPO = "Mounting repository using protocol: {} in mode: {}" |
---|
| 20 | |
---|
| 21 | def mount_repo(): |
---|
| 22 | if ogactiveadmin == "true": |
---|
| 23 | os.environ['boot'] = 'admin' # ATENCIÓN: siempre en modo "admin". |
---|
| 24 | subprocess.run(['umount', OGIMG], stderr=subprocess.DEVNULL) |
---|
| 25 | |
---|
| 26 | protocol = ogprotocol |
---|
| 27 | OGUNIT = f"/{ogunit}" if ogunit else "" |
---|
| 28 | print(MSG_MOUNTREPO.format(protocol, 'admin')) |
---|
| 29 | |
---|
| 30 | if protocol == 'nfs': |
---|
| 31 | subprocess.run(['mount.nfs', f'{ROOTREPO}:{OGIMG}{OGUNIT}', OGIMG, '-o', 'rw,nolock']) |
---|
| 32 | elif protocol == 'smb': |
---|
| 33 | PASS = get_password() |
---|
| 34 | subprocess.run(['mount.cifs', f'//{ROOTREPO}/ogimages{OGUNIT}', OGIMG, '-o', f'rw,serverino,acl,username=opengnsys,password={PASS}']) |
---|
| 35 | elif protocol == 'local': |
---|
| 36 | handle_local_mount() |
---|
| 37 | |
---|
| 38 | def get_password(): |
---|
| 39 | try: |
---|
| 40 | with open('/scripts/ogfunctions') as f: |
---|
| 41 | for line in f: |
---|
| 42 | if 'OPTIONS=' in line: |
---|
| 43 | return line.split('pass=')[1].split()[0] |
---|
| 44 | except Exception: |
---|
| 45 | pass |
---|
| 46 | return 'og' |
---|
| 47 | |
---|
| 48 | def handle_local_mount(): |
---|
| 49 | if ogstatus == "offline" or not SERVER: |
---|
| 50 | TYPE = subprocess.getoutput("blkid | grep REPO | awk -F'TYPE=' '{print $2}' | tr -d '\"'") |
---|
| 51 | if not TYPE: |
---|
| 52 | if os.path.isdir(f'{OGCAC}/{OGIMG}'): |
---|
| 53 | subprocess.run(['mount', '--bind', f'{OGCAC}/{OGIMG}', OGIMG]) |
---|
| 54 | else: |
---|
| 55 | subprocess.run(['mount', '-t', TYPE, 'LABEL=REPO', OGIMG], stderr=subprocess.DEVNULL) |
---|
| 56 | else: |
---|
| 57 | if subprocess.run(['smbclient', '-L', SERVER, '-N'], stderr=subprocess.DEVNULL).returncode == 0: |
---|
| 58 | PASS = get_password() |
---|
| 59 | subprocess.run(['mount.cifs', f'//{ROOTREPO}/ogimages', OGIMG, '-o', f'rw,serverino,acl,username=opengnsys,password={PASS}']) |
---|
| 60 | |
---|
| 61 | if __name__ == "__main__": |
---|
| 62 | mount_repo() |
---|