目录
- Jenkins CLI
- Command create-node
- 通过shell来创建节点
- 通过java程序来创建节点
Jenkins CLI
Jenkins有一个内置的命令行,允许通过程序或shell请求Jenkins。即Jenkins CLI
Jenkins CLI 包含:create-node,update-node,create-job,copy-job,offline-node等命令
jenkins-cli.jar包可以通过自己部署的jenkins,http:jenkins.example.com/cli 来下载。
Command create-node
创建节点命令介绍
代码语言:javascript复制java -jar jenkins-cli.jar -s http://10.1.0.243:8080/jenkins/ create-node [NODE]
Creates a new node by reading stdin as a XML configuration.
NODE : Name of the node
通过上面命令了解到,该命令需要从输入流获取xml配置文件。而节点的xml文件结构可以参考手工创建节点后在.jenkins下面 /home/user/.jenkins/nodes/xxx/config.xml生成的文件
通过shell来创建节点
创建jenkins-create-node.sh文件,如下
代码语言:javascript复制NS_URL='http://10.1.0.243:8180/jenkins'
NODE_NAME=$2
NODE_SLAVE_HOME=$1
EXECUTORS=1
SSH_PORT=22
CRED_ID=$3
HOST_NAME=$4
JAVA_OPT=$5
JAVA_PATH=$6
cat <<EOF | java -jar ./jenkins-cli.jar -s ${NS_URL} create-node $2
<slave>
<name>${NODE_NAME}</name>
<description></description>
<remoteFS>$1</remoteFS>
<numExecutors>${EXECUTORS}</numExecutors>
<mode>NORMAL</mode>
<retentionStrategy class="hudson.slaves.RetentionStrategy$Always"/>
<launcher class="hudson.plugins.sshslaves.SSHLauncher" plugin="ssh-slaves@1.9">
<host>$4</host>
<port>${SSH_PORT}</port>
<credentialsId>${CRED_ID}</credentialsId>
<jvmOptions>${JAVA_OPT}</jvmOptions>
<javaPath>${JAVA_PATH}</javaPath>
</launcher>
<label></label>
<nodeProperties/>
<userId>anonymous</userId>
</slave>
EOF
然后通过sh jenkins-create-node.sh /home/bossbuild/remote-jenkins_243test testll 89060efd-e391-4586-bfe7-95d6b74cfb65 10.1.4.82 -Dfile.encoding=gbk /home/bossbuild/jdk1.7.0_45/bin/java 来执行命令
89060efd-e391-4586-bfe7-95d6b74cfb65是jenkins为用户加密后生成的
通过java程序来创建节点
通过process.getOutputStream()来写入xml,因为Process在程序员的角度是OuputStream,对于程序的角度就是STDIN
代码语言:javascript复制Process process = Runtime.getRuntime().exec("java -jar ./jenkins-cli.jar -s http://10.1.0.243:8180/jenkins create-node aaa");
OutputStream stdin = process.getOutputStream();
InputStream stdout = process.getInputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
writer.write("<slave><name>jobname</name><description></description><remoteFS>/home/bossbuild/remote-jenkins_243test</remoteFS><numExecutors>1</numExecutors><mode>NORMAL</mode><retentionStrategy class='hudson.slaves.RetentionStrategy$Always'/><launcher class='hudson.plugins.sshslaves.SSHLauncher' plugin='ssh-slaves@1.9'><host>10.1.4.82</host><port>22</port><credentialsId>89060efd-e391-4586-bfe7-95d6b74cfb65</credentialsId><jvmOptions>-Dfile.encoding=gbk</jvmOptions><javaPath>/home/bossbuild/jdk1.7.0_45/bin/java</javaPath></launcher><label></label><nodeProperties/><userId>anonymous</userId></slave>");
writer.flush();
writer.close();