由于Openstack更新很快,现在准备搭建基于Queen版本的Openstack,Queen版本要求keystone版本为V3,所以之前大多数接口都不能用了,百度了一下都没有比较新的实例,官方文档又过于简单。所以简单总结下各客户端的调用方式,非常简单的案例,仅供参考。

1 前期准备

1确保已经安装了python

2根据需求安装openstack相关服务调用的python库

pip install python-openstackclient

确保版本大于3.0.0,所有服务调用都基于keystone,一定要安装

pip install python-keystoneclient
pip install python-heatclient
pip install python-glanceclient
pip install python-novaclient
pip install python-neutronclient

3如果安装的openstack的所有的入口ip都映射为controller,我们就需要在本机的/etc/hosts中也加一条映射

<ip> controller

2 获取客户端连接

1创建keystone session :最近版本的openstack所有服务都只能keystone v3版本才能调用,并且推荐session的方式。

from keystoneauth1.identity import v3
from keystoneauth1 import session def get_keystone_session():
# auth_url为keystone的endpoint入口,新版本openstack中(Tenant租户改名为project)
auth = v3.Password(auth_url="http://<ip>:5000/v3", username="",password="", project_name="",user_domain_id="", project_domain_id="")
sess = session.Session(auth=auth)
return sess

2获取keystone客户端

from keystoneclient.v3 import client as keyclient

def get_keystone_client():
sess = get_keystone_session()
keystone = keyclient.Client(session=sess)
return keystone

3获取nova客户端

from novaclient import client

def get_nova_client():
sess = get_keystone_session()
nova = client.Client(2, session=sess)
return nova

4获取glance客户端

from glanceclient import Client

def get_glance_client():
sess = get_keystone_session()
glance = Client('2', session=sess)
return glance

5获取neutron客户端

from neutronclient.v2_0 import client as ntclient

def get_neutron_client():
sess = get_keystone_session()
neutron = ntclient.Client(session=sess)
return neutron

6获取heat客户端

heat客户端的获取比较复杂,尝试了多种方式发现只能通过keystone客户端返回的token的认证

from heatclient import client as hclient

def get_heat_client():
creds = {}
creds['username'] = ''
creds['password'] = ''
creds['auth_url'] = 'http://<ip>:5000/v3'
creds['project_name'] = ''
ks_client = keyclient.Client(**creds)
heat_endpoint = ks_client.service_catalog.url_for(service_type='orchestration', endpoint_type='publicURL')
# 后来需求Heat服务单独改为HTTPS,可以在以下参数中加入 insecure=True
heat = hclient.Client('1', heat_endpoint, token=ks_client.auth_token)
return heat

3简单测试

之前的获取客户端方式写在openstackapi.py中方便调用

1keystone

import openstackapi as api

keystone = api.get_keystone_client()
list = keystone.projects.list()
for p in list:
print p

2nova

python-novaclient8之后network被移除,应该用neutron代替

import openstackapi as api

def nova_create_flavor(nova, instance_name):
nova.servers.create(instance_name) def show_server_info(nova):
instances = nova.servers.list()
for instance in instances:
print instance def show_flavor_info(nova):
flavors = nova.flavors.list()
for flavor in flavors:
print flavor def get_instance_id(nova, instance_name):
instances = nova.servers.list()
for instance in instances:
if instance.name == instance_name:
return instance.id def get_instance(nova, instance_id):
return nova.servers.get(instance_id) def get_flavor_id(nova, flavor_name):
flavors = nova.flavors.list()
for flavor in flavors:
if flavor.name == flavor_name:
return flavor.id if __name__ == '__main__':
nova = api.get_nova_client()
show_server_info(nova)
show_flavor_info(nova)
id = get_instance_id(nova, <name>)
print id
instance = get_instance(nova,id)
print '################################'
print instance

3glance

创建上传qcow2镜像,删除镜像

import openstackapi as api

def list_image():
list = glance.images.list()
for image in list:
print image.name, image.id, image.status def get_id_by_name(name):
list = glance.images.list()
id = ''
for image in list:
if image.name == name:
id = image.id
return id if __name__ == "__main__":
glance = api.get_glance_client()
print '####### list #########'
list_image()
name = "test-py-api"
glance.images.create(name=name, disk_format="qcow2", container_format="bare", is_public="true")
print '####### list after create #########'
list = list_image()
id = get_id_by_name(name)
glance.images.upload(id , open('/目录/***.qcow2', 'rb'))
print '####### list after upload qcow2 #########'
list_image()
glance.images.delete(id)
print '####### list after delete qcow2 #########'
list_image()

4neutron

import openstackapi as api

def show_network_info(neutron):
print ' #### network list ####'
networks = neutron.list_networks()
for network in networks:
print network if __name__ == '__main__':
neutron = api.get_neutron_client()
show_network_info(neutron)

5heat

首先写好一个heat编排模板,然后通过接口上传并且自动执行编排

import openstackapi as api
from heatclient.common import template_utils def get_heat_file():
path = "/目录/***.yaml"
tpl_files, template = template_utils.get_template_contents(path)
create_fields = {
'stack_name': '',
'disable_rollback': 'false',
'parameters': '',
'template': template,
'files': dict(list(tpl_files.items()))
}
return create_fields def list_stack(heat):
list = heat.stacks.list()
for stack in list:
print stack if __name__ == "__main__":
heat = api.get_heat_client()
create_fields = get_heat_file()
heat.stacks.create(**create_fields)
list_stack(heat)

4接口汇总

from keystoneauth1.identity import v3
from keystoneauth1 import session
from keystoneauth1 import loading
from keystoneclient.v3 import client as keyclient
from glanceclient import Client
from novaclient import client
from neutronclient.v2_0 import client as ntclient
from heatclient import client as hclient def get_keystone_session():
loader = loading.get_plugin_loader('password')
auth = v3.Password(auth_url="http://<Openstack Controller Ip>:5000/v3", username="admin",password="<pwd>", project_name="admin",user_domain_id="default", project_domain_id="default")
sess = session.Session(auth=auth)
return sess def get_nova_client():
sess = get_keystone_session()
nova = client.Client(2, session=sess)
return nova def get_glance_client():
sess = get_keystone_session()
glance = Client('2', session=sess)
return glance def get_keystone_client():
sess = get_keystone_session()
keystone = keyclient.Client(session=sess)
return keystone def get_neutron_client():
sess = get_keystone_session()
neutron = ntclient.Client(session=sess)
return neutron def get_heat_client():
creds = {}
creds['username'] = 'admin'
creds['password'] = 'admin1234'
creds['auth_url'] = 'http://<Openstack Controller Ip>:5000/v3'
creds['project_name'] = 'admin'
ks_client = keyclient.Client(**creds)
heat_endpoint = ks_client.service_catalog.url_for(service_type='orchestration', endpoint_type='publicURL')
heat = hclient.Client('1', heat_endpoint, token=ks_client.auth_token)
return heat def list_images():
glance = get_glance_client()
list = glance.images.list()
return list def show_images():
list = list_images()
for image in list:
print image.name, image.id, image.status def get_image_id_by_name(image_name):
list = list_images()
id = ''
for image in list:
if image.name == image_name:
id = image.id
return id
return id def upload_image(image_name, image_path):
glance = get_glance_client()
glance.images.create(name=image_name, disk_format="qcow2", container_format="bare", is_public="true")
id = get_image_id_by_name(image_name)
glance.images.upload(id , open(image_path, 'rb')) def delete_image(image_name):
glance = get_glance_client()
id = get_image_id_by_name(image_name)
if id != '':
glance.images.delete(id)

Python调用 Openstack 主要服务(keystone,nova,glance,neutron,heat)的更多相关文章

  1. python调用openstack的api,create_instance的程序解析

    python调用openstack的api,create_instance的程序解析 2017年10月17日 15:27:24 CloudXli 阅读数:848   版权声明:本文为博主原创文章,未经 ...

  2. 安装OpenStack计算服务(nova)

    1. 配置数据库 数据库安装在控制节(controller)点上 $ mysql -u root -p 2.创建 glance 数据库 CREATE DATABASE nova; GRANT ALL ...

  3. openstack (4)---部署Glance镜像服务,Nova计算服务

    一.Glance Glance是Openstack项目中负责镜像管理的模块,其功能包括虚拟机镜像的查找.注册和检索等. Glance提供Restful API可以查询虚拟机镜像的metadata及获取 ...

  4. CentOS7安装OpenStack(Rocky版)-03.安装Glance镜像服务组件(控制节点)

    上篇文章分享了keystone的安装配置,本文接着分享openstack的镜像服务glance. --------------- 完美的分割线 ---------------- 3.0.glance概 ...

  5. 云计算管理平台之OpenStack计算服务nova

    一.nova简介 nova是openstack中的计算服务,其主要作用是帮助我们在计算节点上管理虚拟机的核心服务:这里的计算节点就是指用于提供运行虚拟机实例的主机,通常像这种计算节点有很多台,那么虚拟 ...

  6. openstack Q版部署-----nova服务配置-控制节点(5)

    一.创建数据库(控制节点) 创建数据库以及用户: CREATE DATABASE nova_api; CREATE DATABASE nova; CREATE DATABASE nova_cell0; ...

  7. OpenStack 计算服务 Nova计算节点部署(八)

    如果使用vmware虚拟机进行部署,需要开启虚拟化:如果是服务器需要在bios上开启. nova计算节点IP是192.168.137.12 环境准备 安装时间同步 yum install ntpdat ...

  8. OpenStack 计算服务 Nova介绍和控制节点部署(七)

    介绍 Nova是openstack最早的两块模块之一,另一个是对象存储swift.在openstack体系中一个叫做计算节点,一个叫做控制节点.这个主要和nova相关,我们把安装为计算节点nova-c ...

  9. OpenStack 计算服务 Nova介绍和控制节点部署 (八)

    一)nova在keystone上服务注册 1.1创建nova务实体 [root@controller ~]# source admin-openrc [root@controller ~]# open ...

随机推荐

  1. C#开发BIMFACE系列21 服务端API之获取模型数据6:获取单模型的楼层信息

    系列目录     [已更新最新开发文章,点击查看详细] 一个文件/模型中可能包含多个楼层信息,获取楼层信息对于前端页面的动态展示非常有帮助.本篇介绍获取一个文件/模型中可能包含多个楼层信息的详细方法. ...

  2. 【LeetCode】[0001] 【两数之和】

    题目描述 思路分析 Java代码 代码链接 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标.你可以假设每种输入只会对 ...

  3. 题解 洛谷P1071【潜伏者】

    题目链接:https://www.luogu.org/problem/P1071 题意概括:给你一段原来截获的英文密码和与之对应的明文,如果密码表非F♂A法,输出"Failed"  ...

  4. Vue之render渲染函数和JSX的应用

    一.模板缺陷 模板的最大特点是扩展难度大,不易扩展.可能会造成逻辑冗余 <Level :type="1">哈哈</Level> <Level :typ ...

  5. 2019最新WEB全栈架构师第八期视频教程

    下载链接:https://www.yinxiangit.com/117.html

  6. FJUT2019暑假周赛三部分题解

    A本来想改到q<1e5,让你们预处理的,然后想了哈作为个逆元模板题吧= =,做不出来自行反思. B贴个题面 因为只有一次机会,那么也就是两点分布期望E = p了,先说说大家的做法,先求出每个n的 ...

  7. java树形结构工具类

    一.树形结构数据一般都是以子父id的形式存在数据库中,查询的时候只是带有子id和parent_id的List集合 并不是树形结构,所以我们现在要将普通的List集合转换为树结构数据(本工具类扩展操作树 ...

  8. CSS——段落处理

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. caffe学习三:使用Faster RCNN训练自己的数据

    本文假设你已经完成了安装,并可以运行demo.py 不会安装且用PASCAL VOC数据集的请看另来两篇博客. caffe学习一:ubuntu16.04下跑Faster R-CNN demo (基于c ...

  10. 03 jvm的组成

    声明:本博客仅仅是一个初学者的学习记录.心得总结,其中肯定有许多错误,不具有参考价值,欢迎大佬指正,谢谢!想和我交流.一起学习.一起进步的朋友可以加我微信Liu__66666666 这是简单学习一遍之 ...