-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplay
executable file
·163 lines (133 loc) · 5.92 KB
/
play
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
#!/usr/bin/env python
"""
This tool executes sequence of AWS tasks. The sequence is loaded from a file in a given path. Default filename is tasks.yml.
You can find sample script in demo/tasks.yml.
One task can look like this:
- name: Create something for any reason
<task>:
task_param: 'some value'
tags:
- tagname
register:
- thrown event
when:
- needed event
or you can use json syntax (it is compatible with yaml):
[
{
"name": "Create something for any reason",
"<task>": {
"task_param": "some value"
},
"register": [
"thrown event"
],
"when": [
"needed event"
],
"tags": [
"tagname"
]
}
]
- attribute name can be used for description of goals and reasons for given task. It's optional. When missing, task will create the name.
- attribute <task> defines action what to do. Tasks can accept their own parameters (see list of known tasks bellow).
- attribute register is a list of events to emit if the task ends with result "created" or "changed"
- attribute when is a list of events for which the task is listening.
- attribute tags is a list of custom tags, you can set some tag at command line, only tasks containing this tag will be executed
"""
__version__ = '0.8.19'
import yaml
import tasks
import sys
import argparse
import os
import subprocess
import tasks.task_runner
import logging
import tasks.AwsSession
class DocAction(argparse.Action):
def __init__(self, option_strings, dest=argparse.SUPPRESS, help=""):
super(DocAction, self).__init__(option_strings=option_strings, dest=dest, default=argparse.SUPPRESS, nargs=0, help=help)
def __call__(self, parser, namespace, values, option_string=None):
print "Apila"
print "====="
print __doc__
tasks.print_doc()
print "Interpret usage"
print "---------------"
self.print_indented(parser.format_help())
print "Dependencies"
print "------------"
print " - python 2.7"
print " - python module yaml ( apt-get install python-yaml )"
print " - python module boto3 ( apt-get install python-pip; pip install boto3 )"
parser.exit()
def print_indented(self, text):
for line in text.splitlines():
print ' ', line
try:
columns = int(subprocess.check_output(['tput', 'cols']))
except:
columns = 80
parser = argparse.ArgumentParser(description='Setup AWS services according to set sequence of tasks.', formatter_class=lambda prog: argparse.HelpFormatter(prog, width=columns))
parser.add_argument('-V', '--version', action='version', version='%(prog)s '+__version__)
parser.add_argument('--doc', help='show known commands and exit', action=DocAction)
parser.add_argument('--debug', help='on crash show backtrace', action='store_true')
parser.add_argument('--syntax-check', help='perform a syntax check on the playbook, but don\'t execute it', action='store_true')
parser.add_argument('-r', '--register', help='explicitly register an event for an execution (can be used more times)', action='append', metavar='EVENT')
parser.add_argument('-u', '--unregister', help='explicitly unregister an event from an actual execution. Actual execution is stored in todo.yml. (can be used more times)', action='append', metavar='EVENT')
parser.add_argument('-t', '--tag', help='execute only tasks with specified tag (can be used more times)', action='append', metavar='TAG')
parser.add_argument('-e', '--exec', help='use an alternative filename to task.yml', metavar='file.yml', dest='entry_point')
parser.add_argument('-c', '--conf', help='use an alternative filename to config.yml', metavar='file.yml')
parser.add_argument('path', help='path to a folder with a AWS service setup file')
args = parser.parse_args()
logging.basicConfig(level=(logging.ERROR if args.debug else logging.CRITICAL))
os.chdir(args.path)
tasks_filename = args.entry_point if args.entry_point else 'tasks.yml'
default_conf_filename = 'config.yml'
if os.path.exists('.aws_session'):
aws_session_data = yaml.load(open('.aws_session').read())
else:
aws_session_data = dict()
aws_session = tasks.AwsSession.AwsSession(aws_session_data.get("serial_number", ""),
aws_session_data.get("secret_access_key", ""),
aws_session_data.get("session_token", ""),
aws_session_data.get("access_key_id", ""))
aws_session.validate()
open('.aws_session', 'w').write(yaml.dump(aws_session.get_data_dict(), default_flow_style=False))
print "Processing content of %s executing %s ..." % (os.getcwd(), tasks_filename)
conf_filename = args.conf if args.conf else default_conf_filename
if conf_filename != default_conf_filename:
print "Using alternative config %s ..." % conf_filename
with open(conf_filename) as conf_file:
config = yaml.load(conf_file.read())
for tag, constructor in tasks.get_yaml_tags_constructors(config).iteritems():
yaml.add_constructor(tag, constructor[0])
with open(tasks_filename) as tasks_file:
receipt = yaml.load(tasks_file.read())
task_list = [tasks.create_task(task_def, config) for task_def in receipt]
errors = []
for task in task_list:
task.validate(errors)
if len(errors) > 0:
print 'Script contains some error:\n %s' % '\n '.join(errors)
sys.exit(1)
if args.syntax_check:
print 'Syntax of %d tasks ok.' % len(task_list)
sys.exit(0)
cache = tasks.Cache()
clients = tasks.Clients(aws_session)
if os.path.exists('todo.yml'):
todo = set(yaml.load(open('todo.yml').read()))
print "Events restored from todo.yml:", ', '.join(map(lambda x: "'%s'" % x, todo))
else:
todo = set()
if args.register:
todo.update(args.register)
if args.unregister:
todo.difference_update(args.unregister)
registered = todo
tags = args.tag
tasks.task_runner.run_tasks(task_list, clients, cache, registered, tags)
tasks.task_runner.clear_todo()