#!/usr/bin/python3

import os
import sys
import json
import urllib.request
import urllib.parse
import urllib.error
import subprocess
from cloudinit.sources.DataSourceConfigDrive import find_candidate_devs, read_config_drive
from cloudinit.util import mount_cb def shell(cmd):
sp = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = sp.communicate()
return out, err # Before resetroot, We must ensure if 169.254.169.254 have gateway
for dev in find_candidate_devs():
results = mount_cb(dev, read_config_drive)
network_data = results.get("network_config")
found = dev
gateways = []
if found:
for line in network_data.splitlines():
if line.startswith("gateway"):
line = line.strip()
split_up = line.split(None, 1)
if len(split_up) <= 1:
continue
gateway = split_up[1]
gateways.append(gateway)
break if gateways:
gateway = gateways[0]
cmd = "route add -host 169.254.169.254 gw %s" % gateway
out,err = shell(cmd) # To reset password
data = urllib.request.urlopen("http://169.254.169.254/openstack/latest/meta_data.json").read().decode()
json_data = json.loads(data)
print(json_data) meta = json_data.get("meta")
if meta:
adminPass = meta.get("admin_pass")
if adminPass:
os.system("echo 'root:%s' | chpasswd" % adminPass)
params = urllib.parse.urlencode({"delete":True}).encode(encoding='UTF8')
f = urllib.request.urlopen("http://169.254.169.254/openstack/latest/password", params)
f.read()
05-21 10:46