Last active
February 1, 2017 10:38
-
-
Save amarao/259f2ee5cd171362a4e1e96c60c810f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import subprocess | |
import sys | |
import json | |
import pprint | |
def _exec(cmd): | |
print 'exec:', cmd | |
return subprocess.check_output(['bash', '-c', cmd]) | |
serv = sys.argv[1] | |
port_list = _exec("neutron port-list --device-id=%s -q|awk '{print $2}'|grep -v -e '^$'|grep -v id" % serv).split('\n') | |
print port_list | |
for port in port_list: | |
if port: | |
print "processing port", port | |
raw = _exec("(neutron --debug port-show {port} 1>/dev/null) 2>&1| grep RESP|grep {server}".format(port=port, server=serv)).replace("RESP BODY: ", "") | |
port_info = json.loads(raw)['port'] | |
print "old port info" | |
pprint.pprint(port_info) | |
print _exec('nova interface-detach %s %s' % (serv, port)) | |
try: | |
_exec('neutron port-delete %s' % port) | |
except subprocess.CalledProcessError: | |
pass | |
fixeds = port_info['fixed_ips'][0] | |
keys = { | |
'subnet': fixeds['subnet_id'], | |
'fixed_ip': fixeds['ip_address'], | |
'tenant': port_info['tenant_id'], | |
'mac': port_info['mac_address'], | |
'net': port_info['network_id'] | |
} | |
try: | |
new_port = _exec("neutron port-create --fixed-ip subnet_id={subnet},ip_address={fixed_ip} --tenant_id={tenant} --mac-address {mac} {net} --vnic-type normal|grep '| id '".format(**keys) + "|awk '{print $4}'") | |
except Exception: | |
new_port = _exec("neutron port-create --fixed-ip subnet_id={subnet},ip_address={fixed_ip} --tenant_id={tenant} --mac-address {mac} {net} |grep '| id '".format(**keys) + "|awk '{print $4}'") | |
print "new port id", new_port | |
print _exec('nova interface-attach %s --port-id %s' % (serv, new_port)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment