Skip to content
Snippets Groups Projects
Commit bc8f0ac9 authored by Stéphane Diemer's avatar Stéphane Diemer
Browse files

Added missing dir creation.

parent 61d05f76
No related branches found
No related tags found
No related merge requests found
...@@ -11,8 +11,13 @@ def add_allowed_keys(path, keys): ...@@ -11,8 +11,13 @@ def add_allowed_keys(path, keys):
if os.path.exists(path): if os.path.exists(path):
with open(path, 'r') as fo: with open(path, 'r') as fo:
content = fo.read() content = fo.read()
elif not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
new_content = content.strip() new_content = content.strip()
for key in keys: for key in keys:
key = key.strip()
if not key:
continue
if key not in new_content: if key not in new_content:
new_content += '\n' + key new_content += '\n' + key
utils.log('The key "%s" will be added in "%s".' % (key.split(' ')[-1], path)) utils.log('The key "%s" will be added in "%s".' % (key.split(' ')[-1], path))
...@@ -28,8 +33,15 @@ def add_allowed_keys(path, keys): ...@@ -28,8 +33,15 @@ def add_allowed_keys(path, keys):
def setup(interactive=True): def setup(interactive=True):
dir_path = utils.get_dir(__file__) dir_path = utils.get_dir(__file__)
cmds = list() # Set allowed SSH keys
allowed_keys = utils.get_conf('SSH_ALLOWED_KEYS', '').strip().split('\n')
with open('%s/ubicast_support.pub' % dir_path, 'r') as fo:
support_key = fo.read()
allowed_keys.append(support_key.strip())
add_allowed_keys('/root/.ssh/authorized_keys', allowed_keys)
add_allowed_keys('/home/ubicast/.ssh/authorized_keys', allowed_keys)
# Create / update ubicast account # Create / update ubicast account
cmds = list()
cmds.append('echo "Checking ubicast account"') cmds.append('echo "Checking ubicast account"')
code, out = utils.exec_cmd(['id', 'ubicast']) code, out = utils.exec_cmd(['id', 'ubicast'])
if code != 0: if code != 0:
...@@ -37,22 +49,13 @@ def setup(interactive=True): ...@@ -37,22 +49,13 @@ def setup(interactive=True):
out = '' out = ''
if 'sudo' not in out: if 'sudo' not in out:
cmds.append('usermod -aG sudo ubicast') cmds.append('usermod -aG sudo ubicast')
# Add SSH key cmds.append('cp "/root/.bashrc" "/home/ubicast/.bashrc"')
cmds.append('echo "Checking ubicast and root SSH keys"') cmds.append('chown ubicast:ubicast /home/ubicast')
allowed_keys = utils.get_conf('SSH_ALLOWED_KEYS', '').strip().split('\n') cmds.append('chown ubicast:ubicast /home/ubicast/.bashrc')
with open('%s/ubicast_support.pub' % dir_path, 'r') as fo: # Set SSH files permissions
support_key = fo.read() cmds.append('echo "Set SSH files permissions"')
allowed_keys.append(support_key.strip())
# root
cmds.append('mkdir -p /root/.ssh')
cmds.append('chmod 700 /root/.ssh') cmds.append('chmod 700 /root/.ssh')
add_allowed_keys('/root/.ssh/authorized_keys', allowed_keys)
# ubicast
cmds.append('mkdir -p /home/ubicast')
cmds.append('mkdir -p /home/ubicast/.ssh')
cmds.append('chmod 700 /home/ubicast/.ssh') cmds.append('chmod 700 /home/ubicast/.ssh')
add_allowed_keys('/home/ubicast/.ssh/authorized_keys', allowed_keys)
cmds.append('cp "/root/.bashrc" "/home/ubicast/.bashrc"')
cmds.append('chown -R ubicast:ubicast /home/ubicast/.ssh') cmds.append('chown -R ubicast:ubicast /home/ubicast/.ssh')
utils.run_commands(cmds) utils.run_commands(cmds)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment