Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
このべんHadoopセットアップ記事-Slaveノード自動起動スクリプト
#!/bin/sh
CURL_COMMAND="/usr/bin/curl -Ssif"
JQ_COMMAND="/usr/bin/jq"
CURL_HTTP_METHOD_POST="-X POST"
CURL_HTTP_METHOD_GET="-X GET"
CURL_HTTP_METHOD_PUT="-X PUT"
CURL_HTTP_HEADER_ACCEPT_JSON='-H Accept:application/json'
CURL_HTTP_CONTENT_TYPE_JSON='-H Content-Type:application/json'
CONOHA_ENDPOINT_IDENTITY_SERVICE="https://identity.tyo1.conoha.io/v2.0/tokens"
CONOHA_ENDPOINT_TOKYO_COMPUTE_SERVICE="https://compute.tyo1.conoha.io/v2"
UUID_IMAGE_CENTOS_66="14961158-a69c-4af1-b375-b9a72982837d"
UUID_IMAGE_HADOOP_SLAVENODE="acfb0e18-2be5-4f35-b073-af7ecd252bfb"
requestNewAPIToken() {
API_USERNAME=${1}
API_PASSWORD=${2}
API_TENANTID=${3}
REQUEST_TOKEN_BODY='{"auth":{"passwordCredentials":{"username":"'${API_USERNAME}'","password":"'${API_PASSWORD}'"},"tenantId":"'${API_TENANTID}'"}}'
JQ_GET_TOKEN_ID="jq .access.token.id"
NEW_TOKEN_COMMAND="${CURL_COMMAND} ${CURL_HTTP_METHOD_POST} ${CURL_HTTP_HEADER_ACCEPT_JSON} -d ${REQUEST_TOKEN_BODY} ${CONOHA_ENDPOINT_IDENTITY_SERVICE}"
NEW_TOKEN=`${NEW_TOKEN_COMMAND} 2>/dev/null | tail -n 1 | ${JQ_GET_TOKEN_ID}`
echo ${NEW_TOKEN}
}
generateHeaderToken() {
API_TOKEN=${1}
echo '-H X-Auth-Token:'${API_TOKEN}''
}
startNewSlaveNode() {
API_TOKEN=${1}
API_TENANTID=${2}
UUID_FLAVOR_SELECTED=${3}
VPS_ROOT_PASSWORD=${4}
SECURITY_GROUP='[{"name":"gncs-ipv4-all"}]'
START_NEW_VM_BODY='{"server":{"imageRef":"'${UUID_IMAGE_HADOOP_SLAVENODE}'","flavorRef":"'${UUID_FLAVOR_SELECTED}'","adminPass":"'${VPS_ROOT_PASSWORD}'","security_groups":'${SECURITY_GROUP}'}}'
HEADER_TOKEN=`generateHeaderToken ${API_TOKEN}`
NEWVM_COMMAND="${CURL_COMMAND} ${CURL_HTTP_METHOD_POST} ${CURL_HTTP_HEADER_ACCEPT_JSON} ${HEADER_TOKEN} -d ${START_NEW_VM_BODY} ${CONOHA_ENDPOINT_TOKYO_COMPUTE_SERVICE}/${API_TENANTID}/servers"
NEW_SLAVENODE_UUID=`${NEWVM_COMMAND} | tail -n 1 | jq '.server.id'`
echo ${NEW_SLAVENODE_UUID}
}
askNametag() {
API_TOKEN=${1}
API_TENANTID=${2}
VM_UUID=${3}
HEADER_TOKEN=`generateHeaderToken ${API_TOKEN}`
ASK_NAMETAG_COMMAND="${CURL_COMMAND} ${CURL_HTTP_METHOD_GET} ${CURL_HTTP_CONTENT_TYPE_JSON} ${CURL_HTTP_HEADER_ACCEPT_JSON} ${HEADER_TOKEN} ${CONOHA_ENDPOINT_TOKYO_COMPUTE_SERVICE}/${API_TENANTID}/servers/${VM_UUID}/metadata"
JQ_GET_HOSTNAME="jq .metadata.instance_name_tag"
NEW_INSTANCE_NAMETAG=`${ASK_NAMETAG_COMMAND}| tail -n 1 |${JQ_GET_HOSTNAME}`
echo ${NEW_INSTANCE_NAMETAG}
}
nametagToDefaultHostname() {
NAMETAG=${1}
echo "host-"${NAMETAG}
}
nametagToIPAddress(){
NAMETAG=${1}
echo ${NAMETAG} | sed -e 's/-/\./g'
}
writeHostsFormat() {
NAMETAG=${1}
echo -e `nametagToIPAddress ${NAMETAG}`"\t"`nametagToDefaultHostname ${NAMETAG}` >> hosts
}
writeAnsibleInventoryFormat() {
NAMETAG=${1}
echo -e `nametagToDefaultHostname ${NAMETAG}` >> inventory
}
updateNametag() {
API_TOKEN=${1}
API_TENANTID=${2}
VM_UUID=${3}
NEW_NAMETAG=${4}
HEADER_TOKEN=`generateHeaderToken ${API_TOKEN}`
UPDATE_NAMETAG_BODY='{"metadata":{"instance_name_tag":"'${NEW_NAMETAG}'"}}'
UPDATE_NAMETAG_COMMAND="${CURL_COMMAND} ${CURL_HTTP_METHOD_POST} ${CURL_HTTP_CONTENT_TYPE_JSON} ${CURL_HTTP_HEADER_ACCEPT_JSON} ${HEADER_TOKEN} -d ${UPDATE_NAMETAG_BODY} ${CONOHA_ENDPOINT_TOKYO_COMPUTE_SERVICE}/${API_TENANTID}/servers/${VM_UUID}/metadata"
# ${UPDATE_NAMETAG_COMMAND}
}
addNewSlaveNode() {
TOKEN=${1}
API_TENANTID=${2}
UUID_FLAVOR_SELECTED=${3}
VPS_ROOT_PASSWORD=${4}
SERVER_UUID=`startNewSlaveNode ${TOKEN} ${API_TENANTID} ${UUID_FLAVOR_SELECTED} ${VPS_ROOT_PASSWORD} | sed -e "s/\"//g"`
NAMETAG_RAW=`askNametag ${TOKEN} ${API_TENANTID} ${SERVER_UUID}`
NAMETAG=`echo ${NAMETAG_RAW}|sed -e "s/\"//g"`
if [ "${NAMETAG}" = "null" ] || [ "${NAMETAG}" = "" ];
then
echo "Failed to start vm or get nametag." 1>&2
exit 1
fi
writeHostsFormat ${NAMETAG}
writeAnsibleInventoryFormat ${NAMETAG}
NEW_NAMETAG="Hadoop-slave-"${NAMETAG}
updateNametag ${TOKEN} ${API_TENANTID} ${SERVER_UUID} ${NEW_NAMETAG}
}
selectFlavor() {
FLAVOR_ARGS=${1}
UUID_FLAVOR_1GB="7eea7469-0d85-4f82-8050-6ae742394681"
UUID_FLAVOR_2GB="294639c7-72ba-43a5-8ff2-513c8995b869"
UUID_FLAVOR_4GB="62e8fb4b-6a26-46cd-be13-e5bbf5614d15"
UUID_FLAVOR_8GB="965affd4-d9e8-4ffb-b9a9-624d63e2d83f"
UUID_FLAVOR_16GB="3aa001cd-95b6-46c9-a91e-e62d6f7f06a3"
UUID_FLAVOR_32GB="a20905c6-3733-46c4-81cc-458c7dca1bae"
UUID_FLAVOR_64GB="c2a97b05-1b4b-4038-bbcb-343201659279"
case ${FLAVOR_ARGS} in
1GB) echo ${UUID_FLAVOR_1GB};;
2GB) echo ${UUID_FLAVOR_2GB};;
4GB) echo ${UUID_FLAVOR_4GB};;
8GB) echo ${UUID_FLAVOR_8GB};;
16GB) echo ${UUID_FLAVOR_16GB};;
32GB) echo ${UUID_FLAVOR_32GB};;
64GB) echo ${UUID_FLAVOR_64GB};;
*) echo "Invalid flavor argument"; exit 1;
esac
}
main() {
API_USERNAME=${1}
API_PASSWORD=${2}
API_TENANTID=${3}
VPS_FLAVOR=${4}
NUMBER_OF_SLAVENODES=${5}
VPS_ROOT_PASSWORD=${6}
TOKEN=`requestNewAPIToken ${API_USERNAME} ${API_PASSWORD} ${API_TENANTID} | sed -e s/\"//g`
if [ "${TOKEN}" = "" ]
then
echo "Failed to get token" 1>&2
exit 1
fi
FLAVOR_UUID=`selectFlavor ${VPS_FLAVOR}`
for i in `seq ${NUMBER_OF_SLAVENODES}`
do
echo ${i}
addNewSlaveNode ${TOKEN} ${API_TENANTID} ${FLAVOR_UUID} ${VPS_ROOT_PASSWORD}
done
}
main $*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.