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