ironic的自动化脚本
# -*- coding:utf-8 -*-
import json
import subprocess
import os
import time
import random trunk_start, trunk_end = 51, 128
can_used_trunks = set(range(trunk_start, trunk_end))
_cache_available_switch_trunks = {}
resource_class = {}
hp_info = {
"vcpus": "",
"memory_mb": "",
"local_gb": "",
"vendor": "HP"
}
resource_class['hp'] = hp_info
dell256_info = {
"vcpus": "",
"memory_mb": "",
"local_gb": "",
"vendor": "Dell Inc."
}
resource_class['dell256'] = dell256_info
dell512_info = {
"vcpus": "",
"memory_mb": "",
"local_gb": "",
"vendor": "Dell Inc."
}
resource_class['dell512'] = dell512_info
hw_info = {
"vcpus": "",
"memory_mb": "",
"local_gb": "",
"vendor": ""
}
resource_class['huawei'] = hw_info def shell(cmd):
sp = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = sp.communicate()
return out, err def get_node_info(node):
uuid = node.get("UUID")
cmd = "openstack baremetal node show %s -f json" % uuid
out, err = shell(cmd)
return json.loads(out) def doinspect(node):
uuid = node.get("UUID")
cmd = "openstack baremetal node inspect %s " % uuid
shell(cmd)
os.system('echo %s >>inspect_log' % cmd)
time.sleep(10) def port_group_node(kwargs):
node_uuid = kwargs['UUID']
# kwargs['node_uuid'] = node_uuid
### The bond_mode here is fixed as 'balance-rr'
kwargs.setdefault('bond_mode', 'balance-rr')
ports = kwargs.get('ports', None)
# if not ports:
# print "the node_info hava no ports, please check"
# return
switch_ids = set()
for port in ports:
switch_id = port['Local Link Connection']['switch_id']
switch_ids.add(switch_id)
port['switch_id'] = switch_id
index = 0
for switch_id in switch_ids:
eth_truck = _cache_available_switch_trunks[switch_id].pop()
kwargs['eth_trunk'] = eth_truck
kwargs['group_name'] = "pg_%s_%s" % (kwargs['Name'], index)
index += 1
cmd = ("openstack --os-baremetal-api-version 1.26 baremetal port group create "
"--node %(UUID)s --name %(group_name)s "
"--mode %(bond_mode)s --property miimon=100 --property Eth-Trunk=%(eth_trunk)s "
"--support-standalone-ports -f json" % kwargs)
print(cmd)
out, err = shell(cmd)
group = json.loads(out)
if not group:
_cache_available_switch_trunks[switch_id].append(eth_trunk)
group_uuid = group['uuid']
print('node:%s group:%s' % (node_uuid, group_uuid))
for port in ports:
if port['switch_id'] == switch_id:
cmd = "openstack --os-baremetal-api-version 1.26 baremetal port set %s --port-group %s" % (
port['UUID'], group_uuid)
print(cmd)
out, err = shell(cmd)
print(err) def get_available_trunk(used_trunks):
for switch_id, used_tunks in used_trunks.items():
swich_availe_trunks = _cache_available_switch_trunks.get(switch_id, None)
if swich_availe_trunks is None:
swich_availe_trunks = (can_used_trunks - set(used_tunks))
_cache_available_switch_trunks[switch_id] = swich_availe_trunks def check_resource_class(node_info):
name = node_info.get('Name')
node_res_info = {
"vcpus": node_info['Properties'].get('cpus', ""),
"memory_mb": node_info['Properties'].get('memory_mb', ""),
"local_gb": node_info['Properties'].get('local_gb', ""),
"vendor": node_info['Extra'].get('system_vendor', {}).get('manufacturer', "")
}
current_resource_class = node_info['Resource Class']
for i in resource_class:
if cmp(resource_class[i], node_res_info) == 0:
if i == current_resource_class:
os.system('echo "%s resource class ok" >> res_check' % name)
else:
cmd = ("ironic node-update %s replace resource_class=%s" % (name, i))
shell(cmd)
os.system('echo %s >> res_check' % cmd)
else:
writelog = "%s invaild properties %s" % (name, node_res_info)
os.system('echo %s >> res_check' % writelog) def domanage(nodes):
for node in nodes:
name = node.get("Name")
cmd = "openstack baremetal node manage %s" % name
shell(cmd)
os.system('echo %s >> exec_log' % cmd) def getports():
cmd = 'openstack baremetal port list --long -f json'
out, err = shell(cmd)
return json.loads(out) def getportgroups():
cmd = 'openstack baremetal port group list --long -f json'
out, err = shell(cmd)
return json.loads(out) def add_port(nodes_dict, ports):
for port in ports:
node_uuid = port["Node UUID"]
node = nodes_dict.get(node_uuid, None)
if node:
node["ports"].append(port) def add_portgroup(nodes_dict, ports_info, portgroups_info, switch_infos=None):
for port in ports_info:
node_uuid = port["Node UUID"]
group_uuid = port['Portgroup UUID']
if not group_uuid:
continue
node = nodes_dict.get(node_uuid, None)
if node:
node_portgroups = node['portgroups']
node_portgroups.setdefault(group_uuid, {})
node_portgroup = node_portgroups[group_uuid]
node_portgroup.setdefault('ports', [])
node_portgroup['ports'].append(port)
switch_id = port['Local Link Connection']['switch_id']
default_switch_id = node_portgroup.setdefault('switch_id', switch_id)
if switch_id != default_switch_id:
print('ERROR::::portgroup %s switch_id(%s %s)' % (group_uuid, switch_id, default_switch_id))
for portgroup in portgroups_info:
node_uuid = portgroup['Node UUID']
group_uuid = portgroup['UUID']
node = nodes_dict.get(node_uuid, None)
if node:
node_portgroups = node["portgroups"]
node_portgroup = node_portgroups[group_uuid]
switch_id = node_portgroup['switch_id']
eth_trunk = portgroup['Properties']['Eth-Trunk']
node_portgroup['eth_trunk'] = eth_trunk
if isinstance(switch_infos, dict):
switch_infos.setdefault(switch_id, {})
switch_infos[switch_id][group_uuid] = eth_trunk def portgroup_list_to_dict(portgroups):
portgroup_dict = {}
for portgroup in portgroups:
group_uuid = portgroup['UUID']
portgroup_dict[group_uuid] = portgroup
return portgroup_dict def get_used_trunk(ports, portgroups):
portgroup_dict = portgroup_list_to_dict(portgroups)
switch_trunks_used = {}
for port in ports:
switch_id = port['Local Link Connection']['switch_id']
switch_info = switch_trunks_used.setdefault(switch_id, {})
group_uuid = port['Portgroup UUID']
if not group_uuid:
continue
switch_info[group_uuid] = portgroup_dict[group_uuid]['Properties']['Eth-Trunk']
ret = {}
for switch_id, trunks in switch_trunks_used.items():
ret[switch_id] = sorted(trunks.values())
return ret def node_list_to_dict(nodes):
nodes_dict = {}
for node in nodes:
if node["Provisioning State"] not in ["enroll"]:
node_uuid = node["UUID"]
node["ports"] = []
node["portgroups"] = {}
nodes_dict[node_uuid] = node
return nodes_dict def doprovide(nodes):
switch_infos = {}
ports_info = getports()
portgroups_info = getportgroups()
random.shuffle(nodes)
nodes_dict = node_list_to_dict(nodes)
add_port(nodes_dict, ports_info)
add_portgroup(nodes_dict, ports_info, portgroups_info, switch_infos)
used_trunks = get_used_trunk(ports_info, portgroups_info)
get_available_trunk(used_trunks)
print _cache_available_switch_trunks
node_need_provide = []
for uuid, node in nodes_dict.items():
properties = node['Properties']
if properties:
# check resource class
check_resource_class(node)
# check node port
node_ports = node["ports"]
node_portgroups = node['portgroups']
if len(node_ports) ==2:
if not node_portgroups or len(node_portgroups.keys()) < 1:
port_group_node(node)
else:
node_need_provide.append(uuid)
else:
output = 'echo "%s %s port num:%s" >>ports_wrong'
os.system(output % (name, state, len(node_ports)))
else:
doinspect(node)
time.sleep(10)
for node in node_need_provide:
cmd='openstack baremetal node provide %s' % node
out, err = shell(cmd)
if err:
print err def get_nodes():
cmd = "openstack baremetal node list --long -f json"
out, err = shell(cmd)
return json.loads(out) def dowithstatus(nodes):
enro_list = []
manage_list = []
for node in nodes:
name = node.get("Name")
stat = node.get("Provisioning State")
if stat == 'enroll':
enro_list.append(node)
elif stat == 'manageable':
manage_list.append(node)
elif stat == 'inspect failed':
os.system("echo 'error! node %s status :inspect failed' >> node_faild " % name)
elif stat == 'error':
os.system("echo 'error! node %s status : error' >> node_faild " % name)
else:
pass
allnodes = {"enroll": enro_list, "manageable": manage_list}
allfuncs = {'enroll': domanage, 'manageable': doprovide}
for key in ("enroll", "manageable"):
nodes = allnodes.get(key)
func = allfuncs.get(key)
if nodes:
func(nodes) if __name__ == "__main__":
while True:
nodes_info = get_nodes()
dowithstatus(nodes_info)
time.sleep(10)
ironic的自动化脚本的更多相关文章
- 自动化脚本中click()或sendKeys()没有反应
前提: 排除xpath引用错误或元素的xpath每次都不同的情形. 问题描述 自动化脚本中click()方法和sendKeys()方法报错, 返回异常InvocationTargetException ...
- appium-desktop录制脚本二次开发,生成我司自动化脚本
目的 通过对appium-desktop脚本录制功能进行二次开发,使录制的java脚本符合我司自动化框架要求. 实现步骤 1.增加元素名称的输入框 由于ATK(我司自动化测试框架)脚本中元素是以“ap ...
- Jenkins构建自动化脚本执行无界面解决方法
场景: jenkins构建selenium自动化用例的时候,会有jenkins自带服务后台运行自动化脚本,可无界面运行IE.Chrome.Firefox. 然而运行IE浏览器时候(IE比较特殊),Je ...
- 【Zabbix】Zabbix-agent自动化脚本
zabbix-agent自动化脚本 作用:批量部署zabbix-agent.用于上百台虚拟机都可以被Zabbix监控. 脚本名:inst-agent.sh #!/bin/bash echo " ...
- PHP学习日记 Windows配置PHP+Nginx+自动化脚本
Windows配置PHP+Nginx+自动化脚本 安装与配置 PHP 下载PHP:传送门 选择合适的版本下载 尽量选Thread Safe 配置PHP: 解压后在文件夹中找到php.ini-devel ...
- python_selenium之第一个自动化脚本
python_selenium之第一个自动化脚本 上一节介绍了xpath的使用,接下来完成第一个自动化脚本 一.步骤: 1. 这里使用火狐浏览器,首先打开火狐浏览器 2. 使浏览器窗口最大化 3.输入 ...
- 【转】jenkins上配置robotframeworkride自动化脚本任务
jenkins上配置robotframeworkride自动化脚本任务 编写好的自动化脚本,集成在jenkins上进行自动运行于监控,这里采用分布式构建,在一台slave上进行任务构建与自动化脚本的运 ...
- Python+selenium第一个自动化脚本
第一个自动化脚本(用Python写的) from selenium import webdriver #从selenium导入webdriber driver=webdriber.Firefox() ...
- Jmeter 接口自动化-脚本数据分离实例
一. 背景: 为了让大家更加的了解Jmeter,并且使用起来游刃有余.这篇我们主要讲一下,如何优雅的使用Jmeter一步步的实现接口自动化,完成脚本与数据分离,把可能对Jmeter脚本的维护转移到c ...
随机推荐
- 1.6 NBU Catalog备份还原
用户的数据保存到了磁盘或者磁带中,并且是安全的,NBU所在的机器还有可能发生故障,需要重新安装或者将NBU部署到其他的机器中继续使用. 在这种情况下,如何让NBU知道用户已经存在的备份策略和存储单元配 ...
- Spring MVC的一些学习笔记-入门配置和HttpMessageConverter
1.初步配置 [1]. 配置web.xml以及在web.xml中配置DispatcherServlet: <context-param> <param-name>context ...
- 改Chrome的User Agent,移动版网络
理论上访问手机版或者iPad等平板电脑版的网络,应该可以剩些流量的,毕竟移动网络是经过优化压缩的,但是PC电脑如果访问移动版的网站呢?我主要使用的浏览器是Chrome,这几天也找了下Chrome下的修 ...
- 动态生成的DOM做点击事件无效
有时候我们的标签都是从后台获取的数据,然后利用JS添加到页面上,当我们写生成的标签的点击事件(click)时没有效果. 例如: <section> 测试动态生成的DOM点击事件 <b ...
- 2.初识CronTrigger
开发工具:Eclipse 代码下载链接:https://github.com/theIndoorTrain/QuartzDemo.git 前言: 在1.初始Quartz里面,我们介绍了quartz的一 ...
- iOS面试题总结(持续更新)
过段时间打算跳槽,找了一些面试题来做,在这里做个总结方便review,希望能对要面试的童鞋有帮助. 以下为面试题: 运行以下代码会有什么结果 NSString *str1 = @"str1& ...
- JNDI整理
JNDI 什么是JNDI JNDI全称为Java Naming and Directory Interface,命名及目录查找接口,是java平台的一种标准扩展,它提供了一系列接口.类和命名空间的概念 ...
- C++ 类型转换(conv.)
隐式类型转换 总结自:隐式类型转换&算数运算符 定义:隐式类型转换是指使用了与表达式规定或当前语境不相符的类型时所进行的类型转换,但是要注意,可能会存在转换出现歧义,从而无法通过编译;一切带有 ...
- using namespace std 是什么意思?
摘录CSDN上面大牛的回答简要意思就是使用标准库,想知道更清楚的继续读下面的. using namespace std 意思: using 和namespace都是C++的关键词. ...
- Python面向对象--高级(一)
## 属性的类型 - 属性可分为类属性和实例属性 - 实例属性可以通过在类中使用self定义,或者直接在类外部使用实例变量定义 class Person(object): def __init__(s ...