# -*- 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.
from buildbot.buildslave import BuildSlave
from buildbot.buildslave import openstack

c['slaves'] = [
                {% for buildslave in buildslaves %}
                BuildSlave("{{ buildslave }}", "{{ qa_stg_buildslave_password }}"),
                {% endfor %}
              ]

{% if deployment_type == 'qadevel-prod' %}
c['slaves'].append(
                    openstack.OpenStackLatentBuildSlave(
                        "{{ qadevel_prod_buildslave_user }}",
                        "{{ qadevel_prod_buildslave_password }}",
                        flavor=3,
                        image="5345b501-9264-4198-a185-eb5a2282fed0",
                        os_username="{{ taskotron_openstack_username }}",
                        os_password="{{ taskotron_openstack_password }}",
                        os_tenant_name="{{ taskotron_openstack_tenant_name }}",
                        os_auth_url="https://fed-cloud02.cloud.fedoraproject.org:5001/v2.0"
                    )
                  )
{% endif %}
# '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'] = {{ buildslave_port }}


####### 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

interval = {{ buildslave_poll_interval }}

c['change_source'] = []
{#
{% if deployment_type == 'qa-prod' %}
#}
c['change_source'].append(GitPoller(
        'https://pagure.io/taskotron/taskotron-trigger.git',
        workdir='gitpoller-workdir-trigger', branch='develop',
        pollinterval=interval,
        project='trigger'))

c['change_source'].append(GitPoller(
        'https://pagure.io/taskotron/libtaskotron.git',
        workdir='gitpoller-workdir-libtaskotron', branch='develop',
        pollinterval=interval,
        project='libtaskotron'))

c['change_source'].append(GitPoller(
        'https://pagure.io/taskotron/resultsdb.git',
        workdir='gitpoller-workdir-resultsdb', branch='develop',
        pollinterval=interval,
        project='resultsdb'))

c['change_source'].append(GitPoller(
        'https://pagure.io/taskotron/resultsdb_api.git',
        workdir='gitpoller-workdir-resultsdb_api', branch='develop',
        pollinterval=interval,
        project='resultsdb_api'))
{#
{% endif %}
#}
####### 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.changes import filter
c['schedulers'] = []

{# {% if deployment_type == 'qadevel-prod' %} #}
c['schedulers'].append(SingleBranchScheduler(
                            name="trigger-scheduler",
                            change_filter=filter.ChangeFilter(project='trigger', branch='develop'),
                            treeStableTimer=None,
                            builderNames=["trigger-builder"]))

c['schedulers'].append(SingleBranchScheduler(
                            name="libtaskotron-scheduler",
                            change_filter=filter.ChangeFilter(project='libtaskotron', branch='develop'),
                            treeStableTimer=None,
                            builderNames=["libtaskotron-builder"]))

c['schedulers'].append(SingleBranchScheduler(
                            name="resultsdb-scheduler",
                            change_filter=filter.ChangeFilter(project='resultsdb', branch='develop'),
                            treeStableTimer=None,
                            builderNames=["resultsdb-builder"]))

#c['schedulers'].append(ForceScheduler(
#                            name="openstack-force",
#                            builderNames=["openstack-builder"]))
{#
{% endif %}
{% if deployment_type == 'qa-stg' %}
c['schedulers'].append(SingleBranchScheduler(
                            name="libtaskotron",
                            builderNames=['libtaskotron-builder'],
                            treeStableTimer=None,
                            change_filter=filter.ChangeFilter(project='libtaskotron',
                                                                branch='develop')))

c['schedulers'].append(ForceScheduler(
                            name="force",
                            builderNames=["libtaskotron-builder"])xf

{% endif %}
#}
####### 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.process.properties import Property, Interpolate
from buildbot.steps.transfer import DirectoryUpload



def create_test_factory(repo_name, func=True):
    factory = BuildFactory()
#    factory.addStep(Git(repourl=Interpolate('{{ repo_base }}/%s.git' % repo_name),
#                    mode='full', env={'GIT_SSL_NO_VERIFY': 'yes'},
#                    method='clobber'))
    factory.addStep(Git(repourl=Interpolate('https://bitbucket.org/tflink/%s.git' % repo_name),
                    mode='full',method='clobber'))
    factory.addStep(ShellCommand(command=['doit', 'envtype=ci', 'test'], descriptionDone = ['run tests']))
    factory.addStep(ShellCommand(command=['doit', 'basedir=/srv/content/', 'envtype=ci', 'chainbuild'], descriptionDone=['Chainbuild RPMs']))
    factory.addStep(ShellCommand(command=['doit', 'basedir=/srv/content/', 'envtype=ci', 'buildtype=release', 'releasedocs'], descriptionDone=['Build Documentation']))
    factory.addStep(ShellCommand(command=['doit', 'basedir=/srv/content/', 'envtype=ci', 'buildtype=release', 'updatelatest'], descriptionDone=['Update Symlinks']))

    return factory

trigger_factory = create_test_factory('taskotron-trigger')
libtaskotron_factory = create_test_factory('libtaskotron-docs')
resultsdb_factory = create_test_factory('resultsdb')
resultsdb_api_factory = create_test_factory('resultsdb_api')

from buildbot.config import BuilderConfig

c['builders'] = []
{#
{% if deployment_type == 'qa-prod' %}
#}
c['builders'].append(
    BuilderConfig(name="trigger-builder",
      slavenames=[{% for buildslave in buildslaves %}"{{ buildslave }}",{% endfor %}],
      factory=trigger_factory))
c['builders'].append(
    BuilderConfig(name="libtaskotron-builder",
      slavenames=[{% for buildslave in buildslaves %}"{{ buildslave }}",{% endfor %}],
      factory=libtaskotron_factory))
c['builders'].append(
    BuilderConfig(name="resultsdb-builder",
      slavenames=[{% for buildslave in buildslaves %}"{{ buildslave }}",{% endfor %}],
      factory=resultsdb_factory))
c['builders'].append(
    BuilderConfig(name="resultsdb_api-builder",
      slavenames=[{% for buildslave in buildslaves %}"{{ buildslave }}",{% endfor %}],
      factory=resultsdb_api_factory))

#c['builders'].append(
#    BuilderConfig(name="openstack-builder",
#      slavenames=["{{ qa_stg_buildslave_user }}"],
#      factory=trigger_factory))
{#
{% endif %}

{% if deployment_type == 'qa-stg' %}
c['builders'].append(
    BuilderConfig(name="libtaskotron-builder",
      slavenames=[{% for buildslave in buildslaves %}"{{ buildslave }}",{% endfor %}],
      factory=libtaskotron_factory))

{% endif %}
#}
####### 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
    {% if deployment_type == 'qa-stg' %}
    auth=auth.BasicAuth([("{{ qa_stg_buildbot_master_user }}","{{ qa_stg_buildbot_master_password }}")]),
    {% endif %}
    {% if deployment_type == 'qa-prod' %}
    auth=auth.BasicAuth([("{{ qa_prod_prod_buildbot_master_user }}","{{ qa_prod_prod_buildbot_master_password }}")]),
    {% endif %}
    gracefulShutdown = False,
    forceBuild = 'auth', # use this to test your slave once it is set up
    forceAllBuilds = False,
    pingBuilder = False,
    stopBuild = False,
    stopAllBuilds = False,
    cancelPendingBuild = 'auth',
)
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'] = "Taskotron CI"
c['titleURL'] = "http://{{ external_hostname }}/{{buildmaster_endpoint}}/"

# 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://{{ external_hostname }}/{{buildmaster_endpoint}}/"

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