Merge "Upload config files for barometer collectd container"
[yardstick.git] / ansible / build_yardstick_image.yml
1 # Copyright (c) 2017 Intel Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 ---
15 - hosts: jumphost
16
17   vars:
18     boot_modes:
19       'amd64': disk1
20       'arm64': uefi1
21     boot_mode: "{{ boot_modes[YARD_IMG_ARCH] }}"
22     image_filename:
23       'xenial': "{{ release }}-server-cloudimg-{{ YARD_IMG_ARCH }}-{{ boot_mode }}.img"
24       'bionic': "{{ release }}-server-cloudimg-{{ YARD_IMG_ARCH }}.img"
25     image_path: "{{ release }}/current/{{ image_filename[release] }}"
26     host: "{{ lookup('env', 'HOST')|default('cloud-images.ubuntu.com', true)}}"
27     image_url: "{{ lookup('env', 'IMAGE_URL')|default('https://' ~ host ~ '/' ~ image_path, true) }}"
28     image_dest: "{{ workspace }}/{{ image_filename[release] }}"
29     sha256sums_path: "{{ release }}/current/SHA256SUMS"
30     sha256sums_filename: "{{ sha256sums_path|basename }}"
31     sha256sums_url: "{{ lookup('env', 'SHA256SUMS_URL')|default('https://' ~ host ~ '/' ~ sha256sums_path, true) }}"
32
33     workspace: "{{ lookup('env', 'workspace')|default('/tmp/workspace/yardstick', true) }}"
34     raw_imgfile_basename: "yardstick-{{ release }}-server.raw"
35     growpart_package:
36       RedHat: cloud-utils-growpart
37       Debian: cloud-guest-utils
38   environment:
39     - PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/bin
40     - "{{ proxy_env }}"
41
42   tasks:
43     - group_by:
44         key: image_builder
45
46     - package: name=parted state=present
47     - package: name=kpartx state=present
48     - package: name="{{ growpart_package[ansible_os_family] }}" state=present
49
50     - set_fact:
51         imgfile: "{{ normal_image_file }}"
52       when: img_property == "normal"
53
54     - set_fact:
55         imgfile: "{{ nsb_image_file }}"
56       when: img_property == "nsb"
57
58     - set_fact:
59         mountdir: "{{ lookup('env', 'mountdir')|default('/mnt/yardstick', true) }}"
60
61     - set_fact:
62         raw_imgfile: "{{ workspace }}/{{ raw_imgfile_basename }}"
63
64     # cleanup non-lxd
65     - name: unmount all old mount points
66       mount:
67         name: "{{ item }}"
68         state: unmounted
69       with_items:
70         # order matters
71         - "{{ mountdir }}/proc"
72         - "{{ mountdir }}"
73         - "/mnt/{{ release }}"
74
75     - name: kpartx -dv to delete all image partition device nodes
76       command: kpartx -dv "{{ raw_imgfile }}"
77       ignore_errors: true
78
79     - name: Debug dump loop devices
80       command: losetup -a
81       ignore_errors: true
82
83     - name: delete loop devices for image file
84       # use this because kpartx -dv will fail if raw_imgfile was delete
85       # but in theory we could have deleted file still attached to loopback device?
86       # use grep because of // and awk
87       shell: losetup -O NAME,BACK-FILE | grep "{{ raw_imgfile_basename }}" | awk '{ print $1 }' | xargs -l1 losetup -v -d
88       ignore_errors: true
89
90     - name: Debug dump loop devices again
91       command: losetup -a
92       ignore_errors: true
93
94     - name: delete {{ raw_imgfile }}
95       file:
96         path: "{{ raw_imgfile }}"
97         state: absent
98
99     # common
100     - name: remove {{ mountdir }}
101       file:
102         path: "{{ mountdir }}"
103         state: absent
104
105     # download-common
106     - name: remove {{ workspace }}
107       file:
108         path: "{{ workspace }}"
109         state: directory
110
111     - name: "fetch {{ image_url }} and verify "
112       fetch_url_and_verify:
113         url: "{{ image_url }}"
114         sha256url: "{{ sha256sums_url }}"
115         dest: "{{ image_dest }}"
116
117     - name: convert image to raw
118       command: "qemu-img convert {{ image_dest }} {{ raw_imgfile }}"
119
120     - name: resize image to allow for more VNFs
121       command: "qemu-img resize -f raw {{ raw_imgfile }} +2G"
122
123     - name: resize parition to allow for more VNFs
124       # use growpart because maybe it handles GPT better than parted
125       command: growpart {{ raw_imgfile }}  1
126
127     - name: create mknod devices in chroot
128       command: "mknod -m 0660 /dev/loop{{ item }} b 7 {{ item }}"
129       args:
130         creates: "/dev/loop{{ item }}"
131       with_sequence: start=0 end=9
132       tags: mknod_devices
133
134     - name: find first partition device
135       command: kpartx -l "{{ raw_imgfile }}"
136       register: kpartx_res
137
138     - set_fact:
139         image_first_partition: "{{ kpartx_res.stdout_lines[0].split()[0] }}"
140
141     - set_fact:
142         # assume / is the first partition
143         image_first_partition_device: "/dev/mapper/{{ image_first_partition }}"
144
145     - name: use kpartx to create device nodes for the raw image loop device
146       # operate on the loop device to avoid /dev namespace missing devices
147       command: kpartx -avs "{{ raw_imgfile }}"
148
149     - name: parted dump raw image
150       command: parted "{{ raw_imgfile }}" print
151       register: parted_res
152
153     - debug:
154         var: parted_res
155         verbosity: 2
156
157     - name: use blkid to find filesystem type of first partition device
158       command: blkid -o value -s TYPE {{ image_first_partition_device }}
159       register: blkid_res
160
161     - set_fact:
162         image_fs_type: "{{ blkid_res.stdout.strip() }}"
163     - fail:
164         msg: "We only support ext4 image filesystems because we have to resize"
165       when: image_fs_type != "ext4"
166
167     - name: fsck the image filesystem
168       command: "e2fsck -y -f {{ image_first_partition_device  }}"
169
170     - name: resize filesystem to full partition size
171       command: resize2fs {{ image_first_partition_device }}
172
173     - name: fsck the image filesystem
174       command: "e2fsck -y -f {{ image_first_partition_device  }}"
175
176     - name: make tmp disposable fstab
177       command: mktemp --tmpdir fake_fstab.XXXXXXXXXX
178       register: mktemp_res
179
180     - set_fact:
181         fake_fstab: "{{ mktemp_res.stdout.strip() }}"
182
183     - name: mount first parition on image device
184       mount:
185         src: "{{ image_first_partition_device }}"
186         name: "{{ mountdir }}"
187         # fstype is required
188         fstype: "{{ image_fs_type }}"
189         # !!!!!!! this is required otherwise we add entries to /etc/fstab
190         # and prevent the system from booting
191         fstab: "{{ fake_fstab }}"
192         state: mounted
193
194     - name: mount chroot /proc
195       mount:
196         src: none
197         name: "{{ mountdir }}/proc"
198         fstype: proc
199         # !!!!!!! this is required otherwise we add entries to /etc/fstab
200         # and prevent the system from booting
201         fstab: "{{ fake_fstab }}"
202         state: mounted
203
204     - name: if arm copy qemu-aarch64-static into chroot
205       copy:
206         src: /usr/bin/qemu-aarch64-static
207         dest: "{{ mountdir }}/usr/bin"
208       when: 'YARD_IMG_ARCH == "arm64"'
209
210     - name: create ubuntu policy-rc.d workaround
211       copy:
212         content: "{{ '#!/bin/sh\nexit 101\n' }}"
213         dest: "{{ mountdir }}/usr/sbin/policy-rc.d"
214         mode: 0755
215       when: "target_os == 'Ubuntu'"
216
217     - name: add chroot as host
218       add_host:
219         name: "{{ mountdir }}"
220         groups: chroot_image,image_builder
221         connection: chroot
222         ansible_python_interpreter: /usr/bin/python3
223         # set this host variable here
224         nameserver_ip: "{{ ansible_dns.nameservers[0] }}"
225         image_type: vm
226
227 - name: include ubuntu_server_cloudimg_modify.yml
228   include: ubuntu_server_cloudimg_modify.yml
229   when: img_property == "normal"
230
231 - name: include ubuntu_server_cloudimg_modify_samplevnfs.yml
232   include: ubuntu_server_cloudimg_modify_samplevnfs.yml
233   when: img_property == "nsb"
234
235 - hosts: localhost
236   tasks:
237     - name: convert image to image file
238       command: "qemu-img convert -c -o compat=0.10 -O qcow2 {{ raw_imgfile }} {{ imgfile }}"
239
240 - name: run post build tasks
241   include: post_build_yardstick_image.yml
242
243 - hosts: localhost
244
245   tasks:
246     - debug:
247         msg: "yardstick image = {{ imgfile }}"