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.
This commit is contained in:
parent
7c481189bd
commit
0d2145a0f3
1 changed files with 11 additions and 9 deletions
20
stack.sh
20
stack.sh
|
@ -93,29 +93,31 @@ set -o xtrace
|
||||||
|
|
||||||
if [[ $EUID -eq 0 ]]; then
|
if [[ $EUID -eq 0 ]]; then
|
||||||
echo "You are running this script as root."
|
echo "You are running this script as root."
|
||||||
|
echo "In 10 seconds, we will create a user 'stack' and run as that user"
|
||||||
|
sleep 10
|
||||||
|
|
||||||
# since this script runs as a normal user, we need to give that user
|
# since this script runs as a normal user, we need to give that user
|
||||||
# ability to run sudo
|
# ability to run sudo
|
||||||
apt-get update
|
apt-get update
|
||||||
apt-get install -y sudo
|
apt-get install -y sudo
|
||||||
|
|
||||||
if ! getent passwd | grep -q stack; then
|
if ! getent passwd stack >/dev/null; then
|
||||||
echo "Creating a user called stack"
|
echo "Creating a user called stack"
|
||||||
useradd -U -G sudo -s /bin/bash -m stack
|
useradd -U -G sudo -s /bin/bash -m stack
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Giving stack user passwordless sudo priviledges"
|
echo "Giving stack user passwordless sudo priviledges"
|
||||||
echo "stack ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
|
( umask 226 && echo "stack ALL=(ALL) NOPASSWD: ALL" \
|
||||||
|
>> /etc/sudoers.d/50_stack_sh )
|
||||||
|
|
||||||
echo "Copying files to stack user"
|
echo "Copying files to stack user"
|
||||||
cp -r -f `pwd` /home/stack/
|
STACK_DIR="/home/stack/${PWD%/*}"
|
||||||
THIS_DIR=$(basename $(dirname $(readlink -f $0)))
|
cp -r -f "$PWD" "$STACK_DIR"
|
||||||
chown -R stack /home/stack/$THIS_DIR
|
chown -R stack "$STACK_DIR"
|
||||||
echo "Running the script as stack in 3 seconds..."
|
|
||||||
sleep 3
|
|
||||||
if [[ "$SHELL_AFTER_RUN" != "no" ]]; then
|
if [[ "$SHELL_AFTER_RUN" != "no" ]]; then
|
||||||
exec su -c "cd /home/stack/$THIS_DIR/; bash stack.sh; bash" stack
|
exec su -ec "cd $STACK_DIR; bash stack.sh; bash" stack
|
||||||
else
|
else
|
||||||
exec su -c "cd /home/stack/$THIS_DIR/; bash stack.sh" stack
|
exec su -ec "cd $STACK_DIR; bash stack.sh" stack
|
||||||
fi
|
fi
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Reference in a new issue