Merge "Add yardstick report for each task"
[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: localhost
16
17   vars:
18     boot_modes:
19       'amd64': disk1
20       'arm64': uefi1
21     boot_mode: "{{ boot_modes[YARD_IMG_ARCH] }}"
22     image_filename: "{{ release }}-server-cloudimg-{{ YARD_IMG_ARCH }}-{{ boot_mode }}.img"
23     image_path: "{{ release }}/current/{{ image_filename }}"
24     host: "{{ lookup('env', 'HOST')|default('cloud-images.ubuntu.com', true)}}"
25     image_url: "{{ lookup('env', 'IMAGE_URL')|default('https://' ~ host ~ '/' ~ image_path, true) }}"
26     image_dest: "{{ workspace }}/{{ image_filename }}"
27     sha256sums_path: "{{ release }}/current/SHA256SUMS"
28     sha256sums_filename: "{{ sha256sums_path|basename }}"
29     sha256sums_url: "{{ lookup('env', 'SHA256SUMS_URL')|default('https://' ~ host ~ '/' ~ sha256sums_path, true) }}"
30
31     mountdir: "{{ lookup('env', 'mountdir')|default('/mnt/yardstick', true) }}"
32     workspace: "{{ lookup('env', 'workspace')|default('/tmp/workspace/yardstick', true) }}"
33     imgfile: "{{ workspace }}/yardstick-image.img"
34     raw_imgfile_basename: "yardstick-{{ release }}-server.raw"
35     raw_imgfile: "{{ workspace }}/{{ raw_imgfile_basename }}"
36   environment:
37     PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/bin
38
39   tasks:
40     - group_by:
41         key: image_builder
42
43     - package: name=parted state=present
44
45   # cleanup non-lxd
46     - name: unmount all old mount points
47       mount:
48         name: "{{ item }}"
49         state: unmounted
50       with_items:
51         # order matters
52         - "{{ mountdir }}/proc"
53         - "{{ mountdir }}"
54         - "/mnt/{{ release }}"
55
56     - name: kpartx -dv to delete all image partition device nodes
57       command: kpartx -dv "{{ raw_imgfile }}"
58       ignore_errors: true
59
60     - name: delete {{ raw_imgfile }}
61       file:
62         path: "{{ raw_imgfile }}"
63         state: absent
64
65     # common
66     - name: remove {{ mountdir }}
67       file:
68         path: "{{ mountdir }}"
69         state: absent
70
71     # download-common
72
73     - name: remove {{ workspace }}
74       file:
75         path: "{{ workspace }}"
76         state: directory
77
78     - name: "fetch {{ image_url }} and verify "
79       fetch_url_and_verify:
80         url: "{{ image_url }}"
81         sha256url: "{{ sha256sums_url }}"
82         dest: "{{ image_dest }}"
83
84 #    - get_url:
85 #        url: "{{ sha256sums_url }}"
86 #        force: yes
87 #        dest: "{{ workspace }}/{{ sha256sums_filename }}"
88
89     # must use wget to workaround ubuntu cloud SSL certs checking failures
90 #    - command: "curl -sS -o {{ workspace }}/{{ sha256sums_filename }} {{ sha256sums_url }}"
91
92
93 #    - command: cat "{{ workspace }}/{{ sha256sums_filename }}"
94 #      register: sha256sum_file
95 #
96 #    - set_fact:
97 #        image_sha256: "{{ sha256sum_file.stdout|regex_search('^([a-f0-9]+).*' ~ img ~ '$', '\\1', multiline=True) }}"
98
99 #    - get_url:
100 #        url: "{{ image_url }}"
101 #        force: yes
102 #        dest: "{{ workspace }}/{{ image_filename }}"
103 #        checksum: "sha256:{{ image_sha256 }}"
104 #        register: fetch_image_status
105 #        timeout: 300
106 #        retries: 2
107 #        until: fetch_image_status|succeeded
108
109 #    - name: "Fetch {{ image_url }}"
110 #    - command: "curl -sS -o {{ workspace }}/{{ image_filename }} {{ image_url }}"
111 #      register: fetch_image_status
112 #      timeout: 300
113 #      retries: 2
114 #      until: fetch_image_status|succeeded
115
116 #    - name: Verify sha256sum of downloaded image
117 #    - command: "sha256sum -c --ignore-missing {{ workspace }}/{{ sha256sums_filename }}"
118
119 #    - name: create loop devices
120 #      command: "mknod -m 660 /dev/loop{{ item }} b 7 {{ item }}"
121 #      args:
122 #        creates: "/dev/loop{{ item }}"
123 #      with_seq:
124 #        -
125
126     # download non-lxd
127
128     - name: convert image to raw
129       command: "qemu-img convert {{ image_dest }} {{ raw_imgfile }}"
130
131
132     # setup non-lxd
133
134 #     - shell: echo -e "d\\nn\\np\\n1\\n\\n\\nw" | parted -l "{{ raw_imgfile }}"
135 #    - parted:
136 #        device: "{{ raw_imgfile }}"
137 #        number: 1
138 #        state: present
139
140     - name: create mknod devices in chroot
141       command: "mknod -m 0660 /dev/loop{{ item }} b 7 {{ item }}"
142       args:
143         creates: "/dev/loop{{ item }}"
144       with_sequence: start=0 end=9
145       tags: mknod_devices
146
147 #    - command: losetup --show --partscan --find "{{ raw_imgfile }}"
148 #      register: loop_device_res
149 #
150 #    - debug:
151 #        var: loop_device_res
152 #        verbosity: 2
153 #
154 #    - set_fact:
155 #        loop_device: "{{ loop_device_res.stdout.strip() }}"
156 #
157 #    - wait_for:
158 #        path: "{{ loop_device }}"
159 #        state: present
160 #
161 #    - command: losetup
162 #    - command: dmsetup ls
163
164     - name: find first partition device
165 #      command: kpartx -l "{{ loop_device }}"
166       command: kpartx -l "{{ raw_imgfile }}"
167       register: kpartx_res
168
169     - set_fact:
170         image_first_partition: "{{ kpartx_res.stdout_lines[0].split()[0] }}"
171
172     - set_fact:
173         # assume / is the first partition
174         image_first_partition_device: "/dev/mapper/{{ image_first_partition }}"
175
176     - name: use kpartx to create device nodes for the raw image loop device
177       # operate on the loop device to avoid /dev namespace missing devices
178 #      command: kpartx -avs "{{ loop_device }}"
179       command: kpartx -avs "{{ raw_imgfile }}"
180
181     - name: parted dump raw image
182 #      command: parted "{{ loop_device }}" print
183       command: parted "{{ raw_imgfile }}" print
184       register: parted_res
185
186     - debug:
187         var: parted_res
188         verbosity: 2
189
190     - name: use blkid to find filesystem type of first partition device
191       command: blkid -o value -s TYPE {{ image_first_partition_device }}
192       register: blkid_res
193
194     - set_fact:
195         image_fs_type: "{{ blkid_res.stdout.strip() }}"
196
197     - name: make tmp disposable fstab
198       command: mktemp fake_fstab.XXXXXXXXXX
199       register: mktemp_res
200
201     - set_fact:
202         fake_fstab: "{{ mktemp_res.stdout.strip() }}"
203
204     - name: mount first parition on image device
205       mount:
206         src: "{{ image_first_partition_device }}"
207         name: "{{ mountdir }}"
208         # fstype is required
209         fstype: "{{ image_fs_type }}"
210         #fstab: "{{ fake_fstab }}"
211         state: mounted
212
213     - name: mount chroot /proc
214       mount:
215         src: none
216         name: "{{ mountdir }}/proc"
217         fstype: proc
218         #fstab: "{{ fake_fstab }}"
219         state: mounted
220
221     - name: if arm copy qemu-aarch64-static into chroot
222       copy:
223         src: /usr/bin/qemu-aarch64-static
224         dest: "{{ mountdir }}/usr/bin"
225       when: 'YARD_IMG_ARCH == "arm64"'
226
227
228     # setup lxd
229 #    - file: "path={{ mountdir }} state=directory"
230 #
231 #    - unarchive:
232 #        src: "{{ image_filename }}"
233 #        dest: "{{ mountdir }}"
234 #        remote_src: yes
235
236     # end setup lxd
237
238     # modify
239
240     - name: create ubuntu policy-rc.d workaround
241       copy:
242         content: "{{ '#!/bin/sh\nexit 101\n' }}"
243         dest: "{{ mountdir }}/usr/sbin/policy-rc.d"
244         mode: 0755
245       when: "target_os == 'Ubuntu'"
246
247     - name: set img_modify_playbook
248       set_fact:
249         img_modify_playbook: ubuntu_server_cloudimg_modify.yml
250
251     - debug:
252         var: img_modify_playbook
253         verbosity: 2
254
255     - name: add chroot as host
256       add_host:
257         name: "{{ mountdir }}"
258         groups: chroot_image,image_builder
259         connection: chroot
260         ansible_python_interpreter: /usr/bin/python3
261         # set this host variable here
262         nameserver_ip: "{{ ansible_dns.nameservers[0] }}"
263
264 - name: include {{ img_modify_playbook }}
265   include: "{{ img_modify_playbook }}"
266
267 - name: run post build tasks
268   include: post_build_yardstick_image.yml