-
Notifications
You must be signed in to change notification settings - Fork 3
/
manager.py
171 lines (147 loc) · 5.44 KB
/
manager.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from fabric.api import *
import sys
import time
from tempfile import mkstemp
from shutil import move
from os import remove, close
import json
import twisted.internet
from twisted.internet import reactor, task
from subprocess import check_output
def save_config(config_dict):
with open('/root/OpenBazaar-Server/abc.json', 'w') as f:
json.dump(config_dict, f)
def load_config():
with open('/root/OpenBazaar-Server/abc.json', 'r') as f:
try:
config_dict = json.load(f)
except:
config_dict = {}
return config_dict
def replace(path, pattern, subst):
fh, temp_path = mkstemp()
with open(temp_path,'w') as temp_file:
with open(path) as file:
for line in file:
temp_file.write(line.replace(pattern, subst))
close(fh)
remove(path)
move(temp_path, path)
def add_store(storename, username, password):
c = load_config()
store_numbers = []
store_names = []
for sn, (storenumber, un, pwd) in c.iteritems():
store_numbers.append(storenumber)
store_names.append(sn)
for n in range(1, 6): # max 5 stores per droplet (the port range goes out of bounds with 6)
if n not in store_numbers and storename not in store_names:
c[storename] = (n, username, password)
save_config(c)
return True
return False
def remove_store(storename):
c = load_config()
removed = c.pop(storename, None)
if removed:
save_config(c)
return removed
def remove_all_stores():
abc = load_config()
for storename, (storenumber, username, password) in abc.iteritems():
remove_store(storename)
def restart_store(storename):
print 'Restarting store %s.' % storename
abc = load_config()
storenumber, username, password = abc[storename]
with cd('~/OpenBazaar-Server'):
local('cp ob_template.cfg ob.cfg')
replace('ob.cfg', '#USERNAME = username', 'USERNAME = %s' % username)
replace('ob.cfg', '#PASSWORD = password', 'PASSWORD = %s' % password)
replace('ob.cfg', '#DATA_FOLDER = /path/to/OpenBazaar/', 'DATA_FOLDER = /root/stores/%s/' % storename)
try:
local("screen -S %s -X stuff $'\003'" % storename)
time.sleep(10)
except:
pass
local('echo \'logfile /root/logs/%s.log\' > /root/.screenrc' % storename)
local('screen -d -m -S %s -L python openbazaard.py start -p 1111%d -r %d8469 -w %d8466 -b %d8470 --pidfile %s.pid -a 0.0.0.0; sleep 1' % (storename, storenumber, storenumber, storenumber, storenumber, storename))
def restart_all_stores():
with cd('~/OpenBazaar-Server'):
local('git pull')
local('pip install -r requirements.txt --upgrade')
abc = load_config()
for storename, (storenumber, username, password) in abc.iteritems():
restart_store(storename)
def stop_store(storename):
abc = load_config()
storenumber, username, password = abc[storename]
try:
local("screen -S %s -X stuff $'\003'" % storename)
time.sleep(10)
except:
pass
def stop_all_stores():
abc = load_config()
for storename, (storenumber, username, password) in abc.iteritems():
stop_store(storename)
def delete_all_logs():
abc = load_config()
for storename, (storenumber, username, password) in abc.iteritems():
with settings(warn_only=True):
with cd('~/logs'):
local('rm %s.log' % storename)
def screen_running(name):
var = check_output(["screen -ls; true"],shell=True)
if "."+name+"\t(" in var:
return True
else:
return False
def maintain():
abc = load_config()
for storename in abc.keys():
if not screen_running(storename):
print 'Store %s went down... restarting...' % storename
restart_store(storename)
def loop_restart_store(storename, interval):
print 'loop restart store: %s, %s' % (storename, interval)
task.LoopingCall(restart_store, storename).start(interval)
def loop_maintain(interval):
task.LoopingCall(maintain).start(interval)
def manage():
print 'Started manager... restarting all stores.'
restart_all_stores()
# handle the restarting of stores
abc = load_config()
interval = 4*3600 # 4 hour in seconds
int_frac = interval / len(abc)
for i, storename in enumerate(abc.keys()):
reactor.callLater((i * int_frac) + int_frac, loop_restart_store, storename, interval)
# restart stores that have gone down
reactor.callLater(60, loop_maintain, 5)
twisted.internet.reactor.run()
# do something based on the command line arguments
if sys.argv[1] == 'spawn_manage':
with settings(warn_only=True):
with cd('~/OpenBazaar-Server'):
local('screen -X -S manage quit')
local('echo \'logfile /root/logs/manage.log\' > /root/.screenrc')
local('screen -d -m -S manage -L python manager.py manage; sleep 1')
if sys.argv[1] == 'manage':
manage()
elif sys.argv[1] == 'add':
add_store(sys.argv[2], sys.argv[3], sys.argv[4])
elif sys.argv[1] == 'remove':
remove_store(sys.argv[2])
elif sys.argv[1] == 'restart':
restart_store(sys.argv[2])
elif sys.argv[1] == 'stop':
stop_store(sys.argv[2])
elif sys.argv[1] == 'restart_all':
restart_all_stores()
elif sys.argv[1] == 'stop_all':
stop_all_stores()
elif sys.argv[1] == 'delete_all_logs':
delete_all_logs()
elif sys.argv[1] == 'remove_all':
remove_all_stores()