-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlaunch_direct.py
148 lines (139 loc) · 5.69 KB
/
launch_direct.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
# for python 2.7 need few modifications
# on Windows machine
# PATH=%PATH%;c:\Python27;c:\Python27\Scripts
# export PATH=$PATH:/usr/lib/python2.7/dist-packages/ansible
from __future__ import print_function
# NOTE: SyntaxError: from __future__ imports must occur at the beginning of the file
import yaml
# NOTE: ImportError: No module named 'yaml'
# there is no pyyaml in LibreOoffice embedded Python
# Windows (Python 2.7 intalled):
# path=%path%;c:\Python27;c:\Python27\scripts
# pip2 install pyyaml
# python ...
# python3 launch_direct.py --input classification.yaml --debug --environment prod --datacenter eastcoast --role server --nodes e44820191,e44820192,e44820193 --password env:USERPROFILE
# role="service-discovery-server-0"
# datacenter="eastcoast"
# environment="prod"
#
# role="service-discovery-server-1"
# datacenter="eastcoast"
# environment="prod"
import getopt
import sys
import re
import pprint
from os import getenv
import json, base64
def get_column(argument):
# dictionary of arguments to columns
# TODO: dynamic pull of identical part
argument_columns = {
'role': 'consul_role'
}
# print('argument: {}'.format(argument))
# print('argument_columns: {}'.format(argument_columns))
if argument in argument_columns:
return argument_columns.get(argument)
else:
return argument
def get_argument_file(argument):
if re.match('^@.*', argument):
argument_filename = argument[1:]
if debug:
print("idenfified argument as file name: {}".format(argument_filename))
with open(argument_filename) as argument_file:
data = re.split(r'\W*\r?\n\W*', argument_file.read())
else:
if debug:
print("Option {} value: {}\nargument:{}".format(option, argument))
data = re.split(r'\W*,\W*', argument)
# NOTE: trailing space leftover needs to be handled
return data
def get_value(argument):
if re.match('^env:*', argument):
return getenv(re.sub('env:', '', argument))
else:
return argument
if __name__ == '__main__':
pp = pprint.PrettyPrinter(indent=2)
# https://docs.python.org/2/library/getopt.html
try:
opts, args = getopt.getopt(sys.argv[1:], 'hdi:e:c:r:n:p:', ['help', 'debug', 'input=', 'environment=','datacenter=', 'role=', 'nodes=', 'password='])
except getopt.GetoptError as err:
print('usage: launch_direct.py --input <classification file> --role <role> --environment <environment> --datacenter <datacenter>')
print(str(err))
exit()
input_file = None
datacenter = None
environment = None
password = None
nodes = []
role = None
global debug
debug = False
for option, argument in opts:
if option in ( '-d', '--debug'):
debug = True
elif option in ('-h', '--help'):
print('usage: launch_direct.py --input <classification file> --role <role> --environment <environment> --datacenter <datacenter>')
exit()
elif option in ('-e', '--environment'):
environment = argument
elif option in ('-p', '--password'):
password = get_value(argument)
elif option in ('-n', '--nodes'):
nodes = get_argument_file(argument)
# if re.match('^@.*', argument):
# argument_filename = argument[1:]
# if debug:
# print("idenfified argument as file name: {}".format(argument_filename))
# with open(argument_filename) as argument_file:
# nodes = re.split(r'\W*\r?\n\W*', argument_file.read())
# else:
# if debug:
# print("Option {} value: {}\nargument:{}".format(option, argument))
# nodes = re.split(r'\W*,\W*', argument)
# # NOTE: trailing space leftover needs to be handled
elif option in ('-r', '--role'):
role = argument
elif option in ('-c', '--datacenter'):
datacenter = argument
elif option in ('-i', '--input'):
input_file = argument
else:
assert False, 'unhandled option: {}'.format(option)
if debug:
print("input_file={}\nrole={}\ndatacenter={}\nenvironment={}\nnodes={}\npassword={}".format(input_file,role,datacenter,environment,nodes,password))
if input_file == None or role == None or datacenter == None or environment == None:
print('usage: launch_direct.py --input <classification file> --role <role> --environment <environment> --datacenter <datacenter>')
exit()
classification = yaml.load(open(input_file))
# /*, Loader=yaml.FullLoader */)
# AttributeError: 'module' object has no attribute 'FullLoader'
if debug:
# print sample entry
pp.pprint(classification[list(classification.keys())[0]])
for host,host_data in classification.iteritems():
if debug:
print('inspecting host: {0}'.format(host))
role_column = get_column('role')
if debug:
# AttributeError: 'dict' object has no attribute 'role'
# print('inspecting role: {0}'.format(host_data.role))
print('inspecting role: {0}'.format(host_data.get(role_column)))
# https://stackoverflow.com/questions/33727149/dict-object-has-no-attribute-has-key
# AttributeError: 'dict' object has no attribute 'has_key'
if role_column in host_data and re.match('^.*{}.*'.format(role), host_data[role_column]):
if debug:
print('inspecting datacenter: {0}'.format(host_data.get('datacenter')))
if 'datacenter' in host_data and host_data['datacenter'] == datacenter:
if debug:
print('inspecting environment: {0}'.format(host_data.get('environment')))
if 'environment' in host_data and re.match('^{}.*'.format(environment), host_data['environment']):
if debug:
print(host_data)
print("role=\"{}\"\ndatacenter=\"{}\"\nenvironment=\"{}\"\n".format(host_data[role_column],host_data['datacenter'],host_data['environment']))