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