-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.py
202 lines (159 loc) · 6.19 KB
/
commands.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import sys
import os
MODULE = 'deploy'
# Commands that are specific to your module
COMMANDS = ['deploy:update', 'deploy:start', 'deploy:stop', 'deploy:restart']
# Checks if binary exists on path
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
def getArg(args, findThis):
for a in args:
p = a.find('--' + findThis)
if p == 0:
return a[3 + len(findThis):]
return None
def getParameters(app, args):
# Parameters have the following priority (highest first):
# - Command line arguments
# - Instance defined with --instance command line argument
# - default instance in application.conf
# Read default or 'instance' from application.conf
instance = getArg(args, 'instance')
if instance == None:
instance = 'default'
host = app.readConf('deploy.' + instance + '.host')
path = app.readConf('deploy.' + instance + '.path')
port = app.readConf('deploy.' + instance + '.port')
login = app.readConf('deploy.' + instance + '.login')
excludes = app.readConf('deploy.' + instance + '.excludes')
play_path = app.readConf('deploy.' + instance + '.play_path')
ssh_key = app.readConf('deploy.' + instance + '.ssh_key')
# Read command line arguments
if getArg(args, 'host') != None:
host = getArg(args, 'host')
if getArg(args, 'path') != None:
path = getArg(args, 'path')
if getArg(args, 'port') != None:
port = getArg(args, 'port')
if getArg(args, 'login') != None:
login = getArg(args, 'login')
if getArg(args, 'excludes') != None:
excludes = getArg(args, 'excludes')
if getArg(args, 'play_path') != None:
play_path = getArg(args, 'play_path')
if getArg(args, 'ssh_key') != None:
play_path = getArg(args, 'ssh_key')
if host == "":
print "deploy: No host specified"
sys.exit(-1)
if path == "":
print "deploy: No path specified"
sys.exit(-1)
if port == "":
print "deploy: No port specified"
sys.exit(-1)
if login == "":
print "deploy: No login specified"
sys.exit(-1)
if play_path == "":
print "~ play_path not specified! Assuming that ./play exists in path on target host"
play_path = "./play"
cmd = login + "@" + host + ":" + path
parameters = {}
parameters["host"] = host
parameters["path"] = path
parameters["port"] = port
parameters["login"] = login
parameters["excludes"] = excludes
parameters["play_path"] = play_path
parameters["cmd"] = cmd
if ssh_key != "":
parameters["ssh"] = "ssh -i " + ssh_key + " " + login + "@" + host
else:
parameters["ssh"] = "ssh " + login + "@" + host
parameters["ssh_key"] = ssh_key
return parameters
def cmdUpdate(app, args):
parameters = getParameters(app, args)
excludesList = ["server.pid", "application.log"]
if parameters["excludes"] != "":
excludesList.extend(parameters["excludes"].split(","))
def excludePaths(path) : return "--exclude '" + path.strip() + "'"
exclude = " ".join(map(excludePaths, excludesList))
print "~ Updating files for " + parameters["cmd"] + ". port=" + parameters["port"]
print "~ Skipping " + ", ".join(excludesList)
if parameters["ssh_key"] != "":
return os.system("rsync -avzrc -e \"ssh -i " + parameters["ssh_key"] + "\" " + exclude + " " + app.path + " " + parameters["cmd"])
return os.system("rsync -avzrc " + exclude + " " + app.path + " " + parameters["cmd"])
def cmdStart(app, args):
parameters = getParameters(app, args)
play_path = parameters["play_path"]
remoteCmd = " \"cd " + parameters["path"] + ";"
remoteCmd += "nice -n 19 " + play_path + " dependencies " + app.name() + ";"
remoteCmd += "nice -n 19 " + play_path + " precompile " + app.name() + ";"
#remoteCmd += "rm -f " + app.name() + "/server.pid;"
remoteCmd += play_path + " start " + app.name() + " -Dprecompiled=true"
if parameters["port"] != "":
print "Using port: " + parameters["port"]
remoteCmd += " --http.port=" + parameters["port"]
remoteCmd += "\""
return os.system(parameters["ssh"] + remoteCmd)
def cmdStop(app, args):
parameters = getParameters(app, args)
play_path = parameters["play_path"]
remoteCmd = " \"cd " + parameters["path"] + ";"
remoteCmd += play_path + " stop " + app.name() + "\""
return os.system(parameters["ssh"] + remoteCmd)
def cmdRestart(app, args):
parameters = getParameters(app, args)
play_path = parameters["play_path"]
remoteCmd = " \"cd " + parameters["path"] + ";"
remoteCmd += "nice -n 19 " + play_path + " precompile " + app.name() + ";"
remoteCmd += play_path + " restart " + app.name() + " -Dprecompiled=true\""
return os.system(parameters["ssh"] + remoteCmd)
def execute(**kargs):
command = kargs.get("command")
app = kargs.get("app")
args = kargs.get("args")
env = kargs.get("env")
if which("rsync") == None:
print "~ rsync needed!"
sys.exit(-1)
if which("ssh") == None:
print "~ ssh needed!"
sys.exit(-1)
if command == "deploy:hello":
print "~ Hello"
if command == "deploy:update":
sys.exit(cmdUpdate(app, args))
if command == "deploy:start":
sys.exit(cmdStart(app, args))
if command == "deploy:stop":
sys.exit(cmdStop(app, args))
if command == "deploy:restart":
sys.exit(cmdRestart(app, args))
# This will be executed before any command (new, run...)
def before(**kargs):
command = kargs.get("command")
app = kargs.get("app")
args = kargs.get("args")
env = kargs.get("env")
# This will be executed after any command (new, run...)
def after(**kargs):
command = kargs.get("command")
app = kargs.get("app")
args = kargs.get("args")
env = kargs.get("env")
if command == "new":
pass