Browse code

some improvements to 'create stack user' path

* Increase the timeout from 3 to 10, so user has a bigger chance
to kill the script if being run interactively before a 'stack'
user is created.
* explicitly ask 'getent' for the stack user rather than getting
all users and grepping (which would match an "openstack" user,
or a user named "bob.stack")
* use $PWD rather than `pwd`
* create file in sudoers.d rather than modifying /etc/sudoers.

Scott Moser authored on 2011/10/08 00:18:10
Showing 1 changed files
... ...
@@ -93,29 +93,31 @@ set -o xtrace
93 93
 
94 94
 if [[ $EUID -eq 0 ]]; then
95 95
     echo "You are running this script as root."
96
+    echo "In 10 seconds, we will create a user 'stack' and run as that user"
97
+    sleep 10 
96 98
 
97 99
     # since this script runs as a normal user, we need to give that user
98 100
     # ability to run sudo
99 101
     apt-get update
100 102
     apt-get install -y sudo
101 103
 
102
-    if ! getent passwd | grep -q stack; then
104
+    if ! getent passwd stack >/dev/null; then
103 105
         echo "Creating a user called stack"
104 106
         useradd -U -G sudo -s /bin/bash -m stack
105 107
     fi
108
+
106 109
     echo "Giving stack user passwordless sudo priviledges"
107
-    echo "stack ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
110
+    ( umask 226 && echo "stack ALL=(ALL) NOPASSWD: ALL" \
111
+        >> /etc/sudoers.d/50_stack_sh )
108 112
 
109 113
     echo "Copying files to stack user"
110
-    cp -r -f `pwd` /home/stack/
111
-    THIS_DIR=$(basename $(dirname $(readlink -f $0)))
112
-    chown -R stack /home/stack/$THIS_DIR
113
-    echo "Running the script as stack in 3 seconds..."
114
-    sleep 3
114
+    STACK_DIR="/home/stack/${PWD%/*}"
115
+    cp -r -f "$PWD" "$STACK_DIR"
116
+    chown -R stack "$STACK_DIR"
115 117
     if [[ "$SHELL_AFTER_RUN" != "no" ]]; then
116
-        exec su -c "cd /home/stack/$THIS_DIR/; bash stack.sh; bash" stack
118
+        exec su -ec "cd $STACK_DIR; bash stack.sh; bash" stack
117 119
     else
118
-        exec su -c "cd /home/stack/$THIS_DIR/; bash stack.sh" stack
120
+        exec su -ec "cd $STACK_DIR; bash stack.sh" stack
119 121
     fi
120 122
     exit 0
121 123
 fi