Skip to content
Snippets Groups Projects
0_setup.py 1.56 KiB
Newer Older
Stéphane Diemer's avatar
Stéphane Diemer committed
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import utils


def setup(interactive=True):
    # TODO: setup IP
    dir_path = utils.get_dir(__file__)
    cmds = [
        # Locale
        'locale-gen en_GB.UTF-8',
        'update-locale LANG=en_GB.UTF-8 LANGUAGE=en_GB.UTF-8 LC_ALL=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8',
        # NTP
        'echo "Replacing /etc/ntp.conf"',
        dict(line='write', template='%s/ntp.conf' % dir_path, target='/etc/ntp.conf', params=(
            ('{{ system_ntp }}', utils.get_conf('system_ntp', 'ntp.ubuntu.com')),
        )),
    ]
    # Create / update ubicast account
    cmds.append('echo "Checking ubicast account"')
    code, out = utils.exec_cmd(['id', 'ubicast'], get_out=True)
    if code != 0:
        cmds.append('useradd -m -s /bin/bash ubicast')
        out = ''
    if 'sudo' not in out:
        cmds.append('usermod -aG sudo ubicast')
    # Add SSH key
    cmds.append('echo "Checking ubicast and root SSH keys"')
    # root
    cmds.append('mkdir -p /root/.ssh')
    cmds.append('chmod 700 /root/.ssh')
    if utils.exec_cmd(['rgrep', 'support@ubicast', '/root/.ssh']) != 0:
        cmds.append('cat "%s/ubicast_support.pub" >> /root/.ssh/authorized_keys' % dir_path)
    # ubicast
    cmds.append('mkdir -p /home/ubicast/.ssh')
    cmds.append('chmod 700 /home/ubicast/.ssh')
    if utils.exec_cmd(['rgrep', 'support@ubicast', '/home/ubicast/.ssh']) != 0:
        cmds.append('cat "%s/ubicast_support.pub" >> /home/ubicast/.ssh/authorized_keys' % dir_path)
    cmds.append('chown -R ubicast:ubicast /home/ubicast/.ssh')

    utils.run_commands(cmds)