SDK でイメージを操作する場合、novaclient
メソッドを呼び出します。
novaclient.v1_1.keypairs.KeypairManager.create メソッドを呼び出し、キーペアを生成します。
import novaclient.v2.client as nvclient
nova = nvclient.Client(...)
keypair_name = "staging"
keypair = nova.keypairs.create(name=keypair_name)
print keypair.private_key
Python スクリプトの出力は、このようになるでしょう。
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA8XkaMqInSPfy0hMfWO+OZRtIgrQAbQkNcaNHmv2GN2G6xZlb\nuBRux5Xk/6SZ
ABaNPm1nRWm/ZDHnxCsFTcAl2LYOQXx3Cl2qKNY4r2di4G48GAkd\n7k5lDP2RgQatUM8npO0CD9PU
...
mmrceYYK08/lQ7JKLmVkdzdQKt77+v1oBBuHiykLfI6h1m77NRDw9r8cV\nzczYeoALifpjTPMkKS8
ECfDCuDn/vc9K1He8CRaJHf8AMLQLM3MN
-----END RSA PRIVATE KEY-----
通常、後ほど使用するために秘密鍵をファイルに書き出します。ファイルは、ファイルの所有者のみが読み取り、書き込みできるようにする必要があります。そうでなければ、SSH クライアントは秘密鍵のファイルの読み込みを拒否します。以下の例にあるように、適切なパーミッションが指定されたファイルを作成すると、最も安全です。
import novaclient.v2.client as nvclient
import os
nova = nvclient.Client(...)
keypair_name = "staging"
private_key_filename = "/home/alice/id-staging"
keypair = nova.keypairs.create(name=keypair_name)
# Create a file for writing that can only be read and written by
owner
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)
~/.ssh/id_rsa.pub
にある公開鍵でキーペアを生成している場合、ファイルの内容を <http://docs. openstack.org/developer/python-novaclient/api/novaclient.v1_1.keypairs .html#novaclient.v1_1.keypairs.KeypairManager.create>`__ メソッドに渡し、公開鍵を Compute にインポートします。
import novaclient.v2.client as nvclient
import os.path
with open(os.path.expanduser('~/.ssh/id_rsa.pub')) as f:
public_key = f.read()
nova = nvclient.Client(...)
nova.keypairs.create('mykey', public_key)
novaclient.v1_1.keypairs.KeypairManager.list メソッドを呼び出して、キーペアを一覧表示します。
import novaclient.v2.client as nvclient
nova = nvclient.Client(...)
keypairs = nova.keypairs.list()
novaclient.v_1.security_groups.SecurityGroupManager.list メソッドを呼び出して、現在のプロジェクトのセキュリティーグループを一覧表示します。
import novaclient.v2.client as nvclient
nova = nvclient.Client(...)
security_groups = nova.security_groups.list()
指定の名前と説明でセキュリティーをグループを作成するには、novaclient.v_1.security_groups.SecurityGroupManager.create メソッドを呼び出します。
import novaclient.v2.client as nvclient
nova = nvclient.Client(...)
nova.security_groups.create(name="web", description="Web servers")
セキュリティーグループを削除するには novaclient.v_1.security_groups.SecurityGroupManager.delete メソッドを呼び出し、novaclient.v1_1.security_groups.SecurityGroup オブジェクトまたはグループ ID を引数として指定します。
import novaclient.v2.client as nvclient
nova = nvclient.Client(...)
group = nova.security_groups.find(name="web")
nova.security_groups.delete(group)
# The following lines would also delete the group:
# nova.security_groups.delete(group.id)
# group.delete()
novaclient.v1_1.security_groups.SecurityGroup オブジェクトの rules
属性からセキュリティーグループにアクセスします。
import novaclient.v2.client as nvclient
nova = nvclient.Client(...)
group = nova.security_groups.find(name="web")
print group.rules
セキュリティーグループにルールを追加するには novaclient.v1_1.security_group_rules.SecurityGroupRuleManager.create メソッドを呼び出します。
import novaclient.v2.client as nvclient
nova = nvclient.Client(...)
group = nova.security_groups.find(name="web")
# Add rules for ICMP, tcp/80 and tcp/443
nova.security_group_rules.create(group.id, ip_protocol="icmp",
from_port=-1, to_port=-1)
nova.security_group_rules.create(group.id, ip_protocol="tcp",
from_port=80, to_port=80)
nova.security_group_rules.create(group.id, ip_protocol="tcp",
from_port=443, to_port=443)
Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.