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