#!/bin/bash #descrition: #启动容器 #2016.09.29 Support Mount File # #precondition: Mir envionment variables Example #export env_etcd_serve r_url=http://172.16.70.73:2379,http://172.16.70.74:2379 #export containername=tomcat8 #export imageName= Hub.chinacloud.com/mir/tomcat:8 #export etcd_cd_phase_log_dir=mir/cicd/cd-phase-log/#export ETCD_TTL=2592000 # Docker_options_mount_info= "file_key1:/usr/vfile/setconfig1.xml;file_key2:/usr/vfile/setconfig2.xml;file_key3:/ Usr/vfile/setconfig3.xml "#export etcd_cdevent_json= Eyjraw5kijoiq0rfdmvudcisim1ldgfkyxrhijp7imv2zw50swqioijmyjuxowexmy1hn2m2ltqxodatyje2my00zjjmzmuxztc4njgilcjuyw1lijoichrvb Wnhdc0xmtexmtffq0rfdmvudcj9lcjzcgvjijp7imrlcgxveuluzm8ionsiyxbwrw52ijoimtaziiwizgvwbg95swqioii5y2ezndfjm2e5owqxmteilcjlbn Zab25lijoimiisimvudnmionsitvltuuxfueftu1dpukqioijodwfjbg91zcisik1zu1fmx1vstci6im15c3fsmtexmtexmtexiiwitvltuuxfvvnfuii6inj Vb3qifswiaw1hz2uioijodwiuy2hpbmfjbg91zc5jb20vbwlyl3rvbwnhdcisinrhzyi6ijgifswichjvamvjdeluzm8ionsichjvamvjdelkijointhkyjfj Mdlhmdu2yzc2ywqwm2Nknmi4ngyzmmy4njuzmdmwowm5mwrinwzknjmxztk0zmriyzuwmmiyy2yyocisinbyb2ply3royw1lijoiagvsbg9fdg9ty2f0in19lcj2zxjzaw9uijoimsj 9 #export containerenvs= #export cd_deploy_id=a4da53eb-6c3b-41af-b4cc-d9b71287a7b5 #export cd_event_id= A4da53eb-6c3b-41af-b4cc-d9b71287a7b5 #steps: #1. Verify that ETCD SERVER is available #2. Docker run #3. Send Cd-phase-log Finish Etcd_server_ Url= $ENV _etcd_server_url #EVENT_ID = "Effective_etcd_server_url=" "exe_res=" 0 "docker_options_p=" Docker_options_v = "" "Docker_container_name=" "#当容器挂载文件更新时, 1: Restart all Label=namespace=app container 0: Do not restart docker_restart_when_conf_update=" 0 "Log_ Seq_num=0 application_addr= "" vm_ip= "" Getvmip () {if ["$SCALR _external_ip"! = ""] then vm_ip= $SCALR _external_ip E Lif ["$SCALR _internal_ip"! = ""] then vm_ip= $SCALR _internal_ip fi log "$LINENO" "DEBUG" "Vm_ip is $VM _ip" #替换容器名 The illegal character in the name '/' docker_container_name=$ (Echo ${containername##*/}) If ["$DOCKER _container_name" = = ""] then Docker_conta iner_name=$ (echo $containerName | sed ' s/\//_/g ') fi} #funCtion generate EventId Generateeventid () {event_id= ' cat/proc/sys/kernel/random/uuid ' Log "$LINENO" "DEBUG" "Generate EventId & Gt $EVENT _id "} #$1: $LINENO #$2:error/debug/info #$3:msg log () {currenttimestamp= ' date ' +%y-%m-%d%h:%m:%s ' #currentTimeS
tamp= ' date-d ' $Time ' +%s ' #currentTimeStamp = ' Date +%s ' ((log_seq_num=log_seq_num+1)); echo "$LOG _seq_num $currentTimeStamp sh:docker-run.sh $CD _deploy_id $CD _event_id lineno:$1 $2::$3::" >>/var/log/
Docker_run_debug.log} deploymentfailure () {log "$LINENO" INFO "" step-4 send CICD-CD Phase finish log. " Sendcdphaselog "Finish" "Failed" Log "$LINENO" "INFO" "step-4 send CICD-CD Phase finish log.
Done "Log" $LINENO "INFO" "finished, deployment failure."} #校验ETCD SERVER is available checketcdserver () {arr= (${etcd_server_url//,/}) for Etcd_url in ${arr[@]} do etcd_version= ' curl- L $ETCD _url/version ' If ["$ETCD _version" = ""] then exe_res= "1" Log "$LINENO" "ERROR" "$ETCD _url unknow ETCD s Erver "Else effective_etcd_Server_url= $ETCD _url fi do if ["$EFFECTIVE _etcd_server_url" = ""] then exe_res= "1" Log "$LINENO" "ERROR" "No
ETCD service available. " Deploymentfailure Exit 0 fi log "$LINENO" "DEBUG" "Effective ETCD server > $EFFECTIVE _etcd_server_url"} #发送CD-pha Se-log #$1:finish #$2:failed/success Sendcdphaselog () {true >/var/log/etcd_event_msg.txt echo "' $ETCD _cdevent_ JSON ' "| Python-c "Import json,base64,sys;obj=json.loads (Base64.b64decode (Sys.stdin.read ())); obj[' Shellname ']= ' Docker-run.sh '; obj[' step ']= '; obj[' Stepstatus ']= ' $ '; obj[' Vmserverid ']= ' $SCALR _server_id '; obj[' vmIp ']= ' $VM _ IP ';p rint json.dumps (obj) >>/var/log/etcd_event_msg.txt #generateEventId If ["$ETCD _ttl" = ""] then Etcd_ TTL = 2592000 fi log_msg= ' curl-l $EFFECTIVE _etcd_server_url/v2/keys/$ETCD _cd_phase_log_dir$cd_event_id/$VM _ip-xput- D ttl= $ETCD _ttl--data-urlencode value@/var/log/etcd_event_msg.txt ' log "$LINENO" "DEBUG" "Send Cd-phase-event[step:$1" , Stepstatus:$2]. Event MSG Content > $LOG _msg "} #$1:repository #$2:tag i=0 checkimage () {((i=i+1));
If ["$i" = [] then log "$LINENO" "ERROR" "Image download failure." Deploymentfailure Exit 0 fi #log "$LINENO" "DEBUG" "$i $---$ image_id= ' docker images | grep $ | grep | awk ' {print $} ' if ["$IMAGE _id" = "] then Sleep 10s checkimage" $ "" $ "Else Log" $LINENO "" DEBUG "Status: Downloaded newer image for $1:$2 "fi} #下载镜像 #return-P 2379:2379-l" service_2379_name=/mir/appmnt/stacks/$SCALR _farm_i d/$SCALR _farm_role_id/$DOCKER _container_name "-L" service_2379_id= $VM _ip:2379 "Downloadimage () {imagename=$ (echo $ ImageName |
Sed ' s/\\//g ') log "$LINENO" "DEBUG" "Docker pull $imageName" Docker pull $imageName arr= (${imagename//:/}) Checkimage "${arr[0]}" "${arr[1"} "#解析dockerFile port number Log" $LINENO "" DEBUG "" Docker inspect $imageName "Ports= ' Docker Inspect $imageName | Python-c "Import json,sys;obj=json.loads (Sys.stdin.read ()) keys=obj[0][' Config ' [' Exposedports '].keyS ();p rint ', '. Join ([Key.split ('/') [0] for key in Keys]); " ' List= (${ports//,/}) for port in ${list[@]} do docker_options_p= "$DOCKER _options_p-p $port: $port-L Service_${por T}_name=\ "/mir/appmnt/stacks/$SCALR _farm_id/$SCALR _farm_role_id/$DOCKER _container_name\"-l service_${port}_id=$ VM_IP: $port "application_addr=" $APPLICATION _addr/http $VM _ip: $port; "Done Log" $LINENO "DEBUG" "Parse Port Parameters: $DOCKER _options_p"} #配置并启动confd服务 startconfd () {#停止confd Pid_co Nfd= ' Ps-ef | GREP/OPT/CONFD |grep-v grep | awk ' {print $} ' if ["$PID _confd"! = ""] then kill-9 $PID _confd Log "$LINENO" "DEBUG" "Killed Confd, pid= $PID _co
NFD. " Fi #清除模版文件 rm-rf/etc/confd/templates/* rm-rf/etc/confd/conf.d/* rm-rf/opt/mir-cicd/conf/managerment/* Log "$L
Ineno "DEBUG" "Delete all confd template file." Sleep 1s #{etcdfilecontentkey1}:{containerfilepath1}:{filename1}; {etcdfilecontentkey2}:{containerfilepath2}:{filename2} #DOCKER_OPTIONS_MOUNT_INFO =51000000-0000-0000-0000-000000000000/setconfig1:/usr/vfile:setconfig1.xml;51000000-0000-0000-0000-000000000000/setconfig2:/
Usr/vfile:setconfig2.xml list= (${docker_options_mount_info//;/})
For arr in ${list[@]} do mountinfo= (${arr//:/}) #mountInfo [0] for Etcdkey, such as: 51000000-0000-0000-0000-000000000000/setconfig1 #mountInfo [1] for the container mount path, such as:/usr/vfile # MOUNTINFO[2] for the configuration file name, such as: Setconfig1.xml log "$LINENO" "DEBUG" "Mount file Etcdkey:/mir/cicd/config/manager/${mountinfo[0 ]} "etcdkey= (${mountinfo[0]//\//}) cp/etc/confd/mir_conf_management_source/mir_cicd_conf_manager.tmpl/etc/confd/ Templates/mir_cicd_conf_manager_${etcdkey[1]}.tmpl #修改读取etcd key Sed-i "s#etcd_key#${mountinfo[0]} #g"/etc/confd/ Templates/mir_cicd_conf_manager_${etcdkey[1]}.tmpl log "$LINENO" "DEBUG" "Generate File>/etc/confd/templates/mir _cicd_conf_manager_${etcdkey[1]}.tmpl "cp/etc/confd/mir_conf_management_source/mir_cicd_conf_manager.toml/etc/ CONFD/CONF.D/MIR_CICD_CONF_MANAGER_${ETCDKEY[1]}.TOML #修改生成目标文件Name Sed-i "s#mir_cicd_conf_manager_etcd_key.conf#${mountinfo[2]} #g"/etc/confd/conf.d/mir_cicd_conf_manager_${ ETCDKEY[1]}.TOML #修改模版文件名 sed-i "s#mir_cicd_conf_manager_etcd_key.tmpl#mir_cicd_conf_manager_${etcdkey[1]}.tmpl#g "/etc/confd/conf.d/mir_cicd_conf_manager_${etcdkey[1]}.toml #修改监听key sed-i" s#\/etcd_key#\/${mountinfo[0]} #g "/etc /CONFD/CONF.D/MIR_CICD_CONF_MANAGER_${ETCDKEY[1]}.TOML if ["$DOCKER _restart_when_conf_update" = = "0"] then #当容器挂载 When the file is updated, 1: Restart all Label=namespace=app container 0: Do not restart Sed-i "s/reload_cmd/#reload_cmd/g"/etc/confd/conf.d/mir_cicd_conf_manager _${etcdkey[1]}.toml fi Log "$LINENO" "DEBUG" "Generate File>/etc/confd/conf.d/mir_cicd_conf_manager_${etcdkey[1]} . toml "docker_options_v=" $DOCKER _options_v-v/opt/mir-cicd/conf/managerment:${mountinfo[1]} "done Log" $LINENO " "DEBUG" "Docker_options_v: $DOCKER _options_v"/opt/confd-backend etcd-node $EFFECTIVE _etcd_server_url-log-level Debug-watch True & #检测配置文件是否生成 for arr in ${list[@]} do ifile=0 mountinfo= (${arr//:/}) #mountInfo [0] for Etcdkey, such as: 51000000-0000-0000-0000-000000000000/setconfig1 #mountInfo [1] for the container mount path, such as:/usr/vfile # MOUNTINFO[2] is a profile name, such as: Setconfig1.xml etcdkey= (${mountinfo[0]//\//}) filename= "/opt/mir-cicd/conf/managerment/${ MOUNTINFO[2]} "log" $LINENO "DEBUG" Check the configuration file exists > $filename;
ETCD key:${etcdkey[1]} "Checkconffile" $filename "" ${etcdkey[1]} "done} ifile=0 Checkconffile () {((ifile=ifile+1));
If ["$iFile" = [] then log "$LINENO" "ERROR" "ETCD configuration file does not exist > $." Deploymentfailure Exit 0 fi if [-f "] then #文件存在, delete the first row sed-i ' 1d '" $ "else #文件不存在 sleep 1s check Conffile "$" "$" fi} #docker run Dockerrun () {#判断是否启动confd服务 if ["$DOCKER _options_mount_info"! = ""] then STA rtconfd fi Log "$LINENO" "DEBUG" "Docker run-d--name $DOCKER _container_name--net=host-l namespace=app-e host_ip=$ Vm_ip $containerEnvs $DOCKER _options_v-v/etc/localtime:/etc/localtime:ro $DOCKER _options_p--restart always $imageName "#IFS = ' cmd= ' DOCKER run-d--name $DOCKER _container_name--net=host-l namespace=app-e host_ip= $VM _ip $containerEnvs $DOCKER _options_v-v/etc/localtime :/etc/localtime:ro $DOCKER _options_p--restart always $imageName "Mkdir-p/tmp/mir-cicd/cd true >/tmp/mir-cicd/cd/c d.sh echo "$cmd" >>/tmp/mir-cicd/cd/cd.sh bash/tmp/mir-cicd/cd/cd.sh sleep 2s container_id= ' docker PS | grep $DOCKER _container_name | awk ' {print $} ' if ["$CONTAINER _id" = "] then exe_res=" 1 "Log" $LINENO "" ERROR "" Failed to start the CONTAINER. "Deploymentfailure exit 0 Else Log" $LINENO "DEBUG" "Start the container successfully. container_id > $CONTAINER _id "fi} deleteinf () {#删除所有容器 (tag: namespace=app) Log" $LINENO "" DEBUG "" Delete all Conta In the Namespace=app container "Docker rm-f ' Docker ps-a-Q--filter" Label=namespace=app "' Sleep 1 #imageName =$ ( echo $imageName | Sed ' s/\\//g ') #log "$LINENO" "Debug" "Delete image > $imageName" #docker rmi $imageName Log "$LINENO" "Debug" "Delete all Ima" GE "Docker RMI" Docker images |grep-v gliderlabs/registrator |awk-f "{print$3} ' |grep-v" IMAGE "'} #检查logstash服务
Status I_logstash=0 Checklogstashstatus () {((i_logstash=i_logstash+1));
If ["$i _logstash" = ten] then log "$LINENO" "ERROR" "Logstash hasn ' t been started." Deploymentfailure Exit 0 fi logstash_process_num= ' ps-ef | grep/opt/logstash-2.3.3 |grep-v grep | awk ' {print $} ' if ["$LOGSTASH _process_num" = "] then sleep 10s checklogstashstatus else Log" $LINENO "" Debu
G "" Logstash process number is $LOGSTASH _process_num. "
FI sleep 3s} main () {log "$LINENO" INFO "" Deploy start. " GETVMIP #1. Check the Logstash service status log "$LINENO" "INFO" "Step-1 check Logstash. Start "Checklogstashstatus log" $LINENO "INFO" "Step-1 check Logstash. Done "#2 Verify that ETCD server is available with log" $LINENO "INFO" step-2 check etcd-server. Start "ChecketCdserver log "$LINENO" "INFO" "Step-2 check etcd-server. Done "#3. Delete container & current container mirror log" $LINENO "INFO" "step-3 clear envionmental information. Start "Deleteinf log" $LINENO "INFO" "step-3 clear envionmental information. Done "#4. Download container image Log" $LINENO "INFO" "step-4 download Docker image. Start "Downloadimage log" $LINENO "INFO" "step-4 download Docker image. Done "#5. Start container Log" $LINENO "INFO" "step-5 start the container. Start "Dockerrun log" $LINENO "INFO" "step-5 start the container. Done "#6. Send Cd-phase-log finish if [" $EXE _res "=" 1 "] then Deploymentfailure else LOG" $LINENO "" INFO "step-6 Send CICD-CD phase finish log. Start "Sendcdphaselog" Finish "" Success "Log" $LINENO "" INFO "" step-6 send CICD-CD Phase finish log.
Done "Log" $LINENO "INFO" "finished, deployment successful. $APPLICATION _addr" fi} Main