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

  • A+
摘要

之前写了一篇用命令行的方法上传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

文件下载

博客主机
lookback

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:14   其中:访客  14   博主  0

    • avatar xiaoyutou 2

      中文,格式全没了。完整的修改在在这里http://www.pastebin.ca/3100147

      if [ ! -x /usr/local/bin/jq ]; then 
      	arch=i686
      	if [[ `uname -m` == "x86_64" ]];then
      		arch="x86_64"
      	fi
      	if [ $arch == "x86_64" ];then
      		wget -4cq https://github.com/stedolan/jq/releases/download/jq-1.4/jq-linux-x86_64 -O /usr/local/bin/jq && chmod +x /usr/local/bin/jq
      	else
      		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
      fi
      • avatar lookback 4

        2015_08_12_16_11_31 加入密码和TenantID在输入的时候星号显示功能

        • avatar aoao 0

          挂载成功后,为啥我打开控制台是黑的?

            • avatar lookback 4

              @aoao 网络问题,只有在打开控制台后在控制台的页面左下角看到下面图上的样子才算连接上了
              [img][/img]

            • avatar 0x00 0

              请问
              {“overLimit”: {“message”: “Invalid metadata size: Metadata property value greater than 255 characters”, “code”: 413}}
              该怎么办?

                • avatar lookback 4

                  @0x00 在哪一步出现的

                • avatar vivan 0

                  我也出现这个报错,是在挂载的时候。报错的。请问是什么问题

                    • avatar lookback 4

                      @vivan 能否提供截图信息

                      • avatar fd 1

                        @vivan 有遇到这个问题,文件名太长的原因,把文件名缩短就搞定了

                          • avatar lookback 4

                            @fd 谢谢反馈

                        • avatar fd 1

                          挂载完windows,需要先登录系统,然后reboot,按任意键进入光驱,开始安装

                          • avatar AWXX 0

                            成功mount以后,开机进入Win2003 安装界面,但是进行到选择分区安装的时候,无论选择哪一个分区,或者把现有分区删除重建分区,下一步,Setup都显示“安装程序只能在IA-64的GPT分区或者X-86的MBR分区安装,按Enter返回“,这个时候会注意到原来Conoha的分区都是GPT的,而CPU是X-86的,所以造成无法安装。我是今天刚申请的Conoha,不知道这个改变是不是最近才有的。

                              • avatar lookback 4

                                @AWXX 这个我不清楚,我没装过Windows。。