猿教程 Logo

如何为Buildbot创建Systemd单元文件 (How To Create Systemd Unit Files for Buildbot)


Introduction

Buildbot is a Python-based continuous integration system for automating software build, test, and release processes. In the prerequisite tutorial, How To Install Buildbot on Ubuntu 16.04, we created a buildbot user and group, installed the buildmaster in /home/buildbot/master and the worker in /home/buildbot/worker, then manually started the processes the new user.

In this tutorial, we'll create systemd unit files so that the server's init system can manage the Buildbot processes.


Prerequisites

One Ubuntu 16.04 server with at least 1 GB of RAM, configured with a non-root sudo user and a firewall by following the Ubuntu 16.04 initial server setup guide with Buildbot installed and configured using the following guide:

  • How To Install Buildbot on Ubuntu 16.04

Once you've completed these requirements, you're ready to begin.


Step 1 — Stopping the Running Services

First, if you're still logged in as the buildbot user from the previous tutorial, type exit to return to the sudo user.

As the sudo user, we'll make sure that the Buildmaster is stopped:

$ sudo buildbot stop /home/buildbot/master
sudo buildbot stop /home/buildbot/master

Then, we'll ensure the worker is stopped as well:

$ sudo buildbot-worker stop /home/buildbot/worker
sudo buildbot-worker stop /home/buildbot/worker

In each case, we'll get feedback that buildbot process 1234 is dead, showing the Process ID that was stopped) orbuildmaster not running`, which indicates the service wasn't running in the first place.


Step 2 — Creating the Buildmaster Unit File

Next, we’ll create and open a file named buildbot-master.service:

$ sudo nano /lib/systemd/system/buildbot-master.service
sudo nano /lib/systemd/system/buildbot-master.service

In the [Unit] section we’ll add a description and require that networking must be available before starting the service. In the [Service] section, we’ll specify that the process runs as the buildbot user and group we created, define the working directory, and provide the command that should be used to start the master. Finally, in the [Install] section, we’ll indicate that it should start as part of the the multi-user target at boot:

[Unit]
Description=BuildBot master service
After=network.target

[Service]
User=buildbot
Group=buildbot
WorkingDirectory=/home/buildbot/master
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
ExecStart=/usr/local/bin/buildbot start \
          --nodaemon

[Install]
WantedBy=multi-user.target

Once we’ve added the content, we’ll save and exit, then test our work.

$ sudo systemctl start buildbot-master
sudo systemctl start buildbot-master

We'll use systemd's status command to check that it started appropriately:

$ sudo systemctl status buildbot-master
sudo systemctl status buildbot-master

The output should contain Active: active (running) and the last line should look something like:

Output
       May 08 21:01:24 BuildBot-Install systemd[1]: Started BuildBot master service.

Finally, we'll enable the buildmaster to start at boot:

$ sudo systemctl enable buildbot-master
sudo systemctl enable buildbot-master
Output
       Created symlink from /etc/systemd/system/multi-user.target.wants/buildbot-master.service to /lib/systemd/system/buildbot-master.service.

Now that the buildmaster is set up, we'll add the worker.


Step 3 — Creating the Worker Unit File

We’ll create and open a file called buildbot-worker.service which is configured like buildbot-master.service but with the values needed to start the worker. In the [Install] section, we’ll use set theWantedBy key to the buildbot-master.service so the worker will be started after the buildmaster.

$ sudo nano /lib/systemd/system/buildbot-worker.service
sudo nano /lib/systemd/system/buildbot-worker.service
[Unit]
Description=BuildBot worker service
After=network.target

[Service]
User=buildbot
Group=buildbot
WorkingDirectory=/home/buildbot/worker
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
ExecStart=/usr/local/bin/buildbot-worker start \
          --nodaemon

[Install]
WantedBy=buildbot-master.service

We’ll save and exit, then use systemctl to start the worker:

$ sudo systemctl start buildbot-worker
sudo systemctl start buildbot-worker

We'll use the status command to verify it started successfully:

$ sudo systemctl status buildbot-worker
sudo systemctl status buildbot-worker

Again, like the master, we should see Active: active (running) and a final line of output that looks something like:

Output
       . . .
May 08 21:54:46 BuildBot-Install systemd[1]: Started BuildBot worker service.

Finally, we'll enable the worker to start at boot:

$ sudo systemctl enable buildbot-worker.service
sudo systemctl enable buildbot-worker.service
Output
       Created symlink from /etc/systemd/system/buildbot-master.service.wants/buildbot-worker.service to /lib/systemd/system/buildbot-worker.service.

The output above indicates that the worker is configured to start at boot, but you might like to reboot the server now to confirm everything starts as expected.


Conclusion

In this tutorial, we've added systemd unit files so the server's init system can manage the Buildbot processes, and we've enabled both the buildmaster and worker to start at boot.

In the next tutorial, we'll secure the web interface with SSL using Let's Encrypt, a free SSL certificate service. Note that you'll need a domain name that you own or control in order to generate a certificate.

介绍

Buildbot是一个基于Python的持续集成系统,用于自动化软件构建,测试和发布流程。 在前提教程中,如何在Ubuntu 16.04上安装Buildbot,我们创建了一个buildbot用户和组,将/ master / buildbot / master中的buildmaster安装到/ home / buildbot / worker中的worker,然后手动启动新用户的进程。

在本教程中,我们将创建systemd单元文件,以便服务器的init系统可以管理Buildbot进程。


先决条件

一个具有至少1 GB RAM的Ubuntu 16.04服务器,配置非root用户和防火墙,遵循使用以下指南安装和配置的Buildbot的Ubuntu 16.04初始服务器设置指南:

  • 如何在Ubuntu 16.04上安装Buildbot

完成这些要求后,即可开始。


步骤1 - 停止正在运行的服务

首先,如果您仍然以前教程中的buildbot用户登录,请键入exit以返回到sudo用户。

作为sudo用户,我们将确保Buildmaster已停止:

$ sudo buildbot stop /home/buildbot/master
sudo buildbot stop /home/buildbot/master

然后,我们将确保工人也被停止:

$ sudo buildbot-worker stop /home/buildbot/worker
sudo buildbot-worker stop /home/buildbot/worker

在每种情况下,我们将获得buildbot进程1234死机的反馈,显示已停止的进程ID)orbuildmaster不运行,这表示该服务没有运行在第一位。


步骤2 - 创建Buildmaster单元文件

接下来,我们将创建并打开名为buildbot-master.service的文件:

$ sudo nano /lib/systemd/system/buildbot-master.service
sudo nano /lib/systemd/system/buildbot-master.service

在[Unit]部分中,我们将添加一个描述,并要求在启动服务之前,该网络必须可用。 在[服务]部分中,我们将指定该进程作为我们创建的buildbot用户和组运行,定义工作目录,并提供应用于启动主服务器的命令。 最后,在[安装]部分中,我们将指出它应该在启动时作为多用户目标的一部分开始:

[Unit]
Description=BuildBot master service
After=network.target

[Service]
User=buildbot
Group=buildbot
WorkingDirectory=/home/buildbot/master
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
ExecStart=/usr/local/bin/buildbot start \
          --nodaemon

[Install]
WantedBy=multi-user.target

一旦我们添加了内容,我们将保存并退出,然后测试我们的工作。

$ sudo systemctl start buildbot-master
sudo systemctl start buildbot-master

我们将使用systemd的status命令检查它是否正确启动:

$ sudo systemctl status buildbot-master
sudo systemctl status buildbot-master

输出应包含Active:active(running),最后一行应该如下所示:

Output
       May 08 21:01:24 BuildBot-Install systemd[1]: Started BuildBot master service.

最后,我们将启动buildmaster在启动时启动:

$ sudo systemctl enable buildbot-master
sudo systemctl enable buildbot-master
Output
       Created symlink from /etc/systemd/system/multi-user.target.wants/buildbot-master.service to /lib/systemd/system/buildbot-master.service.

现在buildmaster已经建立起来了,我们会添加工作人员。


步骤3 - 创建工作单元文件

我们将创建并打开名为buildbot-worker.service的文件,该文件的配置类似于buildbot-master.service,但具有启动该工作者所需的值。 在[安装]部分中,我们将使用将“建议主机”键设置为buildbot-master.service,这样工程师将在buildmaster之后启动。

$ sudo nano /lib/systemd/system/buildbot-worker.service
sudo nano /lib/systemd/system/buildbot-worker.service
[Unit]
Description=BuildBot worker service
After=network.target

[Service]
User=buildbot
Group=buildbot
WorkingDirectory=/home/buildbot/worker
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
ExecStart=/usr/local/bin/buildbot-worker start \
          --nodaemon

[Install]
WantedBy=buildbot-master.service

我们将保存并退出,然后使用systemctl启动worker:

$ sudo systemctl start buildbot-worker
sudo systemctl start buildbot-worker

我们将使用status命令来验证它是否成功启动:

$ sudo systemctl status buildbot-worker
sudo systemctl status buildbot-worker

再次,像主人一样,我们应该看到Active:active(running)和最后一行输出如下:

Output
       . . .
May 08 21:54:46 BuildBot-Install systemd[1]: Started BuildBot worker service.

最后,我们将使工作人员在启动时启动:

$ sudo systemctl enable buildbot-worker.service
sudo systemctl enable buildbot-worker.service
Output
       Created symlink from /etc/systemd/system/buildbot-master.service.wants/buildbot-worker.service to /lib/systemd/system/buildbot-worker.service.

上面的输出表示工作人员被配置为在引导时启动,但是您可能希望立即重新启动服务器,以确保一切按预期开始。


结论

在本教程中,我们添加了systemd单元文件,以便服务器的init系统可以管理Buildbot进程,并且我们已经启动了buildmaster和worker。

在下一个教程中,我们将使用Let's Encrypt(一种免费的SSL证书服务)来保护使用SSL的Web界面。 请注意,您需要您拥有或控制的域名才能生成证书。