-
Notifications
You must be signed in to change notification settings - Fork 5
/
migrate-compute.py
executable file
·215 lines (173 loc) · 7.25 KB
/
migrate-compute.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
#!/usr/bin/env python
import ConfigParser
import socket
import argparse
import MySQLdb
import netifaces
from novanet2neutron import common
from novanet2neutron import utils
from novanet2neutron import virt
CONF = ConfigParser.ConfigParser()
def build_devmap():
mappings = {}
for device in netifaces.interfaces():
try:
mac = netifaces.ifaddresses(device)[netifaces.AF_LINK][0]['addr']
mappings[mac] = device
except:
print("Skipping interface: %s" % device)
return mappings
class NetworkMigration(object):
def __init__(self, network, neutron_client):
self.network = network
self.neutron_client = neutron_client
class NeutronMigration(NetworkMigration):
tap_prefix = 'vnet'
def get_old_bridge(self):
return self.network['bridge']
def get_new_bridge(self):
return utils.get_neutron_bridge_name(self.network['id'])
def get_new_tap(self, instance, index):
ports = self.neutron_client.list_ports(device_id=instance.id,
network_id=self.network['id'])
if not ports['ports']:
print "%s ERROR no neutron port found, cannot migrate" % instance.id
return None
if len(ports['ports']) != 1:
print "%s ERROR multiple neutron ports found, cannot migrate" % instance.id
return None
new_tap = utils.get_neutron_tap_device_name(ports['ports'][0].get('id'))
return new_tap
class NovaMigration(NetworkMigration):
tap_prefix = 'tap'
def get_old_bridge(self):
return utils.get_neutron_bridge_name(self.network['id'])
def get_new_bridge(self):
return self.network['bridge']
def get_new_tap(self, instance, index):
return utils.get_nova_vnet_name(index)
def migrate_interfaces(noop, migrate_manager, neutronc,
cursor, networks, instances):
errors = False
device_map = build_devmap()
tap_index = 0
for network in networks:
manager = migrate_manager(network, neutronc)
old_bridge = manager.get_old_bridge()
new_bridge = manager.get_new_bridge()
raw_device = network['device']
# remove interfaces from bridge
interfaces = utils.get_interfaces_on_bridge(old_bridge)
for interface in interfaces:
# Remove all instance taps from bridge
if interface != raw_device and interface.startswith(manager.tap_prefix):
utils.rm_dev_from_bridge(noop, old_bridge, interface)
if not utils.device_exists(new_bridge):
# Remove pyhsical interfce from bridge
utils.rm_dev_from_bridge(noop, old_bridge, raw_device)
# rename bridge
utils.rename_net_dev(noop, old_bridge, new_bridge)
interfaces = utils.get_interfaces_on_bridge(new_bridge)
if raw_device not in interfaces:
# Add raw device back onto bridge
utils.add_dev_to_bridge(noop, new_bridge, raw_device)
for instance in instances:
print "Migrating %s" % instance.id
mac_address = common.get_mac_db(cursor, instance, network['nova_name'])
if not mac_address:
continue
if instance.status in ['SHUTOFF', 'SUSPENDED']:
old_tap = None
else:
new_tap = manager.get_new_tap(instance, tap_index)
if not new_tap:
errors = True
continue
tap_index += 1
if not utils.device_exists(new_tap):
# Rename tap
mac = mac_address.replace('fa:', 'fe:', 1)
old_tap = device_map[mac]
utils.rename_net_dev(noop, old_tap, new_tap)
if new_tap not in interfaces:
# add interface to bridge
utils.add_dev_to_bridge(noop, new_bridge, new_tap)
# Don't do this anymore as breaks RH based instances
#if not virt.has_virt_device(instance, new_tap):
# virt.virt_switch_interface(noop, instance, mac_address,
# old_bridge, old_tap,
# new_bridge, new_tap)
return errors
def collect_args():
parser = argparse.ArgumentParser(description='novanet2neutron.')
parser.add_argument('-f', '--for-realsies', action='store_true',
default=False, help="Actually do things")
parser.add_argument('-d', '--direction', action='store',
default='neutron', required=True,
help="Either migrate to 'nova' or 'neutron'")
parser.add_argument('-c', '--config', action='store',
default='compute.conf', help="Config file")
return parser.parse_args()
def get_network(neutronc, net_id):
network = neutronc.list_networks(id=net_id)['networks'][0]
subnets = neutronc.list_subnets(network_id=network['id'])['subnets']
for subnet in subnets:
network['subnet_v%s' % subnet['ip_version']] = subnet['id']
return network
def main():
args = collect_args()
common.load_config(CONF, args.config)
noop = True
if args.for_realsies:
noop = False
else:
print "Running migration in noop mode, use -f to actually migrate things"
direction = args.direction
conn = MySQLdb.connect(
host=CONF.get('db', 'host'),
user=CONF.get('db', 'user'),
passwd=CONF.get('db', 'password'),
db=CONF.get('db', 'name'))
cursor = MySQLdb.cursors.DictCursor(conn)
url = CONF.get('creds', 'auth_url')
username = CONF.get('creds', 'username')
password = CONF.get('creds', 'password')
tenant = CONF.get('creds', 'tenant_name')
host = socket.gethostname()
novac = common.get_nova_client(username=username,
password=password,
tenant=tenant,
url=url)
neutronc = common.get_neutron_client(username=username,
password=password,
tenant=tenant,
url=url)
networks = []
for section in CONF.sections():
if section.startswith('network_'):
network_id = CONF.get(section, 'neutron_net_id')
network = get_network(neutronc, network_id)
for option in ('device', 'bridge', 'nova_name'):
network[option] = CONF.get(section, option)
networks.append(network)
instances = common.all_servers(novac, host=host)
if direction == 'neutron':
manager = NeutronMigration
elif direction == 'nova':
manager = NovaMigration
else:
print "unknown direction"
print "Running checks"
errors = migrate_interfaces(True, manager, neutronc, cursor,
networks, instances)
if not noop and not errors:
print "running for real"
migrate_interfaces(False, manager, neutronc,
cursor, networks, instances)
if errors:
print "ERROR: Cannot run due to errors"
cursor.close()
conn.close()
print "SUCCESS!"
if __name__ == "__main__":
main()