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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503 | === added directory 'charm'
=== added file 'charm/Makefile'
--- charm/Makefile 1970-01-01 00:00:00 +0000
+++ charm/Makefile 2017-01-16 18:19:02 +0000
@@ -0,0 +1,69 @@
+NAME ?= launchpad-importd
+RESOURCE_NAME ?= launchpad
+TOPDIR ?= $(CURDIR)/..
+BUILDDIR ?= $(CURDIR)/dist
+TMPDIR ?= $(CURDIR)/tmp
+
+CHARM_SERIES ?= xenial
+CHARM_SRC ?= $(CURDIR)
+JUJU_REPOSITORY = $(BUILDDIR)
+CHARMDIR = $(BUILDDIR)/$(CHARM_SERIES)/$(NAME)
+CHARMREPODIR = $(BUILDDIR)/build
+RESOURCES = $(BUILDDIR)/resources/$(RESOURCE_NAME)
+CHARM = $(CHARMDIR)/.done
+LAYER_PATH = $(TMPDIR)/layer
+INTERFACE_PATH = $(TMPDIR)/interface
+CHARM_WHEELDIR = $(TMPDIR)/wheels
+CHARM_DEPS = $(LAYER_PATH)/.done $(INTERFACE_PATH)/.done
+EXTRA_CHARM_BUILD_ARGS ?=
+DEPLOY_ENV ?= devel
+
+export INTERFACE_PATH
+export LAYER_PATH
+export JUJU_REPOSITORY
+
+
+build: $(CHARM)
+
+resources: $(RESOURCES)
+
+build-with-resources: build resources
+
+$(TMPDIR) $(BUILDDIR):
+ mkdir -p $@
+
+$(CHARM_DEPS): $(TMPDIR) $(CHARM_SRC)/charm-deps
+ cd $(TMPDIR) && codetree $(CHARM_SRC)/charm-deps
+ rsync -a --delete \
+ $(CHARM_SRC)/layer/layer-launchpad/ \
+ $(TMPDIR)/layer/layer-launchpad/
+ touch $(CHARM_DEPS)
+
+$(CHARM): $(CHARM_SRC) $(CHARM_SRC)/* $(CHARM_DEPS) | $(BUILDDIR)
+ rm -rf $(CHARMDIR)
+ PIP_NO_INDEX=true PIP_FIND_LINKS=$(CHARM_WHEELDIR) charm build -o $(BUILDDIR) -s $(CHARM_SERIES) -n $(NAME) $(EXTRA_CHARM_BUILD_ARGS)
+ touch $@
+
+clean:
+ rm -rf $(BUILDDIR) $(TMPDIR)
+
+$(RESOURCES): $(TMPDIR)
+ rm -rf $(TMPDIR)/$(RESOURCE_NAME)/ $(TMPDIR)/$(RESOURCE_NAME).tar.gz
+ rsync -a --delete --exclude /charm \
+ $(TOPDIR)/ $(TMPDIR)/$(RESOURCE_NAME)/
+ tar -C $(TMPDIR) -czf $(TMPDIR)/$(RESOURCE_NAME).tar.gz \
+ $(RESOURCE_NAME)/
+ mkdir -p $(RESOURCES)
+ mv $(TMPDIR)/$(RESOURCE_NAME).tar.gz \
+ $(RESOURCES)/$(RESOURCE_NAME).tar.gz
+
+deploy:
+ juju deploy $(CHARMDIR)
+
+deploy-with-resources:
+ juju deploy \
+ --resource=$(RESOURCE_NAME).tar.gz=$(RESOURCES)/$(RESOURCE_NAME).tar.gz \
+ $(CHARMDIR)
+
+.PHONY: build resources build-with-resources clean
+.PHONY: deploy deploy-with-resources
=== added file 'charm/charm-deps'
--- charm/charm-deps 1970-01-01 00:00:00 +0000
+++ charm/charm-deps 2017-01-16 16:51:45 +0000
@@ -0,0 +1,10 @@
+interface @
+interface/nrpe-external-master lp:~ubuntuone-hackers/ols-charm-deps/interface-nrpe-external-master;revno=4
+
+layer @
+layer/layer-apt git+ssh://git.launchpad.net/layer-apt;revno=608bdf5
+layer/layer-basic lp:~ubuntuone-hackers/ols-charm-deps/layer-basic;revno=62
+# Copied by Makefile instead.
+#layer/layer-launchpad layer/layer-launchpad
+
+wheels git+ssh://git.launchpad.net/~ubuntuone-hackers/ols-charm-deps/+git/wheels
=== added file 'charm/config.yaml'
--- charm/config.yaml 1970-01-01 00:00:00 +0000
+++ charm/config.yaml 2017-01-12 15:28:57 +0000
@@ -0,0 +1,4 @@
+options:
+ install_sources:
+ # XXX something IS-maintained
+ default: ppa:launchpad/ppa
=== added directory 'charm/layer'
=== added file 'charm/layer.yaml'
--- charm/layer.yaml 1970-01-01 00:00:00 +0000
+++ charm/layer.yaml 2017-01-16 17:58:07 +0000
@@ -0,0 +1,12 @@
+repo: lp:launchpad
+includes:
+ - layer:basic
+ - layer:apt
+ - layer:launchpad
+options:
+ apt:
+ packages:
+ - launchpad-dependencies
+ignore:
+ - dist
+ - tmp
=== added directory 'charm/layer/layer-launchpad'
=== added file 'charm/layer/layer-launchpad/Makefile'
--- charm/layer/layer-launchpad/Makefile 1970-01-01 00:00:00 +0000
+++ charm/layer/layer-launchpad/Makefile 2017-01-13 17:50:39 +0000
@@ -0,0 +1,40 @@
+TEST_TIMEOUT := 900
+HOST_SERIES := $(shell lsb_release -sc)
+
+# /!\ Ensure that errors early in pipes cause failures, rather than
+# overridden by the last stage of the pipe. cf. 'test.py | ts'
+SHELL := /bin/bash
+export SHELLOPTS:=errexit:pipefail
+
+
+default:
+ @echo "One of:"
+ @echo " make testdeps"
+ @echo " make lint"
+
+
+_fail_ex:
+ false | ts
+
+_success_ex:
+ true | ts
+
+
+test: lint
+
+testdeps:
+ifeq ($(HOST_SERIES),trusty)
+ sudo apt-get install -y python-tox bzr moreutils \
+ software-properties-common
+else
+ sudo apt-get install -y tox bzr moreutils \
+ software-properties-common
+endif
+ sudo add-apt-repository -y ppa:juju/stable
+ sudo apt-get install charm-tools
+
+
+CHARM_NAME := layer-launchpad
+
+lint:
+ tox -e lint
=== added file 'charm/layer/layer-launchpad/config.yaml'
--- charm/layer/layer-launchpad/config.yaml 1970-01-01 00:00:00 +0000
+++ charm/layer/layer-launchpad/config.yaml 2016-11-29 11:15:20 +0000
@@ -0,0 +1,33 @@
+options:
+ install_sources:
+ # description and type comes from the apt layer.
+ default: ""
+ install_keys:
+ # description and type comes from the apt layer.
+ default: ""
+ nagios_context:
+ type: string
+ default: ""
+ description: |
+ Used by the nrpe subordinate charms.
+ nagios_servicegroups:
+ default: ""
+ type: string
+ description: |
+ A comma-separated list of nagios servicegroups.
+ If left empty, the nagios_context will be used as the servicegroup
+ environment:
+ type: string
+ default: production
+ description: production, staging, or devel
+ statsd_dsn:
+ default: ""
+ type: string
+ description: |
+ statsd url, see http://talisker.readthedocs.io/en/latest/statsd.html for details
+ sentry_dsn:
+ type: string
+ default: ""
+ description: |
+ DSN for configuration of Sentry's raven client.
+ See https://<sentry_host>/sentry/snapdeltaservice/settings/keys/
=== added directory 'charm/layer/layer-launchpad/files'
=== added file 'charm/layer/layer-launchpad/layer.yaml'
--- charm/layer/layer-launchpad/layer.yaml 1970-01-01 00:00:00 +0000
+++ charm/layer/layer-launchpad/layer.yaml 2017-01-13 17:53:07 +0000
@@ -0,0 +1,13 @@
+includes:
+ - layer:basic
+ - layer:apt
+ - interface:nrpe-external-master
+options:
+ basic:
+ use_venv: true
+ include_system_packages: true
+ packages:
+ - python3-yaml
+ launchpad:
+ service_name: launchpad-service
+ user: launchpad
=== added directory 'charm/layer/layer-launchpad/lib'
=== added directory 'charm/layer/layer-launchpad/lib/charms'
=== added directory 'charm/layer/layer-launchpad/lib/charms/layer'
=== added file 'charm/layer/layer-launchpad/lib/charms/layer/launchpad.py'
--- charm/layer/layer-launchpad/lib/charms/layer/launchpad.py 1970-01-01 00:00:00 +0000
+++ charm/layer/layer-launchpad/lib/charms/layer/launchpad.py 2017-01-16 17:38:19 +0000
@@ -0,0 +1,125 @@
+import os
+import subprocess
+
+from charmhelpers.core import (
+ hookenv,
+ host,
+ unitdata,
+ )
+
+
+# Layer configs #
+
+def _layer_options():
+ # import here, bc not available during unittest
+ from charms import layer
+ return layer.options().get('launchpad', {})
+
+
+def service_name():
+ return _layer_options().get('service_name')
+
+
+def user():
+ return _layer_options().get('user', 'ubuntu')
+
+
+def lpconfig():
+ return _layer_options().get('lpconfig')
+
+
+# derived configs #
+
+
+def root_dir():
+ return '/srv'
+
+
+def base_dir():
+ basedir = "{}/{}".format(root_dir(), service_name())
+ return basedir
+
+
+def etc_dir():
+ return '{}/etc'.format(base_dir())
+
+
+def code_dir():
+ return '{}/code'.format(base_dir())
+
+
+def logs_dir():
+ return '{}/logs'.format(base_dir())
+
+
+def readonly_dirs():
+ return [os.path.dirname(base_dir()), base_dir(), etc_dir()]
+
+
+def writable_dirs():
+ return [logs_dir()]
+
+
+def service_config_path():
+ return '{}/{}'.format(etc_dir(), service_config_filename())
+
+
+def service_config_filename():
+ return _layer_options().get('config_filename', 'settings.py')
+
+
+def config_files():
+ files = []
+ files.append(service_config_path())
+ return files
+
+
+# Utilities #
+
+def ensure_directories(readonly, writable, user):
+ hookenv.log('Creating service user')
+ # ensure user and group
+ host.adduser(user, primary_group=user)
+ # ensure RO dirs
+ hookenv.log('Creating service directories')
+ for dirpath in readonly:
+ host.mkdir(dirpath, group=user, perms=0o754)
+ # ensure RW dirs
+ for dirpath in writable:
+ host.mkdir(dirpath, group=user, perms=0o774)
+
+
+def create_virtualenv(wheels_dir, codedir):
+ env_dir = os.path.join(codedir, 'env')
+ if os.path.exists(env_dir):
+ hookenv.log('Virtualenv already exists, not creating it.')
+ else:
+ hookenv.log('Installing python packages into the venv')
+ subprocess.check_call(['virtualenv', '-p', 'python3', env_dir])
+
+ requirements = os.path.join(codedir, 'requirements.txt')
+
+ hookenv.log('Installing from local wheelhouse.')
+ subprocess.check_call(['%s/bin/pip' % env_dir,
+ 'install',
+ '--no-index',
+ '--find-links={}'.format(wheels_dir),
+ '-r', requirements])
+
+
+def render_template(template, context):
+ from jinja2 import Template
+ tmpl = Template(template, lstrip_blocks=True, trim_blocks=True)
+ return tmpl.render(**context)
+
+
+def check_port(key, new_port):
+ """Open/close a port and keep track of it based on key:"""
+ unitdata_key = '{}.port'.format(key)
+ kv = unitdata.kv()
+ existing_port = kv.get(unitdata_key)
+ if existing_port != new_port:
+ hookenv.open_port(new_port)
+ if existing_port: # Dont try to close non existing ports
+ hookenv.close_port(existing_port)
+ kv.set(unitdata_key, new_port)
=== added file 'charm/layer/layer-launchpad/metadata.yaml'
--- charm/layer/layer-launchpad/metadata.yaml 1970-01-01 00:00:00 +0000
+++ charm/layer/layer-launchpad/metadata.yaml 2017-01-13 17:52:34 +0000
@@ -0,0 +1,12 @@
+name: launchpad
+summary: Launchpad
+maintainer: Colin Watson <cjwatson@canonical.com>
+description: |
+ A layer for charms that run Launchpad code.
+tags:
+ - application_development
+subordinate: false
+provides:
+ nrpe-external-master:
+ interface: nrpe-external-master
+ scope: container
=== added directory 'charm/layer/layer-launchpad/reactive'
=== added file 'charm/layer/layer-launchpad/reactive/launchpad.py'
--- charm/layer/layer-launchpad/reactive/launchpad.py 1970-01-01 00:00:00 +0000
+++ charm/layer/layer-launchpad/reactive/launchpad.py 2017-01-16 17:38:48 +0000
@@ -0,0 +1,83 @@
+import os.path
+import shutil
+import subprocess
+
+from charmhelpers.core import (
+ hookenv,
+ host,
+ )
+from charmhelpers.payload import archive
+from charms.apt import status_set
+from charms.reactive import (
+ hook,
+ remove_state,
+ set_state,
+ when,
+ when_not,
+ )
+
+from charms.layer.launchpad import (
+ code_dir,
+ ensure_directories,
+ lpconfig,
+ readonly_dirs,
+ user,
+ writable_dirs,
+ )
+
+# states #
+
+
+@when_not('launchpad.service.installed', 'apt.queued_installs')
+def install_service():
+ writable = writable_dirs()
+ readonly = readonly_dirs()
+ ensure_directories(readonly, writable, user())
+
+ payload = 'launchpad.tar.gz'
+ dest = code_dir()
+ cache_dir = os.path.join(hookenv.charm_dir(), 'cache')
+ host.mkdir(cache_dir, perms=0o755)
+ local_path = os.path.join(cache_dir, payload)
+ resource_path = hookenv.resource_get(payload)
+ if resource_path and os.path.getsize(resource_path):
+ changed = host.file_hash(local_path) != host.file_hash(resource_path)
+ if not changed:
+ return
+ else:
+ status_set('blocked', 'Resource {} missing'.format(payload))
+ return
+ hookenv.log('Extracting resource to {}'.format(dest))
+ archive.extract_tarfile(resource_path, dest)
+ host.chownr(dest, 'root', 'root', chowntopdir=True)
+ subprocess.check_call(['make', '-C', dest, 'LPCONFIG=%s' % lpconfig()])
+ shutil.copy2(resource_path, local_path)
+ hookenv.status_set('waiting', 'Service installed but not configured')
+ set_state('launchpad.service.installed')
+
+
+@hook('upgrade-charm')
+def upgrade_charm():
+ # XXX: remove, only for development.
+ remove_state('launchpad.service.installed')
+ remove_state('launchpad.configured')
+ remove_state('service.configured')
+
+
+@when('config.changed')
+def handle_config_changes():
+ remove_state('launchpad.configured')
+ remove_state('service.configured')
+
+
+@when('service.configured')
+@when_not('launchpad.configured')
+def reconfigure():
+ remove_state('service.configured')
+
+
+@when('launchpad.service.installed')
+@when_not('apt.queued_installs', 'launchpad.configured')
+def ols_configured():
+ set_state('launchpad.configured')
+ hookenv.status_set('waiting', 'Launchpad layer configured')
=== added file 'charm/layer/layer-launchpad/tox.ini'
--- charm/layer/layer-launchpad/tox.ini 1970-01-01 00:00:00 +0000
+++ charm/layer/layer-launchpad/tox.ini 2017-01-16 16:39:34 +0000
@@ -0,0 +1,19 @@
+[tox]
+skipsdist = True
+envlist=lint
+
+[flake8]
+ignore=
+ # closing bracket does not match indentation of opening bracket’s line
+ # Launchpad style differs on this
+ E123
+#max-complexity=10
+
+[testenv:lint]
+basepython=python3
+sitepackages=False
+whitelist_externals=charm
+deps=flake8
+commands=
+ flake8 {posargs:reactive lib}
+#charm proof
=== added file 'charm/metadata.yaml'
--- charm/metadata.yaml 1970-01-01 00:00:00 +0000
+++ charm/metadata.yaml 2017-01-16 18:05:40 +0000
@@ -0,0 +1,16 @@
+name: launchpad-importd
+summary: Launchpad code import daemon
+description: |
+ A Launchpad code import daemon.
+tags:
+ - application_development
+maintainer: Colin Watson <cjwatson@canonical.com>
+subordinate: false
+series:
+ - xenial
+resources:
+ launchpad.tar.gz:
+ type: file
+ filename: launchpad.tar.gz
+ description: The Launchpad source code.
+min-juju-version: 2.0.0
|