From bc8f0ac992b19aee6bee956ddfdd79f2839d90a1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Diemer?= <stephane.diemer@ubicast.eu>
Date: Fri, 10 Mar 2017 16:47:23 +0100
Subject: [PATCH] Added missing dir creation.

---
 1.Base/2.ubicast_shell_access/0_setup.py | 33 +++++++++++++-----------
 1 file changed, 18 insertions(+), 15 deletions(-)

diff --git a/1.Base/2.ubicast_shell_access/0_setup.py b/1.Base/2.ubicast_shell_access/0_setup.py
index 64eb7a73..7bf32f55 100644
--- a/1.Base/2.ubicast_shell_access/0_setup.py
+++ b/1.Base/2.ubicast_shell_access/0_setup.py
@@ -11,8 +11,13 @@ def add_allowed_keys(path, keys):
     if os.path.exists(path):
         with open(path, 'r') as fo:
             content = fo.read()
+    elif not os.path.exists(os.path.dirname(path)):
+        os.makedirs(os.path.dirname(path))
     new_content = content.strip()
     for key in keys:
+        key = key.strip()
+        if not key:
+            continue
         if key not in new_content:
             new_content += '\n' + key
             utils.log('The key "%s" will be added in "%s".' % (key.split(' ')[-1], path))
@@ -28,8 +33,15 @@ def add_allowed_keys(path, keys):
 
 def setup(interactive=True):
     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
+    cmds = list()
     cmds.append('echo "Checking ubicast account"')
     code, out = utils.exec_cmd(['id', 'ubicast'])
     if code != 0:
@@ -37,22 +49,13 @@ def setup(interactive=True):
         out = ''
     if 'sudo' not in out:
         cmds.append('usermod -aG sudo ubicast')
-    # Add SSH key
-    cmds.append('echo "Checking ubicast and root 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())
-    # root
-    cmds.append('mkdir -p /root/.ssh')
+    cmds.append('cp "/root/.bashrc" "/home/ubicast/.bashrc"')
+    cmds.append('chown ubicast:ubicast /home/ubicast')
+    cmds.append('chown ubicast:ubicast /home/ubicast/.bashrc')
+    # Set SSH files permissions
+    cmds.append('echo "Set SSH files permissions"')
     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')
-    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')
 
     utils.run_commands(cmds)
-- 
GitLab