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

from buildbot.plugins import *

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

####### WORKERS

# The 'workers' list defines the set of recognized workers. Each element is
# a Worker object, specifying a unique worker name and password.  The same
# worker name and password must be configured on the worker.
c['workers'] = [worker.Worker("worker1", "pass"),
                worker.Worker("worker2", "pass"),
                worker.Worker("worker3", "pass")]

# 'protocols' contains information about protocols which master will use for
# communicating with workers. You must define at least 'port' option that workers
# could connect to your master with this protocol.
# 'port' must match the value configured into the workers (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 version of a python hello-world project.

c['change_source'] = []
c['change_source'].append(changes.GitPoller(
        'git://github.com/joequant/bitquant.git',
        workdir='bitstation', branch='master',
        pollInterval=300))

####### SCHEDULERS

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

c['schedulers'] = []
c['schedulers'].append(schedulers.SingleBranchScheduler(
                            name="all",
                            change_filter=util.ChangeFilter(branch='master'),
                            treeStableTimer=7200,
                            builderNames=["build-bitstation"]))

c['schedulers'].append(schedulers.SingleBranchScheduler(
                            name="develop",
                            change_filter=util.ChangeFilter(branch='develop'),
                            treeStableTimer=300,
                            builderNames=["build-bitstation-develop"]))

c['schedulers'].append(schedulers.ForceScheduler(
                            name="force",
                            builderNames=["clean-containers",
                                          "backup-volumes",
                                          "build-buildah",
                                          "build-bitstation",
                                          "build-bitstation-develop",
                                          "push-production",
                                          "upload-containers"]))

"""
c['schedulers'].append(
    schedulers.Nightly(name='BeforeWork',
                       branch='default',
                       builderNames=['build-bitstation'],
                       hour=[3], minute=23))
"""

c['schedulers'].append(
    schedulers.Nightly(name='build-cauldron',
                       branch='default',
                       builderNames=['build-buildah'],
                       hour=[1], minute=5))

c['schedulers'].append(
    schedulers.Nightly(name='clean-containers',
                       branch='default',
                       builderNames=['clean-containers'],
                       hour=[1,7,13,20], minute=15))

c['schedulers'].append(
    schedulers.Nightly(name='backup',
                       branch='default',
                       builderNames=['backup-volumes'],
                       hour=[5]))

####### BUILDERS

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

factory = util.BuildFactory()
# check out the source
factory.addStep(steps.Git(repourl='git://github.com/joequant/bitquant.git', 
                                   mode='incremental'))
factory.addStep(steps.ShellCommand(command=["pnpm", "run", "build-base"],
                                   workdir="build/bitstation",
                                   haltOnFailure=True))
factory.addStep(steps.ShellCommand(command=["pnpm", "run", "build-base-minimal"],
                                   workdir="build/bitstation",
                                   haltOnFailure=True))
factory.addStep(steps.ShellCommand(command=["pnpm", "run", "build"],
                                   workdir="build/nextcloud",
                                   haltOnFailure=True))
factory.addStep(steps.Git(repourl='git://github.com/joequant/cacher.git',
                                   mode='incremental', workdir="cacher"))
factory.addStep(steps.ShellCommand(command=["pnpm", "run", "build"],
                                   workdir="cacher",
                                   haltOnFailure=True))

c['builders'] = []
c['builders'].append(
    util.BuilderConfig(name="build-buildah",
      workernames=["worker1", "worker2", "worker3"],
      factory=factory))

factory = util.BuildFactory()
db_lock = util.MasterLock("podman")
factory.addStep(steps.Git(repourl='git://github.com/joequant/bitquant.git',
                          mode='incremental',
                          timeout=30,
                          locks=[db_lock.access('exclusive')]))
factory.addStep(steps.ShellCommand(command=["pnpm", "run", "build"],
                                   workdir="build/bitstation",
                                   haltOnFailure=True,
                                   locks=[db_lock.access('exclusive')]))
factory.addStep(steps.ShellCommand(command=["buildah", "push",
                                   "joequant/bitstation:latest",
                                   "docker-daemon:joequant/bitstation:latest"],
                                   workdir="build/bitstation",
                                   haltOnFailure=True))

c['builders'].append(
    util.BuilderConfig(name="build-bitstation",
      workernames=["worker1", "worker2", "worker3"],
      factory=factory))

factory = util.BuildFactory()
db_lock = util.MasterLock("podman")
factory.addStep(steps.Git(repourl='git://github.com/joequant/bitquant.git',
                          branch="develop",
                          mode='incremental',
                          timeout=30,
                          locks=[db_lock.access('exclusive')]))
factory.addStep(steps.ShellCommand(command=["pnpm", "run", "build"],
                                   workdir="build/bitstation",
                                   haltOnFailure=True,
                                   locks=[db_lock.access('exclusive')]))
factory.addStep(steps.ShellCommand(command=["buildah", "push",
                                   "joequant/bitstation:latest",
                                   "docker-daemon:joequant/bitstation:latest"],
                                   workdir="build/bitstation",
                                   haltOnFailure=True))

c['builders'].append(
    util.BuilderConfig(name="build-bitstation-develop",
      workernames=["worker1", "worker2", "worker3"],
      factory=factory))

factory = util.BuildFactory()
factory.addStep(steps.Git(repourl='git://github.com/joequant/bitquant.git', 
                                   mode='incremental'))
factory.addStep(steps.ShellCommand(command=["pnpm", "run", "build"],
                                   workdir="build/bitstation",
                                   haltOnFailure=True,
                                   env={"PYTHONPATH": "."}))

#c['builders'].append(
#    util.BuilderConfig(name="build-bitstation-docker",
#      workernames=["worker1", "worker2", "worker3"],
#      factory=factory))

factory = util.BuildFactory()
factory.addStep(steps.Git(repourl='git://github.com/joequant/bitquant.git',
                                   mode='incremental'))
factory.addStep(steps.ShellCommand(command=["./util/rm-stopped-containers.sh",
                                            "docker"],
                                   workdir="build/bitstation"))
factory.addStep(steps.ShellCommand(command=["./util/rm-untagged-images.sh",
                                            "docker"],
                                   workdir="build/bitstation"))

# don't clean up podman containers since this will kill storage for cacher
"""
factory.addStep(steps.ShellCommand(command=["./util/rm-stopped-containers.sh",
                                            "podman"],
                                   workdir="build/bitstation"))
factory.addStep(steps.ShellCommand(command=["./util/rm-untagged-images.sh", 
                                            "podman"],
                                   workdir="build/bitstation"))
factory.addStep(steps.ShellCommand(command=["./util/rm-stopped-containers.sh",
                                            "buildah"],
                                   workdir="build/bitstation"))
factory.addStep(steps.ShellCommand(command=["./util/rm-untagged-images.sh", 
                                            "buildah"],
                                   workdir="build/bitstation"))
"""
                                   
c['builders'].append(
    util.BuilderConfig(name="clean-containers",
      workernames=["worker1", "worker2", "worker3"],
      factory=factory))

factory = util.BuildFactory()
factory.addStep(steps.ShellCommand(command=["skopeo", "copy", 
        "containers-storage:localhost/joequant/bitstation:latest",
        "docker://docker.io/joequant/bitstation:latest"], timeout=7200))
factory.addStep(steps.ShellCommand(command=["skopeo", "copy",
        "containers-storage:localhost/joequant/cauldron:latest",
        "docker://docker.io/joequant/cauldron:latest"]))
factory.addStep(steps.ShellCommand(command=["skopeo", "copy",
        "containers-storage:localhost/joequant/nextcloud:latest",
        "docker://docker.io/joequant/nextcloud:latest"]))
factory.addStep(steps.ShellCommand(command=["skopeo", "copy",
        "containers-storage:localhost/joequant/cacher:latest",
        "docker://docker.io/joequant/cacher:latest"]))
c['builders'].append(
    util.BuilderConfig(name="upload-containers",
      workernames=["worker1", "worker2", "worker3"],
      factory=factory))

factory = util.BuildFactory()
factory.addStep(steps.Git(repourl='git://github.com/joequant/bitquant.git',
        mode='incremental'))
factory.addStep(steps.ShellCommand(command=["./util/containers-shell.sh",
                                            "-u", "apache",
                                            "bitstation_nextcloud",
                                            "php", "/usr/share/nextcloud/occ",
                                            "maintenance:mode", "--on"],
                                   workdir="build/bitstation"))
factory.addStep(steps.ShellCommand(command=["./util/backup.sh",
                                            "-d", "/home/joe/local3/backups/",
                                            "bitstation"],
                                   workdir="build/bitstation"))
factory.addStep(steps.ShellCommand(command=["./util/containers-shell.sh",
                                            "-u", "apache",
                                            "bitstation_nextcloud",
                                            "php", "/usr/share/nextcloud/occ",
                                            "maintenance:mode", "--off"],
                                   workdir="build/bitstation"))
factory.addStep(steps.ShellCommand(command=["./backup-moodle.sh",
                                            "-d", "/home/joe/local3/backups/"],
                                   workdir="build/online-campus/moodle"))
factory.addStep(steps.ShellCommand(command=["./util/backup.sh",
                                            "-d", "/home/joe/local3/backups/",
                                            "family"],
                                   workdir="build/bitstation"))
factory.addStep(steps.ShellCommand(command=["./util/backup.sh",
                                            "-d", "/home/joe/local3/backups/",
                                            "demo"],
                                   workdir="build/bitstation"))
c['builders'].append(
    util.BuilderConfig(name="backup-volumes",
                       workernames=["worker1", "worker2", "worker3"],
                       factory=factory))


factory = util.BuildFactory()
factory.addStep(steps.Git(repourl='git://github.com/joequant/bitquant.git',
        mode='incremental'))
factory.addStep(steps.ShellCommand(command=["docker", "tag",
                                            "joequant/bitstation:latest",
                                            "joequant/bitstation:production"],
                                   workdir="build/bitstation"))
factory.addStep(steps.ShellCommand(command=["docker", "tag",
                                            "joequant/nextcloud:latest",
                                            "joequant/nextcloud:production"],
                                   workdir="build/bitstation"))
c['builders'].append(
    util.BuilderConfig(name="push-production",
                       workernames=["worker1", "worker2", "worker3"],
                       factory=factory))


####### BUILDBOT SERVICES

# 'services' is a list of BuildbotService items like reporter targets. The
# status of each build will be pushed to these targets. buildbot/reporters/*.py
# has a variety to choose from, like IRC bots.

c['services'] = []

####### PROJECT IDENTITY

# the 'title' string will appear at the top of this buildbot installation's
# home pages (linked to the 'titleURL').

c['title'] = "Hello World CI"
c['titleURL'] = "https://buildbot.github.io/hello-world/"

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

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

# minimalistic config to activate new web UI
c['www'] = dict(port='tcp:8010:interface=0.0.0.0',
                plugins=dict(waterfall_view={}, console_view={}, grid_view={}))

####### DB URL

c['db'] = {
    # This specifies what database buildbot uses to store its state.
    # It's easy to start with sqlite, but it's recommended to switch to a dedicated
    # database, such as PostgreSQL or MySQL, for use in production environments.
    # http://docs.buildbot.net/current/manual/configuration/global.html#database-specification
    'db_url' : "sqlite:///state.sqlite",
}

c['buildbotNetUsageData'] = "full"
