Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39549
1
#!/usr/bin/env python
2
###############################################################################
3
#
4
# CoCalc: Collaborative Calculation in the Cloud
5
#
6
# Copyright (C) 2016, Sagemath Inc.
7
#
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#
21
###############################################################################
22
23
24
25
"""
26
Create a unix user and setup ssh keys. Usage:
27
28
create_unix_user.py [username]
29
30
If the username is not given, then a random 8-character alpha-numeric username is chosen.
31
32
If the username is given, then any -'s and characters passed the 32nd are removed from the given username.
33
Thus if the username is a V4 uuid, e.g., 36 characters with -'s, then the dashes are removed, giving a 32
34
character username, which is uniquely determined by the V4 uuid.
35
36
You should put the following in visudo:
37
38
salvus ALL=(ALL) NOPASSWD: /usr/local/bin/create_unix_user.py *
39
salvus ALL=(ALL) NOPASSWD: /usr/local/bin/delete_unix_user.py *
40
41
ALSO **IMPORTANT** put a locally built copy of .sagemathcloud (with secret deleted) in
42
scripts/skel to massively speed up new project creation. You might make a symlink like this:
43
44
sudo ln -s /home/salvus/salvus/salvus/scripts/skel .
45
46
"""
47
48
BASE_DIR='/mnt/home'
49
50
from subprocess import Popen, PIPE
51
import os, random, string, sys, uuid
52
53
if len(sys.argv) > 2:
54
sys.stderr.write("Usage: sudo %s [optional username]\n"%sys.argv[0])
55
sys.stderr.flush()
56
sys.exit(1)
57
58
# os.system('whoami')
59
60
skel = os.path.join(os.path.split(os.path.realpath(__file__))[0], 'skel')
61
#print skel
62
63
def cmd(args):
64
if isinstance(args, str):
65
shell = True
66
#print args
67
else:
68
shell = False
69
#print ' '.join(args)
70
out = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=shell)
71
e = out.wait()
72
stdout = out.stdout.read()
73
stderr = out.stderr.read()
74
if e:
75
sys.stdout.write(stdout)
76
sys.stderr.write(stderr)
77
sys.exit(e)
78
return {'stdout':stdout, 'stderr':stderr}
79
80
if len(sys.argv) == 2:
81
username = sys.argv[1].replace('-','')[:32]
82
else:
83
# Using a random username helps to massively reduce the chances of race conditions...
84
alpha = string.ascii_letters + string.digits
85
username = ''.join([random.choice(alpha) for _ in range(8)])
86
87
if os.path.exists(os.path.join(BASE_DIR, username)):
88
# "creating" an existing user is fine -- really the point of this script is to ensure the
89
# user exists.
90
pass
91
else:
92
out = cmd(['useradd', '-b', BASE_DIR, '-m', '-U', '-k', skel, username])
93
94
# coffeescript to determine
95
# BLOCK_SIZE = 4096 # units = bytes; This is used by the quota command via the conversion below.
96
# megabytes_to_blocks = (mb) -> Math.floor(mb*1000000/BLOCK_SIZE) + 1
97
# ensure host system is setup with quota for this to do anything: http://www.ubuntugeek.com/how-to-setup-disk-quotas-in-ubuntu.html
98
99
disk_soft_mb = 512 # 250 megabytes
100
disk_soft = disk_soft_mb * 245
101
disk_hard = 2*disk_soft
102
inode_soft = 20000
103
inode_hard = 2*inode_soft
104
cmd(["setquota", '-u', username, str(disk_soft), str(disk_hard), str(inode_soft), str(inode_hard), '-a'])
105
106
print username
107
108
# Save account info so it persists through reboots/upgrades/etc.
109
if os.path.exists("/mnt/home/etc/"):
110
cmd("cp /etc/passwd /etc/shadow /etc/group /mnt/home/etc/")
111
112