# -*- 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 = {}

####### SECRETS

# These config files hold sensitive configuration data. This includes
# the mapping of build slave names to passwords etc. They're nothing fancy.
# See  *.cfg.example for an example format.
from ConfigParser import ConfigParser

slaves_cfg = ConfigParser()
slaves_cfg.read('slaves.cfg')

pbchange_cfg = ConfigParser()
pbchange_cfg.read('pbchange.cfg')

####### per-build config dictionaries
conf_aespipe = {
    'srcdir' : 'src',
    'dstdir' : '/mnt/www-buildbot/aes-pipe',
    'bin' : 'aes-pipe',
    'url' : 'http://twobit.us/builds/aes-pipe'
}
conf_coreimage = {
    'srcdir' : 'tmp-glibc/deploy/images/qemux86',
    'dstdir' : '/mnt/www-buildbot/core-image-minimal',
    'bin' : 'core-image-minimal-qemux86.ext4',
    'url' : 'http://twobit.us/builds/core-image-minimal'
}
conf_measuredimage = {
    'srcdir' : 'tmp-glibc/deploy/images/minnowboard-tpm-32',
    'dstdir' : '/mnt/www-buildbot/measured-image',
    'bin' : 'core-image-tpm-minnowboard-tpm-32.iso',
    'url' : 'http://twobit.us/builds/measured-image'
}
conf_measured_mortyimage = {
    'srcdir' : 'tmp-glibc/deploy/images/minnowboard-tpm-32',
    'dstdir' : '/mnt/www-buildbot/measured-image_morty',
    'bin' : 'core-image-tpm-minnowboard-tpm-32.iso',
    'url' : 'http://twobit.us/builds/measured-image_morty'
}
conf_selinuximage = {
    'srcdir' : 'tmp-glibc/deploy/images/qemux86',
    'dstdir' : '/mnt/www-buildbot/core-image-selinux-minimal',
    'bin' : 'core-image-selinux-minimal-qemux86.ext4',
    'url' : 'http://twobit.us/builds/core-image-selinux-minimal'
}
####### 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.
from buildbot.buildslave import BuildSlave
c ['slaves'] = []
for section in slaves_cfg.sections():
    max_builds = None
    if slaves_cfg.has_option(section, 'max-builds'):
        max_builds = slaves_cfg.get(section, 'max-builds')
    c['slaves'].append(BuildSlave(slaves_cfg.get(section, 'name'),
                                  slaves_cfg.get(section, 'passwd'),
                                  max_builds=int(max_builds)))

# 'slavePortnum' defines the TCP port to listen on for connections from slaves.
# This must match the value configured into the buildslaves (with their
# --master option)
c ['slavePortnum'] = 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
from buildbot.changes.pb import PBChangeSource

c ['change_source'] = []
for section in pbchange_cfg.sections():
    c['change_source'].append(
        PBChangeSource(port=pbchange_cfg.get(section, 'port'),
                       user=pbchange_cfg.get(section, 'user'),
                       passwd=pbchange_cfg.get(section, 'passwd'))
    )

####### SCHEDULERS

# Configure the Schedulers, which decide how to react to incoming changes.

from buildbot.schedulers.basic import SingleBranchScheduler, AnyBranchScheduler
from buildbot.schedulers.forcesched import ForceScheduler
from buildbot.changes import filter
c ['schedulers'] = []
c ['schedulers'].append (
    SingleBranchScheduler (
        name = 'aes-pipe',
        change_filter = filter.ChangeFilter(
            branch = 'master',
            project = 'aes-pipe'
        ),
        treeStableTimer = 300,
        builderNames = ['aes-pipe']
    )
)
c ['schedulers'].append (
    SingleBranchScheduler (
        name = 'core-image-minimal',
        change_filter = filter.ChangeFilter (
                            project = ['bitbake',
                                       'core-image-minimal',
                                       'openembedded-core'],
                            branch = 'master'
        ),
        treeStableTimer = 300,
        builderNames = ['core-image-minimal']
    )
)
c ['schedulers'].append (
    AnyBranchScheduler (
        name = 'meta-measured',
        change_filter = filter.ChangeFilter (
                            project = ['bitbake',
                                       'openembedded-core',
                                       'meta-intel',
                                       'meta-openembedded',
                                       'meta-measured'],
                            branch = 'master'
                        ),
        treeStableTimer = 300,
        builderNames = ['meta-measured']
    )
)
c ['schedulers'].append (
    AnyBranchScheduler (
        name = 'meta-measured_morty',
        change_filter = filter.ChangeFilter (
                            project = ['bitbake',
                                       'openembedded-core',
                                       'meta-intel',
                                       'meta-openembedded',
                                       'meta-measured'],
                            branch = ['morty',
                                      '1.32']
                        ),
        treeStableTimer = 300,
        builderNames = ['meta-measured_morty']
    )
)
c ['schedulers'].append (
    AnyBranchScheduler (
        name = 'meta-selinux',
        change_filter = filter.ChangeFilter (
                            project = ['bitbake',
                                       'openembedded-core',
                                       'meta-selinux'],
                            branch = 'master'
                        ),
        treeStableTimer = 300,
        builderNames = ['meta-selinux']
    )
)
c ['schedulers'].append (
    ForceScheduler (
        name = 'force',
        builderNames = ['aes-pipe']
    )
)
c ['schedulers'].append (
    ForceScheduler (
        name = 'core-image-minimal_force',
        builderNames = ['core-image-minimal']
    )
)
c ['schedulers'].append (
    ForceScheduler (
        name = 'meta-measured_force',
        builderNames = ['meta-measured']
    )
)
c ['schedulers'].append (
    ForceScheduler (
        name = 'meta-measured-morty_force',
        builderNames = ['meta-measured_morty']
    )
)
c ['schedulers'].append (
    ForceScheduler (
        name = 'meta-selinux_force',
        builderNames = ['meta-selinux']
    )
)

####### 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 GNUAutoconf, BuildFactory
from buildbot.process.properties import WithProperties
from buildbot.steps.master import MasterShellCommand
from buildbot.steps.source.git import Git
from buildbot.steps.shell import Configure, Compile, ShellCommand
from buildbot.steps.transfer import FileUpload

factory_aespipe = GNUAutoconf (
    source = Git(
        repourl = 'git://github.com/flihp/aes-pipe.git',
        branch = 'master',
        mode = 'full',
        method = 'fresh'
    ),
    configure='autoreconf --install && ./configure'
)
factory_aespipe.addStep (
    MasterShellCommand (
        command = ['mkdir', '-p', WithProperties ('{0}/%(buildername)s_%(buildnumber)s'.format (conf_aespipe['dstdir']))],
        name = 'mkdir',
        description = 'making upload dir',
        descriptionDone = 'upload dir',
        haltOnFailure = True
    )
)
factory_aespipe.addStep (
    FileUpload (
        slavesrc = '{0}/{1}'.format (conf_aespipe ['srcdir'], conf_aespipe ['bin']),
        masterdest = WithProperties ('{0}/%(buildername)s_%(buildnumber)s/{1}'.format (conf_aespipe ['dstdir'], conf_aespipe ['bin'])),
        url = WithProperties('{0}/%(buildername)s_%(buildnumber)s/{1}'.format (conf_aespipe ['url'], conf_aespipe ['bin']))
    )
)
# Only necessary because BuildMaster doesn't support changing the umask from
# the default 0077 from twistd. This is fixed in a future version:
# http://trac.buildbot.net/ticket/2169
factory_aespipe.addStep (
    MasterShellCommand (
        command = ['/var/lib/buildbot/masters/bin/perms-fix.sh', WithProperties ('{0}/%(buildername)s_%(buildnumber)s'.format (conf_aespipe['dstdir']))],
        name = 'perms-fix',
        description = 'fixing upload perms',
        descriptionDone = 'perms fixed',
        haltOnFailure = True
    )
)
factory_coreimage = BuildFactory (
    [
        Git (
            repourl = 'git://github.com/flihp/oe-build-scripts.git',
            mode = 'full',
            method = 'clobber',
            branch = 'master',
            alwaysUseLatest = True,
            progress = True
        ),
        ShellCommand (
            command = ['/bin/sh', '-c', '${SLAVE_SETUP_CORE}'],
            description = ['setting up build'],
            descriptionDone = ['done setting up build'],
            haltOnFailure = True
        ),
        Configure (
            command = ['./build_op.py', 'setup', '--build-type', 'oe-core'],
            description = ['configuring build'],
            descriptionDone = ['build configured'],
            haltOnFailure = True
        ),
        ShellCommand(
            command = ['./build_op.py', 'fetch'],
            name = 'fetch',
            description = ['fetching meta layers'],
            descriptionDone = ['meta layers acquired'],
            haltOnFailure = True
        ),
        ShellCommand (
            command = ['./build_op.py', 'manifest', '--archive', 'oe-core'],
            name = 'manifest',
            description = 'generating manifest',
            descriptionDone = 'manifest done',
            haltOnFailure = True
        ),
        MasterShellCommand (
            command = ['mkdir', '-p', WithProperties ('{0}/%(buildername)s_%(buildnumber)s'.format (conf_coreimage['dstdir']))],
            name = 'mkdir',
            description = 'making upload dir',
            descriptionDone = 'upload dir',
            haltOnFailure = True
        ),
        FileUpload (
            slavesrc = './oe-core.tar.bz2',
            masterdest = WithProperties ('{0}/%(buildername)s_%(buildnumber)s/oe-core_%(buildnumber)s.tar.bz2'.format (conf_coreimage ['dstdir'])),
            url = WithProperties('{0}/%(buildername)s_%(buildnumber)s/oe-core_%(buildnumber)s.tar.bz2'.format (conf_coreimage ['url']))
        ),
        MasterShellCommand (
            command = ['/var/lib/buildbot/masters/bin/perms-fix.sh', WithProperties ('{0}/%(buildername)s_%(buildnumber)s'.format (conf_coreimage['dstdir']))],
            name = 'perms-fix_manifest',
            description = 'fixing upload perms',
            descriptionDone = 'perms fixed',
            haltOnFailure = True
        ),
        Compile (
            command = ['./build.sh'],
            description = ['building core-image-minimal'],
            descriptionDone = ['core-image-minimal complete'],
            usePTY = True,
            timeout = 120*60
        ),
        FileUpload (
            slavesrc = '{0}/{1}'.format (conf_coreimage ['srcdir'], conf_coreimage ['bin']),
            masterdest = WithProperties ('{0}/%(buildername)s_%(buildnumber)s/{1}'.format (conf_coreimage ['dstdir'], conf_coreimage ['bin'])),
            url = WithProperties('{0}/%(buildername)s_%(buildnumber)s/{1}'.format (conf_coreimage ['url'], conf_coreimage ['bin']))
        ),
        MasterShellCommand (
            command = ['/var/lib/buildbot/masters/bin/perms-fix.sh', WithProperties ('{0}/%(buildername)s_%(buildnumber)s'.format (conf_coreimage['dstdir']))],
            name = 'perms-fix_image',
            description = 'fixing upload perms',
            descriptionDone = 'perms fixed',
            haltOnFailure = True
        )
    ]
)

factory_metameasured = BuildFactory (
    [
        Git(
            repourl = 'git://github.com/flihp/oe-build-scripts.git',
            mode = 'full',
            method = 'clobber',
            branch = 'master',
            alwaysUseLatest = True,
            progress = True
        ),
        ShellCommand(
            command = ['/bin/sh', '-c', '${SLAVE_SETUP_MEASURED}'],
            description = ['setting up build'],
            descriptionDone = ['done setting up build'],
            haltOnFailure = True
        ),
        Configure (
            command = ['./build_op.py', 'setup', '--build-type', 'meta-measured'],
            description = ['configuring build'],
            descriptionDone = ['build configured'],
            haltOnFailure = True
        ),
        ShellCommand(
            command = ['./build_op.py', 'fetch', '--update'],
            name = 'fetch',
            description = ['fetching meta layers'],
            descriptionDone = ['meta layers acquired'],
            haltOnFailure = True
        ),
        ShellCommand (
            command = ['./build_op.py', 'manifest', '--archive', 'meta-measured'],
            name = 'manifest',
            description = 'generating manifest',
            descriptionDone = 'manifest done',
            haltOnFailure = True
        ),
        MasterShellCommand (
            command = ['mkdir', '-p', WithProperties ('{0}/%(buildername)s_%(buildnumber)s'.format (conf_measuredimage['dstdir']))],
            name = 'mkdir',
            description = 'making upload dir',
            descriptionDone = 'upload dir',
            haltOnFailure = True
        ),
        FileUpload (
            slavesrc = './meta-measured.tar.bz2',
            masterdest = WithProperties ('{0}/%(buildername)s_%(buildnumber)s/meta-measured_%(buildnumber)s.tar.bz2'.format (conf_measuredimage ['dstdir'])),
            url = WithProperties('{0}/%(buildername)s_%(buildnumber)s/meta-measured_%(buildnumber)s.tar.bz2'.format (conf_measuredimage ['url']))
        ),
        MasterShellCommand (
            command = ['/var/lib/buildbot/masters/bin/perms-fix.sh', WithProperties ('{0}/%(buildername)s_%(buildnumber)s'.format (conf_measuredimage['dstdir']))],
            name = 'perms-fix_manifest',
            description = 'fixing upload perms',
            descriptionDone = 'perms fixed',
            haltOnFailure = True
        ),
        Compile(
            command = ['./build.sh'],
            description = ['building measured image'],
            descriptionDone = ['measured image complete'],
            usePTY = True,
            timeout = 120*60
        ),
        FileUpload (
            slavesrc = '{0}/{1}'.format (conf_measuredimage ['srcdir'], conf_measuredimage ['bin']),
            masterdest = WithProperties ('{0}/%(buildername)s_%(buildnumber)s/{1}'.format (conf_measuredimage ['dstdir'], conf_measuredimage ['bin'])),
            url = WithProperties('{0}/%(buildername)s_%(buildnumber)s/{1}'.format (conf_measuredimage ['url'], conf_measuredimage ['bin']))
        ),
        MasterShellCommand (
            command = ['/var/lib/buildbot/masters/bin/perms-fix.sh', WithProperties ('{0}/%(buildername)s_%(buildnumber)s'.format (conf_measuredimage['dstdir']))],
            name = 'perms-fix_image',
            description = 'fixing upload perms',
            descriptionDone = 'perms fixed',
            haltOnFailure = True
        )
    ]
)
    
factory_metameasured_morty = BuildFactory (
    [
        Git(
            repourl = 'git://github.com/flihp/oe-build-scripts.git',
            mode = 'full',
            method = 'clobber',
            branch = 'master',
            alwaysUseLatest = True,
            progress = True
        ),
        ShellCommand(
            command = ['/bin/sh', '-c', '${SLAVE_SETUP_MEASURED}'],
            description = ['setting up build'],
            descriptionDone = ['done setting up build'],
            haltOnFailure = True
        ),
        Configure (
            command = ['./build_op.py', 'setup', '--build-type', 'meta-measured-morty'],
            description = ['configuring build'],
            descriptionDone = ['build configured'],
            haltOnFailure = True
        ),
        ShellCommand(
            command = ['./build_op.py', 'fetch', '--update'],
            name = 'fetch',
            description = ['fetching meta layers'],
            descriptionDone = ['meta layers acquired'],
            haltOnFailure = True
        ),
        ShellCommand (
            command = ['./build_op.py', 'manifest', '--archive', 'meta-measured_morty'],
            name = 'manifest',
            description = 'generating manifest',
            descriptionDone = 'manifest done',
            haltOnFailure = True
        ),
        MasterShellCommand (
            command = ['mkdir', '-p', WithProperties ('{0}/%(buildername)s_%(buildnumber)s'.format (conf_measured_mortyimage['dstdir']))],
            name = 'mkdir',
            description = 'making upload dir',
            descriptionDone = 'upload dir',
            haltOnFailure = True
        ),
        FileUpload (
            slavesrc = './meta-measured_morty.tar.bz2',
            masterdest = WithProperties ('{0}/%(buildername)s_%(buildnumber)s/meta-measured_morty_%(buildnumber)s.tar.bz2'.format (conf_measured_mortyimage ['dstdir'])),
            url = WithProperties('{0}/%(buildername)s_%(buildnumber)s/meta-measured_%(buildnumber)s.tar.bz2'.format (conf_measured_mortyimage ['url']))
        ),
        MasterShellCommand (
            command = ['/var/lib/buildbot/masters/bin/perms-fix.sh', WithProperties ('{0}/%(buildername)s_%(buildnumber)s'.format (conf_measured_mortyimage['dstdir']))],
            name = 'perms-fix_manifest',
            description = 'fixing upload perms',
            descriptionDone = 'perms fixed',
            haltOnFailure = True
        ),
        Compile(
            command = ['./build.sh'],
            description = ['building measured image'],
            descriptionDone = ['measured image complete'],
            usePTY = True,
            timeout = 120*60
        ),
        FileUpload (
            slavesrc = '{0}/{1}'.format (conf_measured_mortyimage ['srcdir'], conf_measured_mortyimage ['bin']),
            masterdest = WithProperties ('{0}/%(buildername)s_%(buildnumber)s/{1}'.format (conf_measured_mortyimage ['dstdir'], conf_measured_mortyimage ['bin'])),
            url = WithProperties('{0}/%(buildername)s_%(buildnumber)s/{1}'.format (conf_measured_mortyimage ['url'], conf_measured_mortyimage ['bin']))
        ),
        MasterShellCommand (
            command = ['/var/lib/buildbot/masters/bin/perms-fix.sh', WithProperties ('{0}/%(buildername)s_%(buildnumber)s'.format (conf_measured_mortyimage['dstdir']))],
            name = 'perms-fix_image',
            description = 'fixing upload perms',
            descriptionDone = 'perms fixed',
            haltOnFailure = True
        )
    ]
)
factory_metaselinux = BuildFactory (
    [
        Git (
            repourl = 'git://github.com/flihp/oe-build-scripts.git',
            mode = 'full',
            method = 'clobber',
            branch = 'master',
            alwaysUseLatest = True,
            progress = True
        ),
        ShellCommand (
            command = ['/bin/sh', '-c', '${SLAVE_SETUP_SELINUX}'],
            description = ['setting up build'],
            descriptionDone = ['done setting up build'],
            haltOnFailure = True
        ),
        Configure (
            command = ['./build_op.py', 'setup', '--build-type', 'meta-selinux'],
            description = ['configuring build'],
            descriptionDone = ['build configured'],
            haltOnFailure = True
        ),
        ShellCommand(
            command = ['./build_op.py', 'fetch', '--update'],
            name = 'fetch',
            description = ['fetching meta layers'],
            descriptionDone = ['meta layers acquired'],
            haltOnFailure = True
        ),
        ShellCommand (
            command = ['./build_op.py', 'manifest', '--archive', 'meta-selinux'],
            name = 'manifest',
            description = 'generating manifest',
            descriptionDone = 'manifest done',
            haltOnFailure = True
        ),
        MasterShellCommand (
            command = ['mkdir', '-p', WithProperties ('{0}/%(buildername)s_%(buildnumber)s'.format (conf_selinuximage['dstdir']))],
            name = 'mkdir',
            description = 'making upload dir',
            descriptionDone = 'upload dir',
            haltOnFailure = True
        ),
        FileUpload (
            slavesrc = './meta-selinux.tar.bz2',
            masterdest = WithProperties ('{0}/%(buildername)s_%(buildnumber)s/meta-selinux_%(buildnumber)s.tar.bz2'.format (conf_selinuximage['dstdir'])),
            url = WithProperties('{0}/%(buildername)s_%(buildnumber)s/meta-selinux_%(buildnumber)s.tar.bz2'.format (conf_selinuximage ['url']))
        ),
        MasterShellCommand (
            command = ['/var/lib/buildbot/masters/bin/perms-fix.sh', WithProperties ('{0}/%(buildername)s_%(buildnumber)s'.format (conf_selinuximage['dstdir']))],
            name = 'perms-fix_manifest',
            description = 'fixing upload perms',
            descriptionDone = 'perms fixed',
            haltOnFailure = True
        ),
        Compile (
            command = ['./build.sh'],
            description = ['building minimal selinux image'],
            descriptionDone = ['minimal selinux image complete'],
            usePTY = True,
            timeout = 120*60
        ),
        FileUpload (
            slavesrc = '{0}/{1}'.format (conf_selinuximage ['srcdir'], conf_selinuximage ['bin']),
            masterdest = WithProperties ('{0}/%(buildername)s_%(buildnumber)s/{1}'.format (conf_selinuximage ['dstdir'], conf_selinuximage ['bin'])),
            url = WithProperties('{0}/%(buildername)s_%(buildnumber)s/{1}'.format (conf_selinuximage ['url'], conf_selinuximage ['bin']))
        ),
        MasterShellCommand (
            command = ['/var/lib/buildbot/masters/bin/perms-fix.sh', WithProperties ('{0}/%(buildername)s_%(buildnumber)s'.format (conf_selinuximage['dstdir']))],
            name = 'perms-fix_image',
            description = 'fixing upload perms',
            descriptionDone = 'perms fixed',
            haltOnFailure = True
        )
    ]
)

from buildbot.config import BuilderConfig

c ['builders'] = []
c ['builders'].append (
    BuilderConfig (
      name = 'aes-pipe',
      slavenames = ['scrapper'],
      factory = factory_aespipe
    )
)
c ['builders'].append (
    BuilderConfig (
        name = 'core-image-minimal',
        slavenames = ['scrapper'],
        factory = factory_coreimage
    )
)
c ['builders'].append (
    BuilderConfig (
        name = 'meta-measured',
        slavenames = ['scrapper'],
        factory = factory_metameasured
    )
)
c ['builders'].append (
    BuilderConfig (
        name = 'meta-measured_morty',
        slavenames = ['scrapper'],
        factory = factory_metameasured_morty
    )
)
c ['builders'].append (
    BuilderConfig (
        name = 'meta-selinux',
        slavenames = ['scrapper'],
        factory = factory_metaselinux
    )
)

####### 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.HTPasswdAuth (basedir + '/twobit.htpasswd'),
    gracefulShutdown = False,
    forceBuild = 'auth', # use this to test your slave once it is set up
    forceAllBuilds = 'auth',
    pingBuilder = False,
    stopBuild = 'auth',
    stopAllBuilds = 'auth',
    cancelPendingBuild = True,
)
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'] = 'twobit'
c ['titleURL'] = 'https://builder.twobit.us/'

# 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://builder.twobit.us:8010/'

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

