linuxmuster/linuxmuster-base7

View on GitHub
sbin/linuxmuster-setup

Summary

Maintainability
Test Coverage
#!/usr/bin/python3
#
# linuxmuster-setup
# thomas@linuxmuster.net
# 20240219
#

import constants
import getopt
import importlib
import os
import sys
from functions import checkFwMajorVer, modIni, printScript, subProc, tee


def usage():
    print('Usage: linuxmuster-setup [options]')
    print(' [options] may be:')
    print(' -n <hostname>,   --servername=<hostname>   : Set server hostname.')
    print(' -d <domainname>, --domainname=<domainname> : Set domainname.')
    print(' -r <dhcprange>,  --dhcprange=<dhcprange>   : Set dhcp range.')
    print(' -a <adminpw>,    --adminpw=<adminpw>       : Set admin password.')
    print(' -e <schoolname>, --schoolname=<schoolname> : Set school name.')
    print(' -l <location>,   --location=<location>     : Set school location.')
    print(' -z <country>,    --country=<country>       : Set school country.')
    print(' -v <state>,      --state=<state>           : Set school state.')
    print(' -c <file>,       --config=<file>           : path to ini file with setup values')
    print(' -u,              --unattended              : unattended mode, do not ask questions')
    print(' -s,              --skip-fw                 : skip firewall setup per ssh')
    print(' -h,              --help                    : print this help')


# get cli args
try:
    opts, args = getopt.getopt(sys.argv[1:], "a:c:d:e:hl:n:r:suv:z:",
                               ["adminpw=", "config=", "domainname=", "schoolname=", "help",
                                "location=", "servername=", "dhcprange=", "skip-fw", "unattended", "state=", "country="])
except getopt.GetoptError as err:
    # print help information and exit:
    print(err)  # will print something like "option -a not recognized"
    usage()
    sys.exit(2)

# default values
unattended = False
skipfw = False
servername = ''
domainname = ''
dhcprange = ''
adminpw = ''
schoolname = ''
location = ''
country = ''
state = ''
cli_customini = ''

# open logfile
global logfile
logfile = constants.SETUPLOG
subProc('touch ' + logfile)
subProc('chmod 600 ' + logfile)
try:
    l = open(logfile, 'w')
    orig_out = sys.stdout
    sys.stdout = tee(sys.stdout, l)
    sys.stderr = tee(sys.stderr, l)
except:
    print('Cannot open logfile ' + logfile + ' !')
    sys.exit()

# evaluate options
for o, a in opts:
    if o in ("-u", "--unattended"):
        unattended = True
    elif o in ("-v", "--state"):
        state = a
    elif o in ("-z", "--country"):
        country = a
    elif o in ("-l", "--location"):
        location = a
    elif o in ("-e", "--schoolname"):
        schoolname = a
    elif o in ("-a", "--adminpw"):
        adminpw = a
    elif o in ("-n", "--servername"):
        servername = a
    elif o in ("-d", "--domainname"):
        domainname = a
    elif o in ("-r", "--dhcprange"):
        dhcprange = a
    elif o in ("-s", "--skip-fw"):
        skipfw = True
    elif o in ("-c", "--config"):
        if os.path.isfile(a):
            cli_customini = a
        else:
            usage()
            sys.exit()
    elif o in ("-h", "--help"):
        usage()
        sys.exit()
    else:
        assert False, "unhandled option"

# start message
printScript(os.path.basename(__file__), 'begin')

# custom ini file given on cli
if cli_customini != '':
    print('Custom inifile ' + cli_customini
          + ' given on cli, ignoring other arguments!')
    subProc('cp ' + cli_customini + ' ' + constants.CUSTOMINI)
    subProc('chmod 600 ' + constants.CUSTOMINI)
else:
    # check params
    print('Processing commandline arguments.')
    if servername != '':
        rc = modIni(constants.CUSTOMINI, 'setup', 'servername', servername)
    if domainname != '':
        rc = modIni(constants.CUSTOMINI, 'setup', 'domainname', domainname)
    if dhcprange != '':
        rc = modIni(constants.CUSTOMINI, 'setup', 'dhcprange', dhcprange)
    if adminpw != '':
        rc = modIni(constants.CUSTOMINI, 'setup', 'adminpw', adminpw)
    if schoolname != '':
        rc = modIni(constants.CUSTOMINI, 'setup', 'schoolname', schoolname)
    if location != '':
        rc = modIni(constants.CUSTOMINI, 'setup', 'location', location)
    if country != '':
        rc = modIni(constants.CUSTOMINI, 'setup', 'country', country)
    if state != '':
        rc = modIni(constants.CUSTOMINI, 'setup', 'state', state)
    rc = modIni(constants.CUSTOMINI, 'setup', 'skipfw', str(skipfw))


# work off setup modules
setup_modules = os.listdir(constants.SETUPDIR)
setup_modules.remove('__pycache__')
setup_modules.sort()
for module_file in setup_modules:
    # skip dialog in unattended mode
    if (unattended and 'dialog.py' in module_file):
        continue
    # check firewall major version
    if (not skipfw and 'templates.py' in module_file):
        if not checkFwMajorVer():
            sys.exit(1)
    # print module name
    module_name = os.path.basename(os.path.splitext(module_file)[0]).split('_')[1]
    printScript('', 'begin')
    printScript(module_name)
    # execute module
    importlib.import_module(module_file.replace('.py', ''))

printScript(os.path.basename(__file__), 'end')