Friday, October 3, 2008

Running a Buildbot buildslave as a Windows service

Buildbot is an open source continuous integration system written in Python. We use it to automate the builds on one of the projects I work on, and in general, it's cool. Pretty easy to set up (just make sure you look at every setting in the config file), comes with its own simple but handy web interface, and allows quick configuration of multiple "builders" and "buildslaves."

However, like most open source projects, it's written for and by *nix users and doesn't have full Windows support. Fortunately, Mark Hammond (of win32api fame) deigned to write the file, which allows installation of the Buildbot buildmaster as a service.

But despite this, there's no out-of-the-box way to run a buildslave as a service. There are a few other hardy souls out there who have figured out various methods for accomplishing this, and I am indebted to their guides as helpful starting points. But I didn't like all the registry hacking and configuration steps, so I went another way. Hammond's scares me, but I gleaned enough from it to put together my own

It's not terribly full-featured, and it requires reinstallation if any settings are changed, but it's compact, effective, and easy to install.

1) Copy code below, correct spacing that Blogger's HTML window stripped out, change paths as appropriate, save as
2) Install the service (w/ optional parameter for automatic startup if you want):
python install [--startup=auto]
3) Start the service:
net start Buildbot_Buildslave
(or use the Services mmc snap-in)

And that's it! Easier than all the registry mess and downloading Win2003 resource packs, no?


Original Author: Ira Pfeifer
Email: ipfeifer -dot- tech -at- gmail

import sys
import os
import subprocess
import win32serviceutil
import win32service
import win32event
import win32api
import time

from buildbot.scripts import runner
from buildbot.scripts.startup import start

# change paths as appropriate
slavepath = "c:\\buildbot\\buildslave"
homepath = "\\"
os.environ['HOMEDRIVE'] = "C:"
os.environ['HOMEPATH'] = homepath
os.environ['PATH'] = ";".join([

class BuildSlaveService(win32serviceutil.ServiceFramework):
_svc_name_ = "BuildBot_BuildSlave"
_svc_display_name_ = "BuildBot BuildSlave"
_svc_description_ = "Buildbot slave based in " + slavepath

def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.stop_event = win32event.CreateEvent(None, 0, 0, None)

def SvcDoRun(self):
# The service starts a subprocess that will run the actual buildbot
# so that it can be stopped by simply killing off the subprocess.
self.child = subprocess.Popen(["python",
isAlive = True
while isAlive:

def SvcStop(self):

handle = win32api.OpenProcess(1,0,
# returns exit code - wrap w/ error handling?
win32api.TerminateProcess(handle, 0)
isAlive = False

SvcShutdown = SvcStop

def start_buildslave():
config = runner.Options()
so = config.subOptions

if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == "start":


Sarah said...

For the record, thanks :)

voyta said...

This "simplified" service can do its job indeed, but when I started to debug it (e.g. it gets stuck on service stop or restart), I realized I am re-implementing the "scary", and when I looked into it, I found out it can actually run buildslave(s) as well. It just needs one step to specify the buildslave directory.

python install [optionally the logon and autostart details]
python start path_to_buildslave

The path is then persisted and the service always starts running the buildslave. It can be repeated for multiple buildslaves/build masters on the machine.

Maciek D. said...

You can use nssm for running the service.