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
# Configure tinc on this node
26
27
import json, os, socket, sys
28
29
salvus_root = os.environ['SALVUS_ROOT']
30
sys.path.append(salvus_root)
31
import misc
32
33
tinc_conf_hosts = os.path.join(salvus_root, 'conf/tinc_hosts')
34
tinc_path = '/home/salvus/salvus/salvus/data/local/etc/tinc/'
35
36
if not os.path.exists(tinc_path):
37
os.makedirs(tinc_path)
38
39
tinc_conf = os.path.join(tinc_path, 'tinc.conf')
40
hosts_path = os.path.join(tinc_path, 'hosts')
41
if not os.path.exists(hosts_path):
42
# symbolic link from tinc_conf_hosts to hosts_path
43
os.symlink(tinc_conf_hosts, hosts_path)
44
45
46
def init():
47
external_address = sys.argv[2]
48
internal_address = misc.local_ip_address()
49
tinc_address = sys.argv[3]
50
tincname = sys.argv[4]
51
52
if not os.path.exists(tinc_path):
53
os.makedirs(tinc_path)
54
55
run_path = '/home/salvus/salvus/salvus/data/local/var/run/'
56
if not os.path.exists(run_path):
57
os.makedirs(run_path)
58
59
tinc_up = os.path.join(tinc_path, 'tinc-up')
60
61
open(tinc_up,'w').write(
62
"#!/bin/sh\nifconfig $INTERFACE %s netmask 255.192.0.0 txqueuelen 10000"%tinc_address)
63
64
os.popen("chmod a+rx %s"%tinc_up)
65
66
open(tinc_conf,'w').write("Name = %s\nKeyExpire = 2592000\nProcessPriority = high\n"%tincname)
67
68
rsa_key_priv = os.path.join(tinc_path, 'rsa_key.priv')
69
rsa_key_pub = os.path.join(tinc_path, 'hosts', tincname)
70
71
if os.path.exists(rsa_key_priv): os.unlink(rsa_key_priv)
72
if os.path.exists(rsa_key_pub): os.unlink(rsa_key_pub)
73
74
os.popen("tincd --config %s -K"%tinc_path).read()
75
76
host_file = os.path.join(hosts_path, tincname)
77
public_key = open(rsa_key_pub).read().strip()
78
79
# We give the internal address for Address= since only other GCE nodes will connect to these nodes, and
80
# though using the external address would work, it would incur significant additional *charges* from Google.
81
open(host_file,'w').write("Address = %s\nTCPonly=yes\nCompression=10\nCipher = aes-128-cbc\nSubnet = %s\n%s"%(
82
internal_address, tinc_address, public_key))
83
84
print json.dumps({"tincname":tincname, "host_file":open(host_file).read()}, separators=(',',':'))
85
86
def connect_to():
87
s = '\n' + '\n'.join(["ConnectTo = %s"%host for host in sys.argv[2:]]) + '\n'
88
open(tinc_conf,'a').write(s)
89
90
91
command = sys.argv[1]
92
if command == "init":
93
init()
94
elif command == "connect_to":
95
connect_to()
96
else:
97
raise RuntimeError("unknown command '%s'"%command)
98
99
100