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