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