~allenap/maas/repackage

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
python := python2.7

# Network activity can be suppressed by setting offline=true (or any
# non-empty string) at the command-line.
ifeq ($(offline),)
virtualenv := virtualenv
else
virtualenv := virtualenv --never-download
endif

# If offline has been selected, attempt to further block HTTP/HTTPS
# activity by setting bogus proxies in the environment.
ifneq ($(offline),)
export http_proxy := broken
export https_proxy := broken
endif

# Prefix commands with this when they need access to the database.
# Remember to add a dependency on bin/database from the targets in
# which those commands appear.
dbrun := bin/database --preserve run --

# For things that care, postgresfixture for example, we always want to
# use the "maas" databases.
export PGDATABASE := maas

# Executables that are usual for a development environment.
define executables
  bin/database
  bin/flake8
  bin/ipython
  bin/sphinx

  bin/maas-dns-server
  bin/maas-region-admin
  bin/maas-region-twistd
  bin/maas-region-worker

  bin/maas-cluster-twistd
  bin/maas-cluster-worker
  bin/maas-probe-dhcp
  bin/maas-provision

  bin/maas
endef
executables := $(strip $(executables))

# The Python packages that form MAAS, in order of installation (pip is
# not great at resolving dependencies in the correct order).
define python-packages
  maas-apiclient
  maas-client
  maas-cluster
  maas-region
  maas-testing
  maas-develop
endef
python-packages := $(strip $(python-packages))

build: $(executables) enums

all: build doc

bin/python:
	$(virtualenv) --python=$(python) --system-site-packages $(CURDIR)

$(executables): bin/python
	$(MAKE) -C pkg
	@for dep in $(python-packages); do \
	    (cd pkg/$${dep} && python setup.py install_deps); done
	bin/python -m pip install \
	    $(addprefix --editable pkg/,$(python-packages))
	@touch --no-create $@

test:
	$(MAKE) -C pkg test

lint: lint-py lint-js lint-doc

pocketlint = $(call available,pocketlint,python-pocket-lint)

# XXX jtv 2014-02-25: Clean up this lint, then make it part of "make lint".
lint-css: sources = src/maasserver/static/css
lint-css:
	@find . -type f -name '*.css' -print0 | \
	    xargs -r0 $(pocketlint) --max-length=120

lint-py: sources = src/*/ templates utilities pkg/common
lint-py: sources += pkg/*/contrib pkg/*/etc pkg/*/*.py pkg/*/twisted
lint-py: bin/flake8
	@find $(sources) -type f -name '*.py' \
	    ! -path '*/migrations/*' ! -path '*/setup.py' -print0 | \
	    xargs -r0 bin/flake8 --ignore=E123 --config=/dev/null

lint-doc:
	@utilities/doc-lint

lint-js: sources = src/maasserver/static/js
lint-js:
	@find . -type f -name '*.js' -print0 | \
	    xargs -r0 $(pocketlint) --max-length=120

# Apply automated formatting to all Python files.
format: sources = src/*/ templates utilities pkg/common
format: sources += pkg/*/contrib pkg/*/etc pkg/*/*.py pkg/*/twisted
format:
	@find $(sources) -name '*.py' -print0 | \
	    xargs -r0 utilities/format-imports

check: clean test

# TODO: Move to maas-develop? Or maas-region. Generating documentation
# may even be better contained in a separate subproject.
docs/api.rst: bin/maas-region-admin src/maasserver/api.py syncdb
	bin/maas-region-admin generate_api_doc > $@

# TODO: Move to maas-develop? Or maas-region. Generating documentation
# may even be better contained in a separate subproject.
doc: bin/sphinx docs/api.rst
	bin/sphinx docs docs/_build/html

# TODO: Move to maas-develop? Or maas-region. Generating documentation
# may even be better contained in a separate subproject.
doc-with-versions: bin/sphinx docs/api.rst
	cd docs/_build; make SPHINXOPTS="-A add_version_switcher=true" html

# TODO: Move to maas-develop? Or maas-region. Generating documentation
# may even be better contained in a separate subproject.
man: $(patsubst docs/man/%.rst,man/%,$(wildcard docs/man/*.rst))

# TODO: Move to maas-develop? Or maas-region. Generating documentation
# may even be better contained in a separate subproject.
man/%: docs/man/%.rst | bin/sphinx
	bin/sphinx -b man docs man $^

# TODO: Dynamically serve enums JavaScript to clients. This build step
# complicates packaging, and introduces some fragility.
enums:
	$(MAKE) -C pkg/maas-region enums

clean:
	$(MAKE) -C acceptance $@
	$(MAKE) -C pkg $@
	find . -type f -name '*.py[co]' -print0 | xargs -r0 $(RM)
	find . -type f -name '*~' -print0 | xargs -r0 $(RM)
	find . -type f -name dropin.cache -print0 | xargs -r0 $(RM)
	$(RM) *.log
	$(RM) docs/api.rst
	$(RM) -r docs/_autosummary docs/_build
	$(RM) -r man/.doctrees

distclean: clean stop
	$(RM) -r bin include lib local
	$(RM) -r build dist logs/*
	$(RM) tags TAGS
	$(RM) -r *.egg *.egg-info
	$(RM) -r run/* services/*/supervise

# TODO: Move to maas-develop? This is a development-only target.
harness: bin/maas-region-admin bin/database
	$(dbrun) bin/maas-region-admin shell

# TODO: Move to maas-develop? This is a development-only target.
dbharness: bin/database
	bin/database --preserve shell

# TODO: Move to maas-develop? This is a development-only target.
syncdb: bin/maas-region-admin bin/database
	$(dbrun) bin/maas-region-admin syncdb --noinput
	$(dbrun) bin/maas-region-admin migrate maasserver --noinput
	$(dbrun) bin/maas-region-admin migrate metadataserver --noinput

# TODO: Move to maas-develop, along with dev_fixture.yaml? This is a
# development-only target.
sampledata: bin/maas-region-admin bin/database syncdb
	$(dbrun) bin/maas-region-admin loaddata \
	    src/maasserver/fixtures/dev_fixture.yaml

define phony_targets
  build
  check
  clean
  dbharness
  distclean
  doc
  enums
  format
  harness
  lint
  lint-css
  lint-doc
  lint-js
  lint-py
  man
  package
  sampledata
  source_package
  syncdb
  test
endef

#
# Development services.
#

service_names_region := database dns region-worker reloader txlongpoll web webapp
service_names_cluster := cluster-worker pserv reloader
service_names_all := $(service_names_region) $(service_names_cluster)

# The following template is intended to be used with `call`, and it
# accepts a single argument: a target name. The target name must
# correspond to a service action (see "Pseudo-magic targets" below).
# A region- and cluster-specific variant of the target will be
# created, in addition to the target itself. These can be used to
# apply the service action to the region services, the cluster
# services, or all services, at the same time.
define service_template
$(1)-region: $(patsubst %,services/%/@$(1),$(service_names_region))
$(1)-cluster: $(patsubst %,services/%/@$(1),$(service_names_cluster))
$(1): $(1)-region $(1)-cluster
phony_services_targets += $(1)-region $(1)-cluster $(1)
endef

# Expand out aggregate service targets using `service_template`.
$(eval $(call service_template,pause))
$(eval $(call service_template,restart))
$(eval $(call service_template,start))
$(eval $(call service_template,status))
$(eval $(call service_template,stop))
$(eval $(call service_template,supervise))

# The `run` targets do not fit into the mould of the others. Note: logs
# directories shouldn't be needed for `run` targets, but some config
# seems to still require it, so we create them. Ideally the configs used
# for running services should log to stdout/stderr, then we could get
# rid of these.
run-region:
	@mkdir -p $(addprefix logs/,$(service_names_region))
	@services/run $(service_names_region)
run-cluster:
	@mkdir -p $(addprefix logs/,$(service_names_cluster))
	@services/run $(service_names_cluster)
run:
	@mkdir -p $(addprefix logs/,$(service_names_all))
	@services/run $(service_names_all)

phony_services_targets += run-region run-cluster run

# This one's for the rapper, yo.
run+webapp:
	@services/run $(service_names_region) +webapp

phony_services_targets += run+webapp

# Convenient variables and functions for service control.

setlock = $(call available,setlock,daemontools)
supervise = $(call available,supervise,daemontools)
svc = $(call available,svc,daemontools)
svok = $(call available,svok,daemontools)
svstat = $(call available,svstat,daemontools)

service_lock = $(setlock) -n /run/lock/maas.dev.$(firstword $(1))

# Pseudo-magic targets for controlling individual services.

# Note: log directories shouldn't be needed for `@run` targets, but some
# config seems to still require it, so we create them. Ideally the
# configs used for running services should log to stdout/stderr, then we
# could get rid of this.
services/%/@run: services/%/@stop services/%/@deps
	@mkdir -p logs/$*
	@$(call service_lock, $*) services/$*/run

services/%/@start: services/%/@supervise
	@$(svc) -u $(@D)

services/%/@pause: services/%/@supervise
	@$(svc) -d $(@D)

services/%/@status:
	@$(svstat) $(@D)

services/%/@restart: services/%/@supervise
	@$(svc) -du $(@D)

services/%/@stop:
	@if $(svok) $(@D); then $(svc) -dx $(@D); fi
	@while $(svok) $(@D); do sleep 0.1; done

services/%/@supervise: services/%/@deps
	@mkdir -p logs/$*
	@touch $(@D)/down
	@if ! $(svok) $(@D); then \
	    logdir=$(CURDIR)/logs/$* \
	        $(call service_lock, $*) $(supervise) $(@D) & fi
	@while ! $(svok) $(@D); do sleep 0.1; done

# Dependencies for individual services.

services/dns/@deps: bin/maas-dns-server

services/cluster-worker/@deps: bin/maas-cluster-worker

services/region-worker/@deps: bin/maas-region-worker

services/database/@deps: bin/database

services/pserv/@deps: bin/maas-cluster-twistd

services/reloader/@deps:

services/txlongpoll/@deps: bin/maas-region-twistd

services/web/@deps:

services/webapp/@deps: bin/maas-region-admin

#
# Package building
#
# This ought to be as simple as using bzr builddeb --export-upstream but it
# has a bug and always considers apt-source tarballs before the specified
# branch.  So instead, export to a local tarball which is always found.
# Make sure debhelper and dh-apport packages are installed before using this.
PACKAGING := $(CURDIR)/../packaging.trunk
PACKAGING_BRANCH := lp:~maas-maintainers/maas/packaging

package_branch:
	@echo Downloading/refreshing packaging branch...
	@if [ ! -d $(PACKAGING) ]; then \
		bzr branch $(PACKAGING_BRANCH) $(PACKAGING); \
		else bzr pull -d $(PACKAGING); fi

# Make sure an orig tarball generated from the current branch is placed in the
# build area.
package_export: VER = $(shell dpkg-parsechangelog -l$(PACKAGING)/debian/changelog | sed -rne 's,^Version: ([^-]+).*,\1,p')
package_export: TARBALL = maas_$(VER).orig.tar.gz
package_export: package_branch
	@$(RM) -f ../build-area/$(TARBALL)
	@mkdir -p ../build-area
	@bzr export --root=maas-$(VER).orig ../build-area/$(TARBALL) $(CURDIR)

package: package_export
	bzr bd --merge $(PACKAGING) --result-dir=../build-area -- -uc -us
	@echo Binary packages built, see ../build-area/ directory.

source_package: package_export
	bzr bd --merge $(PACKAGING) --result-dir=../build-area -- -S -uc -us
	@echo Source package built, see ../build-area/ directory.

#
# Phony stuff.
#

define phony
  $(phony_services_targets)
  $(phony_targets)
endef

phony := $(sort $(strip $(phony)))

.PHONY: $(phony)

#
# Functions.
#

# Check if a command is found on PATH. Raise an error if not, citing
# the package to install. Return the command otherwise.
# Usage: $(call available,<command>,<package>)
define available
  $(if $(shell which $(1)),$(1),$(error $(1) not found; \
    install it with 'sudo apt-get install $(2)'))
endef