drain runner queue and undo cancel_join_thread
[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     - name: remove {{ workspace }}
77       file:
78         path: "{{ workspace }}"
79         state: directory
80
81     - name: "fetch {{ image_url }} and verify "
82       fetch_url_and_verify:
83         url: "{{ image_url }}"
84         sha256url: "{{ sha256sums_url }}"
85         dest: "{{ image_dest }}"
86
87     - name: convert image to raw
88       command: "qemu-img convert {{ image_dest }} {{ raw_imgfile }}"
89
90     - name: resize image to allow for more VNFs
91       command: "qemu-img resize -f raw {{ raw_imgfile }} +2G"
92
93     - name: resize parition to allow for more VNFs
94       command: "parted -s -a optimal {{ raw_imgfile }} resizepart 1 100%"
95
96     - name: create mknod devices in chroot
97       command: "mknod -m 0660 /dev/loop{{ item }} b 7 {{ item }}"
98       args:
99         creates: "/dev/loop{{ item }}"
100       with_sequence: start=0 end=9
101       tags: mknod_devices
102
103     - name: find first partition device
104 #      command: kpartx -l "{{ loop_device }}"
105       command: kpartx -l "{{ raw_imgfile }}"
106       register: kpartx_res
107
108     - set_fact:
109         image_first_partition: "{{ kpartx_res.stdout_lines[0].split()[0] }}"
110
111     - set_fact:
112         # assume / is the first partition
113         image_first_partition_device: "/dev/mapper/{{ image_first_partition }}"
114
115     - name: use kpartx to create device nodes for the raw image loop device
116       # operate on the loop device to avoid /dev namespace missing devices
117       command: kpartx -avs "{{ raw_imgfile }}"
118
119     - name: parted dump raw image
120       command: parted "{{ raw_imgfile }}" print
121       register: parted_res
122
123     - debug:
124         var: parted_res
125         verbosity: 2
126
127     - name: use blkid to find filesystem type of first partition device
128       command: blkid -o value -s TYPE {{ image_first_partition_device }}
129       register: blkid_res
130
131     - set_fact:
132         image_fs_type: "{{ blkid_res.stdout.strip() }}"
133     - fail:
134         msg: "We only support ext4 image filesystems because we have to resize"
135       when: image_fs_type != "ext4"
136
137     - name: fsck the image filesystem
138       command: "e2fsck -y -f {{ image_first_partition_device  }}"
139
140     - name: resize filesystem to full partition size
141       command: resize2fs {{ image_first_partition_device }}
142
143     - name: fsck the image filesystem
144       command: "e2fsck -y -f {{ image_first_partition_device  }}"
145
146     - name: make tmp disposable fstab
147       command: mktemp --tmpdir fake_fstab.XXXXXXXXXX
148       register: mktemp_res
149
150     - set_fact:
151         fake_fstab: "{{ mktemp_res.stdout.strip() }}"
152
153     - name: mount first parition on image device
154       mount:
155         src: "{{ image_first_partition_device }}"
156         name: "{{ mountdir }}"
157         # fstype is required
158         fstype: "{{ image_fs_type }}"
159         # !!!!!!! this is required otherwise we add entries to /etc/fstab
160         # and prevent the system from booting
161         fstab: "{{ fake_fstab }}"
162         state: mounted
163
164     - name: mount chroot /proc
165       mount:
166         src: none
167         name: "{{ mountdir }}/proc"
168         fstype: proc
169         # !!!!!!! this is required otherwise we add entries to /etc/fstab
170         # and prevent the system from booting
171         fstab: "{{ fake_fstab }}"
172         state: mounted
173
174     - name: if arm copy qemu-aarch64-static into chroot
175       copy:
176         src: /usr/bin/qemu-aarch64-static
177         dest: "{{ mountdir }}/usr/bin"
178       when: 'YARD_IMG_ARCH == "arm64"'
179
180     - name: create ubuntu policy-rc.d workaround
181       copy:
182         content: "{{ '#!/bin/sh\nexit 101\n' }}"
183         dest: "{{ mountdir }}/usr/sbin/policy-rc.d"
184         mode: 0755
185       when: "target_os == 'Ubuntu'"
186
187     - name: set img_modify_playbook
188       set_fact:
189         img_modify_playbook: ubuntu_server_cloudimg_modify.yml
190
191     - debug:
192         var: img_modify_playbook
193         verbosity: 2
194
195     - name: add chroot as host
196       add_host:
197         name: "{{ mountdir }}"
198         groups: chroot_image,image_builder
199         connection: chroot
200         ansible_python_interpreter: /usr/bin/python3
201         # set this host variable here
202         nameserver_ip: "{{ ansible_dns.nameservers[0] }}"
203         image_type: vm
204
205 - name: include {{ img_modify_playbook }}
206   include: "{{ img_modify_playbook }}"
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 = {{ raw_imgfile }}"