-
Notifications
You must be signed in to change notification settings - Fork 1
/
users_ubuntu
145 lines (116 loc) · 4.11 KB
/
users_ubuntu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
### BEGIN INIT INFO
# Provides: users
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Default-Start: 1 2 3 4 5 6
# Default-Stop: 0
# Short-Description: adding/deleting (disabling) users
# Description: # chkconfig: 123456 50 50
### END INIT INFO
# Source function library.
. /lib/lsb/init-functions
manage=$(cat<<EOF
import boto
import boto.utils
import json
import os
import pwd
import ssl
import sys
userdata = json.loads(boto.utils.get_instance_userdata())
# we always succeed when there is nothing to do
if not ('security' in userdata and 'users' in userdata['security']):
exit(0)
# Buckets with a . in their name do not have
# a valid ssl cert (*.s3.amazonaws.com)
# Create ssl context that does not validate certificates
if hasattr(ssl, '_create_unverified_context'):
ssl._create_default_https_context = ssl._create_unverified_context
s3 = boto.connect_s3()
keys = s3.get_bucket(userdata['security']['bucket'], validate=False)
key = boto.s3.key.Key(keys)
if "provision" == sys.argv[1]:
for username in userdata['security']['users']:
user = userdata['security']['users'][username]
sys.stdout.write(" {0}".format(username))
os.system("/usr/sbin/adduser --disabled-password --gecos \"{0}\" {1}".format(
user['comment'],
username))
command = "adduser %s %s"%(username, ",".join(user['groups']))
os.system(command)
key.key = user['email']
os.system("/bin/mkdir --mode=0755 -p /home/{0}/.ssh".format(username))
key.get_contents_to_filename(
"/home/{0}/.ssh/authorized_keys".format(username))
os.system("chown -R {0}.{0} /home/{0}".format(username))
elif "remove" == sys.argv[1]:
for username in userdata['security']['users']:
user = userdata['security']['users'][username]
sys.stdout.write(" {0}".format(username))
os.system("deluser {0} --remove-home".format(username))
elif "update" == sys.argv[1]:
for username in userdata['security']['users']:
user = userdata['security']['users'][username]
sys.stdout.write(" {0}".format(username))
key.key = user['email']
# also delete the file when it is gone
try:
key.exists()
key.get_contents_to_filename(
"/home/{0}/.ssh/authorized_keys".format(username))
except:
os.remove("/home/{0}/.ssh/authorized_keys".format(username))
os.system("chown -R {0}.{0} /home/{0}".format(username))
else:
pass
EOF
)
# for some reason it 'stops' on runlevel 6, so we ignore that one
runlevel=`runlevel`
# /usr/bin/touch /root/runlevelis"${runlevel}".txt
if [ "2 6" == "${runlevel}" ]; then
# /usr/bin/touch /root/stopiscalled"${runlevel}".txt
exit 0
fi
service=users
start() {
echo -n "Provisioning ${service}: "
/usr/bin/python -c "${manage}" provision
provisioned=$?
return ${provisioned}
}
stop() {
echo -n "Removing ${service}: "
/usr/bin/python -c "${manage}" remove
removed=$?
return ${removed}
}
restart() {
echo -n "Updating ${service}: "
/usr/bin/python -c "${manage}" update
updated=$?
return ${updated}
}
case "$1" in
start)
echo "start"
start
echo
;;
stop)
echo "stop"
stop
echo
;;
restart)
echo "restart"
restart
echo
;;
*)
echo "Usage: ${service} {start|stop|restart}"
exit 1
;;
esac
exit $?