ConoHa之利用API上传镜像的脚本-ITB运维部落—http://www.itbcn.cn—ITB运维技术交流之家平台
记录工作点滴
分享运维知识

ConoHa之利用API上传镜像的脚本

摘要

之前写了一篇用命令行的方法上传ISO镜像到ConoHa的方法,今天来一篇借助脚本和API接口实现自动上传的方法。

 

2015_08_22更新1.2版实现全自动上传挂载 卸载,自动关机、自动开机、自动设置vnc键盘

wget http://www.dwhd.org/wp-content/uploads/2015/08/ConoHa_UPISO.sh
bash ConoHa_UPISO.sh uoload #上传镜像
bash ConoHa_UPISO.sh mount  #挂载镜像
bash ConoHa_UPISO.sh umount #卸载镜像
bash ConoHa_UPISO.sh check  #上传查询

 

 

上传镜像的方法
ConoHa之利用API上传镜像的脚本
检测镜像上传结果的方法
ConoHa之利用API上传镜像的脚本
镜像挂载卸载的方法
ConoHa之利用API上传镜像的脚本
脚本使用方法
ConoHa之利用API上传镜像的脚本

 

事实证明可以上传ftp:// http:// ftps:// https://格式的url路径的镜像

ConoHa之利用API上传镜像的脚本
ConoHa之利用API上传镜像的脚本
挂载后的效果

ConoHa之利用API上传镜像的脚本

脚本源码

#!/bin/bash
#########################################################################
# File Name: ConoHa_UPISO.sh
# Author: LookBack
# Email: admin#dwhd.org
# Version:
# Created Time: 2015年08月11日 星期二 21时37分03秒
#########################################################################

hidingInput() {
	outinfo="" && read -s -n 1 -p "$1" info
	while [ -n "$info" ];do
		outinfo=$outinfo$info && echo -n \*
		read -s -n 1 info
	done
	echo -e "\n" && eval $2=$outinfo
}

getInfo() {
	read -p "Plz input API username : " UserName
	hidingInput "Plz input API password : " PassWord

	while true; do
		if ! egrep -o '^.{32}\b' <<< $TenantID >/dev/null 2>&1; then
			[ -n "$TenantID" ] && echo -e "\nTenantID Err, Plz input TenantID again~~~!"
			hidingInput "Plz input API Tenant ID : " TenantID
		else
			break
		fi
	done

	[ "$1" = "no" ] || while true; do
		if ! egrep -o '^(http展开?|ftp展开?)://' <<< $ISOURL >/dev/null 2>&1; then
			[ -n "$ISOURL" ] && echo -e "\nISO URl Err, Plz input URL again~~~!"
			read -p "Plz input ISO URL : " ISOURL
		else
			break
		fi
	done

	while true; do
		echo -e "\nPlz select the location of the node
Japan=1
Singapore=2
USA=3"
		read -p 'Plz input "1" or "2" or "3" : ' Node
		if [ "$Node" = "1" ]; then
			Node=tyo1 && break
		elif [ "$Node" = "2" ]; then
			Node=sin1 && break
		elif [ "$Node" = "3" ]; then
			Node=sjc1 && break
		else
			echo -e "\nInput Err, Plz select again~~"
		fi
	done
	[ "$?" = "0" ] && return 0 || return 1
}

checkInfo() {
	while true; do
		if [ ! -x /usr/local/bin/jq ]; then
			[ "$(getconf LONG_BIT)" = "64" ] && wget -4cq https://github.com/stedolan/jq/releases/download/jq-1.4/jq-linux-x86_64 -O /usr/local/bin/jq
			[ "$(getconf LONG_BIT)" = "32" ] && wget -4cq https://github.com/stedolan/jq/releases/download/jq-1.4/jq-linux-x86 -O /usr/local/bin/jq
			chmod +x /usr/local/bin/jq
		fi
		[ "$1" = "no" ] && getInfo no || getInfo
		XAuthToken=`curl -s -X POST -H "Accept: application/json" \
-d "{\"auth\": {\"passwordCredentials\": {\
\"username\": \"$UserName\",\"password\": \"$PassWord\"},\"tenantId\": \"$TenantID\"\
}}" https://identity.${Node}.conoha.io/v2.0/tokens | jq . | \
awk -F'([ ]|"|:)+' '$2=="id"{print $3;exit}'`
		egrep -o '^.{32}\b' <<< $XAuthToken >/dev/null 2>&1
		[ "$?" = "0" ] && break || echo "Info Err, Plz input again ~~"
	done
	[ "$?" = "0" ] && return 0 || return 1
}

upISO() {
	curl -s -X POST \
	-H 'Content-Type: application/json' -H 'Accept: application/json' -H "X-Auth-Token: $XAuthToken" \
	-d "{\"iso-image\": {\"url\": \"$ISOURL\"}}" https://compute.${Node}.conoha.io/v2/$TenantID/iso-images | jq .
	[ "$?" = "0" ] && return 0 || return 1
}

checkState() {
	curl -s -X GET \
	-H 'Content-Type: application/json' -H 'Accept: application/json' -H "X-Auth-Token: $XAuthToken" \
	https://compute.${Node}.conoha.io/v2/$TenantID/iso-images | jq .
	[ "$?" = "0" ] && return 0 || return 1
}

getPath() {
	curl -s -X GET -H 'Content-Type: application/json' -H 'Accept: application/json' \
	-H "X-Auth-Token: $XAuthToken" https://compute.${Node}.conoha.io/v2/$TenantID/iso-images | \
	jq . | awk -F'"' '/path/{a[++b]=$4;print "path["b"]="a[b]}'
}

QAQ() {
	getPath && eval `getPath`
	echo && read -p "Plz enter the PATH that needs to be ${1}mount : " pathNO
	echo && read -p "Plz input your VPS_UUID : " VPSUUID
}

mountISO(){
	[ -z "$XAuthToken" ] && checkInfo no

	[ -z "$1" ] && QAQ
	curl -s -X POST -H "Accept: application/json" -H "X-Auth-Token: $XAuthToken" \
	-d "{\"mountImage\": \"${path[$pathNO]}\"}" \
	https://compute.${Node}.conoha.io/v2/$TenantID/servers/$VPSUUID/action
	curl -s -X GET -H "Accept: application/json" -H "X-Auth-Token: $XAuthToken" \
	https://compute.${Node}.conoha.io/v2/$TenantID/servers/$VPSUUID/ | jq .
	[ "$?" = "0" ] && return 0 || return 1
}

umountISO() {
	[ -z "$XAuthToken" ] && checkInfo no
	QAQ u
	curl -s -X POST -H "Accept: application/json" -H "X-Auth-Token: $XAuthToken" \
	-d "{\"unmountImage\": \"${path[$pathNO]}\"}" https://compute.${Node}.conoha.io/v2/$TenantID/servers/$VPSUUID/action | jq .
	[ "$?" = "0" ] && return 0 || return 1
}

SelectExit() {
	while true; do
		echo 'Wait a moment to query the upload state.
Enter "Yes", continue to wait
Enter "No|Quit", exit script'
		read -p "Plz input : " Select
		case "$Select" in
			y|Y|[yY][eE][sS])
				echo "Plz wait 30s" && sleep 30
				checkState
				;;
			n|N|[nN][oO]|q|Q|[qQ][uU][iI][tT])
				break
				;;
			*)
				echo -e "Input Err......\n"
		esac
	done
}

if [ -n "$1" ]; then
	case "$1" in
		upload|u)
			checkInfo && upISO && SelectExit
			;;
		check|c)
			checkInfo no && checkState && SelectExit
			;;
		mount|m)
			mountISO
			;;
		umount|um)
			umountISO
			;;
		*)
			echo $"Example :
Usage: $0 {[upload|u]|[check|c]|[mount|m]|[umount|um]}"
	esac
else
	checkInfo && upISO
	[ "$?" = "0" ] && SelectExit
fi

 

文件下载

博客主机
未经允许不得转载:ITB运维部落—http://www.itbcn.cn—ITB运维技术交流之家平台 » ConoHa之利用API上传镜像的脚本

如果文章对你有帮助,欢迎点击上方按钮打赏作者

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址