LoginSignup
9
9

More than 5 years have passed since last update.

conoha apiを使ってvpsを立てる

Posted at
gem i openstack
conoha_create.rb
require 'openstack'

module OpenStack
  class Authentication
    def self.init(conn)
      if conn.auth_path =~ /.*v2.0\//
        AuthV20.new(conn)
      else
        AuthV10.new(conn)
      end
    end
  end
end

flavor_name = 'g-1gb'
image_name = 'vmi-ubuntu-14.04-amd64'

# https://www.conoha.jp/docs/compute-create_vm.html
os = OpenStack::Connection.create(
  username: ENV['CONOHA_USER'],
  api_key: ENV['CONOHA_PASS'],
  authtenant_id: ENV['CONOHA_TENANT_ID'],
  auth_url: 'https://identity.tyo1.conoha.io/v2.0/tokens',
)

#pp os.servers
os_flavor = os.flavors.select{|flavor|flavor[:name]==flavor_name}.first
os_image = os.images.select{|image|image[:name]==image_name&&image[:status]=='ACTIVE'}.first

newserver = os.create_server(
  name: 'New Server',
  metadata: {
    instance_name_tag: ENV['CONOHA_NAME_TAG'],
  },
  imageRef: os_image[:id],
  flavorRef: os_flavor[:id],
  adminPass: ENV['CONOHA_ADMINPASS'],
  key_name: ENV['CONOHA_KEY_NAME'],
  security_groups: ['gncs-ipv4-all'],
)
while newserver.accessipv4.nil?
  sleep 15 # ip振り分けられるのを少し待つ
  newserver.refresh
end
puts newserver.accessipv4

各種環境変数の設定をして実行しましょう。ipアドレスが出力されるのでしばらく待ってvps立ち上がるの待ってから(1分ぐらい?)

ssh root@123.456.789.012

とかで行けると思います。

カスタマイズ

自分好みに設定諸々を変更したくなると思います。その際に必要になるであろう情報をば。

リファレンスが以下
https://www.conoha.jp/docs/

リファレンスで出てくるX-Auth-Tokenの取得方法

curl -s -X POST \
-H "Accept: application/json" \
-d '{"auth":{"passwordCredentials":{"username":"xxxxxxxxxxx","password":"yyyyyyyyyy"},"tenantId":"zzzzzzzzzzzzzzzz"}}' \
https://identity.tyo1.conoha.io/v2.0/tokens | jq '.access.token.id'
"aaaaaaaaaaaaaaaaaaaaaa"

1日だけ有効のトークンが取得されます。そのトークンを使ってこんな風に

curl -s -X GET \
-H "Accept: application/json" \
-H "X-Auth-Token: aaaaaaaaaaaaaaaaaaaaaa" \
https://networking.tyo1.conoha.io/v2.0/security-groups | jq '.security_groups[].name'
"gncs-ipv4-ssh"
"gncs-ipv4-all"
"default"
"gncs-ipv6-ssh"
"gncs-ipv4-web"
"gncs-ipv6-all"

セキュリティグループの一覧取得したりとかして設定値とか取得して設定変えたりとかしていきましょう。

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9