Last active
January 12, 2016 00:36
-
-
Save wang-zhijun/299338292ecb67b8dd6b to your computer and use it in GitHub Desktop.
IDCFクラウドで自動的にVM作成、CentOS 6.5 64-bit限定、事前にプライベート鍵の登録が必要
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# VM名を入力、Enter押して、Ctrl-dを押す | |
echo -n "VM名を入力: " | |
read VM_NAME | |
# Portを入力、Enter押して、Ctrl-dを押す | |
echo -n "SSHでログインするためのパブリックポートを入力: " | |
read PUBLIC_PORT | |
# REGION="west" | |
echo -n "リージョン[west, east]: " | |
read REGION | |
#ZONENAME="joule" | |
echo -n "ゾーン名[joule, augusta]: " | |
read ZONENAME | |
SERVICE_OFFERING_NAME="light.S2" | |
TEMPLATE_NAME="CentOS 6.5 64-bit" | |
KEY_PAIR="tmp_id_rsa_pub" | |
CIRDRLIST="0.0.0.0/0" | |
PROTOCOL="TCP" | |
SSH_PORT="22" | |
TMP_ID_RSA_PUB="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCZLPEaUYIsxd3vjceWUQ5ui5gFgkr3HfoyrgVCiQ/fJj/McZl4tS5BOE5eMjaZaesDtyjr9F+Mbhjlq5HGLYxl9GpJoE0tLtYUMa020FwUlwJAbsVyKQglJfPrl8V1CC4jF4AniBZHv/lsnPcmEdMTY7MZCGMi48onJmwweSZ5uYwGM9QJVGaxQa4eXBVMgqmkPh0nrBL34az3+UVGnP+VnybQyc2Zt0H/ieXBjDDh9momNRTyG2T7IJSUQtJ/PrLqh+eZRqU3za91Mt4Q9Uin+dcV97B+0mhAHrNbnjUmSFM/FZwdBiEJ9GQMYuwI/XKXrk7cFEOkWBWq60q/uHMP [email protected]" | |
zone_id=$(cloudmonkey -p ${REGION} list zones name=${ZONENAME} | jq ".zone[].id") | |
echo "zone id = $zone_id" | |
ipaddress=$(cloudmonkey -p ${REGION} list publicipaddresses zoneid=${zone_id} issourcenat=true | jq ".publicipaddress[].ipaddress") | |
echo "ip address = $ipaddress" | |
ipaddress_id=$(cloudmonkey -p ${REGION} list publicipaddresses zoneid=${zone_id} issourcenat=true | jq ".publicipaddress[].id") | |
echo "ip address id = $ipaddress_id" | |
service_offering_id=$(cloudmonkey -p ${REGION} list serviceofferings name=${SERVICE_OFFERING_NAME} | jq ".serviceoffering[].id") | |
echo "service offering id = $service_offering_id" | |
template_id=$(cloudmonkey -p ${REGION} list templates templatefilter=all name="${TEMPLATE_NAME}" | jq ".template[0].id") | |
# template_id=$(cloudmonkey -p ${REGION} list templates templatefilter=all name="CentOS 6.6 64-bit" | jq ".template[0].id") | |
echo "tempalte id = $template_id" | |
# すでに登録されている場合エラーになるが、問題ない | |
register_ssh=$(cloudmonkey -p ${REGION} register sshkeypair name="${KEY_PAIR}" publickey="${TMP_ID_RSA_PUB}") | |
ssh_error=$(echo $register_ssh | jq ".errorcode") | |
# vm_nameに`_`など不正な文字を入れないことに注意 | |
vm=$(cloudmonkey -p ${REGION} deploy virtualmachine name="${VM_NAME}" zoneid=${zone_id} serviceofferingid="${service_offering_id}" templateid="${template_id}" keypair="${KEY_PAIR}") | |
vm_uuid=$(cloudmonkey -p ${REGION} list virtualmachines name=${VM_NAME} | jq ".virtualmachine[].id") | |
echo "vm uuid = $vm_uuid" | |
firewall_rule=$(cloudmonkey -p ${REGION} create firewallrule cidrlist=${CIRDRLIST} ipaddressid=${ipaddress_id} protocol=${PROTOCOL} startport=1 endport=65535) | |
port_forwarding_rule=$(cloudmonkey -p ${REGION} create portforwardingrule ipaddressid=${ipaddress_id} privateport=${SSH_PORT} protocol=${PROTOCOL} publicport=${PUBLIC_PORT} virtualmachineid=${vm_uuid} openfirewall=false) | |
echo "$port_forwarding_rule" | |
# ipaddressのダブルクォーテーションを除去 | |
ip_temp=${ipaddress%\"} | |
ipaddress=${ip_temp#\"} | |
# .ssh/confにログイン情報を書き込む | |
cat << EOF >> ~/.ssh/config | |
Host ${VM_NAME} | |
HostName ${ipaddress} | |
User root | |
IdentityFile ~/.ssh/tmp_id_rsa | |
Port ${PUBLIC_PORT} | |
Protocol 2 | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment