简体   繁体   English

重启 Jenkins slave from master

[英]Restart Jenkins slave from master

I use jenkins master-slave configuration for capturing Performance metrics of a product.我使用 jenkins 主从配置来捕获产品的性能指标。 We have observed that jenkins-slave tends to accumulate memory and thus influences the Performance metrics being captured.我们观察到 jenkins-slave 倾向于累积 memory,从而影响捕获的性能指标。

To ensure consistency of the metrics being captured, we are thinking of restarting jenkins slave every day from the master, when there are no jobs running on the slave.为了确保捕获的指标的一致性,我们正在考虑每天从主服务器上重启 jenkins 从服务器,当从服务器上没有运行任何作业时。 Is this feasible?这可行吗?

How can we accomplish it?我们怎样才能做到呢?

Note: Using jenkins-slave as a service is not an option because we are having other security access issues with it.注意:使用 jenkins-slave 作为服务不是一种选择,因为我们有其他安全访问问题。

I know this answer is coming in a bit late : 我知道这个答案有点晚了:

This is how I did the same for the same reasons, not sure if this is the best way to achieve this, but it solved many of our problems : 这就是我出于同样的原因做同样的事情,不确定这是否是实现这一目标的最佳方法,但它解决了我们的许多问题:

For Windows Machines : 对于Windows机器:

  1. Create a job that simply runs "shutdown -r -f" on windows machines. 创建一个只在Windows机器上运行“shutdown -r -f”的作业。 It will restart the machines. 它将重启机器。
  2. Now bringing it back online part. 现在把它带回在线部分。 For similar reasons as yours, I didn't use "jenkins-slave as a service". 出于与你类似的原因,我没有使用“jenkins-slave作为服务”。 Instead I configured the nodes to connect via JNLP client, and then added the slave.jar command for each node in Window's task scheduler (to run on startup) 相反,我将节点配置为通过JNLP客户端连接,然后为Window的任务调度程序中的每个节点添加slave.jar命令(在启动时运行)
  3. Now the job restarts the machine and the Windows machine bring itself online on Jenkins itself right after restart. 现在,作业重新启动机器,Windows机器在重启后立即在Jenkins上自动联机。

For Mac Machines : 对于Mac机器:

  1. The process is comparatively easier on mac. 在mac上,这个过程相对容易一些。 First, make a job to run "shutdown -r now" on Mac node 首先,在Mac节点上运行“shutdown -r now”

  2. The node should simply be setup to get connected via ssh. 应该简单地设置节点以通过ssh连接。 That will take care of bringing it up online on Jenkins. 这将负责在Jenkins上线。

This was the "execute shell" part of my script to restart all the machines used for our automation : 这是我的脚本的“执行shell”部分,用于重新启动用于自动化的所有机器:

distro=`uname`
if [ "$distro" = "Windows_NT" ] || [ "$distro" = "WindowsNT" ] ;then
echo "Restarting Windows Machine...."
shutdown -r -f
else
echo "Restarting Mac Machine...."
sudo shutdown -r now
fi

PS: PS:

It's not exactly related to the question, but may be useful for the situation that you specified. 它与问题并不完全相关,但可能对您指定的情况有用。 It may be a good idea to add a batch script to clean temp files on startup of Windows machines. 在Windows机器启动时添加批处理脚本来清理临时文件可能是个好主意。 Add following to a batch script (Say, cleanTemp.bat) in the startup folder of your Windows machine. 将以下内容添加到Windows计算机的启动文件夹中的批处理脚本(Say,cleanTemp.bat)中。 (For Windows 10, C:\\Users\\\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup) (对于Windows 10,C:\\ Users \\\\ AppData \\ Roaming \\ Microsoft \\ Windows \\ Start Menu \\ Programs \\ Startup)

rmdir %temp% /s /q

md %temp%

If you still need an answer: https://wiki.apache.org/general/Jenkins#How_do_I_restart_a_Jenkins_Unix_Slave.3F 如果您仍需要答案: https//wiki.apache.org/general/Jenkins#How_do_I_restart_a_Jenkins_Unix_Slave.3F

Although, I just did a disconnect and then I saw that the processes died in the slave. 虽然,我只是断开连接,然后我看到进程在奴隶中死亡。 I did not have to kill them manually. 我没有必要手动杀死它们。 Then launch the slave again and that's it. 然后再次启动奴隶,就是这样。

This is good from web UI. 从Web UI可以很好。 I have not searched for CLI for this yet. 我还没有为此搜索过CLI。

  1. Create a job eg "Reboot-Slave", and set it with shell "shutdown -r -t 0", and take the target slave name as a parameter. 创建一个作业,例如“Reboot-Slave”,并使用shell“shutdown -r -t 0”进行设置,并将目标从属名称作为参数。 (in this way, the restart command will be executed directly on the target slave that you want to restart.) (这样,restart命令将直接在你想重启的目标从站上执行。)

  2. Create another job eg "Reboot-Check-Slave-Online", in this job, you should call the 1st job and pass the target slave name as parameter, plus, you'd better write some logic to determine whether your slave finished the restarting and connected to Jenkins server again, you can implement it by adding an "Execute system groovy script" step in your job and write below code: 创建另一个作业,例如“Reboot-Check-Slave-Online”,在这个作业中,你应该调用第一个作业并传递目标从属名称作为参数,另外,你最好写一些逻辑来确定你的奴隶是否完成了重启并再次连接到Jenkins服务器,您可以通过在作业中添加“执行系统groovy脚本”步骤来实现它,并在下面编写代码:

     import hudson.model.* def target_slave_param = "target_slave" def resolver = build.buildVariableResolver def target_slave = resolver.resolve(target_slave_param) println "target_slave is: ${target_slave}" def status = 0; //do{ println "Searching for ${target_slave}"; slave = Hudson.instance.slaves.find({it.name == target_slave}); if (slave != null) { computer = slave.getComputer(); if (computer.isOffline()) { println "Error! $target_slave is offline."; status = 1; } else { println "OK: $target_slave is online"; } } else { println "Slave $target_slave not found!"; status = 1; } //} 

Steps:脚步:

  1. Install Node and Label parameter plugin安装Node和Label参数插件
  2. Check This project is parameterized option:勾选This project is parameterized选项: 在此处输入图像描述
  3. Use following command in Execute shell field:执行 shell字段中使用以下命令: 在此处输入图像描述
(sudo bash -c "(sleep 30 && sudo shutdown -r now) &") &

Jenkins job is detached correctly and shows success execution. Jenkins 作业已正确分离并显示成功执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM