持续集成-shell自动创建jenkins节点并通过jnlp链接jenkins服务器

#!/bin/bash
echo "get real Ip..."
currentIp=`hostname -I`
jenkinsuser="jenkins用户名"
jenkinspassword="jenkins密码"
jenkinsurl="http://jenkinsRootUrl"
echo "currentIp is ${currentIp}"
currentIps=(${currentIp})
echo "the first ip is : ${currentIps[0]}"
localIp=${currentIps[0]}
jenkinsnodetesturl="http://jenkinsRootUrl/computer/${localIp}/api/json"
status_code=$(curl --write-out %{http_code} --silent --output /dev/null ${jenkinsnodetesturl})
NODE_NAME=${localIp}
JSON_OBJECT="{ 'name':+'${NODE_NAME}',+'nodeDescription':+'Android pkg docker agent',+'numExecutors':+'5',+'remoteFS':+'/root/jenkinsspace',+'labelString':+'docker-android-pkg',+'mode':+'NORMAL',+'':+['hudson.slaves.JNLPLauncher',+'hudson.slaves.RetentionStrategy\$Always'],+'launcher':+{'stapler-class':+'hudson.slaves.JNLPLauncher',+'\$class':+'hudson.slaves.JNLPLauncher',+'workDirSettings':+{'disabled':+true,+'workDirPath':+'',+'internalDir':+'remoting',+'failIfWorkDirIsMissing':+false}},+'retentionStrategy':+{'stapler-class':+'hudson.slaves.RetentionStrategy\$Always',+'\$class':+'hudson.slaves.RetentionStrategy\$Always'},+'nodeProperties':+{'stapler-class-bag':+'true'}}"
echo "jsonObject is ${JSON_OBJECT}"
if [[ "$status_code" -ne 200 ]] ; then
  curl -L -s -o /dev/null -v -k -w "%{http_code}" -u "${jenkinsuser}:${jenkinspassword}" -H "Content-Type:application/x-www-form-urlencoded" -X POST -d "json=${JSON_OBJECT}" "${jenkinsurl}/computer/doCreateItem?name=${NODE_NAME}&type=hudson.slaves.DumbSlave"
  secret=`curl -L -s -u ${jenkinsuser}:${jenkinspassword}  ${jenkinsurl}computer/${localIp}/slave-agent.jnlp | sed "s/.*<application-desc main-class=\"hudson.remoting.jnlp.Main\"><argument>\([a-z0-9]*\).*/\1/"`
  echo "secret is ${secret}"
  wget -O ~/agent.jar http://xxx/agent.jar
  mkdir -p ~/jenkinsspace
  nohup java -jar ~/agent.jar -jnlpUrl "${jenkinsurl}/computer/${localIp}/slave-agent.jnlp" -secret "${secret}" -workDir ~/jenkinsspace > /root/jnlp.log &
  echo "create node and start agent success..."
  tail -f /root/jnlp.log
else
  testResp=`curl ${jenkinsnodetesturl} | jq ".offline"` 
  if [[ "${testResp}" == 'true' ]];then
    echo "agent is offline , staring..."
    secret=`curl -L -s -u ${jenkinsuser}:${jenkinspassword}  ${jenkinsurl}computer/${localIp}/slave-agent.jnlp | sed "s/.*<application-desc main-class=\"hudson.remoting.jnlp.Main\"><argument>\([a-z0-9]*\).*/\1/"`
    echo "secret is ${secret}"
    wget -O ~/agent.jar http://xxx/agent.jar
    mkdir -p ~/jenkinsspace
    nohup java -jar ~/agent.jar -jnlpUrl "${jenkinsurl}/computer/${localIp}/slave-agent.jnlp" -secret "${secret}" -workDir ~/jenkinsspace > /root/jnlp.log &
    echo "starting agent success..."
    tail -f /root/jnlp.log
  fi
  exit 0
fi

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值