# -*- 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. Uva 11572 唯一的雪花

    题目链接:https://uva.onlinejudge.org/external/115/11572.pdf 题意:找到一个尽量长的连续子序列 Al ~ AR ,使得该序列没有相同的元素. 分析:枚 ...

  2. ORA-01262,oracle启动报错,及Oracle启动原理

    错误状态: SQL> startup ORA-01261: Parameter db_recovery_file_dest destination string cannot be transl ...

  3. 你不得不掌握的thinkphp5

    thinkphp官网在去年的时候发布了tp的颠覆版本thinkphp5,tp5确实比之前的版本好用了很多,增加了很多的一些特性,它采用全新的架构思想,引入了更多的PHP新特性,优化了核心,减少了依赖, ...

  4. js图片库 案例

    事件处理函数:事件处理函数的作用是,在特定事件发生时调用特定的JavaScript代码.本例中想要在用户点击某个链接的时候触发一个动作,所以需要使用onclick事件处理函数. 添加事件处理函数的语法 ...

  5. Spring的jdbcTemplate 与原始jdbc 整合c3p0的DBUtils 及Hibernate 对比 Spring配置文件生成约束的菜单方法

    以User为操作对象 package com.swift.jdbc; public class User { private Long user_id; private String user_cod ...

  6. iOS程序猿如何快速掌握 PHP,化身"全栈攻城狮"?

    这是一篇以 iOS 开发人员的视角写给广大iOS 程序猿的 PHP 入门指南.在这篇文章里我努力去发掘 objectiv-c 与 php 之间的共性,来帮助有一定 iOS 开发经验的攻城狮来快速上手一 ...

  7. Java基础-方法区以及static的内存分配图

    转载自: https://blog.csdn.net/Wang_1997/article/details/52267688 前面的几篇都没有太明确地指出 方法区 是什么?现在通过一些资料的收集和学习, ...

  8. RabbitMQ安装---rpm安装

    首先介绍一下个人的安装环境是Linux-centos7: 一.安装和配置rabbitmq的准备工作: 下载erlang:    wget http://www.rabbitmq.com/release ...

  9. opendaylight安装

    OpenDaylight安装 环境 jdk-1.8 maven 环境配置安装 Java环境 查看Java环境 java  -version 安装jdk-1.8 yum install java-1.8 ...

  10. js面向(基于)对象编程—类(原型对象)与对象

    JS分三个部分: 1. ECMAScript标准--基础语法 2. DOM  Document Object Model 文档对象模型 3. BOM  Browser Object Moldel 浏览 ...