Page tree
Skip to end of metadata
Go to start of metadata

For the latest version, please see http://artifacts.opnfv.org/snaps/docs/how-to-use/index.html#document-LibraryUsage

Overview

The pattern used within the SNAPS-OO library for creating OpenStack instances have been made as consistent as possible amongst the different instance types. Each consists of a constructor that takes in a credentials object and generally takes in a single "settings" (configuration) object. The only exception to this rule is with the OpenStackVMInstance (creates an OpenStack Server) where it takes in the additional settings used for the associated image and SSH key-pairs credentials as those objects contain additional attributes required of SNAPS, primarily when one needs to obtain remote access. After instantiation, the create() method must be called to initiate all of the necessary remote API calls to OpenStack required for proper instance creation.

Installation

To install SNAPS-OO into your Python runtime:

  1. git clone <repo URL>
  2. cd snaps
  3. pip install -e .

Credentials - OSCreds

As communicating with OpenStack is performed via secure remote RESTful API calls, any function or method performing any type of query or CRUD operation must know how to connect to the NFVI. The class OSCreds defined in snaps.openstack.os_credentials.py contains everything required to connect to any Keystone v2.0 or v3 authorization server. The attributes are listed below:

  • username
  • password
  • auth_url
  • project_name (aka. tenant_name)
  • identity_api_version (for obtaining Keystone authorization token. Versions 2.0 & v3 only validated.)
  • image_api_version (Glance version 1 currently only validated)
  • network_api_version (Neutron version 2 currently only validated)
  • compute_api_version (Nova version 2 currently only validated)
  • user_domain_id (default='default')
  • project_domain_id (default='default')
  • proxy_settings
    • host (the HTTP proxy host)
    • port (the HTTP proxy port)
    • ssh_proxy_cmd (same as the value placed into ssh -o ProxyCommand='<this config value>')
Create OS Credentials Object
from snaps.openstack.os_credentials import OSCreds
 
os_creds = OSCreds(username='admin', password='admin', auth_url='http://localhost:5000/v3, project_name='admin',
                   identity_api_version=3)

 

Object Creators

Each creator minimally requires an OSCreds object for connecting to the NFVI, associated *Settings object for instance configuration, create() method to make the necessary remote API calls and create all of the necessary OpenStack instances required, and clean() method that is responsible for deleting all associated OpenStack instances. Please see the class diagram here. Below is a textual representation of the requirements of each creator classes with their associated setting classes and a sample code snippet on how to use the code.

  • User - snaps.openstack.create_user.OpenStackUser
    • snaps.openstack.create_user.UserSettings
      • name - the username (required)
      • password - the user's password (required)
      • project_name - the name of the project to associated to this user (optional)
      • domain_name - the user's domain (default='default')
      • email - the user's email address (optional)
      • enabled - flag to determine whether or not the user should be enabled (default=True)
Create User
from snaps.openstack.create_user import UserSettings, OpenStackUser
 
user_settings = UserSettings(name='username', password='password')
user_creator = OpenStackUser(os_creds, user_settings)
user_creator.create()
 
# Retrieve OS creds for new user for creating other OpenStack instance
user_os_creds = user_creator.get_os_creds(os_creds.project_name)

# Create other objects with new user...
... 
# Perform application-specific logic
...
# Cleanup
user_creator.clean()

 

  • Project - snaps.openstack.create_project.OpenStackProject
    • snaps.openstack.create_project.ProjectSettings
      • name - the project name (required)
      • domain - the project's domain (default='default')
      • description - the project's description (optional)
      • enables - flag to determine whether or not the project should be enabled (default=True)
Create Project
from snaps.openstack.create_project import ProjectSettings, OpenStackProject
 
project_settings = ProjectSettings(name='project-name')
project_creator = OpenStackProject(os_creds, project_settings)
project_creator.create()
 
# Perform application-specific logic
...
# Cleanup
project_creator.clean()

 

  • Flavor - snaps.openstack.create_flavor.OpenStackFlavor
    • snaps.openstack.create_flavor.FlavorSettings
      • name - the flavor name (required)
      • flavor_id - the flavor's string ID (default='auto')
      • ram - memory in MB to allocate to VM (required)
      • disk - disk storage in GB (required)
      • vcpus - the number of CPUs to allocate to VM (required)
      • ephemeral - the size of the ephemeral disk in GB (default=0)
      • swap - the size of the swap disk in GB (default=0)
      • rxtx_factor - the receive/transmit factor to be set on ports if backend supports QoS extension (default=1.0)
      • is_public - flag that denotes whether or not other projects can access image (default=True)
Create Flavor
from snaps.openstack.create_flavor import FlavorSettings, OpenStackFlavor
 
flavor_settings = FlavorSettings(name='flavor-name', ram=4, disk=10, vcpus=2)
flavor_creator = OpenStackFlavor(os_creds, flavor_settings)
flavor_creator.create()
 
# Perform application-specific logic
...
# Cleanup
flavor_creator.clean()

 

  • Image - snaps.openstack.create_image.OpenStackImage
    • snaps.openstack.create_image.ImageSettings
      • name - the image name (required)
      • image_user - the default image user generally used by OpenStackVMInstance class for obtaining an SSH connection (required)
      • img_format - the image's format (i.e. qcow2) (required)
      • url - the download URL to obtain the image file (this or image_file must be configured, not both)
      • image_file - the location of the file to be sourced from the local filesystem (this or url must be configured, not both)
      • nic_config_pb_loc - the location of the ansible playbook that can configure additional NICs. Floating IPs are required to perform this operation. (optional)
Create Image
from snaps.openstack.create_image import ImageSettings, OpenStackImage
 
image_settings = ImageSettings(name='image-name', image_user='ubuntu', img_format='qcow2',
                               url='http://uec-images.ubuntu.com/releases/trusty/14.04/ubuntu-14.04-server-cloudimg-amd64-disk1.img')
image_creator = OpenStackImage(os_creds, image_settings)
image_creator.create()
 
# Perform application-specific logic
...
# Cleanup
image_creator.clean()

 

  • Keypair - snaps.openstack.create_keypair.OpenStackKeypair
    • snaps.openstack.create_keypair.KeypairSettings
      • name - the keypair name (required)
      • public_filepath - the file location to where the public key is to be written or currently resides (optional)
      • private_filepath - the file location to where the private key file is to be written or currently resides (optional but highly recommended to leverage or the private key will be lost forever)
Create Keypair
from snaps.openstack.create_keypair import KeypairSettings, OpenStackKeypair
 
keypair_settings = KeypairSettings(name='keypair-name', private_filepath='/tmp/priv-kp')
keypair_creator = OpenStackKeypair(os_creds, keypair_settings)
keypair_creator.create()
 
# Perform application-specific logic
...
# Cleanup
keypair_creator.clean()

 

  • Network - snaps.openstack.create_network.OpenStackNetwork
    • snaps.openstack.create_network.NetworkSettings
      • name - the name of the network (required)
      • admin_state_up - flag denoting the administrative status of the network (True = up, False = down)
      • shared - flag indicating whether the network can be shared across projects/tenants (default=True)
      • project_name - the name of the project (optional - can only be set by admin users)
      • external - flag determining if network has external access (default=False)
      • network_type - the type of network (i.e. vlan|vxlan|flat)
      • physical_network - the name of the physical network (required when network_type is 'flat')
      • subnet_settings (list of optional snaps.openstack.create_network.SubnetSettings objects)
        • cidr - the subnet's CIDR (required)
        • ip_version - 4 or 6 (default=4)
        • name - the subnet name (required)
        • project_name - the name of the project (optional - can only be set by admin users)
        • start - the start address for the allocation pools
        • end - the end address for the allocation pools
        • gateway_ip - the gateway IP
        • enable_dhcp - flag to determine whether or not to enable DHCP (optional)
        • dns_nameservers - a list of DNS nameservers
        • host_routes - list of host route dictionaries for subnet (optional, see pydoc and Neutron API for more details)
        • destination - the destination for static route (optional)
        • nexthop - the next hop for the destination (optional)
        • ipv6_ra_mode - valid values include: 'dhcpv6-stateful', 'dhcp6v-stateless', 'slaac' (optional)
        • ipvc_address_mode - valid values include: 'dhcpv6-stateful', 'dhcp6v-stateless', 'slaac' (optional)
Create Network
from snaps.openstack.create_network import NetworkSettings, SubnetSettings, OpenStackNetwork
 
subnet_settings = SubnetSettings(name='subnet-name', cidr='10.0.0.0/24')
network_settings = NetworkSettings(name='network-name', subnet_settings=[subnet_settings])
network_creator = OpenStackNetwork(os_creds, network_settings)
network_creator.create()
 
# Perform application-specific logic
...
# Cleanup
network_creator.clean()

 

  • Security Group - snaps.openstack.create_security_group.OpenStackSecurityGroup
    • snaps.openstack.create_security_group.SecurityGroupSettings
      • name - the security group's name (required)
      • description - the description (optional)
      • project_name - the name of the project (optional - can only be set by admin users)
      • rule_settings (list of optional snaps.openstack.create_security_group.SecurityGroupRuleSettings objects)
        • sec_grp_name - the name of the associated security group (required)
        • description - the description (optional)
        • direction - enum snaps.openstack.create_security_group.Direction (required)
        • remote_group_id - the group ID to associate with this rule
        • protocol - enum snaps.openstack.create_security_group.Protocol (optional)
        • ethertype - enum snaps.openstack.create_security_group.Ethertype (optional)
        • port_range_min - the max port number in the range that is matched by the security group rule (optional)
        • port_range_max - the min port number in the range that is matched by the security group rule (optional)
        • sec_grp_rule - the rule object to a security group rule object to associate (note: does not work currently)
        • remote_ip_prefix - the remote IP prefix to associate with this metering rule packet (optional)
Create Security Group
from snaps.openstack.create_security_group import SecurityGroupSettings, SecurityGroupRuleSettings,
  Direction, OpenStackSecurityGroup
 
sec_grp_name = 'sec-grp-name'
rule_settings = SecurityGroupRuleSettings(sec_grp_name=sec_grp_name, direction=Direction.ingress)
security_group_settings = SecurityGroupSettings(name=sec_grp_name, rule_settings=[rule_settings])
security_group_creator = OpenStackSecurityGroup(os_creds, security_group_settings)
security_group_creator.create()
 
# Perform application-specific logic
...
# Cleanup
security_group_creator.clean()

 

  • Router - snaps.openstack.create_router.OpenStackRouter
    • snaps.openstack.create_router.RouterSettings
      • name - the router name (required)
      • project_name - the name of the project (optional - can only be set by admin users)
      • external_gateway - the name of the external network (optional)
      • admin_state_up - flag to denote the administrative status of the router (default=True)
      • external_fixed_ips - dictionary containing the IP address parameters (parameter not tested)
      • internal_subnets - list of subnet names to which this router will connect (optional)
      • port_settings (list of optional snaps.openstack.create_router.PortSettings objects) - creates custom ports to internal subnets (similar to internal_subnets with more control)
        • name
        • network_name
        • admin_state_up
        • project_name - the name of the project (optional - can only be set by admin users)
        • mac_address
        • ip_addrs
        • fixed_ips
        • security_groups
        • allowed_address_pairs
        • opt_value
        • opt_name
        • device_owner
        • device_id
Create Router
from snaps.openstack.create_router import RouterSettings, OpenStackRouter
 
router_settings = RouterSettings(name='router-name', external_gateway='external')
router_creator = OpenStackRouter(os_creds, router_settings)
router_creator.create()
 
# Perform application-specific logic
...
# Cleanup
router_creator.clean()

 

  • VM Instances - snaps.openstack.create_instance.OpenStackVmInstance
    • snaps.openstack.create_instance.VmInstanceSettings
      • name - the name of the VM (required)
      • flavor - the name of the flavor (required)
      • port_settings - list of snaps.openstack.create_network.PortSettings objects where each denote a NIC (see above in create router section for details) API does not require, but newer NFVIs now require VMs have at least one network
      • security_group_names - a list of security group names to apply to VM
      • floating_ip_settings (list of snaps.openstack_create_instance.FloatingIpSettings objects)
        • name - a name to a floating IP for easy lookup 
        • port_name - the name of the VM port on which the floating IP should be applied (required)
        • router_name - the name of the router to the external network (required)
        • subnet_name - the name of the subnet on which to attach the floating IP (optional)
        • provisioning - when true, this floating IP will be used for provisioning which will come into play once we are able to get multiple floating IPs working.
      • sudo_user - overrides the image_settings.image_user value when attempting to connect via SSH
      • vm_boot_timeout - the number of seconds that the thread will block when querying the VM's status when building (default=900)
      • vm_delete_timeout - the number of seconds that the thread will block when querying the VM's status when deleting (default=300)
      • ssh_connect_timeout - the number of seconds that the thread will block when attempting to obtain an SSH connection (default=180)
      • availability_zone - the name of the compute server on which to deploy the VM (optional must be admin)
      • userdata - the cloud-init script to execute after VM has been started
    • image_settings - see snaps.openstack.create_image.ImageSettings above (required)
    • keypair_settings - see snaps.openstack.create_keypairs.KeypairSettings above (optional)
Create VM Instance
from snaps.openstack.create_instance import VmInstanceSettings, FloatingIpSettings, OpenStackVmInstance
from snaps.openstack.create_network import PortSettings
 
# Please see above for other variable references
port_settings = PortSettings(name='port-name', network_name=network_settings.name)
floating_ip_settings = FloatingIpSettings(name='fip1', port_name=port_settings.name, router_settings.name)
instance_settings = VmInstanceSettings(name='vm-name', flavor=flavor_settings.name, port_settings=[port_settings],
                                       floating_ip_settings=[floating_ip_settings])
instance_creator = OpenStackVmInstance(os_creds, instance_settings, image_settings, keypair_settings)
instance_creator.create()
 
# Perform application-specific logic
...
ssh_client = instance_creator.ssh_client()
...
# Cleanup
instance_creator.clean()

Ansible Provisioning

Being able to easily create OpenStack instances such as virtual networks and VMs is a good start to the problem of NFV; however, an NFVI is useless unless there is some software performing some function. This is why we added Ansible playbook support to SNAPS-OO which can be located in snaps.provisioning.ansible_utils#apply_playbook. See below for a description of that function's parameters:

  • playbook_path - the file location of the ansible playbook
  • hosts_inv - a list of hosts/IP addresses to which the playbook will be applied
  • host_user - the user (preferably sudo) to use for applying the playbook
  • ssh_priv_key_file_path - the location to the private key file used for SSH
  • variables - a dict() of substitution values for Jinga2 templates leveraged by Ansible
  • proxy_setting - used to extract the SSH proxy command (optional)
Apply Ansible Playbook Utility
from snaps.provisioning import ansible_utils

# Please see above for other variable references 
ansible_utils.apply_playbook(playbook_path='/tmp/playbook.yml',
                             hosts_inv=[instance_creator.get_port_ip(instance_settings.port_settings[0].name)],
                             host_user=instance_creator.get_image_user(),
                             ssh_priv_key_file_path=instance_creator.keypair_settings.private_filepath)

OpenStack Utilities

For those who do like working procedurally, SNAPS-OO also leverages utilitarian functions for nearly every query or request made to OpenStack. This pattern will make it easier to deal with API version changes as they would all be made in one place. (see keystone_utils for an example of this pattern as this is the only API where SNAPS is supporting more than one version)

  • snaps.openstack.utils.keystone_utils - for calls to the Keystone APIs
  • snaps.openstack.utils.glance_utils - for calls to the Glance APIs
  • snaps.openstack.utils.neutron_utils - for calls to the Neutron APIs
  • snaps.openstack.utils.nova_utils - for calls to the Nova APIs

 

  • No labels