source: client/shared/etc/preinit/mountrepo.py @ 1b42de9

ogClonningEnginetest-python-scriptsticket-693ticket-700
Last change on this file since 1b42de9 was 5b1449d, checked in by Antonio Emmanuel Guerrero Silva <aguerrero@…>, 7 months ago

refs #700 shared files convert to Python3

  • Property mode set to 100644
File size: 2.2 KB
Line 
1#!/usr/bin/env python3
2
3import os
4import subprocess
5
6#/**
7# @file    mountrepo.py
8# @brief   Script para montar el repositorio de datos remoto.
9#*/
10
11OGIMG = os.getenv('OGIMG', '/opt/opengnsys/images')
12ROOTREPO = os.getenv('ROOTREPO', os.getenv('ROOTSERVER'))
13ogactiveadmin = os.getenv('ogactiveadmin')
14ogprotocol = os.getenv('ogprotocol', 'smb')
15ogunit = os.getenv('ogunit', '')
16ogstatus = os.getenv('ogstatus')
17SERVER = os.getenv('SERVER')
18OGCAC = os.getenv('OGCAC')
19MSG_MOUNTREPO = "Mounting repository using protocol: {} in mode: {}"
20
21def 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
38def 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
48def 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
61if __name__ == "__main__":
62    mount_repo()
Note: See TracBrowser for help on using the repository browser.