-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudlets_redirects.py
executable file
·109 lines (88 loc) · 3.95 KB
/
cloudlets_redirects.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
#! /bin/env python
#
# distributed from ansible
#
# this module being imported in akapi
import json, os
import difflib
import csv
import urlparse as parse
def get_current_version(cloudlets_live_file, httpCaller, apisession, akconfig):
version_path = "/cloudlets/api/v2/policies/%s/versions" % policy
version_result = httpCaller.getResult(version_path)
version = version_result[0]["version"]
headers = {'Content-Type': 'application/octet-stream'}
path = "/cloudlets/api/v2/policies/{}/versions/{}/download".format(policy, version)
baseurl = 'https://{}/'.format(akconfig.get('default','host'))
products_result = apisession.get(parse.urljoin(baseurl,path),headers=headers)
current_version = '{}-{}.csv'.format(cloudlets_live_file, version)
with open(current_version, 'w') as f:
writer = csv.writer(f)
reader = csv.reader(products_result.text.splitlines())
for row in reader:
writer.writerow(row)
return current_version
def cloudlets_activate(policy, version, activate, httpCaller):
# now we know which version and we've set the policy
activation_path = "/cloudlets/api/v2/policies/%s/versions/%s/activations" % (policy, version)
print "activating cloudlets version {} in {}".format(version, activate)
activation_obj = {
"network": activate
}
activation_result = httpCaller.postResult(activation_path, json.dumps(activation_obj))
def create_cloudlets_version(ticket, partner, httpCaller):
print"creating cloudlets version {} {}".format(ticket, partner)
new_version_info = {
"description":"{} Adding a new partner {} match rules".format(ticket, partner),
"matchRuleFormat":"1.0",
"matchRules": []
}
create_path = "/cloudlets/api/v2/policies/%s/versions" % policy
create_result = httpCaller.postResult(create_path, json.dumps(new_version_info))
def cloudlets_edge_redirector(args, httpCaller, apisession, akconfig):
ticket = args.ticket
partner = args.partner
activate = args.activate
if cloudlets_version == "create" and activate != "production":
# get the current version and save it in tmp for processing later
old_version = get_current_version(cloudlets_live_file, httpCaller, apisession, akconfig)
create_cloudlets_version(ticket, partner, httpCaller)
# get the new verson to upload rules.
version_path = "/cloudlets/api/v2/policies/%s/versions" % policy
version_result = httpCaller.getResult(version_path)
# print json.dumps(version_result)
version = version_result[0]["version"]
# print version
# now we know which version and we've set the policy
# Open the JSON filename with mappings and activate in staging,
# print the diff at the end
if activate == "staging":
with open(filename) as data_file:
data = json.load(data_file)
for rule in data:
rule_path = "/cloudlets/api/v2/policies/%s/versions/%s/rules" % (policy, version)
rule_result = httpCaller.postResult(rule_path, json.dumps(rule))
# print rule_result
print "uploading rules......"
# get the new version to compare before activating in staging
new_version = get_current_version(cloudlets_live_file, httpCaller, apisession, akconfig)
with open(old_version, 'rb') as f1:
file_one = f1.readlines()
with open(new_version, 'rb') as f2:
file_two = f2.readlines()
print os.linesep.join(difflib.unified_diff(file_one, file_two))
# clean up files after the diff
for f in [ old_version, new_version ]:
if os.path.exists(f):
os.remove(f)
# activate the new version in stage
cloudlets_activate(policy, version, activate, httpCaller)
else:
# assuming we have version activated in staging
# go ahead and activate in prod
cloudlets_activate(policy, version, activate, httpCaller)
cloudlets_version = "create"
cloudlets_live_file = "/tmp/cloudlets_live"
# set up policy and redirect file
policy = "12345"
filename = "redirects.json"