forked from iranzo/rhevm-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rhev-vlan.py
executable file
·111 lines (92 loc) · 4.56 KB
/
rhev-vlan.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
#!/usr/bin/env python
#
# Author: Pablo Iranzo Gomez ([email protected])
#
# Description: Script for creating VLAN in datacenter and attach to cluster and it's hosts
#
# Requires rhevm-sdk to work
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import optparse
from rhev_functions import *
description = """
RHEV-vlan is a script for creating via API new VLAN's in RHEV and attach it to DC/Cluster/hosts.
"""
# Option parsing
p = optparse.OptionParser("rhev-vlan.py [arguments]", description=description)
p.add_option("-u", "--user", dest="username", help="Username to connect to RHEVM API", metavar="admin@internal",
default="admin@internal")
p.add_option("-w", "--password", dest="password", help="Password to use with username", metavar="admin",
default="admin")
p.add_option("-k", action="store_true", dest="keyring", help="use python keyring for user/password", metavar="keyring",
default=False)
p.add_option("-W", action="store_true", dest="askpassword", help="Ask for password", metavar="admin", default=False)
p.add_option("-s", "--server", dest="server", help="RHEV-M server address/hostname to contact", metavar="127.0.0.1",
default="127.0.0.1")
p.add_option("-p", "--port", dest="port", help="API port to contact", metavar="443", default="443")
p.add_option('-v', "--verbosity", dest="verbosity", help="Show messages while running", metavar='[0-n]', default=0,
type='int')
p.add_option('-d', "--datacenter", dest="datacenter", help="datacenter to create the vlan at", metavar='datacenter')
p.add_option('-l', "--vlan", dest="vlan", help="VLAN ID", metavar='vlan')
p.add_option('-n', "--vlanname", dest="vlanname", help="VLANname", metavar='vlanname')
p.add_option('-c', "--cluster", dest="cluster", help="Cluster to attach to", metavar='cluster')
p.add_option('-b', "--bond", dest="bond", help="Bond to create under", metavar='bond', default="bond0")
(options, args) = p.parse_args()
options.username, options.password = getuserpass(options)
baseurl = "https://%s:%s" % (options.server, options.port)
api = apilogin(url=baseurl, username=options.username, password=options.password)
if __name__ == "__main__":
dc = options.datacenter
vlan = options.vlan
if not options.vlanname:
vlanname = "VLAN_%s" % vlan
else:
vlanname = options.vlanname
datacenter = api.datacenters.get(name=dc)
description = "Network for %s %s" % (vlanname, vlan)
nueva = params.Network(name=vlanname, data_center=datacenter, vlan=params.VLAN(id=vlan), description=description)
nueva.vlan_id = int(vlan)
try:
red = api.networks.add(nueva)
except:
print("ERROR creating VLAN %s with ID %s" % (vlanname, vlan))
red = api.networks.get(name=vlanname)
if not red:
print("Network %s was not found, exitting" % vlanname)
sys.exit(1)
if red.name != vlanname:
print("ERROR Found network is not the same as the VLAN we're trying to add!!!!")
sys.exit(1)
if options.cluster:
if options.verbosity > 4:
print("Attaching network %s to cluster" % red.name)
cluster = api.clusters.get(name=options.cluster)
try:
cluster.networks.add(red)
except:
if options.verbosity > 4:
print("Network %s already attached to cluster" % red.name)
query = "cluster = %s" % api.clusters.get(id=cluster.id).name
for host in paginate(api.hosts, query):
if host.cluster.id == cluster.id:
if options.verbosity > 4:
print("Host %s is in cluster" % host.name)
accion = params.Action(network=params.Network(name=red.name))
tarjeta = host.nics.get(name=options.bond)
try:
tarjeta.attach(accion)
except:
if options.verbosity > 4:
print("Error attaching network %s to NIC %s" % (red.name, tarjeta.name))
try:
host.commitnetconfig()
except:
if options.verbosity > 4:
print("Error commiting network %s config to host %s" % (red.name, host.name))