-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpan_vpn_automation.py
executable file
·200 lines (162 loc) · 6.83 KB
/
pan_vpn_automation.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
#!/usr/bin/env python
import os, sys, subprocess
import json, uuid, time
class CommandProcessor:
def __init__(self):
pass
def run_command(self, cmd):
cmd_stat = None
try:
cmd_stat = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
output, err = cmd_stat.communicate()
status = cmd_stat.wait()
print "Command output: ", output
print "Command exit status / return code: ", status
except Exception, e:
print("Return code: {}\n"\
"Exception: {}".format(cmd_stat.returncode, e)
)
def run_check_output(self, cmd):
output = subprocess.check_output(cmd, shell=True)
print output
if "Updating" in output:
return 1
else:
return 0
def retry_run_check_output(self, cmd):
cntr = 0
print "Retrying command..."
while True and cntr < 600:
output = subprocess.check_output(cmd, shell=True)
print output
if "Updating" in output:
time.sleep(10)
continue
elif "Succeeded" in output:
"Command successfully completed"
return
def construct_command(self, command, azure_hndle, resource_name, request_json_filename):
"""
Construct the command to be executed.
"""
final_cmd = "{} -op:{} -subscriptionid:{} -apiversion:{} -endpoint:{} -resourcegroup:{} -resourcename:{} -requestjsonfile:{}"\
.format(azure_hndle.azure_driver_filename, command, azure_hndle.subscription, azure_hndle.apiversion,
azure_hndle.endpoint, azure_hndle.resource_group, resource_name, request_json_filename)
return final_cmd
class Azure:
commands_list = {
"PutVirtualWan": "",
"PutVpnSite": "",
"PutVirtualHub": "",
"PutVpnGateway": "",
"GetVpnConfiguration": ""
}
command_order = ['PutVirtualWan', 'PutVpnSite', 'PutVirtualHub','PutVpnGateway']
get_commands = {
"PutVirtualHub": "GetVirtualHub",
"PutVpnGateway": "GetVpnGateway",
"PutVirtualWan": "GetVirtualWan",
"PutVpnSite": "GetVpnSite"
}
def __init__(self, azure_driver_filename, resource_group, resource_prefix, subscription, apiversion, endpoint, **kwargs):
self.azure_driver_filename = azure_driver_filename
self.subscription = subscription
self.apiversion = apiversion
self.endpoint = endpoint
self.resource_group = resource_group
self.cmd_to_file_map = self._construct_cmd_to_config_map(**kwargs.get('config_filemap'))
self.resource_prefix = resource_prefix
self.resource_names = kwargs['resource_names']
def __str__(self):
return "Azure Details: \n"\
"Subscription: {}\n"\
"API Version: {}\n"\
"Endpoint: {}\n"\
"Resource Names: {}\n"\
"Command to File Map: {}\n".format("***************",
self.apiversion,
self.endpoint,
self.resource_names,
self.commands_list)
def _construct_cmd_to_config_map(self, **kwargs):
for key, value in kwargs.items():
if "wan" in key:
self.commands_list['PutVirtualWan'] = value
elif "site" in key:
self.commands_list['PutVpnSite'] = value
elif "hub" in key:
self.commands_list["PutVirtualHub"] = value
elif "gateway" in key:
self.commands_list['PutVpnGateway'] = value
def get_resource_name_for_command(self, command):
"""
Retrieve the name of the resource to use for the command.
"""
if "VirtualWan" in command:
return self.resource_names['VirtualWanName']
elif "VpnSite" in command:
return self.resource_names['VpnSiteName']
elif "VirtualHub" in command:
return self.resource_names['VirtualHubName']
elif "VpnGateway" in command:
return self.resource_names['VpnGatewayName']
def parse_output(self, output):
#print output
print type(output)
sl = output.splitlines()
for _line in sl:
if 'provisioningState' in _line:
_v = _line.split(":")
print _v[1], type(_v[1])
def parse_azure_config_file(filename):
"""
Parse and configure the Azure Interface
"""
data = None
with open(filename, 'r') as fd:
data = json.load(fd)
resource_files = data.get('azure_resources', None)
if not resource_files:
raise Exception('The Azure resource files have not been populated."\
Please check the json configuration file and populate these sections.')
az_hndl = Azure(data.get('azure_driver_filename'),
data.get('resource_group'),
data.get('resource_prefix'),
data.get('subscription'),
data.get('apiversion'),
data.get('endpoint'),
**resource_files
)
print str(az_hndl)
return az_hndl
def main():
print "****************************************"
print "Palo Alto Networks VPN Automation System"
print "****************************************"
if len(sys.argv) != 2:
print "Usage: python pan_vpn_automation.py <azure virtual wan JSON filename>"
sys.exit(0)
az_hndl = parse_azure_config_file(sys.argv[1])
cmdp = CommandProcessor()
for _cmd in az_hndl.command_order:
cur_cmd = None
resource_name = az_hndl.get_resource_name_for_command(_cmd)
cur_cmd = cmdp.construct_command(_cmd, az_hndl, resource_name, az_hndl.commands_list.get(_cmd))
time.sleep(5)
print "*****************************"
print "Executing Operation: {} Resource Name: {}".format(_cmd, resource_name)
print "*****************************"
output = cmdp.run_check_output(cur_cmd)
if output:
_new_op = az_hndl.get_commands.get(_cmd)
print _new_op
_new_cmd = cmdp.construct_command(_new_op, az_hndl, resource_name, az_hndl.commands_list.get(_cmd))
cmdp.retry_run_check_output(_new_cmd)
print "*****************************"
print "Executing command: {} complete".format(cur_cmd)
print "*****************************"
print "****************************************"
print "Completed the provisioning of the Azure Virtual WAN."
print "****************************************"
if __name__ == "__main__":
main()