-
Notifications
You must be signed in to change notification settings - Fork 7
/
appflow.py
executable file
·324 lines (252 loc) · 9.82 KB
/
appflow.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
#!/usr/bin/env python3
"""
Appflow CLI tool.
Type appflow to have a list of available commands.
Type appflow command -- --help to have help for the specified command.
"""
import json
import os
import fire
import lib.appflow_ansible as apansible
import lib.appflow_tools as tools
import lib.appflow_utils as utils
import lib.appflow_yaml as apyaml
__version__ = "1.0.1.5"
# We need some default configurations
# This will allow to call "appflow action *args" without always specifying
# Tenant and environment.
if os.path.exists(os.getenv('HOME') + "/.appflow/config.yml"):
DEFAULT_CONFIG = json.loads(apyaml.get_value("config"))
DEFAULT_TENANT = DEFAULT_CONFIG.get("appflow")["tenant"]["name"]
DEFAULT_ENV = DEFAULT_CONFIG.get("appflow")["tenant"]["default_env"]
else:
DEFAULT_CONFIG = ""
DEFAULT_TENANT = ""
DEFAULT_ENV = ""
print("Default configs not set")
print("Run:")
print("appflow init")
print("to setup the default configs")
print("")
class AppFlow(object):
"""
Appflow CLI tool.
Type appflow to have a list of available commands.
Type appflow command -- --help to have help for the specified command.
"""
def update(self, branch="master"):
"""
Simple function to update Appflow.
This is handy for the appflow-git package.
You can specify which branch you want to use
:type branch: string
:param branch: The name of the branch (default Master)
"""
tools.git_update_playbooks(branch)
def init(self, tenant=None, env=None):
"""
This will initialize all the folders for Assh.
This will also setup autocompletion for the CLI tool.
:type tenant: string
:param tenant: The name of the tenant.
:type env: string
:param env: The name of the tenant.
"""
tools.initialize(tenant, env)
def ssh(self, tenant=DEFAULT_TENANT, env=DEFAULT_ENV):
"""
This will deploy the ssh keys from your tenant/env
to the Assh folders.
:type tenant: string
:param tenant: The name of the tenant.
:type env: string
:param env: The name of the tenant.
"""
tools.setup_ssh(tenant, env)
def vhosts(self, tenant=DEFAULT_TENANT):
"""
This will setup your /etc/hosts to reflect the configs
int your tenant/development host_vars.
** Needs Root Access **
:type tenant: string
:param tenant: The name of the tenant.
"""
tools.set_vhosts_hosts(tenant)
def reset(self, tenant=DEFAULT_TENANT, env=DEFAULT_ENV):
"""
Reset your local tenant repository.
This will restore the status to the latest git pull.
This will also reset any unpushed work.
:type tenant: string
:param tenant: The name of the tenant.
:type env: string
:param env: The name of the tenant.
"""
tools.git_reset(tenant, env)
def status(self, tenant=DEFAULT_TENANT, env=DEFAULT_ENV):
"""
Outputs your local tenant status, any modified files.
This is handy to have an overview of what's going to be pushed
as a dry run.
:type tenant: string
:param tenant: The name of the tenant.
:type env: string
:param env: The name of the tenant.
"""
result = tools.git_status(tenant, env)
if result is False:
print('Files Already Encrypted')
else:
print('Changed files:')
print('\n'.join(result))
def checkout(self, tenant=DEFAULT_TENANT, env=DEFAULT_ENV):
"""
Git pull your local tenant repository.
This will download the lates available code.
This will also overwrite any unpushed work.
:type tenant: string
:param tenant: The name of the tenant.
:type env: string
:param env: The name of the tenant.
"""
tools.git_check_out(tenant, env)
def checkin(self, tenant=DEFAULT_TENANT, env=DEFAULT_ENV,
commit="Auto Commit"):
"""
Git push from yout local tenant repository.
This will only push the files that were modified.
Before any push, all the files are encrypted.
:type tenant: string
:param tenant: The name of the tenant.
:type env: string
:param env: The name of the tenant.
:type commit: string
:param commit: The commit message to use
when committing. (default Auto Commit)
"""
tools.git_check_in(tenant, env, commit)
def decrypt(self, tenant=DEFAULT_TENANT, env=DEFAULT_ENV):
"""
Decrypt your local tenant repository
:type tenant: string
:param tenant: The name of the tenant.
:type env: string
:param env: The name of the tenant.
"""
print(utils.get_provision_color_string('decrypt', tenant, env))
apansible.decrypt(tenant, env)
def encrypt(self, tenant=DEFAULT_TENANT, env=DEFAULT_ENV):
"""
Encrypt your local tenant repository
:type tenant: string
:param tenant: The name of the tenant.
:type env: string
:param env: The name of the tenant.
"""
print(utils.get_provision_color_string('encrypt', tenant, env))
apansible.encrypt(tenant, env)
def tags(self, tenant=DEFAULT_TENANT, env=DEFAULT_ENV):
"""
Show available tags. This is handy to provision only a part of them
or skipping some of them.
:type tenant: string
:param tenant: The name of the tenant.
:type env: string
:param env: The name of the tenant.
"""
print(utils.get_provision_color_string('tags', tenant, env))
apansible.list_tags(tenant, env)
def provision(self, tenant=DEFAULT_TENANT, env=DEFAULT_ENV,
limit: str = None, tags: str = None, skip_tags: str = None,
firstrun: bool = False, local: bool = False, debug: bool = False):
"""
Provision your machines.
Syntax is:
appflow provision "machine1,machine2" tag1,tag2 skiptag1,skiptag2
tags: will run only the tags specified
skip_tags: will run all the tags except for the specified ones
limit: limit to only some specified hosts.
Optionally it is possible to specify custom tenant and environment
appflow provision tenant-name env-name...
this is optional and by default will read the
default config in ~/.appflow/config.yml
:type tenant: string
:param tenant: The name of the tenant.
:type env: string
:param env: The name of the tenant.
:type limit: string
:param limit: Comma separated list of hosts to provision. (default None)
:type tags: string
:param tags: Comma separated list of tags to exec (default All).
:type skip_tags: string
:param skip_tags: Comma separated list of tags to skip (default None).
:type firstrun: bool
:param firstrun: if it's first run (default False)
:type local: bool
:param local: if it's doing a local auto-provision (default False)
:type debug: bool
:param debug: if it's a debug run (default False)
"""
print(utils.get_provision_color_string('provision', tenant, env))
apansible.provision(tenant, env, limit, tags,
skip_tags, firstrun, local, debug)
def get(self, file, key=None):
"""
This will print the key you are searcing (or the whole file if key is not specified)
Syntax:
appflow get tenant.environment.folder.to.file.searched key.subkey.value
:type file: string
:param file: path.to.file (dot encoded) where to search the key.
:type key: string
:param key: The key to search.
"""
print(apyaml.get_value(file, key))
def set(self, file, key, value):
"""
This will modify and then print the key you are specifying.
Syntax:
appflow get tenant.environment.folder.to.file.searched key.subkey.value
:type file: string
:param file: path.to.file (dot encoded) where to set the key.
:type key: string
:param key: The key to search.
:type value: T
:param value: the value to set.
"""
print(apyaml.set_value(file, key, value))
def rm(self, file, key):
"""
This will remove and then print the key you are specifying.
Syntax:
appflow get tenant.environment.folder.to.file.searched key.subkey.value
:type file: string
:param file: path.to.file (dot encoded) where to remove the key.
:type key: string
:param key: The key to search.
"""
print(apyaml.rm_value(file, key))
def add(self, file, key, value):
"""
This will create and then print the key you are specifying.
Syntax:
appflow get tenant.environment.folder.to.file.searched key.subkey.value
:type file: string
:param file: path.to.file (dot encoded) where to set the key.
:type key: string
:param key: The key to search. (this function will add it if not found.)
:type value: T
:param value: the value to set.
"""
print(apyaml.add_value(file, key, value))
def version(self):
"""
This will print the appflow version and the current appflow-playbooks
informations.
"""
print("Appflow Version:", __version__)
playbooks_folder = utils.get_appflow_folder() + "/playbooks"
if os.path.exists(playbooks_folder):
print("Playbooks Version", open(
playbooks_folder + "/version").read())
if __name__ == '__main__':
fire.Fire(AppFlow)