forked from ops-snappy/ops-aaa-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoprovision.py
executable file
·260 lines (207 loc) · 7.51 KB
/
autoprovision.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
#!/usr/bin/env python
# Copyright (C) 2015-2016 Hewlett Packard Enterprise Development LP
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import sys
import urllib2
import httplib
import cookielib
import ovs.dirs
import ovs.vlog
import ovs.db.idl
from ovs.db import error
# ovs definitions
idl = None
# OPS_TODO: Need to pull this from the build env
DEF_DB = 'unix:/var/run/openvswitch/db.sock'
CFGDB_SCHEMA = '/usr/share/openvswitch/configdb.ovsschema'
OVS_SCHEMA = '/usr/share/openvswitch/vswitch.ovsschema'
type_startup_config = "startup"
SYSTEM_TABLE = "System"
AAA_TRUE_FLAG = "True"
PERFORMED = "performed"
URL = "url"
AUTOPROVISION_SCRIPT = '/var/tmp/autoprovision'
AUTOPROVISION_STATUS_FILE = '/var/local/autoprovision'
# VLOG
vlog = ovs.vlog.Vlog("ops_aaautilspamcfg")
vlog.init("/var/log/messages")
# ------------------ wait_for_config_complete() ----------------
def wait_for_config_complete(idl):
system_is_configured = 0
while True:
idl.run()
for ovs_rec in idl.tables[SYSTEM_TABLE].rows.itervalues():
if ovs_rec.cur_cfg is not None and ovs_rec.cur_cfg != 0:
system_is_configured = ovs_rec.cur_cfg
break
if(system_is_configured != 0):
break
poller = ovs.poller.Poller()
idl.wait(poller)
poller.block()
#------------------ check_for_startup_config() ----------------
def check_for_startup_config(remote):
'''
Connect to the db server and specify the configdb database.
Walk through the rows in the config table (if any)
looking for a row with type == startup.
'''
schema_helper_cfg = ovs.db.idl.SchemaHelper(location=CFGDB_SCHEMA)
schema_helper_cfg.register_table("config")
idl_cfg = ovs.db.idl.Idl(remote, schema_helper_cfg)
# Sequence number when we last processed the db.
seqno = idl_cfg.change_seqno
# Wait until the ovsdb sync up.
while (seqno == idl_cfg.change_seqno):
idl_cfg.run()
poller = ovs.poller.Poller()
idl_cfg.wait(poller)
poller.block()
tbl_found = False
for ovs_rec in idl_cfg.tables["config"].rows.itervalues():
if ovs_rec.type:
if ovs_rec.type == type_startup_config:
if ovs_rec.config:
tbl_found = True
else:
vlog.err("startup config row does not have config column")
break
if not tbl_found:
vlog.err("No startup config rows found in the config table")
idl_cfg.close()
return tbl_found
# ------------------ fetch_autoprovision_script() ----------------
def fetch_autoprovision_script(url):
ret = False
try:
cj = cookielib.CookieJar()
header = {'User-Agent': 'OPS-AutoProvision/1.0', 'OPS-MANUFACTURER':
'OpenSwitch', 'OPS-VENDOR': 'OpenSwitch'}
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
request = urllib2.Request(url, None, header)
vlog.info("Sending HTTP GET to %s" % url)
f = opener.open(request)
data = f.read()
f.close()
opener.close()
except urllib2.HTTPError, e:
print('HTTPError = ' + str(e.code))
return ret
except urllib2.URLError, e:
print('URLError = ' + str(e.reason))
return ret
except httplib.HTTPException, e:
print('HTTPException = ' + str(e.reason))
return ret
except Exception, e:
print('generic exception: ' + str(e))
return ret
if os.path.exists(AUTOPROVISION_SCRIPT):
os.remove(AUTOPROVISION_SCRIPT)
if ("OPS-PROVISIONING" in data):
try:
autoprovision_file = open(AUTOPROVISION_SCRIPT, "wb")
autoprovision_file.write(data)
autoprovision_file.close()
ret = True
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
return ret
except Exception, e:
print('generic exception: ' + str(e))
return ret
else:
vlog.err("Error, downloaded autoprovision script does not contain"
"OPS-PROVISIONING string in comment")
ret = False
return ret
#------------------ update_autoprovision_status() ----------------
def update_autoprovision_status(performed_value, url):
global idl
data = {}
data[PERFORMED] = performed_value
data[URL] = url
# create the transaction
txn = ovs.db.idl.Transaction(idl)
for ovs_rec in idl.tables[SYSTEM_TABLE].rows.itervalues():
break
setattr(ovs_rec, "auto_provisioning_status", data)
txn.commit_block()
return True
############################### main ###########################
def main():
global idl
argv = sys.argv
n_args = 2
if len(argv) != n_args:
print("Requires %d arguments but %d provided \n" % (n_args, len(argv)))
return
# Locate default config if it exists
schema_helper = ovs.db.idl.SchemaHelper(location=OVS_SCHEMA)
schema_helper.register_columns(SYSTEM_TABLE, ["cur_cfg"])
schema_helper.register_columns(SYSTEM_TABLE,
["auto_provisioning_status"])
idl = ovs.db.idl.Idl(DEF_DB, schema_helper)
seqno = idl.change_seqno # Sequence number when last processed the db
# Wait until the ovsdb sync up.
while True:
idl.run()
if(seqno != idl.change_seqno):
break
poller = ovs.poller.Poller()
idl.wait(poller)
poller.block()
wait_for_config_complete(idl)
if os.path.exists(AUTOPROVISION_STATUS_FILE):
vlog.info("Autoprovisioning already completed")
update_autoprovision_status(AAA_TRUE_FLAG, argv[1])
idl.close()
return
if(fetch_autoprovision_script(argv[1]) is False):
print("Downloading autoprovisioning script failed")
idl.close()
return
sys.stdout.flush()
ret = 1
if os.path.exists(AUTOPROVISION_SCRIPT):
ret = os.system('chmod +x ' + AUTOPROVISION_SCRIPT)
ret = os.system(AUTOPROVISION_SCRIPT)
if (ret == 0):
try:
autoprovision_file = open(AUTOPROVISION_STATUS_FILE, "w")
autoprovision_file.close()
except IOError as e:
print "Creating autoprovision status file, I/O error({0}): \
{1}".format(e.errno, e.strerror)
idl.close()
return
except Exception, e:
print('Creating autoprovision status file,'\
'generic exception: ' + str(e))
idl.close()
return
update_autoprovision_status(AAA_TRUE_FLAG, argv[1])
vlog.info("Autoprovision status: performed = %s URL = %s"
% (AAA_TRUE_FLAG, argv[1]))
else:
vlog.err("Error, executing autoprovision script returned error %d"
% ret)
idl.close()
if __name__ == '__main__':
try:
main()
except error.Error, e:
print("Error: \"%s\" \n" % e)