Skip to content

Instantly share code, notes, and snippets.

@bageljp
Last active December 18, 2015 15:59
Show Gist options
  • Select an option

  • Save bageljp/5808024 to your computer and use it in GitHub Desktop.

Select an option

Save bageljp/5808024 to your computer and use it in GitHub Desktop.
さくらのクラウドAPI
#!/bin/bash
ACCESS_KEY="<access-key>"
ACCESS_KEY_SECRET="<access-key-secret>"
SERVER_NUM=${1:-1}
HOST_NAME="<hostname>"
DISK_ID="<disk-id>"
SSH_KEY_ID="<ssh-key-id>"
PASSWORD="<password>"
for i in `seq 1 1 ${SERVER_NUM}`; do
curl --user "${ACCESS_KEY}":"${ACCESS_KEY_SECRET}" -X POST \
-d '{"Disk":{"Name":"${HOST_NAME}","Description":"","Zone":{"ID":31001},"Plan":{"ID":4},"Connection":"virtio","SourceDisk":{"ID":"${DISK_ID}"}},"Count":0}' \
"https://secure.sakura.ad.jp/cloud/api/cloud/1.0/disk" | jq '.Disk.ID' | sed -e 's/\"//g' | while read disk; do
loop=1
while [ ${loop} -eq 1 ]; do
curl --user "${ACCESS_KEY}":"${ACCESS_KEY_SECRET}" \
"https://secure.sakura.ad.jp/cloud/api/cloud/1.0/disk/${disk}" | jq '.Disk.Availability' | grep "available" > /dev/null
loop=$?
sleep 5
done
curl --user "${ACCESS_KEY}":"${ACCESS_KEY_SECRET}" -X POST \
-d '{"Server":{"Name":"${HOST_NAME}","Zone":{"ID":31001},"ServerPlan":{"ID":104},"Icon":{"ID":"112300511981"},"Tags":["api_create"],"ConnectedSwitches":[{"Scope":"shared","BandWidthMbps":100}]},"Count":2}}' \
"https://secure.sakura.ad.jp/cloud/api/cloud/1.0/server" | jq '.Server.Instance.Server.ID' | sed -e 's/\"//g' | while read server; do
curl --user "${ACCESS_KEY}":"${ACCESS_KEY_SECRET}" -X PUT -d '' \
"https://secure.sakura.ad.jp/cloud/api/cloud/1.0/disk/${disk}/to/server/${server}" | jq '.'
curl --user "${ACCESS_KEY}":"${ACCESS_KEY_SECRET}" -X PUT \
-d {"UserSubnet":{},"HostName":"${HOST_NAME}","Password":"${PASSWORD}","SSHKey":{"ID":"${SSH_KEY_ID}"},"Count":0} \
"https://secure.sakura.ad.jp/cloud/api/cloud/1.0/disk/${disk}/config" | jq '.'
curl --user "${ACCESS_KEY}":"${ACCESS_KEY_SECRET}" -X PUT -d '' \
"https://secure.sakura.ad.jp/cloud/api/cloud/1.0/server/${server}/power" | jq '.'
done
done
done
#!/bin/bash
ACCESS_KEY="<access-key>"
ACCESS_KEY_SECRET="<access-key-secret>"
curl --user "${ACCESS_KEY}":"${ACCESS_KEY_SECRET}" \
"https://secure.sakura.ad.jp/cloud/api/cloud/1.0/server" | \
jq '.Servers | .[] | select(.Instance.Status == "up")' | jq '.Interfaces[].IPAddress'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment