#!/usr/bin/python

import os
import json
import subprocess
from cloudinit.sources.DataSourceConfigDrive import find_candidate_devs, read_config_drive
from cloudinit.util import mount_cb
from six.moves.urllib.request import urlopen
from six.moves.urllib.parse import urlencode def shell(cmd):
sp = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = sp.communicate()
return out, err INSTANCE_ID_RUN_PATH = "/run/cloud-init/.instance-id" # Ensure /run/cloud-init/.instance-id not exists so we can reload DataSource from ConfigDrive.
if os.path.isfile(INSTANCE_ID_RUN_PATH):
os.unlink(INSTANCE_ID_RUN_PATH) # Before resetroot, We must ensure if 169.254.169.254 have gateway
gateways = []
for dev in find_candidate_devs():
results = mount_cb(dev, read_config_drive)
network_data = results.get("networkdata")
networks = network_data.get("networks",None)
found = dev
if found:
if not networks:
continue
for network in networks:
routes = network.get("routes", None)
if not routes:
continue
for route in routes:
gateway = route.get('gateway')
gateways.append(gateway)
break if gateways:
gateway = gateways[0]
cmd = "route add -host 169.254.169.254 gw %s" % gateway
out,err = shell(cmd) data = urlopen("http://169.254.169.254/openstack/latest/meta_data.json").read()
json_data = json.loads(data.decode("utf-8")) meta = json_data.get("meta")
if meta:
adminPass = meta.get("admin_pass")
if adminPass:
os.system("echo 'root:%s' | chpasswd" % adminPass)
params = urlencode({"delete":True})
f = urlopen("http://169.254.169.254/openstack/latest/password", params)
f.read()

resetroot_169route_python2(用于ubuntu12.04和14.04,centos系列)的更多相关文章

  1. ubuntu12.10升级至14.04

    之前执行apt-get 不管是什么软件或apt-get update都会遇到fail to fetch http://us.archive.ubuntu.com quantal-updates/mai ...

  2. Ubuntu 16.04或14.04里下安装搜狗输入法(图文详解)(全网最简单)

    不多说,直接上干货! 其实啊,很简单 分三步走 1.添加fcitx的键盘输入法系统,因为sogou是基于fcitx的,而系统默认的是iBus: 2.安装sogou输入法: 3.设置系统参数及一些注意点 ...

  3. 在Ubuntu12.0至14.04版本之间用Apache搭建网站运行环境

    为了顺利安装各种软件,先更新下系统. apt-get update 安装Apache服务 apt-get install apache2 -y 安装php apt-get install php5 - ...

  4. 超简单让ubuntu开启wifi热点(亲测16.04与14.04可用)

    今天教大家一个简单方法让ubuntu发散wifi热点给手机或者其他设备使用. 首先,创建一个普通的热点,点击右上角的网络,然后选择下拉菜单中的编辑连接,然后出现以下界面. 然后点击增加,连接类型选接W ...

  5. 【转】Windows 7下用VMware Workstation 10虚拟机安装 Ubuntu 14.04

    一.软件下载 1.VMware Workstation v10.0.1虚拟机官方简体中文版下载(附永久KEY注册密钥) http://www.linuxidc.com/Linux/2012-11/73 ...

  6. Ubuntu 12.04 升级到14.04之后,pidgin-sipe 出现的问题: Trouble with the pidgin and self-signed SSL certificate

    Once again, I run into trouble when upgrading my LinuxMint. In last few days, my Linux mint notifies ...

  7. Windows 多用户远程访问 Ubuntu 14.04桌面

    使用X2Go实现多用户远程访问 Ubuntu 14.04桌面:VNC也可以,但是每次连接VNC就回新创建一个Seession,想要在下次远程登录的时候返回上次活动,需要记住开启的线程,这种繁琐的操作不 ...

  8. Ubuntu 14.04 禁用ipv6

    参考: 关闭IPV6,ubuntu 14.04 Ubuntu 14.04 禁用ipv6 1.检查ipv6是否开启: cat /proc/sys/net/ipv6/conf/all/disable_ip ...

  9. Ubuntu 14.04 java环境安装配置(不是openJAVA)

    两种配置方式 第一: 在 Ubuntu 中使用 PPA 安装 Java 8 ( 支持 Ubuntu 10.04 - Ubuntu 14.04 ): sudo add-apt-repository pp ...

随机推荐

  1. Java运行时异常与一般异常以及错误的异同

    Java提供了两类主要的异常:runtime exception和checked exception.checked 异常也就是我们经常遇到的IO异常,以及SQL异常都是这种异常.对于这种异常,JAV ...

  2. hadoop核心逻辑shuffle代码分析-map端 (转)

    一直对书和各种介绍不太满意, 终于看到一篇比较好的了,迅速转载. 首先要推荐一下:http://www.alidata.org/archives/1470 阿里的大牛在上面的文章中比较详细的介绍了sh ...

  3. 数据库优先生成EF CRUD演示

    ①准备我们的数据库: Northwind ②新建 实体数据模型,由数据库优先创建 ③创建控制器,这里我们只针对了Customers这张表做演示,实际会复杂的多 注:你可以把上面两步合成一步来写,创建控 ...

  4. 一个nginx反向代理, 负载均衡的例子

    #/etc/nginx/conf.d/master.conf #区分大小写 #设定负载均衡的服务器列表 upstream master.balancing { #weigth参数表示权值,权值越高被分 ...

  5. JavaScript js调用堆栈(三)

    本文主要深入介绍JavaScript内存机制 内存模型 JS内存空间分为栈(stack),堆(heap),池(一般也会归类为栈中),其中栈存放变量,堆存放复杂对象,池存放常量. 注:闭包中的变量并不保 ...

  6. 18.Shiro与Springboot整合下登陆验证UserService未注入的问题

    Shiro与Springboot整合下登陆验证UserService未注入的问题 前言: 刚开始整合的情况下,UserService一执行,就会报空指针异常. 看了网上各位大神的讲解,什么不能用ser ...

  7. 关于利用HashSet,split,deleteCharAt等方法详解

    1.首先了解一下HashSet的原理: Set接口  Set是对数学上集的抽象,Set中不包含重复的元素.如何界定是否是重复元素?Set最多可含一个null元素;对于任意的非null元素e1和e2,都 ...

  8. span没有name属性

    <span id="test" name="测试数据">测试咯</span> 在eclipse中这么写发现会有警告提示.百度发现原来sp ...

  9. 不再手写import - VSCode自动引入Vue组件和Js模块

    :first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...

  10. Linux系统修改/etc/sysconfig/i18n文件,桌面无法正常显示

    在Windows环境下使用SSH Secure Shell Client登陆VMware Workstation中Linux系统查询hive表时,中文显示乱码:数字和url显示为NULL,网上说: 1 ...