Ketika bekerja dengan image di SDK, Anda akan memanggil metode novaclient
.
Untuk menghasilkan keypair, panggil metode 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
Output script Python terlihat seperti ini:
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA8XkaMqInSPfy0hMfWO+OZRtIgrQAbQkNcaNHmv2GN2G6xZlb\nuBRux5Xk/6SZ
ABaNPm1nRWm/ZDHnxCsFTcAl2LYOQXx3Cl2qKNY4r2di4G48GAkd\n7k5lDP2RgQatUM8npO0CD9PU
...
mmrceYYK08/lQ7JKLmVkdzdQKt77+v1oBBuHiykLfI6h1m77NRDw9r8cV\nzczYeoALifpjTPMkKS8
ECfDCuDn/vc9K1He8CRaJHf8AMLQLM3MN
-----END RSA PRIVATE KEY-----
Anda biasanya menulis kunci pribadi (private key) ke file untuk digunakan nanti. File harus dibaca dan ditulisi dengan hanya pemilik file; jika tidak, klien SSH akan menolak untuk membaca file kunci pribadi. Cara paling aman adalah dengan membuat file dengan hak akses yang sesuai, seperti yang ditunjukkan dalam contoh berikut:
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)
Jika Anda telah dihasilkan sepasang kunci (keypair) dengan kunci publik (public key) yang terletak di ~/.ssh/id_rsa.pub
, lewatkan isi file ke metode novaclient.v1_1.keypairs.KeypairManager.create untuk mengimpor kunci publik (public key) ke 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)
Untuk mendaftar pasangan kunci, panggil novaclient.v1_1.keypairs.KeypairManager.list metode:
import novaclient.v2.client as nvclient
nova = nvclient.Client(...)
keypairs = nova.keypairs.list()
Untuk mendaftar kelompok keamanan untuk proyek saat ini, panggil novaclient.v_1.security_groups.SecurityGroupManager.list metode:
import novaclient.v2.client as nvclient
nova = nvclient.Client(...)
security_groups = nova.security_groups.list()
Untuk membuat grup keamanan dengan nama tertentu dan deskripsinya, panggil novaclient.v_1.security_groups.SecurityGroupManager.create metode:
import novaclient.v2.client as nvclient
nova = nvclient.Client(...)
nova.security_groups.create(name="web", description="Web servers")
Untuk menghapus grup keamanan, panggil novaclient.v_1.security_groups.SecurityGroupManager.delete metode, melewati juga novaclient.v1_1.security_groups.SecurityGroup objek atau ID grup sebagai argumen:
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()
Mengakses aturan kelompok keamanan dari atribut rules
dari objek novaclient.v1_1.security_groups.SecurityGroup:
import novaclient.v2.client as nvclient
nova = nvclient.Client(...)
group = nova.security_groups.find(name="web")
print group.rules
Untuk menambahkan aturan untuk grup keamanan, panggil novaclient.v1_1.security_group_rules.SecurityGroupRuleManager.create metode:
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.