forked from iranzo/rhevm-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathovirt-vlan.py
executable file
·91 lines (74 loc) · 3.18 KB
/
ovirt-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
#!/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 ovirt_functions import *
description = """
RHEV-vlan is a script for creating via API new VLAN's in RHEV and attach it to DC/Cluster/hosts.
"""
#Parse args in ovirt_functions: parseoptions(basename, description, args)
options = parseoptions(sys.argv[0], description, sys.argv[1:])
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))