1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
from buildbot.plugins import *
from buildbot_common.build_helpers import *
import os
__all__ = [ "configure", "E" ]
class E():
PROJECT = "test"
BUILDBOT_URL = "https://git.immae.eu/buildbot/{}/".format(PROJECT)
SOCKET = "unix:/run/buildbot/{}.sock".format(PROJECT)
PB_SOCKET = "unix:address=/run/buildbot/{}_pb.sock".format(PROJECT)
RELEASE_PATH = "/var/lib/ftp/release.immae.eu/{}".format(PROJECT)
RELEASE_URL = "https://release.immae.eu/{}".format(PROJECT)
GIT_URL = "https://git.immae.eu/perso/Immae/TestProject.git"
SSH_KEY_PATH = "/var/lib/buildbot/buildbot_key"
PUPPET_HOST = "root@backup-1.v.immae.eu"
LDAP_HOST = "ldap.immae.eu"
LDAP_DN = "cn=buildbot,ou=services,dc=immae,dc=eu"
LDAP_ROLES_BASE = "ou=roles,ou=hosts,dc=immae,dc=eu"
# master.cfg
SECRETS_FILE = os.getcwd() + "/secrets"
LDAP_URL = "ldaps://ldap.immae.eu:636"
LDAP_ADMIN_USER = "cn=buildbot,ou=services,dc=immae,dc=eu"
LDAP_BASE = "dc=immae,dc=eu"
LDAP_PATTERN = "(uid=%(username)s)"
LDAP_GROUP_PATTERN = "(&(memberOf=cn=groups,ou=test,cn=buildbot,ou=services,dc=immae,dc=eu)(member=%(dn)s))"
TITLE_URL = "https://git.immae.eu/?p=perso/Immae/TestProject.git;a=summary"
TITLE = "Test project"
def configure(c):
c["buildbotURL"] = E.BUILDBOT_URL
c["www"]["port"] = E.SOCKET
c['workers'].append(worker.LocalWorker("generic-worker-test"))
c['workers'].append(worker.LocalWorker("deploy-worker-test"))
c['schedulers'].append(hook_scheduler("TestProject", timer=1))
c['schedulers'].append(force_scheduler("force_test", ["TestProject_build"]))
c['schedulers'].append(deploy_scheduler("deploy_test", ["TestProject_deploy"]))
c['builders'].append(factory())
c['builders'].append(deploy_factory())
c['services'].append(SlackStatusPush(
name="slack_status_test_project",
builders=["TestProject_build", "TestProject_deploy"],
serverUrl=open(E.SECRETS_FILE + "/slack_webhook", "r").read().rstrip()))
def factory():
package = util.Interpolate("test_%(kw:clean_branch)s.tar.gz", clean_branch=clean_branch)
package_dest = util.Interpolate("{}/test_%(kw:clean_branch)s.tar.gz".format(E.RELEASE_PATH), clean_branch=clean_branch)
package_url = util.Interpolate("{}/test_%(kw:clean_branch)s.tar.gz".format(E.RELEASE_URL), clean_branch=clean_branch)
factory = util.BuildFactory()
factory.addStep(steps.Git(logEnviron=False,
repourl=E.GIT_URL, mode="full", method="copy"))
factory.addStep(steps.ShellCommand(name="env",
logEnviron=False, command=["env"]))
factory.addStep(steps.ShellCommand(name="pwd",
logEnviron=False, command=["pwd"]))
factory.addStep(steps.ShellCommand(name="true",
logEnviron=False, command=["true"]))
factory.addStep(steps.ShellCommand(name="echo",
logEnviron=False, command=["echo", package]))
factory.addSteps(package_and_upload(package, package_dest, package_url))
return util.BuilderConfig(name="TestProject_build", workernames=["generic-worker-test"], factory=factory)
def compute_build_infos():
@util.renderer
def compute(props):
import re, hashlib
build_file = props.getProperty("build")
package_dest = "{}/{}".format(E.RELEASE_PATH, build_file)
version = re.match(r"{0}_(.*).tar.gz".format("test"), build_file).group(1)
with open(package_dest, "rb") as f:
sha = hashlib.sha256(f.read()).hexdigest()
return {
"build_version": version,
"build_hash": sha,
}
return compute
@util.renderer
def puppet_host(props):
return E.PUPPET_HOST
def deploy_factory():
package_dest = util.Interpolate("{}/%(prop:build)s".format(E.RELEASE_PATH))
factory = util.BuildFactory()
factory.addStep(steps.MasterShellCommand(command=["test", "-f", package_dest]))
factory.addStep(steps.SetProperties(properties=compute_build_infos()))
factory.addStep(LdapPush(environment=util.Property("environment"),
build_version=util.Property("build_version"),
build_hash=util.Property("build_hash"),
ldap_password=util.Secret("ldap")))
factory.addStep(steps.MasterShellCommand(command=[
"ssh", "-o", "UserKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=no", "-o", "CheckHostIP=no", "-i", E.SSH_KEY_PATH, puppet_host]))
return util.BuilderConfig(name="TestProject_deploy", workernames=["deploy-worker-test"], factory=factory)
from twisted.internet import defer
from buildbot.process.buildstep import FAILURE
from buildbot.process.buildstep import SUCCESS
from buildbot.process.buildstep import BuildStep
class LdapPush(BuildStep):
name = "LdapPush"
renderables = ["environment", "build_version", "build_hash", "ldap_password"]
def __init__(self, **kwargs):
self.environment = kwargs.pop("environment")
self.build_version = kwargs.pop("build_version")
self.build_hash = kwargs.pop("build_hash")
self.ldap_password = kwargs.pop("ldap_password")
self.ldap_host = kwargs.pop("ldap_host", E.LDAP_HOST)
super().__init__(**kwargs)
def run(self):
import json
from ldap3 import Reader, Writer, Server, Connection, ObjectDef
server = Server(self.ldap_host)
conn = Connection(server,
user=E.LDAP_DN,
password=self.ldap_password)
conn.bind()
obj = ObjectDef("immaePuppetClass", conn)
r = Reader(conn, obj,
"cn=test.{},{}".format(self.environment, E.LDAP_ROLES_BASE))
r.search()
if len(r) > 0:
w = Writer.from_cursor(r)
for value in w[0].immaePuppetJson.values:
config = json.loads(value)
if "test_version" in config:
config["test_version"] = self.build_version
config["test_sha256"] = self.build_hash
w[0].immaePuppetJson -= value
w[0].immaePuppetJson += json.dumps(config, indent=" ")
w.commit()
return defer.succeed(SUCCESS)
return defer.succeed(FAILURE)
|