-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.py
134 lines (111 loc) · 4.33 KB
/
builder.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
from fswrap import File, Folder
from gitbot import stack
from commando.conf import ConfigDict
from gitbot.lib import hyde
from gitbot.lib.s3 import Bucket
from gitbot.lib.git import Tree
import yaml
def get_worker_outputs(data):
result = None
try:
region = data.region or 'us-east-1'
root = Folder(data.root or '~')
source = root.child_folder('src')
source.make()
source = source.child_folder('worker')
repo = data.worker_repo or 'git://github.com/gitbot/worker.git'
branch = data.worker_branch or 'master'
# 1. Pull worker repo
tree = Tree(source, repo, branch)
tree.clone(tip_only=True)
# 2. Call gitbot.stack.publish with 'gitbot.yaml'
worker_stack_name = stack.publish_stack(
source.child_file('gitbot.yaml'),
wait=True)
result = stack.get_outputs(worker_stack_name, region)
except Exception, e:
print repr(e)
raise
finally:
source.delete()
return result
def check_revision_already_published(proj, bucket_name, tree):
b = Bucket(bucket_name)
if not b.connect():
return None
sha = tree.get_revision_remote()
key_folder = Folder(proj).child_folder(tree.branch_name)
key_folder = key_folder.child_folder(sha)
key_path = key_folder.child(proj + '.zip')
return b.bucket.get_key(key_path)
def __upload(proj, repo, branch, data, maker, force=True):
root, source, dist = (None, None, None)
try:
root = Folder(data.root or '~')
source = root.child_folder('src')
source.make()
source = source.child_folder(proj)
dist = root.child_folder('dist')
tree = Tree(source, repo=repo, branch=branch)
key = None
if not force:
key = check_revision_already_published(proj, data.bucket, tree)
if not key:
b = Bucket(data.bucket)
b.make()
key_folder = Folder(proj).child_folder(branch)
zippath = dist.child_file(proj + '.zip')
tree.clone(tip_only=True)
sha = tree.get_revision(short=False)
key_folder = key_folder.child_folder(sha)
target = dist.child_folder(proj)
target.make()
maker(source, target)
target.zip(zippath.path)
b.add_file(zippath, target_folder=key_folder.path)
key = b.bucket.get_key(key_folder.child(zippath.name))
finally:
if source:
source.delete()
if dist:
dist.delete()
return key.generate_url(30000)
def upload_www(data, force=True):
def maker(source, target):
hyde.gen(source, ConfigDict(dict(data=data)), target=target.path)
return __upload('www', data.www_repo, data.www_branch, data, maker, force)
def upload_app(data, force=True):
def maker(source, target):
stuff = ['apps', 'lib', 'app.js', 'package.json']
for f in stuff:
fs = Folder.file_or_folder(source.child(f))
fs.copy_to(target)
return __upload('app', data.app_repo, data.app_branch, data, maker, force)
def publish(data, push_www=True, push_app=False):
data = ConfigDict(data)
www_archive = upload_www(data, push_www)
app_archive = upload_app(data, push_app)
config_file = File(File(__file__).parent.child('stack/gitbot.yaml'))
config = yaml.load(config_file.read_all())
config['file_path'] = config_file.path
params = data.get('stack_params', dict())
params.update(dict(AppSource=app_archive, WebSource=www_archive))
worker_params = get_worker_outputs(data)
if not worker_params or 'QueueURL' not in worker_params:
raise Exception('Failed to create the worker stack')
params.update(dict(
WorkerQueueURL=worker_params['QueueURL'],
ManagerAccessKey=worker_params['ManagerKey'],
ManagerSecretKey=worker_params['ManagerSecret']
))
config['data'] = data
stack.publish_stack(config,
params=params,
debug=True,
wait=True)
def validate(data):
data = ConfigDict(data)
config_file = File(File(__file__).parent.child('stack/gitbot.yaml'))
config = yaml.load(config_file.read_all())
config['file_path'] = config_file.path
stack.validate_stack(config)