Shell Map implements Docker simple deployment script

Source: Internet
Author: User
Tags docker run

With the popularity of Docker, the application of Docker is more and more, the work is certainly not necessary, but the individual feel that some of Docker commands require a bit more parameters, such as running a container, –name to the container name,-D background run and so on. According to personal habits, a lot of parameters are fixed, there is no need to knock once every operation, efficiency is particularly low. Especially for people who are not familiar with Docker, such as OPS, it is necessary to provide them with a deployment script, because the more they can simplify their work, the less chance they have to find us to solve the problem. The
has implemented a simple deployment script that simplifies Docker installations, container runs, and so on, with different processing based on the first parameter passed to the script as a command. For different commands in the shell using case to match the different processing, and later found that with the increase in command, case matching code more and more long, and need to modify many places, modification is very laborious, after all, processing to implement functional processing, but also to provide some help information.
These two days have been thinking how to improve the script how to write, so that it can be well extended, add new commands easier. After a night of thinking and search on the internet, and ultimately the main use of map to simplify case operations, the final script is quite satisfactory, the last one to add a command will not modify the original code, just need to add new code in the same place, add the Help information in the map, and then add a processing function corresponding to the good. The implementation is as follows, and only a few simple commands are added to the script:

#!/bin/bash #声明一个命令帮助容器, the first parameter entered when the script is run is treated as a command, depending on the command, Declare-a cmdhelpmap= () redcolor= "\033[31m" normalcolor= "\033[ 
0m "yellowcolor=" \033[33m "scriptname=$0 #运行的脚本名字 inputargsnum=$# #运行脚本时输入参数的个数 #打印信息, find script names for printing information, red print parameters to be passed

    function println () {findscriptname=0 #是否找到脚本名字 index=0;
            #遍历打印信息 for Arg in $@ does if [${arg} = ${scriptname}] then findscriptname=1
        Echo-en "\n\t" fi if [${index}-gt ${inputargsnum}] then Echo-en ${redcolor}
        Fi echo-en "${arg}" if [${findscriptname}-eq 1] then let index++
    Fi done Echo-e ${normalcolor}} #参数处理 to determine whether the number of input parameters is equal to the number of arguments required in the help information, and the non-equal prints the Help information and exits function parameterhandling () {

    Findscriptname=0 argsnum=0; Key=$1 helpmsg=${cmdhelpmap[$key]} #遍历帮助信息总计需要输入的参数个数, start counting after the script name appears in the Help message, #自动以空格分割帮助信息进行遍历 for MSG in ${he LPMSG} do if [${findscriptnaMe}-eq 1] then let's argsnum++ fi if [${msg} = ${scriptname}] Then findscriptname=1 fi do #参数个数不相等处理 if [${inputargsnum}-ne ${argsnum}] then Printl N ${helpmsg} exit Fi} #确认操作 function confirm () {Echo-ne "${yellowcolor}$*? [ 
        Y/n]${normalcolor} "read input if [-Z ${input}] Then exit fi if [${input}! =" Y "] Then Exit fi} ############################################################################################# #添加命 The order takes only two steps: #1, Command key, add help information to Cmdhelpmap #2, define command handler functions for the function name #例如以下的help命令 #help命令 cmdhelpmap["Help"]= "see the Helpful information: ${
    ScriptName} "function Help () {inputargsnum=0 println" Usage: "prefunc=${funcname[1]} number=1; For key in ${!cmdhelpmap[@]} does println ${number}.${cmdhelpmap[$key]} let number++ done} #instal L Command cmdhelpmap["install"]= "Installation Docker: ${scriptname} Install" Function Install () {docker-v if [$?-eq 0]; then confirm Docker is installed to confirm the installation of fi Curl-ssl https://g Et.daocloud.io/docker |
    SH} #run命令 cmdhelpmap["Run"]= "Running container: ${scriptname} Run [Imagename:imagetag] [Hostport:containerport]" function run () { Image=$2 containername=${image/:/-} port=$3 docker run-d--name ${containername}--restart=always-p ${port } ${image} If [$?-ne 0]; Then println ${redcolor} failed to run ${containername}, check that the mirror ${image} has exit fi} #rmi命令 cmdhelpmap["RMI"]= "delete Except Mirror: ${scriptname} RMI [Imagename:imagetag] "function rmi () {image=$2 Confirm OK delete Mirror ${image} docker RMI ${im AGE} If [$?-ne 0]; Then println ${redcolor} failed to delete the mirror, check that the mirror ${image} is present fi} #rm命令 cmdhelpmap["rm"]= "Delete container: ${scriptname} RM [Imagen Ame-imagetag] "function rm () {containername=$2 docker rm-f ${containername} If [$?-ne 0]; then PR Intln ${redcolor} Failed to delete container, check the container ${containername} for presence of Fi} # # ########################################################################################### #如果第一个参数为空或者不存在map中, The default first parameter is Help cmd=$1 if [-Z $cmd] | |
    [-Z "${cmdhelpmap[$cmd]}"] then if [$#-ne 0] then println ${redcolor} invalid parameter: $* fi cmd= "Help" Inputargsnum=1 fi #参数处理 parameterhandling $cmd #执行相应处理函数 $cmd $*

The

script is saved directly as deploy.sh, and here are some simple action tips:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.