Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ConoHaでVMを立ち上げてRDPに全自動接続するような何か (試作)
#!/bin/sh
# ConoHaでVMを立ち上げてRDPに全自動接続するような何か (試作)
# (響きはよくても、TODOが多くエラーチェックもやってないので未だ使い物にならないと思うんですわ。座敷わらしに怒られても知らんでっちゅうことでひとつ。)
#
# Copyright (c) 2016 SHIMADA Hirofumi
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Setting
USERNAME="<api username>" # ConoHa API access Account
PASSWORD="<api password>"
TENANT="<your tenant id>"
ACCOUNT_SERVICE="https://identity.tyo1.conoha.io/" # TODO : リージョン指定にしてここは自動取得したい。とりあえずはリージョンに合った identity API の URL をここに指定すれば動くはず。
TC_IMAGENAME="<thinclient image name>"
TC_FLAVOR="g-1gb" # VM Plan (この場合1GBプラン)
APPLY_SECURITY_GROUP="gncs-ipv4-all"
VM_ID="" # Set if already booted the VM (for Debug)
# Get token
ident_resp=$( curl -X POST -H "Accept: application/json" -d "{\"auth\":{\"passwordCredentials\":{\"username\":\"$USERNAME\",\"password\":\"$PASSWORD\"},\"tenantId\":\"$TENANT\"}}" "$ACCOUNT_SERVICE/v2.0/tokens" )
token=$( echo $ident_resp | jq ".access.token.id" | sed "s/\"//g" )
image_service=$( echo $ident_resp | jq ".access.serviceCatalog[] | select(.type == \"image\") | .endpoints[0].publicURL" | sed "s/\"//g" )
compute_service=$( echo $ident_resp | jq ".access.serviceCatalog[] | select(.type == \"compute\") | .endpoints[0].publicURL" | sed "s/\"//g" )
network_service=$( echo $ident_resp | jq ".access.serviceCatalog[] | select(.type == \"network\") | .endpoints[0].publicURL" | sed "s/\"//g" )
echo "$network_service"
if test -z "$VM_ID"; then
# Get thin-client image id
imagelist_resp=$( curl -X GET -H "Accept: application/json" -H "X-Auth-Token: $token" "$image_service/v2/images" )
tcimage_id=$( echo $imagelist_resp | jq ".images[] | select(.name == \"$TC_IMAGENAME\") | .id" | sed "s/\"//g" )
echo "Thin client image ID : $tcimage_id"
# Get flavor id
flavorlist_resp=$( curl -X GET -H "Accept: application/json" -H "X-Auth-Token: $token" "$compute_service/flavors/detail" )
flavor_id=$( echo $flavorlist_resp | jq ".flavors[] | select(.name == \"$TC_FLAVOR\") | .id" | sed "s/\"//g" )
echo "Flavor ID : $flavor_id"
# Make ConoHa VM
makevm_resp=$( curl -X POST -H "Accept: application/json" -H "X-Auth-Token: $token" -d "{\"server\": {\"imageRef\": \"$tcimage_id\",\"flavorRef\": \"$flavor_id\",\"security_groups\":[{\"name\": \"default\"},{\"name\": \"$APPLY_SECURITY_GROUP\"}]}}" "$compute_service/servers" )
vm_id=$( echo $makevm_resp | jq ".server.id" | sed "s/\"//g" )
echo "VM ID : $vm_id"
# TODO : ここでVMが正常に作成されるまでloopを回し、監視する必要がある
sleep 350
else
# Already have VM
vm_id="$VM_ID"
fi
# Power ON
poweron_resp=$( curl -X POST -H "Accept: application/json" -H "X-Auth-Token: $token" -d '{"os-start": null}' "$compute_service/servers/$vm_id/action" )
# Get IP Address
ipaddr_resp=$( curl -X GET -H "Accept: application/json" -H "Content-Type: application/json" -H "X-Auth-Token: $token" "$compute_service/servers/$vm_id/ips" )
ipaddr=$( echo $ipaddr_resp | jq ".addresses[] | .[] | select(.version == 4) | .addr" | sed "s/\"//g" )
echo "IP Address is : $ipaddr"
# TODO : RDP portなどが正常に繋がるまでloopを回し、監視する必要がある
sleep 60
xfreerdp /v:$ipaddr
# TODO : 頃合いを見てVMを削除したい(一定時間ごとに監視し、xfreerdpが接続にいっていなければ削除)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment