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