-
Notifications
You must be signed in to change notification settings - Fork 0
/
propelc.py
516 lines (435 loc) · 15.2 KB
/
propelc.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
"""
PROPELC
PropelC is a python deployment tool using Ansible on the client side, and Propel on the Server.
Hence 'C' is for Client in PropelC
"""
import os
import re
import sys
import sh
import yaml
import socket
import argparse
yaml.Dumper.ignore_aliases = lambda *args: True
__title__ = "PropelC"
__version__ = "0.2.0"
CWD = os.getcwd()
CWD_PROPEL = "%s/_propelc" % CWD
ANSIBLE_FILE = "%s/_propelc/playbook.yml" % CWD
PROPEL_FILE = "%s/propel.yml" % CWD
HOST_FILE = "%s/_propelc/hosts" % CWD
PROPELC_EXISTS = os.path.isdir(CWD_PROPEL)
ROLES = {
"setup": [
{
"name": "SETUP: Checking Propel existence",
"stat": "path=/var/propel",
"register": "propelexists"
},
{
"name": "SETUP: PIP Installing Propel",
"action": "command pip install propel",
"when": "propelexists.stat.exists == False"
},
{
"name": "SETUP: Propel",
"action": "command propel-setup",
"when": "propelexists.stat.exists == False"
},
{
"name": "SETUP: Checking App existence",
"stat": "path={{ app_path }}",
"register": "direxists"
},
{
"name": "SETUP: Create App - Setting up app dir and Propel",
"command": "propel -c {{ app_path }} --git-push-cmd {{ app_path }} 'echo \"{{ app_path}} has been updated!\" '",
"when": "direxists.stat.exists == False",
}
],
"upgrade": [
{
"name": "UPDATE DIST",
"apt": "update_cache=yes"
},
{
"name": "UPGRADE PROPEL",
"action": "command pip install propel --upgrade",
}
],
"route53": [
{
"name": "ROUTE53: Collecting Host Ips",
"run_once": True,
"set_fact": "ips={{ ','.join(play_hosts) }}",
},
{
"name": "ROUTE53: Create Records",
"run_once": True,
"local_action": {
"module": "route53",
"command": "create",
"overwrite": True,
"zone": "{% set zonename = '.'.join(item.split('.')[-2:3]) %}{{ zonename }}.",
"record": "{{ item }}.",
"type": "A",
"value": "{{ ips }}"
},
"with_items": "{{ route53 }}"
}
],
"route53_delete": [
{
"name": "ROUTE53: Collecting route53 records",
"local_action": {
"module": "route53",
"command": "get",
"zone": "{% set zonename = '.'.join(item.split('.')[-2:3]) %}{{ zonename }}.",
"record": "{{ item }}",
"type": "A",
"register": "rec",
},
"with_items": "{{ route53_delete }}"
},
{
"name": "ROUTE53: Delete records",
"local_action": {
"module": "route53",
"command": "delete",
"zone": "{{ rec.results.0.set.zone }}",
"record": "{{ rec.results.0.set.record }}",
"ttl": "{{ rec.results.0.set.ttl }}",
"type": "{{ rec.results.0.set.type }}",
"value": "{{ rec.results.0.set.value }}",
"when": "rec.results.0"
}
}
],
"s3buckets": [
{
"name": "S3BUCKETS: Create buckets",
"run_once": True,
"local_action": {
"module": "s3",
"bucket": "{{ item }}",
"mode": "create",
"validate_certs": "no",
"region": "us-east-1",
},
"with_items": "{{ s3buckets }}"
}
],
"delete_app": [
{
"name": "DELETE: Delete app",
"shell": "rm -rf {{ app_path }}*"
}
],
"run_propel": [
{
"name": "PROPEL: Run commands",
"shell": "cd {{ app_path }}; propel {{ propel_vars }}",
}
],
"git_push": [
{
"name": "GIT: Push remote to master",
"local_action": "command git push -f {{ git_remote}} master"
}
]
}
# This is the template
TEMPLATE = """
################################################################################
# PropelC
################################################################################
# ------------------------------------------------------------------------------
propelc:
# The full path where the app will reside: /home/domain/domain.com
app_path:
envs:
# Key of the environments to be used when deploying
webs:
# List of all hosts
hosts:
-
# List of all domains for route 53, that will point there
domains:
-
# dict of propel commands that will be ran: webs|scripts|workers
propel:
# List of all the domains to deploy from
webs:
-
# List of all the scripts name
scripts:
-
# List of all the workers name
workers:
-
# Misc ansible tasks to run before and after deployment
tasks:
# List of tasks to run before the deploy
pre:
-
# List of tasks to run after
post:
-
# Dict of extra variables
vars:
key: value
# List of all s3 bucket to create
s3buckets:
-
# ------------------------------------------------------------------------------
"""
# ------------------------------------------------------------------------------
def sh_verbose(line):
print(line)
def run_playbook(*args, **kwargs):
playbook = "_propelc/playbook.yml"
with sh.pushd(CWD):
sh.ansible_playbook(playbook, "-i", "_propelc/hosts", _out=sh_verbose,
*args, **kwargs)
def git_remote_name(name):
return "propel_%s" % name
def set_git_remotes(name, hosts):
with sh.pushd(CWD):
name = git_remote_name(name)
remote_list = sh.git("remote").strip().split()
if name in remote_list:
sh.git("remote", "remove", name)
sh.git("remote", "add", name, hosts[0])
if len(hosts) > 1:
for h in hosts:
sh.git("remote", "set-url", name, "--push", "--add", h)
def get_propelc_config():
with open(PROPEL_FILE) as p:
propel_conf = yaml.load(p)
return propel_conf.get("propelc")
def gen_playbook():
plays = []
propel = get_propelc_config()
if not propel:
raise Exception("Missing `propelc` in propel.yml")
app_path = propel.get("app_path")
envs = propel.get("envs")
user = "root"
upgrade_propel = propel.get("upgrade", False)
serial = propel.get("serial", 4)
if not app_path:
raise Exception("Missing `app_path` in propelc node")
if "/" not in app_path or "." not in app_path:
raise Exception(
"`app_path` must be directory path with the domain in the format `/home/site/site.com`")
hostsd = {k: v.get("hosts") for k, v in envs.items()}
# CREATE HOST FILE
with open(HOST_FILE, "w") as h:
s = "# PropelC: To deploy propel app\n"
s += "# Generated automatically. All changes must be made in propel.yml\n\n"
for k, v in hostsd.items():
s += "[%s]\n" % k
if v and filter(None, v):
s += "\n".join(map(get_ip_by_hostname, v))
s += "\n\n"
h.write(s)
# SETUP GIT REMOTE
for k, v in hostsd.items():
v = filter(None, v)
if v:
git_hosts = ["ssh://%s@%s/%s.git" % (user, h, app_path) for h in v]
set_git_remotes(k, git_hosts)
# PROVISION
vars = {"app_path": app_path}
if propel.get("delete_app"):
tasks = [r for r in ROLES["delete_app"]]
else:
tasks = ROLES["setup"]
if upgrade_propel:
tasks.extend(ROLES["upgrade"])
# require variables for the items too
for k in ["s3buckets", "route53_delete"]:
if k in propel:
_vars = propel.get(k)
if _vars and filter(None, _vars):
vars.update({k: _vars})
tasks.extend(ROLES[k])
plays.append([
{
"hosts": "*",
"user": "root",
"serial": serial,
"name": "PROVISION HOSTS",
"tags": ["provision"],
"vars": vars,
"tasks": tasks
}
])
# CONTAINERS
for name, d in envs.items():
hosts = d.get("hosts")
domains = d.get("domains")
commands = d.get("propel")
pre_tasks = d.get("tasks", {}).get("pre")
post_tasks = d.get("tasks", {}).get("post")
use_git = d.get("use_git", True)
vars = {"app_path": app_path}
vars.update(d.get("vars", {}))
tasks = []
# Propel variables
propel_vars = ""
if commands:
for k, cmd in {"webs": "-w", "scripts": "-s",
"workers": "-k"}.items():
if k in commands:
if not isinstance(commands.get(k), list):
raise Exception(
"Propel targets `%s.commands.%s` must be a list" % (
name, k))
if filter(None, commands.get(k)):
propel_vars += "%s %s " % (
cmd, " ".join(filter(None, commands.get(k))))
# Route 53
if domains and filter(None, domains):
vars.update({"route53": domains})
tasks.extend(ROLES.get("route53"))
# pre tasks
if pre_tasks and isinstance(pre_tasks, list) and filter(None,
pre_tasks):
tasks.extend(pre_tasks)
# Push the data with git
if use_git:
tasks.extend(ROLES.get("git_push"))
vars.update({"git_remote": git_remote_name(name)})
if propel_vars:
tasks.extend(ROLES.get("run_propel"))
vars.update({"propel_vars": propel_vars})
# post tasks
if post_tasks and isinstance(post_tasks, list) and filter(None,
post_tasks):
tasks.extend(post_tasks)
# deploy task
plays.append([
{
"name": "DEPLOYING CONTAINERS: %s" % name,
"hosts": name,
"serial": serial,
"user": "root",
"tags": [name],
"vars": vars,
"tasks": tasks
}
])
# Create the ansible file
with open(ANSIBLE_FILE, "w") as a:
s = yaml.dump(plays, default_flow_style=False, width=1000,
explicit_start=True)
a.write(s.replace("- -", " -"))
def get_ip_by_hostname(hostname):
return socket.gethostbyname(hostname.strip())
def get_endpoints():
with open(PROPEL_FILE) as p:
propel = yaml.load(p)
xnodep = propel.get("propelc")
return xnodep.get("envs").keys()
# ------------------------------------------------------------------------------
def header(msg):
print("-" * 80)
print("===== PropelC ======")
print("-" * 80)
print("::: %s :::" % msg)
print("")
def deploy(target="all"):
header("Deploy to target: %s" % target)
if target != "all" and target not in get_endpoints():
print("ERROR: Invalid target: %s" % target)
print("++++")
print("* Here are the valid targets:")
print("\n".join(get_endpoints()))
print("")
else:
gen_playbook()
cmds = [] if target == "all" else ["--tags", target]
run_playbook(*cmds)
print("Done!")
def cmd():
try:
parser = argparse.ArgumentParser(
description="%s %s" % (__title__, __version__))
parser.add_argument("--init",
help="Initialize PropelC in the current directory",
action="store_true")
parser.add_argument("-p", "--provision",
help="Provision new application",
action="store_true")
parser.add_argument("-l", "--list",
help="List all the environments to deploy",
action="store_true")
parser.add_argument("-d", "--deploy",
help="Deploy environments to hosts ie [-d admin network web ...]",
nargs='*')
parser.add_argument("-a", "--all",
help="Deploy all ie[-a]",
action="store_true")
parser.add_argument("--restart",
help="Restart target ie [-d admin network web ...]",
nargs='*')
parser.add_argument("--undeploy",
help="Restart target ie [-d admin network web ...]",
nargs='*')
parser.add_argument("--mkfile",
help="With --init, to create the propel.yml file ie[--init --mkfile]",
action="store_true")
arg = parser.parse_args()
if arg.init:
header("Init PropelC")
if not os.path.isfile(PROPEL_FILE):
if arg.mkfile:
with open(PROPEL_FILE, "w") as f:
f.write("# Propel.yml - Created by PropelC")
f.write("\n\n")
f.write(TEMPLATE)
else:
raise Exception(
"Missing `propel.yml` file. Type --init --mkfile to automatically create the propel.yml")
if not os.path.isdir(CWD_PROPEL):
os.makedirs(CWD_PROPEL)
if not get_propelc_config():
with open(PROPEL_FILE, "a") as f:
f.write(TEMPLATE)
else:
if not PROPELC_EXISTS:
raise Exception("PropelC is not setup yet. Run `propelc init`")
gen_playbook()
# PROVISION
if arg.provision:
header("Running provision...")
run_playbook("--tags", "provision")
print("Done!")
# DEPLOY
elif arg.deploy or arg.all:
tags = ""
cmd = ["--skip-tags", "provision"]
if arg.deploy:
tags = ",".join(arg.deploy)
cmd.append("--tags")
cmd.append(tags)
else:
tags = "ALL"
header("Deploying Environments: %s" % tags)
run_playbook(*cmd)
print("Done!")
# List Containers
else:
header("Environments list")
for c in get_endpoints():
print("- %s" % c)
print("")
except Exception as e:
print("")
print("")
print("=" * 80)
print("ERROR: %s" % e)
print("=" * 80)
print("")
print("-" * 80)