Jenkins Builds All Applications on a Server

Keywords: jenkins shell xml CentOS

1. Background

In order to save costs, the test environment is deployed on the PC of the office environment, more than 200 web applications or other applications, 10 + centos servers, and then automatically deployed through the jenkins + shell script. However, because there is no computer room and no special cabinet, it often leads to an unexpected shutdown of a server, and then it is necessary to restart the application of this server. If they are all pure web applications, they are actually very good to handle. They can be done directly by shell scripts. But some of the projects are web and some of them are worker, which can not be unified.


2, train of thought

Build all the applications on a server by jenkins trigger directly, rebuild them, and restart them.


3, problems

How do you know what applications are deployed on a server on jenkins? (web + non-web)

After searching for a long time and no ready-made plug-ins, I decided to analyze them by myself (of course, it can also be done by hand, but this is too inefficient to advocate).


4,DO It

(1) First of all, we should be clear about the deployment structure of jenkins and know where to store what.

Typically, each job configuration file of Jenkins is stored in ~/.jenkins/jobs/job_name/config.xml

The configuration file details the parameters of the current job configuration and the configuration of the plug-ins used.

Here's what we're looking for.

  <jenkins.plugins.publish__over__ssh.BapSshPublisher>
              <configName>192.168.8.211</configName>
In fact, the main thing is this configName, which is the name of the ssh server that the system configures (of course, first of all, your configname and your ip need to be consistent in order to play like this).


(2) Now that we know where we're looking for something, there's no difficulty left. Here's a simple shell script that lists the configuration and the corresponding job name.

      

#!/bin/bash
domainsArray=`ls | grep dmall.com`
for domain  in $domainsArray
do
   configPath=${domain}/config.xml
   ip=`cat $configPath | grep configName | sed 's/^\s*//g'| cut -c 13-25`
   echo $ip $domain
done
Redirect the output to a text and you get the result we want.


(3) Next, we use python and jenkins plug-ins to develop scripts and code directly.

#coding:utf-8
import jenkins


jenkins_url="http://192.168.8.111:8080"
username="User name"
password="Password"
server=jenkins.Jenkins(jenkins_url,username=username,password=password)
joblist=[]
#Get jobs under a view and build them
#Here is to get all the job s under All view and build them.
#If you want to get another view here, just change All to the name of the other view.
def buildByViewName(view):	
	joblist=server.get_jobs(folder_depth=0,view_name=view)
	for job in joblist:
		temp=job['fullname']
		print temp
		server.build_job(temp)

#If you build a single job, you can use the following method directly, of course, you need to comment out the for loop code above.
def buildByJobName(jobname):
	server.build_job(jobname)

##Building all applications on a server
def buildByIp(target):
	file_in=open("ipANDdomains.txt")
	for line in file_in:
		temp=line.split(" ")
		ip=temp[0]
		domain=temp[1]
		if(target==ip):
			server.build_job(domain)


#buildByIp('192.168.8.208')
#buildByJobName("presale.dmall.com")
#BuilByViewName ("Athena")

5. ok, if you want to deploy a server, you have to work under a view, just execute the script name directly, Ma Ma Ma no longer has to worry about the application deployment is very troublesome.

   




Posted by claire on Thu, 18 Apr 2019 09:12:33 -0700