# -*- python -*-
# ex: set syntax=python:

# This is a sample buildmaster config file. It must be installed as
# 'master.cfg' in your buildmaster's base directory.

# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}

####### BUILDSLAVES

# The 'slaves' list defines the set of recognized buildslaves. Each element is
# a BuildSlave object, specifying a unique slave name and password.  The same
# slave name and password must be configured on the slave.
import os.path
from json import load
from buildbot.buildslave import BuildSlave
passwd = load (open ("passwd.json"))
c['slaves'] = [BuildSlave (slave['name'], slave['passwd'])
		for slave in passwd['slaves']]

# 'protocols' contains information about protocols which master will use for
# communicating with slaves.
# You must define at least 'port' option that slaves could connect to your master
# with this protocol.
# 'port' must match the value configured into the buildslaves (with their
# --master option)
c['protocols'] = {'pb': {'port': 9989}}

####### CHANGESOURCES

# the 'change_source' setting tells the buildmaster how it should find out
# about source code changes.  Here we point to the buildbot clone of pyflakes.

from buildbot.changes.gitpoller import GitPoller
c['change_source'] = []
c['change_source'].append(GitPoller(
        'git://git.pcp.io/pcp',
        workdir='gitpoller-workdir', branch='master',
        pollinterval=3600))

####### SCHEDULERS

# Configure the Schedulers, which decide how to react to incoming changes.  In this
# case, just kick off a 'runtests' build

from buildbot.schedulers.basic import SingleBranchScheduler
from buildbot.schedulers.forcesched import ForceScheduler
from buildbot.schedulers import timed
from buildbot.changes import filter
c['schedulers'] = []
c['schedulers'].append(SingleBranchScheduler(
                            name="master watch",
                            change_filter=filter.ChangeFilter(branch='master'),
                            treeStableTimer=60,
                            builderNames=["fedora"]))
                            #builderNames=["fedora", "ubuntu"]))
c['schedulers'].append(ForceScheduler(
                            name="force",
                            #builderNames=["fedora", "ubuntu"]))
                            builderNames=["fedora"]))
c['schedulers'].append(timed.Periodic(
			    name="debug",
			    builderNames=["debian"],
			    #builderNames=["fedora", "debian"],
			    periodicBuildTimer=60*60*3))
			    #periodicBuildTimer=60*5))

####### BUILDERS

# The 'builders' list defines the Builders, which tell Buildbot how to perform a build:
# what steps, and which slaves can execute them.  Note that any particular build will
# only take place on one slave.

from buildbot.process.factory import BuildFactory
from buildbot.steps.source.git import Git
from buildbot.steps.shell import ShellCommand
from buildbot.steps.transfer import FileDownload

fedora_factory = BuildFactory()
# check out the source
fedora_factory.addStep(Git(repourl='git://git.pcp.io/pcp.git', mode='incremental'))
fedora_factory.addStep(ShellCommand(command=["./Makepkgs", "--verbose"], name="Makepkgs"))
fedora_factory.addStep(ShellCommand(command=["sudo", "yum", "erase", "pcp", "pcp-libs", "-y"], name="Erase any previous PCP Installs"))
fedora_factory.addStep(ShellCommand(command="sudo yum install -y ./pcp*.rpm ./python-*.rpm ./python3-*.rpm ./perl*.rpm", name="Install packages", workdir="build/pcp-3.10.4/build/rpm/"))
fedora_factory.addStep(ShellCommand(command=["sudo", "systemctl", "daemon-reload"], name="systemd reload daemon"))
fedora_factory.addStep(ShellCommand(command=["sudo", "systemctl", "restart", "pmcd", "pmlogger"], name="Restart Servies"))
fedora_factory.addStep(ShellCommand(command=["sudo", "systemctl", "enable", "pmcd", "pmlogger"], name="Enable Services"))
fedora_factory.addStep(ShellCommand(command=["sudo", "-i", "-u", "pcpqa", "./check"], name="Run Testsuite", workdir="/var/lib/pcp/testsuite/", want_stderr=True, env={'DISPLAY': ":0.0"}))

debian_factory = BuildFactory()
debian_factory.addStep(Git(repourl='git://git.pcp.io/pcp.git', mode='incremental'))
debian_factory.addStep(ShellCommand(command=["sudo", "apt-get", "build-dep", "pcp", "-y"], name="Ensure PCP build deps installed"))
debian_factory.addStep(FileDownload(mastersrc="~/debian_packages.sh",
                       slavedest="debian_remove_packages.sh", mode=755))
debian_factory.addStep(ShellCommand(command="sudo ./debian_remove_packages.sh", name="Remove old PCPs", workdir="build/"))
debian_factory.addStep(ShellCommand(command=["./Makepkgs", "--verbose"], name="Makepkgs", workdir=""))
#debian_factory.addStep(ShellCommand(command=["sudo apt-get remove -y pcp `dpkg --get-selections pcp-* libpcp* | cut -f1`"], name="Remove old PCP's"))
debian_factory.addStep(ShellCommand(command="sudo dpkg -i --force-confold ./*.deb", name="Install packages", workdir="build/build/deb/"))
debian_factory.addStep(ShellCommand(command="sudo apt-get -fy install", name="Install Any Missed debs", workdir="build/pcp-3.10.4/build/deb/"))
#debian_factory.addStep(ShellCommand(command="sudo service pmcd start", name="Start pmcd service"))
#debian_factory.addStep(ShellCommand(command="sudo service pmlogger start", name="Start pmcd service"))
debian_factory.addStep(ShellCommand(command=["sudo", "systemctl", "daemon-reload"], name="systemd reload daemon"))
debian_factory.addStep(ShellCommand(command=["sudo", "systemctl", "restart", "pmcd", "pmlogger"], name="Restart Servies"))
debian_factory.addStep(ShellCommand(command=["sudo", "systemctl", "enable", "pmcd", "pmlogger"], name="Restart Servies"))
debian_factory.addStep(ShellCommand(command=["sudo", "-i", "-u", "pcpqa", "./chk.setup"], name="Run Testsuite", workdir="/var/lib/pcp/testsuite/", want_stderr=True))
debian_factory.addStep(ShellCommand(command=["sudo", "-i", "-u", "pcpqa", "./check"], name="Run Testsuite", workdir="/var/lib/pcp/testsuite/", want_stderr=True))

from buildbot.config import BuilderConfig

c['builders'] = []
c['builders'].append(
    BuilderConfig(name="fedora",
    #BuilderConfig(name="runtests",
      slavenames=["fedora-21-x86_64", "fedora-20-x86_64"],
      factory=fedora_factory))

#c['builders'].append(
#    BuilderConfig(name="ubuntu",
#      slavenames=[""],
#      factory=ubuntu_factory))
c['builders'].append(
    BuilderConfig(name="debian",
      slavenames=["debian7-x86_64"],
      factory=debian_factory))
#c['builders'].append(
#    BuilderConfig(name="centos",
#      slavenames=[""],
#      factory=centos_factory))
#c['builders'].append(
#    BuilderConfig(name="rhel",
#      slavenames=[""],
#      factory=rhel_factory))
#c['builders'].append(
#    BuilderConfig(name="solaris",
#      slavenames=[""],
#      factory=solaris_factory))
#c['builders'].append(
#    BuilderConfig(name="mac",
#      slavenames=[""],
#      factory=mac_factory))

####### STATUS TARGETS

# 'status' is a list of Status Targets. The results of each build will be
# pushed to these targets. buildbot/status/*.py has a variety to choose from,
# including web pages, email senders, and IRC bots.

c['status'] = []

from buildbot.status import html
from buildbot.status.web import authz, auth

authz_cfg=authz.Authz(
    # change any of these to True to enable; see the manual for more
    # options
    auth=auth.BasicAuth([(str(passwd['auth'][0]['username']),str(passwd['auth'][0]['passwd']))]),
    gracefulShutdown = False,
    forceBuild = 'auth', # use this to test your slave once it is set up
    forceAllBuilds = 'auth',  # ..or this
    pingBuilder = False,
    stopBuild = False,
    stopAllBuilds = False,
    cancelPendingBuild = False,
)
c['status'].append(html.WebStatus(http_port=8010, authz=authz_cfg))

####### PROJECT IDENTITY

# the 'title' string will appear at the top of this buildbot
# installation's html.WebStatus home page (linked to the
# 'titleURL') and is embedded in the title of the waterfall HTML page.

c['title'] = "Performance Co-Pilot"
c['titleURL'] = "http://pcp.io"

# the 'buildbotURL' string should point to the location where the buildbot's
# internal web server (usually the html.WebStatus page) is visible. This
# typically uses the port number set in the Waterfall 'status' entry, but
# with an externally-visible host name which the buildbot cannot figure out
# without some help.

c['buildbotURL'] = "http://localhost:80/"

####### DB URL

c['db'] = {
    # This specifies what database buildbot uses to store its state.  You can leave
    # this at its default for all but the largest installations.
    'db_url' : "sqlite:///state.sqlite",
}
