e7036e1634561861a2b052bb22335d50ec92c348
[armband.git] / patches / opnfv-fuel / upstream-backports / 0003-f_repobuild-Use-packetary-to-build-partial-mirror.patch
1 From: Alexandru Avadanii <Alexandru.Avadanii@enea.com>
2 Date: Wed, 6 Jul 2016 17:03:49 +0200
3 Subject: [PATCH] f_repobuild: Use packetary to build partial mirror
4
5 Packetary [1] will replace fuel-mirror in upstream Fuel.
6 fuel-mirror is using packetary under the hood already, so the
7 transition should not be too hard.
8
9 Adapt OPNFV Ubuntu partial mirror build:
10 - obsolete opnfv-config.yaml (packetary uses CLI args for this);
11 - new opnfv_config.yaml will hold old ubuntu.yaml data, split at
12   build runtime into:
13   * mos_<arch>_mirror.yaml - MOS mirror cfg, only for deps analysis;
14   * ubuntu_<arch>_mirror.yaml - Ubuntu mirror cfg, for pkgs download;
15   * ubuntu_<arch>_packages.yaml - Ubuntu packages to download;
16
17 While the consumers of our mirror still use `debootstrap`, which only
18 supports the "main" repo component, make sure the "main" package set
19 is complete, in the scope of:
20   * ubuntu_<arch>_mirror_main.yaml - Ubuntu [main only] mirror;
21   * ubuntu_<arch>_packages_main.yaml - Ubuntu packages for [main];
22
23 New workflow (executed for EACH architecture):
24 1. Collect bootstrap package deps from <fuel_bootstrap_cli.yaml>;
25 2. Collect all fixture release pkgs from fuel-web's <openstack.yaml>;
26 3. Add OPNFV extra pkg deps from "opnfv_config.yaml"
27    (based on old fuel-mirror)
28
29    Currently, this is based on old fuel-mirror's
30    <fuel-mirror/contrib/fuel_mirror/data/ubuntu.yaml>, holding a
31    hardcoded list of package deps; plus eventual extra OPNFV packages,
32    like additional kernels, bootloaders etc.
33    Since we now inspect MOS repos for deps with packetary, this old
34    list should be easy to throw away as soon as we confirm each item
35    is directly required by another, already existing package in the
36    mirror. For now, keep the old list as safety net for missing deps.
37
38    NOTE: Dependencies up to and including this group should also
39    be satisfiable from Ubuntu "main" repo component only.
40
41 4. Append plugin dependencies to the package list;
42
43 5. Run `packetary unresolved` for MOS repos, gathering MOS deps that
44    should be fetched from Ubuntu. The new packages will be appended
45    to the predefined list from old "ubuntu.yaml";
46
47 6. Run `packetary clone` to download all required pkgs for the
48    partial Ubuntu mirror.
49
50 7. IF repo component merging is disabled, run `packetary clone` only
51    on Ubuntu[main] repo components to download any (still) missing
52    dependencies for `debootstrap`, which only looks at this component,
53    and not the whole mirror.
54
55 8. IF repo component merging is enabled, run first `dpkg-scanpackages`
56    to filter out old duplicate versions of packages, then run
57    `packetary create` to merge all repo comps into a single "main".
58
59 v1 -> v2:
60 - multiarch support (activated via UBUNTU_ARCH envvar);
61 - fixed debootstrap missing deps in "main" component of Ubuntu mirror;
62 - factored out some hardcodes into variables/functions;
63 - add .gitignore file;
64 - move generated config files to "opnfv_config" subdir;
65 - added arm64 Ubuntu repo URL (ports.ubuntu.com is separated from the
66   main Ubuntu mirror, archive.ubuntu.com, which only holds x86 pkgs);
67
68 v3 -> v4:
69 - introduce env var "MIRROR_UBUNTU_MERGE" to control local mirror
70   repo-component merging into a single "main";
71 - enable mirror repo-component merging by default, edit
72   fuel_bootstrap_cli.yaml accordingly;
73
74 v4 -> v6:
75 - edit dea_base to use only main mirror;
76 - fix duplicated logs;
77 - remove `-q` flag for `create`;
78
79 v6 -> v7:
80 - rebase for Newton fuel_bootstrap_cli.yaml (s/trusty/xenial/);
81 - include OPNFV plugin deps in local Ubuntu mirror;
82 - add `vgabios` dependency, required for arm64 guest firmware;
83 - remove all fuel-mirror references;
84 - merge old fuel-mirror pkglist to OPNFV pkglist;
85 - update packetary commit to include upstream bugfixes [2, 3];
86
87 NOTE: Without filtering out old package verisons using
88 `dpkg-scanpackages`, bootstrap build cannot solve all deps.
89
90 FIXME: Packetary solves missing dependecies by also accepting
91 different packages that provide the same package name (e.g. "ifupdown"
92 is satisfied by "netscript" package).
93
94 FIXME: Repo component merging is sort of slow, since packetary insists
95 on copying the source files to the destination dir even if they are
96 pointing to the same repo.
97
98 FIXME: Packetary `create` uses a different directory scheme for the
99 created mirror than the upstream Ubuntu, e.g.:
100 [p] mirrors/ubuntu/pool/main/l/lvm2_2.02.98-6ubuntu2_amd64.deb
101 [U] mirrors/ubuntu/pool/main/l/lvm2/lvm2_2.02.98-6ubuntu2_amd64.deb
102
103 This disencourages creating the "merged" mirror in the same location
104 as the source partial mirror.
105
106 [1] https://wiki.openstack.org/wiki/Packetary
107 [2] https://review.openstack.org/#/c/392936/
108 [3] https://review.openstack.org/#/c/392937/
109
110 JIRA: FUEL-218
111 JIRA: FUEL-223
112 JIRA: ARMBAND-169
113 JIRA: ARMBAND-185
114
115 Change-Id: If2ee86f348b7683c83bfaf686baba4f1b8f555f0
116 Signed-off-by: Alexandru Avadanii <Alexandru.Avadanii@enea.com>
117 ---
118  build/config.mk                                    |   4 +
119  build/f_isoroot/f_repobuild/.gitignore             |   4 +
120  build/f_isoroot/f_repobuild/Makefile               |  37 ++-
121  build/f_isoroot/f_repobuild/config.mk              |  23 ++
122  .../f_isoroot/f_repobuild/fuel_bootstrap_cli.yaml  |  22 +-
123  build/f_isoroot/f_repobuild/opnfv_config.yaml      | 160 ++++++++++++
124  build/f_isoroot/f_repobuild/opnfv_mirror_conf.py   |  57 -----
125  build/f_isoroot/f_repobuild/opnfv_mirror_ubuntu.py | 277 +++++++++++++++++++++
126  deploy/config/dea_base.yaml                        |  16 +-
127  9 files changed, 492 insertions(+), 108 deletions(-)
128  create mode 100644 build/f_isoroot/f_repobuild/.gitignore
129  create mode 100644 build/f_isoroot/f_repobuild/config.mk
130  create mode 100644 build/f_isoroot/f_repobuild/opnfv_config.yaml
131  delete mode 100755 build/f_isoroot/f_repobuild/opnfv_mirror_conf.py
132  create mode 100755 build/f_isoroot/f_repobuild/opnfv_mirror_ubuntu.py
133
134 diff --git a/build/config.mk b/build/config.mk
135 index 56317cb..5448c5e 100644
136 --- a/build/config.mk
137 +++ b/build/config.mk
138 @@ -17,4 +17,9 @@ FUEL_MAIN_TAG = master
139  FUEL_TRACK_REMOTES =
140 +
141 +# List of space-separated Ubuntu architectures supported with current build
142 +# Format: same as `dpkg-architecture -qDEB_HOST_ARCH`
143 +# NOTE: Currently only amd64 is supported by Fuel@OPNFV. Armband adds arm64.
144 +export UBUNTU_ARCH ?= amd64
145
146  ##############################################################################
147  # Fuel components pinning / remote tracking; use submodules from f_repos
148 diff --git a/build/f_isoroot/f_repobuild/.gitignore b/build/f_isoroot/f_repobuild/.gitignore
149 new file mode 100644
150 index 0000000..09baca8
151 --- /dev/null
152 +++ b/build/f_isoroot/f_repobuild/.gitignore
153 @@ -0,0 +1,4 @@
154 +packetary
155 +fuel-web
156 +nailgun
157 +opnfv_config
158 diff --git a/build/f_isoroot/f_repobuild/Makefile b/build/f_isoroot/f_repobuild/Makefile
159 index 8beb882..9784edf 100644
160 --- a/build/f_isoroot/f_repobuild/Makefile
161 +++ b/build/f_isoroot/f_repobuild/Makefile
162 @@ -1,7 +1,8 @@
163  ##############################################################################
164 -# Copyright (c) 2015 Ericsson AB and others.
165 +# Copyright (c) 2015,2016 Ericsson AB, Enea AB and others.
166  # stefan.k.berg@ericsson.com
167  # jonas.bjurel@ericsson.com
168 +# Alexandru.Avadanii@enea.com
169  # All rights reserved. This program and the accompanying materials
170  # are made available under the terms of the Apache License, Version 2.0
171  # which accompanies this distribution, and is available at
172 @@ -10,41 +11,37 @@
173
174  SHELL := /bin/bash
175  TOP := $(shell pwd)
176 -TMP_ROOT_DIR := $(shell echo "$(MIRROR_UBUNTU_ROOT)" | cut -d "/" -f2)
177
178  include ../../config.mk
179 +include config.mk
180
181  export MOS_VERSION
182 -export OPENSTACK_VERSION
183 +export MIRROR_UBUNTU_OPNFV_PATH:=$(TOP)/nailgun/mirrors/ubuntu
184
185  .PHONY: all
186  all: nailgun
187
188  nailgun:
189 -       sudo apt-get install -y git libxml2-dev libxslt-dev python-dev  python-pip libz-dev libyaml-dev createrepo python-yaml
190 -       rm -Rf nailgun
191 -       sudo mkdir -p /var/www/nailgun
192 -       ln -sf ${F_SUBMOD_DIR}/fuel-mirror fuel-mirror
193 -       sudo pip install -U -r ./fuel-mirror/requirements.txt
194 -       sudo pip install ./fuel-mirror
195 -       sudo pip install ./fuel-mirror/contrib/fuel_mirror
196 -       ./opnfv_mirror_conf.py
197 -       sudo fuel-mirror --debug --config ./opnfv-config.yaml create --group ubuntu --pattern=ubuntu
198 -       sudo chmod -R 755 /var/www/nailgun
199 -       cp -Rp /var/www/nailgun .
200 -       # In the end we want to have ubuntu repository in mirrors/ubuntu directory
201 -       -if [ "$(MIRROR_UBUNTU_ROOT)" != "/ubuntu/" ]; then \
202 -         mkdir -p nailgun/mirrors/ubuntu;\
203 -         mv nailgun/mirrors$(MIRROR_UBUNTU_ROOT)* nailgun/mirrors/ubuntu;\
204 -         [ "$(MIRROR_UBUNTU_ROOT)" != "/" ] && rm -rf nailgun/mirrors/$(TMP_ROOT_DIR);\
205 +       sudo apt-get install -y createrepo git libxml2-dev libxslt1-dev \
206 +               python-dev zlib1g-dev
207 +       rm -Rf nailgun packetary opnfv_config && mkdir opnfv_config
208 +       # We will analyze fuel-web's fixture files for package lists
209 +       ln -sf ${F_SUBMOD_DIR}/fuel-web fuel-web
210 +       git clone --quiet $(PACKETARY_REPO)
211 +       if [ -n $(PACKETARY_COMMIT) ]; then \
212 +               git -C packetary checkout $(PACKETARY_COMMIT); \
213         fi
214 +       sudo pip install -U -r ./packetary/requirements.txt
215 +       sudo pip install ./packetary
216 +       # Handle config and mirror build in one place
217 +       ./opnfv_mirror_ubuntu.py
218         # Store artifact in cache straight away if caching is enabled
219         # (no .cacheid will be present unless this is a cached build)
220         test -f .cacheid && $(MAKE) -f Makefile put-cache || exit 0
221
222  .PHONY: clean
223  clean:
224 -       @rm -rf ../release/opnfv/nailgun nailgun fuel-mirror opnfv-config.yaml ubuntu.yaml
225 +       @rm -rf ../release/opnfv/nailgun nailgun packetary fuel-mirror opnfv_config
226
227  .PHONY: release
228  release:nailgun
229 diff --git a/build/f_isoroot/f_repobuild/config.mk b/build/f_isoroot/f_repobuild/config.mk
230 new file mode 100644
231 index 0000000..9852093
232 --- /dev/null
233 +++ b/build/f_isoroot/f_repobuild/config.mk
234 @@ -0,0 +1,23 @@
235 +##############################################################################
236 +# Copyright (c) 2016 Ericsson AB, Enea AB and others.
237 +# stefan.k.berg@ericsson.com
238 +# jonas.bjurel@ericsson.com
239 +# Alexandru.Avadanii@enea.com
240 +# All rights reserved. This program and the accompanying materials
241 +# are made available under the terms of the Apache License, Version 2.0
242 +# which accompanies this distribution, and is available at
243 +# http://www.apache.org/licenses/LICENSE-2.0
244 +##############################################################################
245 +
246 +# Use a recent master commit, since tags/branches are not yet mature
247 +export PACKETARY_REPO?=https://github.com/openstack/packetary
248 +export PACKETARY_COMMIT?=3021c001561b4baef352bf0b881d064ac687cc20
249 +
250 +# arm64 Ubuntu mirror is separated from archive.ubuntu.com
251 +export MIRROR_UBUNTU_URL_arm64=http://ports.ubuntu.com/ubuntu-ports/
252 +export MIRROR_UBUNTU_ROOT_arm64=ubuntu-ports
253 +
254 +# Merge all local mirror repo components/section into single "main"
255 +# NOTE: When changing this, make sure to also update all consumer config, like:
256 +# - fuel_bootstrap_cli.yaml
257 +export MIRROR_UBUNTU_MERGE=true
258 diff --git a/build/f_isoroot/f_repobuild/fuel_bootstrap_cli.yaml b/build/f_isoroot/f_repobuild/fuel_bootstrap_cli.yaml
259 index fcf4257..7cbea29 100644
260 --- a/build/f_isoroot/f_repobuild/fuel_bootstrap_cli.yaml
261 +++ b/build/f_isoroot/f_repobuild/fuel_bootstrap_cli.yaml
262 @@ -47,22 +47,10 @@
263    https_proxy: ""
264    repos:
265      - name: ubuntu
266 -      section: "main universe multiverse"
267 +      section: "main"
268        uri: "http://127.0.0.1:8080/mirrors/ubuntu"
269        priority:
270        suite: trusty
271 -      type: deb
272 -    - name: ubuntu-updates
273 -      section: "main universe multiverse"
274 -      uri: "http://127.0.0.1:8080/mirrors/ubuntu"
275 -      priority:
276 -      suite: trusty-updates
277 -      type: deb
278 -    - name: ubuntu-security
279 -      section: "main universe multiverse"
280 -      uri: "http://127.0.0.1:8080/mirrors/ubuntu"
281 -      priority:
282 -      suite: trusty-security
283        type: deb
284      - name: mos
285        section: "main restricted"
286 diff --git a/build/f_isoroot/f_repobuild/opnfv_config.yaml b/build/f_isoroot/f_repobuild/opnfv_config.yaml
287 new file mode 100644
288 index 0000000..bb78747
289 --- /dev/null
290 +++ b/build/f_isoroot/f_repobuild/opnfv_config.yaml
291 @@ -0,0 +1,160 @@
292 +##############################################################################
293 +# Copyright (c) 2016 Enea AB and others.
294 +# Alexandru.Avadanii@enea.com
295 +# All rights reserved. This program and the accompanying materials
296 +# are made available under the terms of the Apache License, Version 2.0
297 +# which accompanies this distribution, and is available at
298 +# http://www.apache.org/licenses/LICENSE-2.0
299 +##############################################################################
300 +
301 +# REPO definitions
302 +
303 +# Based on old fuel-mirror data [1], adapted for OPNFV and packetary.
304 +# NOTE: 'uri' field will be added by opnfv_mirror_ubuntu.py from ENV.
305 +# [1] https://github.com/openstack/fuel-mirror/blob/stable/mitaka/
306 +#     contrib/fuel_mirror/data/ubuntu.yaml
307 +
308 +# Main is a required parameter which defines what repository will be used
309 +# for images creation and that mirror should contain all packages for minimal
310 +# system creation.
311 +groups:
312 +  ubuntu:
313 +    - name: "ubuntu"
314 +      main: true
315 +      suite: "trusty"
316 +      section:
317 +        - "main"
318 +        - "multiverse"
319 +        - "restricted"
320 +        - "universe"
321 +      type: "deb"
322 +      priority: null
323 +
324 +    - name: "ubuntu-updates"
325 +      suite: "trusty-updates"
326 +      section:
327 +        - "main"
328 +        - "multiverse"
329 +        - "restricted"
330 +        - "universe"
331 +      type: "deb"
332 +      priority: null
333 +
334 +    - name: "ubuntu-security"
335 +      suite: "trusty-security"
336 +      section:
337 +        - "main"
338 +        - "multiverse"
339 +        - "restricted"
340 +        - "universe"
341 +      type: "deb"
342 +      priority: null
343 +
344 +  mos:
345 +    - name: "mos"
346 +      suite: "mos$mos_version"
347 +      section:
348 +        - "main"
349 +        - "restricted"
350 +      type: "deb"
351 +      priority: 1000
352 +
353 +    - name: "mos-updates"
354 +      suite: "mos$mos_version-updates"
355 +      section:
356 +        - "main"
357 +        - "restricted"
358 +      type: "deb"
359 +      priority: 1000
360 +
361 +    - name: "mos-security"
362 +      suite: "mos$mos_version-security"
363 +      section:
364 +        - "main"
365 +        - "restricted"
366 +      type: "deb"
367 +      priority: 1000
368 +
369 +    - name: "mos-holdback"
370 +      suite: "mos$mos_version-holdback"
371 +      section:
372 +        - "main"
373 +        - "restricted"
374 +      type: "deb"
375 +      priority: 1000
376 +
377 +# PACKAGES
378 +
379 +# This section lists packages that should be present in the local Ubuntu mirror,
380 +# but are not direct dependencies of any other packages from  MOS or Ubuntu.
381 +# e.g.: additional kernels, bootloaders etc.
382 +
383 +packages:
384 +  - name: "vgabios"
385 +# Packages are required to build bootstrap images for a system.
386 +# The mirror should contiain such packages in addition to local mirror.
387 +  - name: "acpi-support"
388 +  - name: "anacron"
389 +  - name: "aptitude"
390 +  - name: "atop"
391 +  - name: "acct"
392 +  - name: "bash-completion"
393 +  - name: "bc"
394 +  - name: "build-essential"
395 +  - name: "cloud-init"
396 +  - name: "conntrackd"
397 +  - name: "cpu-checker"
398 +  - name: "cpufrequtils"
399 +  - name: "debconf-utils"
400 +  - name: "devscripts"
401 +  - name: "fping"
402 +  - name: "git"
403 +  - name: "grub-pc"
404 +  - name: "htop"
405 +  - name: "hwloc"
406 +  - name: "ifenslave"
407 +  - name: "iperf"
408 +  - name: "iptables-persistent"
409 +  - name: "irqbalance"
410 +  - name: "language-pack-en"
411 +  - name: "libapache2-mod-fastcgi"
412 +  - name: "libnss3-tools"
413 +  - name: "linux-firmware-nonfree"
414 +  - name: "linux-headers-generic-lts-trusty"
415 +  - name: "linux-headers-generic-lts-xenial"
416 +  - name: "linux-image-generic-lts-trusty"
417 +  - name: "linux-image-generic-lts-xenial"
418 +  - name: "live-boot"
419 +  - name: "livecd-rootfs"
420 +  - name: "mc"
421 +  - name: "memcached"
422 +  - name: "monit"
423 +  - name: "msmtp-mta"
424 +  - name: "multipath-tools"
425 +  - name: "multipath-tools-boot"
426 +  - name: "nginx"
427 +  - name: "ntp"
428 +  - name: "openssh-server"
429 +  - name: "percona-toolkit"
430 +  - name: "percona-xtrabackup"
431 +  - name: "pm-utils"
432 +  - name: "puppet"
433 +  - name: "python-lesscpy"
434 +  - name: "python-pip"
435 +  - name: "rsyslog-gnutls"
436 +  - name: "rsyslog-relp"
437 +  - name: "screen"
438 +  - name: "squashfs-tools"
439 +  - name: "swift-plugin-s3"
440 +  - name: "sysfsutils"
441 +  - name: "sysstat"
442 +  - name: "telnet"
443 +  - name: "tmux"
444 +  - name: "traceroute"
445 +  - name: "ubuntu-standard"
446 +  - name: "vim"
447 +  - name: "virt-what"
448 +  - name: "xinetd"
449 +  - name: "xmlstarlet"
450 +  - name: "tftpd-hpa"
451 +  - name: "syslinux"
452 diff --git a/build/f_isoroot/f_repobuild/opnfv_mirror_conf.py b/build/f_isoroot/f_repobuild/opnfv_mirror_conf.py
453 deleted file mode 100755
454 index 1c7eb59..0000000
455 --- a/build/f_isoroot/f_repobuild/opnfv_mirror_conf.py
456 +++ /dev/null
457 @@ -1,57 +0,0 @@
458 -#!/usr/bin/env python
459 -##############################################################################
460 -# Copyright (c) 2015 Ericsson AB and others.
461 -# mskalski@mirantis.com
462 -# All rights reserved. This program and the accompanying materials
463 -# are made available under the terms of the Apache License, Version 2.0
464 -# which accompanies this distribution, and is available at
465 -# http://www.apache.org/licenses/LICENSE-2.0
466 -##############################################################################
467 -import os
468 -import yaml
469 -
470 -current_snapshot =  os.environ["LATEST_TARGET_UBUNTU"]
471 -mos_version = os.environ['MOS_VERSION']
472 -openstack_version = os.environ['OPENSTACK_VERSION']
473 -mos_ubuntu = os.environ['MIRROR_MOS_UBUNTU']
474 -mos_ubuntu_root = os.environ['MIRROR_MOS_UBUNTU_ROOT']
475 -mirror_ubuntu = os.environ['MIRROR_UBUNTU_URL']
476 -if os.environ.get('BUILD_FUEL_PLUGINS'):
477 -  plugins = os.environ['BUILD_FUEL_PLUGINS']
478 -else:
479 -  plugins = os.environ['PLUGINS']
480 -
481 -
482 -configuration_file = open('fuel-mirror/contrib/fuel_mirror/etc/config.yaml').read()
483 -conf = yaml.load(configuration_file)
484 -conf['pattern_dir'] = '.'
485 -conf['openstack_version'] = openstack_version
486 -conf['mos_version'] = mos_version
487 -
488 -with open('opnfv-config.yaml', 'w') as outfile:
489 -  outfile.write( yaml.dump(conf, default_flow_style=False) )
490 -
491 -pattern_file = open('fuel-mirror/contrib/fuel_mirror/data/ubuntu.yaml').read()
492 -pattern = yaml.load(pattern_file)
493 -pattern['mos_baseurl'] = "http://{}{}".format(mos_ubuntu, mos_ubuntu_root)
494 -pattern['ubuntu_baseurl'] = mirror_ubuntu
495 -for group in pattern['groups']['mos']:
496 -  group['uri'] = pattern['mos_baseurl']
497 -for group in pattern['groups']['ubuntu']:
498 -  group['uri'] = pattern['ubuntu_baseurl']
499 -
500 -for plugin in plugins.split():
501 -  path = "../{}/packages.yaml".format(plugin)
502 -  if os.path.isfile(path):
503 -    f = open(path).read()
504 -    plugin_yaml = yaml.load(f)
505 -    plugin_set = set(plugin_yaml['packages'])
506 -    main_set = set(pattern['packages'])
507 -    new_packages = plugin_set - main_set
508 -    print "Plugin {} require new packages: {}".format(plugin, ', '.join(new_packages))
509 -    pattern['packages'] = pattern['packages'] + list(new_packages)
510 -
511 -pattern['requirements']['ubuntu'] = pattern['packages']
512 -
513 -with open('ubuntu.yaml', 'w') as outfile:
514 -  outfile.write( yaml.safe_dump(pattern, default_flow_style=False) )
515 diff --git a/build/f_isoroot/f_repobuild/opnfv_mirror_ubuntu.py b/build/f_isoroot/f_repobuild/opnfv_mirror_ubuntu.py
516 new file mode 100755
517 index 0000000..5b59182
518 --- /dev/null
519 +++ b/build/f_isoroot/f_repobuild/opnfv_mirror_ubuntu.py
520 @@ -0,0 +1,277 @@
521 +#!/usr/bin/env python
522 +##############################################################################
523 +# Copyright (c) 2015,2016 Ericsson AB, Mirantis Inc., Enea AB and others.
524 +# mskalski@mirantis.com
525 +# Alexandru.Avadanii@enea.com
526 +# All rights reserved. This program and the accompanying materials
527 +# are made available under the terms of the Apache License, Version 2.0
528 +# which accompanies this distribution, and is available at
529 +# http://www.apache.org/licenses/LICENSE-2.0
530 +##############################################################################
531 +
532 +##############################################################################
533 +# Build multiarch partial local Ubuntu mirror
534 +##############################################################################
535 +# Design quirks / workarounds:
536 +# 1. Fuel-agent uses `debootstrap` to build bootstrap and target chroots from
537 +#    the local mirror; which only uses the "main" component from the first
538 +#    repository, i.e. does not include "updates"/"security".
539 +#    In order to fullfill all debootstrap dependencies in "main" repo, we will
540 +#    do an extra packetary run using a reduced scope:
541 +#    - only "main" component of the first mirror;
542 +#    - reduced package dependency list (without MOS/OPNFV plugin deps).
543 +# 2. If repo structure is not mandatory to be in sync with official mirrors,
544 +#    we can mitigate the issue by "merging" all repo-components into a single
545 +#    "main".
546 +##############################################################################
547 +# Mirror build steps (for EACH architecture in UBUNTU_ARCH):
548 +# 1. Collect bootstrap package deps from <fuel_bootstrap_cli.yaml>;
549 +# 2. Collect all fixture release packages from fuel-web's <openstack.yaml>;
550 +# 3. Parse new "opnfv_config.yaml" list of packages (from old fuel-mirror);
551 +# 4. Inherit enviroment variable(s) for mirror URLs, paths etc.
552 +#    - Allow arch-specific overrides for each env var;
553 +# 5. Mirror config is defined based on common config + OPNFV overrides;
554 +#    - Convert old configuration format to packetary style where needed;
555 +# 6. Package lists are defined based on common config + OPNFV deps;
556 +#    - Keep track of "main" packages separately, required by debootstrap;
557 +# 7. Clone/update all mirror components;
558 +# 8. IF mirror merging is disabled OR workaround for ifupdown (see below):
559 +#    - Clone/update "main" mirror component (fix missing debootstrap deps);
560 +# 9. IF mirror merging is enabled:
561 +#    - Use `dpkg-scanpackages` to filter out old versions of duplicate pkgs;
562 +#    - Run `packetary create` on the set of downloaded packages, merging
563 +#      them on the fly into a single-component mirror;
564 +##############################################################################
565 +
566 +import copy
567 +import os
568 +import shutil
569 +import sys
570 +import yaml
571 +from contextlib import contextmanager
572 +from cStringIO import StringIO
573 +from packetary.cli.app import main
574 +
575 +@contextmanager
576 +def captureStdOut(output):
577 +    stdout = sys.stdout
578 +    sys.stdout = output
579 +    yield
580 +    sys.stdout = stdout
581 +
582 +# FIXME: Find a better approach for eliminating duplicate logs than this
583 +def force_logger_reload():
584 +  for mod in sys.modules.keys():
585 +    if mod.startswith('logging'):
586 +      try:
587 +        reload(sys.modules[mod])
588 +      except:
589 +        pass
590 +
591 +# Determine missing package dependecies for a mirror defition
592 +def get_unres_pkgs(arch, cfg_mirror):
593 +  unresolved_pkgs = list()
594 +  packetary_output = StringIO()
595 +  with captureStdOut(packetary_output):
596 +    main('unresolved -a {0} -r {1} -c name version --sep ;'
597 +      .format(_ARCHITECTURES[arch], cfg_mirror).split(' '))
598 +  for dep_pkg in packetary_output.getvalue().splitlines():
599 +    if dep_pkg.startswith('#'):
600 +      continue
601 +    dep = dep_pkg.split(';')
602 +    unresolved_pkgs += [ {'name': dep[0], 'version': dep[1]} ]
603 +  force_logger_reload()
604 +  return unresolved_pkgs
605 +
606 +# Package list conversion from `old fuel-mirror` to `packetary` style
607 +def from_legacy_pkglist(legacy_pkglist):
608 +  pkglist = list()
609 +  for pkg in legacy_pkglist:
610 +    pkglist += [ {'name': pkg} ]
611 +  return pkglist
612 +
613 +def to_legacy_pkglist(pkglist):
614 +  legacy_pkglist = list()
615 +  for pkg in pkglist:
616 +    legacy_pkglist.append(pkg['name'])
617 +  return legacy_pkglist
618 +
619 +def legacy_diff(base_pkglist, new_pkglist, requester, arch):
620 +  diff_set = set(new_pkglist)
621 +  if base_pkglist:
622 +    diff_set -= set(base_pkglist)
623 +  if diff_set:
624 +    print(' * {0} requires new packages for arch [{1}]: {2}'
625 +          .format(requester, arch, ', '.join(diff_set)))
626 +  return list(diff_set)
627 +
628 +# Create single-component local repo (one arch per call)
629 +def do_local_repo(arch, cfg_repo, cfg_packages_paths):
630 +  # Packetary does not use a global config file, so pass old settings here.
631 +  main('create -t deb -a {0} --repository {1} --package-files {2}'
632 +    ' --ignore-errors-num 2 --retries-num 3 --threads-num 10'
633 +    .format(_ARCHITECTURES[arch], cfg_repo, cfg_packages_paths).split(' '))
634 +  force_logger_reload()
635 +
636 +# Clone partial local mirror (one arch per call)
637 +def do_partial_mirror(arch, cfg_mirror, cfg_packages):
638 +  # Note: '-d .' is ignored, as each mirror defines its own path.
639 +  main('clone -t deb -a {0} -r {1} -R {2} -d .'
640 +    ' --ignore-errors-num 2 --retries-num 3 --threads-num 10'
641 +    .format(_ARCHITECTURES[arch], cfg_mirror, cfg_packages).split(' '))
642 +  force_logger_reload()
643 +
644 +# Write configuration (yaml) file (package list / mirror defition)
645 +def write_cfg_file(cfg_mirror, data):
646 +  with open(cfg_mirror, 'w') as outfile:
647 +    outfile.write( yaml.safe_dump(data, default_flow_style=False) )
648 +
649 +# Allow arch-specific overrides of env vars
650 +def get_env(env_var, arch=None):
651 +  if arch:
652 +    env_var_arch = '{0}_{1}'.format(env_var, arch)
653 +    if os.environ.get(env_var_arch):
654 +      return os.environ[env_var_arch]
655 +  if os.environ.get(env_var):
656 +    return os.environ[env_var]
657 +  return None
658 +
659 +# Architecture name mapping (dpkg:packetary) for packetary CLI invocation
660 +_ARCHITECTURES = {
661 +    "i386": "i386",
662 +    "amd64": "x86_64",
663 +    "arm64": "aarch64",
664 +}
665 +
666 +# Arch-indepedent configuration (old fuel-mirror + OPNFV extra packages)
667 +cfg_dir = 'opnfv_config'
668 +cfg_p_opnfv = 'opnfv_config.yaml'
669 +mos_version = get_env('MOS_VERSION')
670 +ubuntu_arch = get_env('UBUNTU_ARCH')
671 +mirror_ubuntu_path = get_env('MIRROR_UBUNTU_OPNFV_PATH')
672 +mirror_ubuntu_tmp_path = '{0}.tmp'.format(mirror_ubuntu_path)
673 +mirror_ubuntu_merge = get_env('MIRROR_UBUNTU_MERGE')
674 +cfg_mm_ubuntu = '{0}/ubuntu_mirror_local.yaml'.format(cfg_dir)
675 +fuel_bootstrap_cli_file = open('fuel_bootstrap_cli.yaml').read()
676 +fuel_bootstrap_cli = yaml.load(fuel_bootstrap_cli_file)
677 +fixture_file = open('fuel-web/nailgun/nailgun/fixtures/openstack.yaml').read()
678 +fixture = yaml.load(fixture_file)
679 +opnfv_cfg_yaml = open(cfg_p_opnfv).read()
680 +opnfv_cfg = yaml.load(opnfv_cfg_yaml)
681 +
682 +# FIXME: Packetary solves missing dependecies by also accepting
683 +# different packages that provide the same name (e.g. "ifupdown" dependency
684 +# is satisfied by "netscript" package from "universe" repo-component).
685 +# Work around this by resolving all deps in "main" repo-component,
686 +# then scan and keep only latest debs for the whole <merged> repo.
687 +mirror_ubuntu_resolve_main_deps = True
688 +
689 +# Create local partial mirror using packetary, one arch at a time
690 +for arch in ubuntu_arch.split(' '):
691 +  # Mirror / Package env vars, arch-overrideable
692 +  mos_ubuntu = get_env('MIRROR_MOS_UBUNTU', arch)
693 +  mos_ubuntu_root = get_env('MIRROR_MOS_UBUNTU_ROOT', arch)
694 +  mirror_ubuntu = get_env('MIRROR_UBUNTU_URL', arch)
695 +  plugins = get_env('BUILD_FUEL_PLUGINS', arch)
696 +  if plugins is None:
697 +    plugins = get_env('PLUGINS', arch)
698 +
699 +  # Mirror / Package list configuration files (arch-specific)
700 +  cfg_m_mos = '{0}/mos_{1}_mirror.yaml'.format(cfg_dir, arch)
701 +  cfg_m_ubuntu = '{0}/ubuntu_{1}_mirror.yaml'.format(cfg_dir, arch)
702 +  cfg_p_ubuntu = '{0}/ubuntu_{1}_packages.yaml'.format(cfg_dir, arch)
703 +  cfg_m_ubuntu_main = '{0}/ubuntu_{1}_mirror_main.yaml'.format(cfg_dir, arch)
704 +  cfg_p_ubuntu_main = '{0}/ubuntu_{1}_packages_main.yaml'.format(cfg_dir, arch)
705 +
706 +  # Mirror config fork before customizing (arch-specific)
707 +  arch_group_mos = 'mos_{0}'.format(arch)
708 +  arch_group_ubuntu = 'ubuntu_{0}'.format(arch)
709 +  arch_packages = 'packages_{0}'.format(arch)
710 +  opnfv_cfg['groups'][arch_group_mos] = copy.deepcopy(opnfv_cfg['groups']['mos'])
711 +  opnfv_cfg['groups'][arch_group_ubuntu] = copy.deepcopy(opnfv_cfg['groups']['ubuntu'])
712 +  opnfv_cfg[arch_packages] = opnfv_cfg['packages']
713 +
714 +  # Mirror config update & conversion to packetary input
715 +  group_main_ubuntu = dict()
716 +  for group in opnfv_cfg['groups'][arch_group_mos]:
717 +    group['uri'] = "http://{}{}".format(mos_ubuntu, mos_ubuntu_root)
718 +    group['suite'] = group['suite'].replace('$mos_version', mos_version)
719 +  for group in opnfv_cfg['groups'][arch_group_ubuntu]:
720 +    group['uri'] = mirror_ubuntu
721 +    # FIXME: At `create`, packetary insists on copying all pkgs to dest dir,
722 +    # so configure it for another dir, which will replace the orig at the end.
723 +    group['path'] = mirror_ubuntu_tmp_path
724 +    if not group_main_ubuntu and 'main' in group:
725 +      group_main_ubuntu = [ copy.deepcopy(group) ]
726 +      group_main_ubuntu[0]['section'] = [ 'main' ]
727 +
728 +  # Mirror config dump: MOS (for dep resolution), Ubuntu, Ubuntu[main]
729 +  write_cfg_file(cfg_m_mos, opnfv_cfg['groups'][arch_group_mos])
730 +  write_cfg_file(cfg_m_ubuntu, opnfv_cfg['groups'][arch_group_ubuntu])
731 +  if mirror_ubuntu_resolve_main_deps or mirror_ubuntu_merge is None:
732 +    write_cfg_file(cfg_m_ubuntu_main, group_main_ubuntu)
733 +  if mirror_ubuntu_merge is not None:
734 +    # FIXME: For multiarch, only one dump would be enough
735 +    group_main_ubuntu[0]['origin'] = 'Ubuntu'
736 +    group_main_ubuntu[0]['path'] = mirror_ubuntu_path
737 +    group_main_ubuntu[0]['uri'] = mirror_ubuntu_path
738 +    write_cfg_file(cfg_mm_ubuntu, group_main_ubuntu[0])
739 +
740 +  # Collect package dependencies from:
741 +  ## 1. fuel_bootstrap_cli.yaml (bootstrap image additional packages)
742 +  legacy_unresolved = legacy_diff(None, fuel_bootstrap_cli['packages'] + [
743 +      fuel_bootstrap_cli['kernel_flavor'],
744 +      fuel_bootstrap_cli['kernel_flavor'].replace('image', 'headers')],
745 +    'Bootstrap', arch)
746 +  ## 2. openstack.yaml fixture definition (default target image packages)
747 +  for release in fixture:
748 +    editable = release['fields']['attributes_metadata']['editable']
749 +    if 'provision' in editable and 'packages' in editable['provision']:
750 +      release_pkgs = editable['provision']['packages']['value'].split()
751 +      legacy_unresolved += legacy_diff(legacy_unresolved,
752 +        release_pkgs, 'Release {0}'.format(release['fields']['name']), arch)
753 +  ## 3. OPNFV additional packages (includes old fuel-mirror ubuntu.yaml pkgs)
754 +  unresolved = dict()
755 +  unresolved['mandatory'] = 'exact'
756 +  unresolved['packages'] = from_legacy_pkglist(legacy_unresolved)
757 +  if 'packages' in opnfv_cfg:
758 +    legacy_diff(legacy_unresolved, to_legacy_pkglist(opnfv_cfg['packages']),
759 +      'OPNFV config', arch)
760 +    unresolved['packages'] += opnfv_cfg['packages']
761 +
762 +  # OPNFV plugins dependency resolution
763 +  for plugin in plugins.split():
764 +    path = "../{}/packages.yaml".format(plugin)
765 +    if os.path.isfile(path):
766 +      f = open(path).read()
767 +      plugin_yaml = yaml.load(f)
768 +      new_packages = legacy_diff(to_legacy_pkglist(unresolved['packages']),
769 +        plugin_yaml['packages'], 'Plugin {0}'.format(plugin), arch)
770 +      unresolved['packages'] += from_legacy_pkglist(new_packages)
771 +
772 +  # Package list (reduced, i.e. no MOS deps, but with OPNFV plugin deps)
773 +  # FIXME: This helps work around packetary solving main deps from universe
774 +  if mirror_ubuntu_resolve_main_deps or mirror_ubuntu_merge is None:
775 +    write_cfg_file(cfg_p_ubuntu_main, unresolved)
776 +
777 +  # Mirror package list (full, including MOS/OPNFV plugin deps)
778 +  unresolved['packages'] += get_unres_pkgs(arch, cfg_m_mos)
779 +  write_cfg_file(cfg_p_ubuntu, unresolved)
780 +  do_partial_mirror(arch, cfg_m_ubuntu, cfg_p_ubuntu)
781 +  if mirror_ubuntu_resolve_main_deps or mirror_ubuntu_merge is None:
782 +    # Ubuntu[main] must be evaluated after Ubuntu
783 +    do_partial_mirror(arch, cfg_m_ubuntu_main, cfg_p_ubuntu_main)
784 +
785 +if mirror_ubuntu_merge is None:
786 +  shutil.move(mirror_ubuntu_tmp_path, mirror_ubuntu_path)
787 +else:
788 +  # Construct single-component mirror from all components
789 +  for arch in ubuntu_arch.split(' '):
790 +    cfg_pp_ubuntu = '{0}/ubuntu_{1}_packages_paths.yaml'.format(cfg_dir, arch)
791 +    # FIXME: We need scanpackages to omit older DEBs
792 +    # Inspired from http://askubuntu.com/questions/198474/
793 +    os.system('dpkg-scanpackages -a {0} {1} 2>/dev/null | '
794 +      'grep -e "^Filename:" | sed "s|Filename: |- file://|g" > {2}'
795 +        .format(arch, mirror_ubuntu_tmp_path, cfg_pp_ubuntu))
796 +    do_local_repo(arch, cfg_mm_ubuntu, cfg_pp_ubuntu)
797 +  shutil.rmtree(mirror_ubuntu_tmp_path)
798 diff --git a/deploy/config/dea_base.yaml b/deploy/config/dea_base.yaml
799 index ce81994..2520cf5 100644
800 --- a/deploy/config/dea_base.yaml
801 +++ b/deploy/config/dea_base.yaml
802 @@ -622,22 +622,10 @@ settings:
803          value:
804          - name: ubuntu
805            priority: null
806 -          section: main universe multiverse
807 +          section: main
808            suite: trusty
809            type: deb
810 -          uri: http://10.20.0.2:8080/mirrors/ubuntu/
811 -        - name: ubuntu-updates
812 -          priority: null
813 -          section: main universe multiverse
814 -          suite: trusty-updates
815 -          type: deb
816 -          uri: http://10.20.0.2:8080/mirrors/ubuntu/
817 -        - name: ubuntu-security
818 -          priority: null
819 -          section: main universe multiverse
820 -          suite: trusty-security
821 -          type: deb
822            uri: http://10.20.0.2:8080/mirrors/ubuntu/
823          - name: mos
824            priority: 1050
825            section: main restricted