#!/usr/bin/env python3 # -*- coding: utf-8 -*- import subprocess import utils def setup(interactive=True): dir_path = utils.get_dir(__file__) cmds = list() # Create / update ubicast account cmds.append('echo "Checking ubicast account"') code, out = utils.exec_cmd(['id', 'ubicast']) 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') code, out = utils.exec_cmd(['rgrep', 'support@ubicast', '/root/.ssh']) if code != 0: cmds.append('cat "%s/ubicast_support.pub" >> /root/.ssh/authorized_keys' % dir_path) else: utils.log('The key "ubicast_support.pub" is already in /root/.ssh/authorized_keys.') # ubicast cmds.append('mkdir -p /home/ubicast/.ssh') cmds.append('chmod 700 /home/ubicast/.ssh') code, out = utils.exec_cmd(['rgrep', 'support@ubicast', '/home/ubicast/.ssh']) if code != 0: cmds.append('cat "%s/ubicast_support.pub" >> /home/ubicast/.ssh/authorized_keys' % dir_path) else: utils.log('The key "ubicast_support.pub" is already in /home/ubicast/.ssh/authorized_keys.') cmds.append('chown -R ubicast:ubicast /home/ubicast/.ssh') utils.run_commands(cmds) # Set ubicast password if any pwd = utils.get_conf('SHELL_UBICAST_PWD') if pwd: p = subprocess.Popen(['passwd', '-q', 'ubicast'], stdin=subprocess.PIPE) p.communicate(input=b'%(pwd)s\n%(pwd)s' % dict(pwd=pwd.encode('utf-8'))) if p.returncode != 0: raise Exception('Failed to set ubicast account password.') utils.log('\033[1;33m The ubicast account password has been set. \033[0m')