/
deploy.py
executable file
·66 lines (56 loc) · 1.77 KB
/
deploy.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
#!/usr/bin/env python
# +-------------------------+
# | v0.2 by Afenioux |
# | ./deploy.py <username> |
# | tested with python 2.7 |
# +-------------------------+
from pprint import pprint
import yaml
import getpass
import sys
import time
import re
from jsonrpclib import Server
#
# used deploy the eos.conf configuration on equipement listed in sw_list.yml
# eos.conf should be a "static" file, no template in this example
#
login = sys.argv[1]
pwd=getpass.getpass("passwd:")
sname = raw_input("Type configuration session name: ")
commands = ['configure session '+sname]
with open('eos.conf') as f:
for line in f:
line = line.strip()
# this removes empty lines
if not line:
continue
# do not push comments
if re.match(r"(\s*!)", line):
continue
commands.append(line)
commands.append('show session-config diffs')
#print(commands)
#sys.exit()
f=open('sw_list.yml')
devices=yaml.load(f.read())
f.close()
for host in devices:
print("\n - - - - - -\n Proceeding with device : "+host)
#time.sleep(2)
switch = Server( "http://{}:{}@{}/command-api".format(login, pwd, host))
response = switch.runCmds( 1, [ "show version" ], 'json' )
print("Model and version are: {} - {}\n".format(response[0][ "modelName" ],response[0][ "version" ]))
response = switch.runCmds( 1, commands, 'text')
print(response[-1]['output'])
commit = raw_input("Commit? (y/N) ")
if commit in ["y", "Y", "Yes", "yes"]:
print("Committing session {}".format(sname))
response = switch.runCmds( 1, ["configure session {} commit".format(sname), 'write'], 'text')
else:
print("Rollback: aborting session {}".format(sname))
response = switch.runCmds( 1, ["configure session {} abort".format(sname)], 'text')
print("Exiting...")
sys.exit()
print("done.")
print("all done!")