# -*- 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的自动化脚本的更多相关文章

  1. 自动化脚本中click()或sendKeys()没有反应

    前提: 排除xpath引用错误或元素的xpath每次都不同的情形. 问题描述 自动化脚本中click()方法和sendKeys()方法报错, 返回异常InvocationTargetException ...

  2. appium-desktop录制脚本二次开发,生成我司自动化脚本

    目的 通过对appium-desktop脚本录制功能进行二次开发,使录制的java脚本符合我司自动化框架要求. 实现步骤 1.增加元素名称的输入框 由于ATK(我司自动化测试框架)脚本中元素是以“ap ...

  3. Jenkins构建自动化脚本执行无界面解决方法

    场景: jenkins构建selenium自动化用例的时候,会有jenkins自带服务后台运行自动化脚本,可无界面运行IE.Chrome.Firefox. 然而运行IE浏览器时候(IE比较特殊),Je ...

  4. 【Zabbix】Zabbix-agent自动化脚本

    zabbix-agent自动化脚本 作用:批量部署zabbix-agent.用于上百台虚拟机都可以被Zabbix监控. 脚本名:inst-agent.sh #!/bin/bash echo " ...

  5. PHP学习日记 Windows配置PHP+Nginx+自动化脚本

    Windows配置PHP+Nginx+自动化脚本 安装与配置 PHP 下载PHP:传送门 选择合适的版本下载 尽量选Thread Safe 配置PHP: 解压后在文件夹中找到php.ini-devel ...

  6. python_selenium之第一个自动化脚本

    python_selenium之第一个自动化脚本 上一节介绍了xpath的使用,接下来完成第一个自动化脚本 一.步骤: 1. 这里使用火狐浏览器,首先打开火狐浏览器 2. 使浏览器窗口最大化 3.输入 ...

  7. 【转】jenkins上配置robotframeworkride自动化脚本任务

    jenkins上配置robotframeworkride自动化脚本任务 编写好的自动化脚本,集成在jenkins上进行自动运行于监控,这里采用分布式构建,在一台slave上进行任务构建与自动化脚本的运 ...

  8. Python+selenium第一个自动化脚本

    第一个自动化脚本(用Python写的) from selenium import webdriver  #从selenium导入webdriber driver=webdriber.Firefox() ...

  9. Jmeter 接口自动化-脚本数据分离实例

    一. 背景:  为了让大家更加的了解Jmeter,并且使用起来游刃有余.这篇我们主要讲一下,如何优雅的使用Jmeter一步步的实现接口自动化,完成脚本与数据分离,把可能对Jmeter脚本的维护转移到c ...

随机推荐

  1. vuejs动态组件和v-once指令

    场景,点击某个按钮,两个子组件交替显示 <div id='root'> <child-one v-if='type==="child-one"'></ ...

  2. DevExpress控件经验集合

    关于GridControl的可以先看这里:http://blog.csdn.net/dong413876225/article/details/8313094 增加新行,我用了AddNewRow,但是 ...

  3. C#浏览器中在线操作文档

    源码地址:https://github.com/SeaLee02/FunctionModule   文件夹 UploadFiles/WebDemo/COM/OnlineEdit.aspx 就是源码 用 ...

  4. 牛客小白月赛2 D 虚虚实实 【欧拉图】【连通图】

    链接:https://www.nowcoder.com/acm/contest/86/D来源:牛客网 题目描述 震为雷,临危不乱,亨通畅达:巽为风,柔顺伸展,厚载万物. 震卦:洊雷,震,君子以恐惧修省 ...

  5. Oracle口令文件管理

    Oracle的口令文件目录 $ORACLE_HOME/dbs/orapw$ORACLE_SID 建立口令文件 orapwd file=$ORACLE_HOME/dba/orapw$ORACLE_SID ...

  6. Mysql: pt-table-checksum 和 pt-table-sync 检查主从一致性,实验过程

    一.安装 percona 包 1.安装仓库的包 https://www.percona.com/doc/percona-repo-config/yum-repo.html sudo yum insta ...

  7. linux mysql5.7 安装、 开机启动

    一.安装 wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz h ...

  8. 记一次samba排错 Failed to start Samba SMB Daemon.

       记录一次服务出错排错的过程,很多新手出了点错不百度直接巴拉巴拉的问,一般老手根据经验可以给出一点建议,但是由于个体环境的差异并不适用,反而埋怨起来.这种真的无F**K可说,所以要培养自己的排错能 ...

  9. dts--framework(一)

    dts 大体框架 framework 定义类 定义方法 tests framework调用所需要的函数 ./dpdk/usertools/cpu_layout.py /sys/devices/syst ...

  10. Mysql 索引 简介

    Mysql索引 索引的分类 索引的创建 索引的注意事项 什么是索引 索引是存储引擎用于快速查找记录的一种数据结构. 索引由数据库中一列或者多列组成,作用是提高表的查询速度. 索引的优点,提高检索数据的 ...