"Openstack-nova" use-novaclient Create virtual machine (createvms.py)

Source: Internet
Author: User

Import Sys
Import time
Import Keystoneclient.v2_0.client as Keystoneclient
Import Novaclient.v1_1.client as Novaclient
Import Neutronclient.v2_0.client as Neutronclient
From Credentials Import *


‘‘‘
Creates a network and a COUNT of instances
Using the User/project configured in STACKRC file
‘‘‘


instance_name = ' Fiva '
Network_name= ' route-66 '
Subnet_cidr= ' 10.2.33.0/24 '
Instance_count = 3


Kcreds = Get_keystone_creds ()


Print "Connecting to Keystone"
Keystone = Keystoneclient. Client (**kcreds)
Tokenlen=len (Keystone.auth_token)
Print keystone.auth_token[0:20] + "..." + Keystone.auth_token[tokenlen-20:tokenlen]


Ncreds = Get_nova_creds ()
Nova = novaclient. Client (**ncreds)


Flavors = nova.flavors.list (is_public=true)
Print Flavors


Images = Nova.images.list (detailed=false)
Print Images


# Get networks from Quantum
Print "Find or create Network ..."
Network_url = keystone.service_catalog.url_for (service_type= ' network ')
Neutron = neutronclient. Client (Endpoint_url=network_url, Token=keystone.auth_token)
Networks = Neutron.list_networks () [' Networks ']
Print "Networks:"
Print [(nw[' name '],nw[' ID ']) for NW in networks]


NET = None
net_id = None
Networks = Neutron.list_networks (Name=network_name) [' Networks ']
If Len (networks) >0 and networks[0][' name '] = = Network_name:
net_id = networks[0][' id ']
Print "Network found", Network_name, net_id
Else
NET = neutron.create_network ({' Network ':
{' name ': Network_name, ' admin_state_up ': True}})
print "Created Network", net
net_id = net[' network ' [' ID ']
Sub = neutron.create_subnet ({' Subnet ': {
' Name ': ' Subnet ',
' network_id ': net_id,
' Ip_version ': 4,
' CIDR ': SUBNET_CIDR
}
})
Print "Created Subnet", Sub


Print "List instances:"
# Check what do we get so far for instances
instances = Nova.servers.list ()


For instance in instances:
print ' name: ', Instance.name
print ' Host ID: ', instance.hostid




Print "Creating instances:"
Instance = nova.servers.create (instance_name, images[0], flavors[0]
# The actual number is based on the quota.
# See http://www.gossamer-threads.com/lists/openstack/dev/17629
, Min_count=1, Max_count=instance_count
# If NICs not specified, 'll connect to all project networks
, nics=[{' Net-id ': net_id}]
)

# Poll at 5 second intervals, until the status is no longer ' BUILD '
Status = Instance.status
Sys.stdout.write (' Building ... ')
While status = = ' BUILD ':
Time.sleep (1)
Sys.stdout.write (".")
Sys.stdout.flush ()
# Retrieve The instance again so the Status field updates
Instance = Nova.servers.get (instance.id)
Status = Instance.status


Print "Status:%s"% status

"Openstack-nova" use-novaclient Create virtual machine (createvms.py)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.