OpenStack 数据库操作 demo
#!/usr/bin/env python from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Float, Boolean sql_connection = "mysql+pymysql://nova:533e988a566a25d4@192.168.200.100:3306/nova"
engine = create_engine(sql_connection, echo=True)
Session = sessionmaker(bind=engine)
session = Session() BASE = declarative_base() class InstanceTypes(BASE):
__tablename__ = "instance_types" id = Column(Integer, primary_key=True)
name = Column(String(255))
memory_mb = Column(Integer)
vcpus = Column(Integer)
root_gb = Column(Integer)
ephemeral_gb = Column(Integer)
flavorid = Column(String(255))
swap = Column(Integer, nullable=False, default=0)
rxtx_factor = Column(Float, nullable=False, default=1)
vcpu_weight = Column(Integer, nullable=True)
disabled = Column(Boolean, default=False)
is_public = Column(Boolean, default=True) flavors = session.query(InstanceTypes).all() for flavor in flavors:
print flavor.id, flavor.name
执行结果:
[root@controller ~]# python sql.py
2018-11-01 22:35:08,943 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'
2018-11-01 22:35:08,943 INFO sqlalchemy.engine.base.Engine ()
2018-11-01 22:35:08,944 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()
2018-11-01 22:35:08,944 INFO sqlalchemy.engine.base.Engine ()
2018-11-01 22:35:08,944 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8' and `Collation` = 'utf8_bin'
2018-11-01 22:35:08,944 INFO sqlalchemy.engine.base.Engine ()
2018-11-01 22:35:08,946 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1
2018-11-01 22:35:08,946 INFO sqlalchemy.engine.base.Engine ()
2018-11-01 22:35:08,946 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1
2018-11-01 22:35:08,947 INFO sqlalchemy.engine.base.Engine ()
2018-11-01 22:35:08,947 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin AS anon_1
2018-11-01 22:35:08,947 INFO sqlalchemy.engine.base.Engine ()
2018-11-01 22:35:08,948 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2018-11-01 22:35:08,949 INFO sqlalchemy.engine.base.Engine SELECT instance_types.id AS instance_types_id, instance_types.name AS instance_types_name, instance_types.memory_mb AS instance_types_memory_mb, instance_types.vcpus AS instance_types_vcpus, instance_types.root_gb AS instance_types_root_gb, instance_types.ephemeral_gb AS instance_types_ephemeral_gb, instance_types.flavorid AS instance_types_flavorid, instance_types.swap AS instance_types_swap, instance_types.rxtx_factor AS instance_types_rxtx_factor, instance_types.vcpu_weight AS instance_types_vcpu_weight, instance_types.disabled AS instance_types_disabled, instance_types.is_public AS instance_types_is_public
FROM instance_types
2018-11-01 22:35:08,949 INFO sqlalchemy.engine.base.Engine ()
1 m1.medium
2 m1.tiny
3 m1.large
4 m1.xlarge
5 m1.small
6 4Vcpu_4096Mem_50Disk_disk0type
7 m1.vcomputer
DEMO2:
#!/usr/bin/env python from nova.db.sqlalchemy.models import Instance
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from oslo_config import cfg
from oslo_db import options as oslo_db_options CONF = cfg.CONF
CONF.register_opts(oslo_db_options.database_opts, 'database')
CONF(project='nova', default_config_files=['/etc/nova/nova.conf']) def get_session():
sql_connection = CONF.database.connection
engine = create_engine(sql_connection, echo=False)
session = sessionmaker(bind=engine) return session() session = get_session()
instances = session.query(Instance).all()
for instance in instances:
print instance.uuid, instance.vm_state, instance.display_name
运行测试:
[root@controller ~]# python sql.py
3b2618d6-e939--a5a2-402529c54ffc deleted centos01
aec899d3-71ee-4e1e-b61b-519e0b57b30b deleted centos02
72ba077b-fdc8-422d-a246-62b0fc172170 deleted centos02
c40003b4----2d01374ba910 deleted centos02
1d735ae7-e17d-465d-a223-ba059daff3ee deleted centos02
58fe007f-b4d6-48c9-ae02- deleted centos03
88e13b55-5a96-4ea6-927a-5e3b04c9d5d9 deleted centos02
b1454d87---92ec-55cda2231cf5 deleted centos01
33b50afe-dbea--a3b0-27712d903639 deleted centos04
a18d964d-86a0-4e68-92ee-83ce2fb2134e deleted centos05
ca44a126-bf6c-4a2e-a929-6a8c98e0dbfc deleted centos06
3bdc3bef-d8a3-4cea-bc3f-d622fcd4fe14 active vm1
eb6f2354-a473--b424-fe24768dcee5 active vm2
fb307b80-006a-488c-b2e0-c63080bd68ab active vm3
35ac1093-b518-4cf2-a709-bc5e359bd90b active vm4
-86f6-4eda-b584-de9e13617017 active vm5
-ca87-4eea-b94c-80bc8d1fb91b active vm6
6af8a528-33ac-435d-a059-26b68aff2c95 deleted vm7
b1ae14b9-6e7c-43a7-b1cc-bc9c2573a005 deleted vm8
db705de1-4d3d-47d5--a45f95bec82b deleted vmaa
3e62700b-70d4-4b66-b086-407cc01b816b deleted vmbb
OpenStack 数据库操作 demo的更多相关文章
- cordova-sqlite-plugin常用数据库操作
ionic项目需要做本地缓存,研究了一番,总结出了常用的数据库操作: 1.新建数据库,用angularJS+ionic: //声明一个数据库对象 var db = null; //事件devicere ...
- Python之路【第八篇】:堡垒机实例以及数据库操作
Python之路[第八篇]:堡垒机实例以及数据库操作 堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient ...
- 安卓 SQLite数据库操作实例
前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...
- 在安卓开发中使用SQLite数据库操作实例
前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...
- iOS 中SQLite数据库操作
在iOS中实现SQLite数据库的操作:1.导入框架(libsqlite3.0.tbd) 2.导入头文件<sqlite3.h> 3.实现数据的增删改查 实现简单 SQLite数据库操作 的 ...
- iOS 数据库操作(使用FMDB)
iOS 数据库操作(使用FMDB) iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.Plausibl ...
- iOS学习笔记(十五)——数据库操作(SQLite)
SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的 ...
- iOS学习笔记(十六)——数据库操作(使用FMDB)
iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.PlausibleDatabase.sqlitepers ...
- Python之路:堡垒机实例以及数据库操作
一.堡垒机前戏 开发堡垒机之前,先学习Python的paramiko模块,该模块基于SSH用于连接远程服务器并执行相关操作. SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: ...
随机推荐
- php自定义session存储路径
1.找到php.ini配置文件,找到session.save_path,修改如下: 其中2表示session存储的目录深度,也就是分目录,避免一个目录下文件太多,造成IO负担. session.sav ...
- MySql删除表、数据
程度从强到弱 1.drop table tb drop将表格直接删除,没有办法找回 2.truncate (table) tb 删除表中的所有数据,不能与where一起使用 ...
- css样式优先级和权重问题
内联样式: <div style="font-size: 12px;">姓名</div> 外部样式: <link rel="styleshe ...
- .net core webapi 部署windows server 2008 r2 笔记
WebAPI部署文档 安装dotnet-dev-win-x64.1.0.4 安装DotNetCore.1.1.0-WindowsHosting 安装vc_redist.x64 安装Windows6.1 ...
- Codeforces 670D1. Magic Powder - 1 暴力
D1. Magic Powder - 1 time limit per test: 1 second memory limit per test: 256 megabytes input: stand ...
- FTP 搭建
FTP 搭建 FTP 是 File Transfer Protocol(文件传输协议)的英文简称,它工作在 0SI 模型的第七层,TCP 模型的第四屋上,即应用层. 一.FTP 简介 FTP 会话时包 ...
- C语言点滴
static修饰的变量和函数不可以在其他文件extern引用该变量或者函数. static变量放在静态内存区. static变量赋值只生效一次,再无法调用赋值语句.但是可以运算,例如++等. exte ...
- ubuntu16下Elasticsearch5.1.1安装部署
本人在安装es5.1.1版本时候整理的一些过程,参照了网上部分过程:其中过程中也出现一些其它问题,出现的问题和解决方案都整理在此文中. 1Elasticsearch5.1.1安装 到ES官网https ...
- Unsorted, maximum ==> sorted
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...
- RESTful架构概念
本文转载自:http://www.ruanyifeng.com/blog/2011/09/restful.html 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软 ...