# -*- 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 Worker
from buildbot.plugins import *
passwd = load (open ("passwd.json"))
c['workers'] = [worker.Worker (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', branches=True,
        pollinterval=3600))
c['change_source'].append(GitPoller(
        'git://git.pcp.io/lberk/pcp.git',
        workdir='gitpoller-workdir-lberk', branches=True,
        pollinterval=3600))
c['change_source'].append(GitPoller(
        'git://git.pcp.io/brolley/pcp.git',
        workdir='gitpoller-workdir-brolley', branches=True,
        pollinterval=3600))
c['change_source'].append(GitPoller(
        'git://git.pcp.io/nathans/pcp.git',
        workdir='gitpoller-workdir-nathans', branches=True,
        pollinterval=3600))
c['change_source'].append(GitPoller(
        'git://git.pcp.io/mgoodwin/pcp.git',
        workdir='gitpoller-workdir-mgoodwin', branches=True,
        pollinterval=3600))
c['change_source'].append(GitPoller(
        'git://sourceware.org/git/pcpfans.git',
        workdir='gitpoller-workdir-pcpfans', branches=True,
        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.basic import AnyBranchScheduler
from buildbot.schedulers.forcesched import ForceScheduler
from buildbot.schedulers import timed
from buildbot.changes import filter
c['schedulers'] = []
c['schedulers'].append(AnyBranchScheduler(
                            name="Master Watch",
                            treeStableTimer=1200,
			    change_filter=filter.ChangeFilter(repository='git://git.pcp.io/pcp'),
                            builderNames=["fedora23 (docker)", "fedora24 (docker)", "debian (docker)", "centos7 (docker)", "fedora24-mingw64"]))
                            #builderNames=["fedora23 (docker)", "fedora24 (docker)", "debian (docker)", "fedora-mingw64", "fedora-arm7l", "centos7 (docker)", "centos6"]))
#c['schedulers'].append(AnyBranchScheduler(
                            #name="New Commit Watch",
                            #treeStableTimer=1200,
                            #builderNames=["centos7"])) #We'll expand this as more builders become stable
#c['schedulers'].append(ForceScheduler(
#                            name="force",
#                            builderNames=["df"]))
#c['schedulers'].append(timed.Periodic(name="debug",builderNames=["df"],periodicBuildTimer=60*5))
			   # builderNames=["debian"],
			    #builderNames=["fedora", "debian"],
			#builderNames=["df"],
			    #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.process.properties import Property
#from buildbot.steps.shell.SetPropertyFromCommand import SetPropertyFromCommand
from buildbot.plugins import steps, util
from buildbot.steps.source.git import Git
from buildbot.steps.shell import ShellCommand
from buildbot.steps.transfer import FileDownload

fedora24_docker_factory = BuildFactory()
fedora24_docker_factory.addStep(Git(repourl='git://git.pcp.io/pcp/pcp.git', mode='incremental'))
fedora24_docker_factory.addStep(steps.SetPropertyFromCommand(command="git rev-parse --short HEAD", property="commit-id"))
fedora24_docker_factory.addStep(ShellCommand(command=["docker", "build", "--no-cache", "--force-rm", util.Interpolate("--tag=pcpqa.fedora24.qa.%(prop:commit-id)s"), "-f", "qa/buildbot/Dockerfile.fedora24.qa", "."]))
fedora24_docker_factory.addStep(ShellCommand(command=["docker", "run", "-d", util.Interpolate("pcpqa.fedora24.qa.%(prop:commit-id)s")]))
fedora24_docker_factory.addStep(steps.SetPropertyFromCommand(command="docker ps -q --filter 'ancestor=pcpqa.fedora24.qa'", property="docker-ci"))
fedora24_docker_factory.addStep(steps.SetPropertyFromCommand(command=util.Interpolate("docker ps -q --filter 'ancestor=pcpqa.fedora24.qa.%(prop:commit-id)s'"), property="docker-ci"))
# we need a bigger timeout because currently qa/666 can take over 20 minutes(!) to complete at times
# also need to debug why pmwebapi tests seem to hang
fedora24_docker_factory.addStep(ShellCommand(command=["docker", "exec", util.Property('docker-ci', default='none'), "sh", "-c", "su - pcpqa -c './check -x pmwebapi'"], timeout=36000))
fedora24_docker_factory.addStep(ShellCommand(command=["docker", "stop", util.Property('docker-ci', default='none')]))
fedora24_docker_factory.addStep(ShellCommand(command=["docker", "tag", util.Interpolate("pcpqa.fedora24.qa.%(prop:commit-id)s"), "registry.lberk.org:5001/pcpqa.fedora24.master"]))
fedora24_docker_factory.addStep(ShellCommand(command=["docker", "push", "registry.lberk.org:5001/pcpqa.fedora24.master"]))
fedora24_docker_factory.addStep(ShellCommand(command=["docker", "rm", util.Property('docker-ci', default='none')]))
fedora24_docker_factory.addStep(ShellCommand(command=["docker", "rmi", util.Interpolate("pcpqa.fedora24.qa.%(prop:docker-ci)s")]))

fedora23_docker_factory = BuildFactory()
fedora23_docker_factory.addStep(Git(repourl='git://git.pcp.io/pcp/pcp.git', mode='incremental'))
fedora23_docker_factory.addStep(steps.SetPropertyFromCommand(command="git rev-parse --short HEAD", property="commit-id"))
fedora23_docker_factory.addStep(ShellCommand(command=["docker", "build", "--no-cache", "--force-rm", util.Interpolate("--tag=pcpqa.fedora23.qa.%(prop:commit-id)s"), "-f", "qa/buildbot/Dockerfile.fedora23.qa", "."]))
fedora23_docker_factory.addStep(ShellCommand(command=["docker", "run", "-d", util.Interpolate("pcpqa.fedora23.qa.%(prop:commit-id)s")]))
fedora23_docker_factory.addStep(steps.SetPropertyFromCommand(command=util.Interpolate("docker ps -q --filter 'ancestor=pcpqa.fedora23.qa.%(prop:commit-id)s'"), property="docker-ci"))
# we need a bigger timeout because currently qa/666 can take over 20 minutes(!) to complete at times
# also need to debug why pmwebapi tests seem to hang
fedora23_docker_factory.addStep(ShellCommand(command=["docker", "exec", util.Property('docker-ci', default='none'), "sh", "-c", "su - pcpqa -c './check -x pmwebapi'"], timeout=36000))
fedora23_docker_factory.addStep(ShellCommand(command=["docker", "stop", util.Property('docker-ci', default='none')]))
fedora23_docker_factory.addStep(ShellCommand(command=["docker", "tag", util.Interpolate("pcpqa.fedora23.qa.%(prop:commit-id)s"), "registry.lberk.org:5001/pcpqa.fedora23.master"]))
fedora23_docker_factory.addStep(ShellCommand(command=["docker", "push", "registry.lberk.org:5001/pcpqa.fedora23.master"]))
fedora23_docker_factory.addStep(ShellCommand(command=["docker", "rm", util.Property('docker-ci', default='none')]))
fedora23_docker_factory.addStep(ShellCommand(command=["docker", "rmi", util.Interpolate("pcpqa.fedora23.qa.%(prop:docker-ci)s")]))

centos7_docker_factory = BuildFactory()
centos7_docker_factory.addStep(Git(repourl='git://git.pcp.io/pcp/pcp.git', mode='incremental'))
centos7_docker_factory.addStep(steps.SetPropertyFromCommand(command="git rev-parse --short HEAD", property="commit-id"))
centos7_docker_factory.addStep(ShellCommand(command=["docker", "build", "--no-cache", "--force-rm", util.Interpolate("--tag=pcpqa.centos7.qa.%(prop:commit-id)s"), "-f", "qa/buildbot/Dockerfile.centos7.qa", "."]))
centos7_docker_factory.addStep(ShellCommand(command=["docker", "run", "-d", util.Interpolate("pcpqa.centos7.qa.%(prop:commit-id)s")]))
centos7_docker_factory.addStep(steps.SetPropertyFromCommand(command=util.Interpolate("docker ps -q --filter 'ancestor=pcpqa.centos7.qa.%(prop:commit-id)s'"), property="docker-ci"))

# we need a bigger timeout because currently qa/666 can take over 20 minutes(!) to complete at times
# also need to debug why pmwebapi tests seem to hang
centos7_docker_factory.addStep(ShellCommand(command=["docker", "exec", util.Property('docker-ci', default='none'), "sh", "-c", "su - pcpqa -c './check -x pmwebapi'"], timeout=36000))
centos7_docker_factory.addStep(ShellCommand(command=["docker", "stop", util.Property('docker-ci', default='none')]))
centos7_docker_factory.addStep(ShellCommand(command=["docker", "tag", util.Interpolate("pcpqa.centos7.qa.%(prop:commit-id)s"), "registry.lberk.org:5001/pcpqa.centos7.master"]))
centos7_docker_factory.addStep(ShellCommand(command=["docker", "push", "registry.lberk.org:5001/pcpqa.centos7.master"]))
centos7_docker_factory.addStep(ShellCommand(command=["docker", "rm", util.Property('docker-ci', default='none')]))
centos7_docker_factory.addStep(ShellCommand(command=["docker", "rmi", util.Interpolate("pcpqa.centos7.qa.%(prop:docker-ci)s")]))

debian8_docker_factory = BuildFactory()
debian8_docker_factory.addStep(Git(repourl='git://git.pcp.io/pcp/pcp.git', mode='incremental'))
debian8_docker_factory.addStep(steps.SetPropertyFromCommand(command="git rev-parse --short HEAD", property="commit-id"))
debian8_docker_factory.addStep(ShellCommand(command=["docker", "build", "--no-cache", "--force-rm", util.Interpolate("--tag=pcpqa.debian8.qa.%(prop:commit-id)s"), "-f", "qa/buildbot/Dockerfile.debian8.qa", "."]))
debian8_docker_factory.addStep(ShellCommand(command=["docker", "run", "-d", util.Interpolate("pcpqa.debian8.qa.%(prop:commit-id)s")]))
debian8_docker_factory.addStep(steps.SetPropertyFromCommand(command=util.Interpolate("docker ps -q --filter 'ancestor=pcpqa.debian8.qa.%(prop:commit-id)s'"), property="docker-ci"))
# we need a bigger timeout because currently qa/666 can take over 20 minutes(!) to complete at times
# also need to debug why pmwebapi tests seem to hang
debian8_docker_factory.addStep(ShellCommand(command=["docker", "exec", util.Property('docker-ci', default='none'), "sh", "-c", "su - pcpqa -c './check -x pmwebapi'"], timeout=36000))
debian8_docker_factory.addStep(ShellCommand(command=["docker", "stop", util.Property('docker-ci', default='none')]))
debian8_docker_factory.addStep(ShellCommand(command=["docker", "tag", util.Interpolate("pcpqa.debian8.qa.%(prop:commit-id)s"), "registry.lberk.org:5001/pcpqa.debian8.master"]))
debian8_docker_factory.addStep(ShellCommand(command=["docker", "push", "registry.lberk.org:5001/pcpqa.debian8.master"]))
debian8_docker_factory.addStep(ShellCommand(command=["docker", "rm", util.Property('docker-ci', default='none')]))
debian8_docker_factory.addStep(ShellCommand(command=["docker", "rmi", util.Interpolate("pcpqa.debian8.qa.%(prop:docker-ci)s")]))

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

el6_factory = BuildFactory()
# check out the source
el6_factory.addStep(Git(repourl= Property ("repository"), mode='incremental'))
el6_factory.addStep(ShellCommand(command=["./Makepkgs", "--verbose"], name="Makepkgs"))
el6_factory.addStep(ShellCommand(command=["sudo", "yum", "erase", "pcp-*", "pcp-libs", "-y"], name="Erase any previous PCP Installs"))
el6_factory.addStep(ShellCommand(command=["sudo", "rm", "-rf", "/var/lib/pcp/" ], name="Erase any previous PCP Artifacts"))
el6_factory.addStep(ShellCommand(command=["sudo", "rm", "-rf", "/var/log/pcp/" ], name="Erase any previous PCP Logs"))
el6_factory.addStep(ShellCommand(command="sudo yum install -y ./pcp*.rpm ./python-*.rpm ./python3-*.rpm ./perl*.rpm", name="Install packages", workdir="build/pcp-3.11.3/build/rpm/"))
#el6_factory.addStep(ShellCommand(command=["sudo", "systemctl", "daemon-reload"], name="systemd reload daemon"))
el6_factory.addStep(ShellCommand(command=["sudo", "service", "pmcd", "restart", ], name="Restart pmcd"))
el6_factory.addStep(ShellCommand(command=["sudo", "service", "pmlogger", "restart", ], name="Restart pmlogger"))
el6_factory.addStep(ShellCommand(command=["sudo", "chkconfig", "pmcd", "on"], name="Enable pmcd"))
el6_factory.addStep(ShellCommand(command=["sudo", "chkconfig", "pmlogger", "on"], name="Enable pmlogger"))
el6_factory.addStep(ShellCommand(command=["sudo", "-i", "-u", "pcpqa", "./check", "-x", "flakey", "-g", "sanity"], name="Run Testsuite", workdir="/var/lib/pcp/testsuite/", want_stderr=True, env={'DISPLAY': ":0.0"}))

mingw_factory = BuildFactory()
mingw_factory.addStep(Git(repourl= Property ("repository"), mode='incremental'))
mingw_factory.addStep(ShellCommand(command=["./Makepkgs", "--target", "mingw64", "--verbose"]))

from buildbot.config import BuilderConfig

c['builders'] = []
c['builders'].append(
    BuilderConfig(name="fedora24 (docker)",
                  workernames=["fedora24"],
                  factory=fedora24_docker_factory))
c['builders'].append(
    BuilderConfig(name="fedora23 (docker)",
                  workernames=["fedora23"],
                  factory=fedora24_docker_factory))
#c['builders'].append(
    #BuilderConfig(name="fedora-arm7l",
                  #workernames=["Fedora22-armv7l"],
                  #factory=fedora_dnf_factory))
c['builders'].append(
    BuilderConfig(name="fedora24-mingw64",
                  workernames=["fedora-mingw"],
                  factory=mingw_factory))
c['builders'].append(
    BuilderConfig(name="debian (docker)",
                  workernames=["debian8"],
                  factory=debian8_docker_factory))
c['builders'].append(
    BuilderConfig(name="centos7 (docker)",
                  workernames=["centos7"],
                  factory=centos7_docker_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.plugins import 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,
#)

from buildbot.plugins import reporters

mn = reporters.MailNotifier(fromaddr="buildbot@pcp.io",
                            sendToInterestedUsers=False,
                            mode=('failing',),
                            extraRecipients=['lberk@redhat.com'],
                            messageFormatter=html_message_formatter)
c['status'].append()


#c['#status'].append(html.WebStatus(http_port=8010, authz=authz_cfg))
#c['status'].append(status.IRC(host="irc.freenode.org", nick="pcpqa",
				#useColors="true",
				#notify_events={
                   		#'started': 0,
                   		#'successToFailure': 1,
                   		#'failureToSuccess': 0,
				#'finished': 0,
				#'failure': 1,
                 		#},
                              #channels=["#pcp-ci"]))
####### 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://buildbot.pcp.io/"




# minimalistic config to activate new web UI
c['www'] = dict(port=8010,
		plugins=dict(waterfall_view={}, console_view={}),
		auth=util.UserPasswordAuth([(str(passwd['auth'][0]['username']),str(passwd['auth'][0]['passwd']))]),
)


####### 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",
}
