Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39550
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
### DEPRECATED ##
26
27
# This script is run by /etc/rc.local when booting up gce machines. It does special configuration
28
# depending on what devices are available, etc.
29
30
import os, socket
31
32
hostname = socket.gethostname()
33
34
def cmd(s):
35
print s
36
from subprocess import Popen, PIPE
37
out = Popen(s, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
38
e = out.wait()
39
t = out.stdout.read() + out.stderr.read()
40
if e:
41
print t
42
return t
43
44
def get_disks():
45
ans = {}
46
for x in cmd("/bin/ls -l /dev/disk/by-uuid/").splitlines():
47
v = x.split()
48
if len(v) > 3:
49
device = os.path.join('/dev', os.path.split(v[-1])[-1])
50
uuid = v[-3]
51
ans[device] = uuid
52
return ans
53
54
def mount_conf():
55
# mount the /mnt/conf partition if available and return True if so.
56
# return False if not available
57
d = get_disks()
58
if '/dev/sdb1' in d:
59
# have a conf partition
60
# critical to protect visibility, since contains vpn keys.
61
cmd("mkdir -p /mnt/conf; mount /dev/sdb1 /mnt/conf; chmod og-rwx /mnt/conf; chown root. /mnt/conf")
62
return True
63
else:
64
return False
65
66
def conf():
67
# assuming /mnt/conf got mounted, do the configuration.
68
69
if os.path.exists("/mnt/conf/pre"):
70
cmd("/mnt/conf/pre")
71
72
if os.path.exists("/mnt/conf/fstab"):
73
# mkdir each mount point
74
for x in open("/mnt/conf/fstab").readlines():
75
x = x.strip()
76
if not x.startswith('#'):
77
v = x.split()
78
if len(v) >= 2:
79
cmd('mkdir -p "%s"'%v[1])
80
81
# append /mnt/conf/fstab to the end of fstab and do "mount -a"
82
if os.path.exists('/mnt/conf/fstab'):
83
fstab0 = open('/etc/fstab').read()
84
fstab1 = open('/mnt/conf/fstab').read()
85
i = fstab0.find("#SALVUS")
86
if i != -1:
87
fstab0 = fstab0[:i]
88
open('/etc/fstab','w').write(fstab0 + '\n#SALVUS -- everything below this is automatically added from /mnt/conf/fstab! \n' + fstab1)
89
cmd("mount -a")
90
91
# hostname
92
if os.path.exists('/mnt/conf/hostname'):
93
cmd("cp /mnt/conf/hostname /etc/hostname")
94
cmd("hostname `cat /mnt/conf/hostname`")
95
96
# tinc
97
if os.path.exists('/mnt/conf/tinc'):
98
cmd("mkdir -p /home/salvus/salvus/salvus/data/local/etc/tinc")
99
cmd("mount -o bind /mnt/conf/tinc /home/salvus/salvus/salvus/data/local/etc/tinc")
100
cmd("cp /mnt/conf/tinc/hosts.0/* /mnt/conf/tinc/hosts/")
101
cmd("mkdir -p /home/salvus/salvus/salvus/data/local/var/run/")
102
cmd("/home/salvus/salvus/salvus/data/local/sbin/tincd -k; sleep 2")
103
cmd("nice --19 /home/salvus/salvus/salvus/data/local/sbin/tincd")
104
105
# Copy over newest version of certain scripts and set permissions
106
for s in ['bup_storage.py', 'hashring.py']:
107
os.system("cp /home/salvus/salvus/salvus/scripts/%s /usr/local/bin/; chmod og-w /usr/local/bin/%s; chmod og+rx /usr/local/bin/%s"%(s,s,s))
108
109
# make it so there is a stable mac address for people who want to run their legal copy of magma, etc. in a private project.
110
cmd("ip link add link eth0 address f0:de:f1:b0:66:8e eth0.1 type macvlan")
111
cmd("ip link add link eth0 address 5e:d4:a9:c7:c8:f4 eth0.2 type macvlan")
112
113
# run post-configuration script
114
if os.path.exists("/mnt/conf/post"):
115
cmd("/mnt/conf/post")
116
117
cmd("chmod og-rwx -R /home/salvus/")
118
119
if hostname.startswith('devel'):
120
os.system('rm -rf /home/salvus/salvus/salvus/data/secrets/cassandra')
121
122
if hostname.startswith('compute'):
123
# Create a firewall so that only the hub nodes can connect to things like ipython and the raw server.
124
cmd("/home/salvus/salvus/salvus/scripts/compute_firewall.sh")
125
# Delete data that doesn't need to be on this node
126
cmd("rm -rf /home/salvus/salvus/salvus/data/secrets/cassandra")
127
# Start the storage server
128
os.system("umount /projects; umount /bup/conf; umount /bup/bups; zpool import -f bup; zfs set mountpoint=/projects bup/projects; chmod og-r /projects; su - salvus -c 'cd /home/salvus/salvus/salvus/&& . smc-env&& ./bup_server start'")
129
# Install crontab for snapshotting the bup pool, etc.
130
os.system("crontab /home/salvus/salvus/salvus/scripts/root-compute.crontab")
131
132
if hostname.startswith("cassandra"):
133
# Delete data that doesn't need to be on this node
134
cmd("rm -rf /home/salvus/salvus/salvus/data/secrets/")
135
# Copy custom config, start cassandra Daemon
136
cmd("mkdir /cassandra; mount /dev/sdb2 /cassandra")
137
cmd("rm -rf /var/log/cassandra; ln -s /cassandra/log /var/log/cassandra; cp /cassandra/etc/* /etc/cassandra/; rm -rf /var/lib/cassandra; ln -s /cassandra/lib /var/lib/cassandra; service cassandra start")
138
cmd("rm -rf /home/salvus/salvus/salvus/data/secrets")
139
140
141
if __name__ == "__main__":
142
if mount_conf():
143
conf()
144
145