BlueBoxはOpenStackのAPIがそのまま利用できます。
OpenStack APIを使えば、インスタンスの作成やインスタンスにアクセスするために必要なセキュリティ設定などを全部自動化することが可能です。
Python言語用のSDKがリリースされているので、今回はこれを使って自動化してみましょう。
まずは、OpenStack Python SDKが利用できるように、以下のURLを参考にOpenStack Clientをセットアップをしましょう。
http://matsuo20150919.blogspot.jp/2015/12/blue-boxapi.html
今回テストするOSの環境変数は以下のようになります。
set OS_AUTH_URL=https://icos-sea.openstack.blueboxgrid.com:5001/v2.0
set OS_TENANT_ID=162342da7f4b42adb7adfcb47e3746fd
set OS_USERNAME=XXXXXXXX
set OS_PASSWORD=XXXXXXXX
set OS_VERSION=2
Pythonからこれらの環境変数を読み込むためのモジュールを作りましょう。
import os
def get_nova_creds():
d = {}
d['version'] = os.environ['OS_VERSION']
d['username'] = os.environ['OS_USERNAME']
d['api_key'] = os.environ['OS_PASSWORD']
d['auth_url'] = os.environ['OS_AUTH_URL']
d['tenant_id'] = os.environ['OS_TENANT_ID']
return d
以下はプログラム本体です。
import os
import time
import novaclient.client as nvclient
from credentials import get_nova_creds
#
# Define constant variables
#
keypair_name="matsuos-key"
private_key_filename="C:\users\matsuo\dev\\bb\key-matsuos"
secgroup_name="matsuos-sg-ssh"
instance_name="matsuos-instance"
userdata_filename="C:/users/matsuo/dev/bb/userdata.sh"
#
# Read BlueBox credentials from OS environment
#
try:
creds = get_nova_creds()
nova = nvclient.Client(**creds)
except:
print "OS parameter may not be exist: OS_VERSION, OS_USERNAME, OS_PASSWORD, OS_AUTH_URL, OS_TENANT_ID"
exit()
#
# Create Key Pair
#
keypair = None
try:
keypair = nova.keypairs.create(name=keypair_name)
except:
print "Keypair: \""+keypair_name+"\" is already exist"
else:
fp = os.open(private_key_filename, os.O_WRONLY | os.O_CREAT, 0o600)
with os.fdopen(fp,'w') as f:
f.write(keypair.private_key)
print "Private Key has been writen to "+fp.name
#
# Create Security Group
#
sglist = nova.security_groups.list()
for sg in sglist:
if (sg.name == secgroup_name):
print "Secgroup: \""+sg.name+"\" is already exist"
break
else:
sg = nova.security_groups.create(
name=secgroup_name,
description="All network to server via SSH for admin")
sg_rule = nova.security_group_rules.create(
sg.id,
ip_protocol="tcp",
from_port=22,
to_port=22)
print "Created secgroup: \""+nova.security_groups.get(sg.id).name+"\""
#
# Create Instance
#
inslist = nova.servers.list()
for ins in inslist:
if (ins.name == instance_name):
print "Instance: \""+ins.name+"\" is already exist"
break
else:
fpuserdata = open(userdata_filename)
userdata = fpuserdata.read()
print "Userdata="+userdata
image = nova.images.find(name="ubuntu-14.04")
flavor = nova.flavors.find(name="m1.tiny")
net = nova.networks.find(label="internal")
nics = [{'net-id': net.id}]
instance = nova.servers.create(
name=instance_name,
image=image,
flavor=flavor,
nics=nics,
userdata=userdata,
key_name=keypair_name)
print "Created instance: \""+nova.servers.get(instance.id).name+"\""
# Poll at 5 second intervals, until the status is no longer 'BUILD'
status = instance.status
while status == 'BUILD':
time.sleep(5)
# Retrieve the instance again so the status field updates
instance = nova.servers.get(instance.id)
status = instance.status
print " status: %s" % status
#
# Create and assign Floating IP
#
fiplist = nova.floating_ips.list()
for fip in fiplist:
if (fip.instance_id == None):
print "Floating IP: "+fip.ip+" will be used because of being not assigned any instance"
break
else:
fip = nova.floating_ips.create()
print "Floating IP: "+fip.ip+" has been created"
instance.add_floating_ip(fip)
print "Floating IP: "+fip.ip+" is assigned to "+instance.name
0 件のコメント:
コメントを投稿