User Tools

Site Tools


cloud:recipe:coding

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
cloud:recipe:coding [2020/05/14 08:24] – created chudlercloud:recipe:coding [2020/05/14 18:32] (current) chudler
Line 2: Line 2:
 This document gives a quick introduction to using the Openstack Python client to discover and consume compute resources. This document gives a quick introduction to using the Openstack Python client to discover and consume compute resources.
  
-Before beginning, get an account as described in the [[ cloud:intro | Introduction ]]+Before beginning, get an account as described in the [[ cloud:intro | Introduction ]]. This is the right place to read about writing python scripts. A companion document shows how to use the [[ cloud:cli | Command Line ]]. 
 + 
 +======Installation====== 
 + 
 +From a suitable host (this was done on linux.cs.uchicago.edu), create your environment. 
 + 
 +<code> 
 +$ python3 -m venv cloudsdk 
 +$ source cloudsdk/bin/activate 
 +$ pip3 install openstacksdk 
 +$ python -m openstack version 
 +</code> 
 + 
 +=====Configuration===== 
 + 
 +Obtain your configuration from the [[ https://overcloud.cs.uchicago.edu/project/api_access/clouds.yaml/ | Web Interface ]]. The preceding link is for authenticated and authorized users only. Please read [[ cloud:intro | Intro ]] for access. After logging in to the web interface at [[ https://overcloud.cs.uchicago.edu ]], click the menu on the left side, Project->API Access. On the right side of the top of the page, click "Download Openstack RC File"->"Openstack clouds.yaml file"
 + 
 +Save the clouds.yaml file to the current working directory, or see the [[ https://docs.openstack.org/openstacksdk/latest/user/config/configuration.html#config-files | Upstream Docs ]] about config file search order: 
 + 
 +<code> 
 +USER_CONFIG_DIR 
 +    Linux: ~/.config/openstack 
 +    OSX: ~/Library/Application Support/openstack 
 +    Windows: C:\Users\USERNAME\AppData\Local\OpenStack\openstack 
 +SITE_CONFIG_DIR 
 +    Linux: /etc/openstack 
 +    OSX: /Library/Application Support/openstack 
 +    Windows: C:\ProgramData\OpenStack\openstack 
 +</code> 
 + 
 +====Sample Configuration File==== 
 +<code> 
 +clouds: 
 +  openstack: 
 +    auth: 
 +      auth_url: https://overcloud.cs.uchicago.edu:5000 
 +      username: "CNetID" 
 +      password: "sekret" 
 +      project_id: YOUR PROJECT UUID 
 +      project_name: "CNetID" 
 +      user_domain_name: "CS_LDAP" 
 +    region_name: "RegionOne" 
 +    interface: "public" 
 +    identity_api_version:
 +</code> 
 + 
 +The main flaw here is that you are taking risks by storing your University credentials in a file. Instead, you can use the API or web interface to create an Application Credential. For example, 
 + 
 +<code> 
 +openstack application credential create --secret sekret --role member --expiration 2020-10-10:10:10:10 --restricted myapp 
 +</code> 
 +=====Testing and Usage===== 
 +Ensure you can load the python module 
 +<code> 
 +python -m openstack version 
 +</code> 
 + 
 +Check your configuration 
 +<code> 
 +python -m openstack.config.loader 
 +</code> 
 +Check the exact error if one is displayed. For example, if you see 
 +<code> 
 +keystoneauth1.exceptions.auth_plugins.MissingRequiredOptions: Auth plugin requires parameters which were not given: auth_url 
 +</code> 
 +You have failed to provide a configuration file. This is different from a syntax error, but not so different from a permission problem, for example. 
 + 
 +======Demo Code====== 
 + 
 +The following Python code shows how to create and destroy a server, and obtain information about Networks, Images, and Flavors. Consult the [[ https://docs.openstack.org/openstacksdk/latest/user/index.html#api-documentation | API Resource Documentation ]] to see what else is possible. 
 + 
 +For your learning, contrast this with the equivalent actions that are taken in the [[ cloud:cli | Command Line Usage Guide ]]. 
 + 
 +<code Python> 
 + 
 +import openstack 
 +from openstack.config import loader 
 +# to enable debug logging 
 +# openstack.enable_logging(True) 
 +config = loader.OpenStackConfig() 
 +cloud = openstack.connect(cloud='openstack'
 +flavor = cloud.get_flavor_by_ram(2048) 
 +# print(flavor.name) 
 +image = cloud.get_image('20.04'
 +# pretty print 
 +# cloud.pprint(image) 
 +network = cloud.get_network('campus37'
 +try: 
 +    server = cloud.create_server('myserver', image=image, flavor=flavor, network=network, wait=True, auto_ip=True) 
 +finally: 
 +    cloud.delete_server('myserver', wait=True, delete_ips=True) 
 +</code>
/var/lib/dokuwiki/data/attic/cloud/recipe/coding.1589462663.txt.gz · Last modified: 2020/05/14 08:24 by chudler

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki