Merge changes from topics 'YARDSTICK-875', 'YARDSTICK-874'
[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: "{{ 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     raw_imgfile_basename: "yardstick-{{ release }}-server.raw"
33   environment:
34     - PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/bin
35     - "{{ proxy_env }}"
36
37   tasks:
38     - group_by:
39         key: image_builder
40
41     - package: name=parted state=present
42
43     - set_fact:
44         imgfile: "{{ workspace }}/yardstick-image.img"
45
46     - set_fact:
47         mountdir: "{{ lookup('env', 'mountdir')|default('/mnt/yardstick', true) }}"
48
49     - set_fact:
50         raw_imgfile: "{{ workspace }}/{{ raw_imgfile_basename }}"
51
52   # cleanup non-lxd
53     - name: unmount all old mount points
54       mount:
55         name: "{{ item }}"
56         state: unmounted
57       with_items:
58         # order matters
59         - "{{ mountdir }}/proc"
60         - "{{ mountdir }}"
61         - "/mnt/{{ release }}"
62
63     - name: kpartx -dv to delete all image partition device nodes
64       command: kpartx -dv "{{ raw_imgfile }}"
65       ignore_errors: true
66
67     - name: delete {{ raw_imgfile }}
68       file:
69         path: "{{ raw_imgfile }}"
70         state: absent
71
72     # common
73     - name: remove {{ mountdir }}
74       file:
75         path: "{{ mountdir }}"
76         state: absent
77
78     # download-common
79     - name: remove {{ workspace }}
80       file:
81         path: "{{ workspace }}"
82         state: directory
83
84     - name: "fetch {{ image_url }} and verify "
85       fetch_url_and_verify:
86         url: "{{ image_url }}"
87         sha256url: "{{ sha256sums_url }}"
88         dest: "{{ image_dest }}"
89
90     - name: convert image to raw
91       command: "qemu-img convert {{ image_dest }} {{ raw_imgfile }}"
92
93     - name: resize image to allow for more VNFs
94       command: "qemu-img resize -f raw {{ raw_imgfile }} +2G"
95
96     - name: resize parition to allow for more VNFs
97       command: "parted -s -a optimal {{ raw_imgfile }} resizepart 1 100%"
98
99     - name: create mknod devices in chroot
100       command: "mknod -m 0660 /dev/loop{{ item }} b 7 {{ item }}"
101       args:
102         creates: "/dev/loop{{ item }}"
103       with_sequence: start=0 end=9
104       tags: mknod_devices
105
106     - name: find first partition device
107 #      command: kpartx -l "{{ loop_device }}"
108       command: kpartx -l "{{ raw_imgfile }}"
109       register: kpartx_res
110
111     - set_fact:
112         image_first_partition: "{{ kpartx_res.stdout_lines[0].split()[0] }}"
113
114     - set_fact:
115         # assume / is the first partition
116         image_first_partition_device: "/dev/mapper/{{ image_first_partition }}"
117
118     - name: use kpartx to create device nodes for the raw image loop device
119       # operate on the loop device to avoid /dev namespace missing devices
120       command: kpartx -avs "{{ raw_imgfile }}"
121
122     - name: parted dump raw image
123       command: parted "{{ raw_imgfile }}" print
124       register: parted_res
125
126     - debug:
127         var: parted_res
128         verbosity: 2
129
130     - name: use blkid to find filesystem type of first partition device
131       command: blkid -o value -s TYPE {{ image_first_partition_device }}
132       register: blkid_res
133
134     - set_fact:
135         image_fs_type: "{{ blkid_res.stdout.strip() }}"
136     - fail:
137         msg: "We only support ext4 image filesystems because we have to resize"
138       when: image_fs_type != "ext4"
139
140     - name: fsck the image filesystem
141       command: "e2fsck -y -f {{ image_first_partition_device  }}"
142
143     - name: resize filesystem to full partition size
144       command: resize2fs {{ image_first_partition_device }}
145
146     - name: fsck the image filesystem
147       command: "e2fsck -y -f {{ image_first_partition_device  }}"
148
149     - name: make tmp disposable fstab
150       command: mktemp --tmpdir fake_fstab.XXXXXXXXXX
151       register: mktemp_res
152
153     - set_fact:
154         fake_fstab: "{{ mktemp_res.stdout.strip() }}"
155
156     - name: mount first parition on image device
157       mount:
158         src: "{{ image_first_partition_device }}"
159         name: "{{ mountdir }}"
160         # fstype is required
161         fstype: "{{ image_fs_type }}"
162         # !!!!!!! this is required otherwise we add entries to /etc/fstab
163         # and prevent the system from booting
164         fstab: "{{ fake_fstab }}"
165         state: mounted
166
167     - name: mount chroot /proc
168       mount:
169         src: none
170         name: "{{ mountdir }}/proc"
171         fstype: proc
172         # !!!!!!! this is required otherwise we add entries to /etc/fstab
173         # and prevent the system from booting
174         fstab: "{{ fake_fstab }}"
175         state: mounted
176
177     - name: if arm copy qemu-aarch64-static into chroot
178       copy:
179         src: /usr/bin/qemu-aarch64-static
180         dest: "{{ mountdir }}/usr/bin"
181       when: 'YARD_IMG_ARCH == "arm64"'
182
183     - name: create ubuntu policy-rc.d workaround
184       copy:
185         content: "{{ '#!/bin/sh\nexit 101\n' }}"
186         dest: "{{ mountdir }}/usr/sbin/policy-rc.d"
187         mode: 0755
188       when: "target_os == 'Ubuntu'"
189
190     - name: add chroot as host
191       add_host:
192         name: "{{ mountdir }}"
193         groups: chroot_image,image_builder
194         connection: chroot
195         ansible_python_interpreter: /usr/bin/python3
196         # set this host variable here
197         nameserver_ip: "{{ ansible_dns.nameservers[0] }}"
198         image_type: vm
199
200 - name: include {{ img_modify_playbook }}
201   include: "{{ img_modify_playbook }}"
202
203 - hosts: localhost
204   tasks:
205     - name: convert image to image file
206       command: "qemu-img convert -c -o compat=0.10 -O qcow2 {{ raw_imgfile }} {{ imgfile }}"
207
208 - name: run post build tasks
209   include: post_build_yardstick_image.yml
210
211 - hosts: localhost
212
213   tasks:
214     - debug:
215         msg: "yardstick image = {{ imgfile }}"