Skip to content

Instantly share code, notes, and snippets.

@amarao
Last active January 19, 2017 19:47
Show Gist options
  • Save amarao/fba1e766cfa217b0342d0fe066aeedd7 to your computer and use it in GitHub Desktop.
Save amarao/fba1e766cfa217b0342d0fe066aeedd7 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import subprocess
import json
import sys
def deugly(obj):
try:
ret = {}
for elem in obj[1]:
ret[elem[0]] = elem[1]
return ret
except:
return obj
j = json.loads(subprocess.check_output(['ovs-vsctl', '--format=json', '--data=json', 'list', 'port']))
data = j['data']
headings = j['headings']
other_config = headings.index('other_config')
tag = headings.index('tag')
name = headings.index('name')
uuid = headings.index('_uuid')
def fix(name, uuid, old_tag, new_tag):
print "Replacing old tag {old} to new tag {new} for port {uuid} with name {name}".format(
name=name,
uuid=uuid,
old=old_tag,
new=new_tag
)
if '--fix' in sys.argv:
cmd = ['ovs-vsctl', 'set', 'port', uuid, 'tag=%s' % new_tag]
else:
cmd = ['echo', 'ovs-vsctl', 'set', 'port', uuid, 'tag=%s' % new_tag]
print cmd
print subprocess.check_output(cmd)
for obj in data:
cfg = deugly(obj[other_config])
try:
if str(cfg['tag']) != str(obj[tag]):
fix(obj[name], obj[uuid][1], obj[tag], cfg['tag'])
except KeyError:
#print "keyerror", obj[name]
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment