网络下载的代码,因我的是pike版本,与代码的版本有区别,运行出现诸多版本问题,经调试修改代码,测试通过,记录如下:
#!/usr/bin/python
from keystoneclient.auth.identity import v3
from keystoneclient import session
from keystoneclient.v3 import client as keystoneapi
from novaclient import client as novapi
auth_url = 'http://192.168.0.100/identity/v3'
username = 'admin'
user_domain_name = 'Default'
project_name = 'admin'
project_domain_name = 'Default'
password = '****'
auth = v3.Password(auth_url=auth_url,
username=username,
password=password,
project_name=project_name,
project_domain_name=project_domain_name,
user_domain_name=user_domain_name)
sess = session.Session(auth=auth)
keystone = keystoneapi.Client(session=sess)
#print keystone.projects.list() 列出所有项目
nova = novapi.Client(2, session=keystone.session)
#print nova.glance.list() 列出所有镜像
image = nova.glance.find_image('cirros-0.3.5-x86_64-disk')
#print nova.flavors.list() 列出所有主机类型
flavor = nova.flavors.find(name='m1.tiny')
#print nova.neutron.list() 列出所有网络,有问题,后来通过直接查数据库解决
network = nova.neutron.find_network('vxlan100')
#创建虚拟机
server = nova.servers.create(name="vm_api", image=image, flavor=flavor,nics=[{'net-id':network.id}])
print server
创建的实例正常运行
相关数据库
================================
use neutron
mysql> select id,name,status from networks;
+--------------------------------------+----------+--------+
| id | name | status |
+--------------------------------------+----------+--------+
| 14fdb7c7-1852-4c89-a735-7ad2be3d5867 | vxlan100 | ACTIVE |
| 8fdea82c-29e5-48ec-b3b6-871a9401b84e | net_out | ACTIVE |
+--------------------------------------+----------+--------+
use glance
mysql> select id,name from images;
+--------------------------------------+--------------------------+
| id | name |
+--------------------------------------+--------------------------+
| d978f308-50ec-410d-bf3f-05e86e855e7a | cirros-0.3.5-x86_64-disk |
+--------------------------------------+--------------------------+
use keystone;
mysql> select id,name from project where name not in('<
+----------------------------------+----------+
| id | name |
+----------------------------------+----------+
| 61b8e4b854914cef9a0112ce60cc29ae | admin |
| 5f7be2a742084eedaa979d92d75463cc | alt_demo |
| d62787e13b864d539bcf1753be36858b | demo |
+----------------------------------+----------+
3 rows in set (0.00 sec)
use nova_api
mysql> select id,name,memory_mb,root_gb from flavors;
+----+-----------+-----------+---------+
| id | name | memory_mb | root_gb |
+----+-----------+-----------+---------+
| 1 | cirros256 | 256 | 0 |
| 2 | ds512M | 512 | 5 |
| 3 | ds1G | 1024 | 10 |
| 4 | ds2G | 2048 | 10 |
| 5 | ds4G | 4096 | 20 |
| 6 | m1.tiny | 512 | 1 |
| 7 | m1.small | 2048 | 20 |
| 8 | m1.medium | 4096 | 40 |
| 9 | m1.large | 8192 | 80 |
| 10 | m1.xlarge | 16384 | 160 |
| 11 | m1.nano | 64 | 0 |
| 12 | m1.micro | 128 | 0 |
+----+-----------+-----------+---------+
12 rows in set (0.00 sec)