Merge "Rebase for "Improve reliability of upload_cirros""
[armband.git] / patches / fuel-library / 0006-upload_cirros-Add-direct-kernel-boot-support.patch
1 From: Stanislaw Kardach <stanislaw.kardach@caviumnetworks.com>
2 Date: Tue, 15 Mar 2016 15:01:34 +0100
3 Subject: [PATCH] upload_cirros: Add direct kernel boot support.
4
5 AArch64 currently only supports direct kernel boot, so add the
6 functionality of uploading and connecting kernel and initramfs
7 images (disk formats `AKI` and `ARI`).
8
9 Signed-off-by: Stanislaw Kardach <stanislaw.kardach@cavium.com>
10 ---
11  .../osnailyfacter/modular/astute/upload_cirros.rb  | 53 ++++++++++++++++++++--
12  1 file changed, 50 insertions(+), 3 deletions(-)
13
14 diff --git a/deployment/puppet/osnailyfacter/modular/astute/upload_cirros.rb b/deployment/puppet/osnailyfacter/modular/astute/upload_cirros.rb
15 index f0441b0..cefc3ed 100755
16 --- a/deployment/puppet/osnailyfacter/modular/astute/upload_cirros.rb
17 +++ b/deployment/puppet/osnailyfacter/modular/astute/upload_cirros.rb
18 @@ -56,7 +56,7 @@ def image_list
19      fields = line.split('|').map { |f| f.chomp.strip }
20      next if fields[1] == 'ID'
21      next unless fields[2]
22 -    images << {fields[2] => fields[6]}
23 +    images << {fields[2] => { :id => fields[1], :status => fields[6]}}
24    end
25    {:images => images, :exit_code => return_code}
26  end
27 @@ -78,6 +78,15 @@ EOF
28    [ stdout, return_code ]
29  end
30  
31 +# Calls glance update-image with a given property and value
32 +def update_image(image_id, property, value)
33 +  command = "/usr/bin/glance image-update --#{property} #{value} #{image_id}"
34 +  puts command
35 +  stdout = `#{command}`
36 +  return_code = $?.exitstatus
37 +  [ stdout, return_code ]
38 +end
39 +
40  # check if Glance is online
41  # waited until the glance is started because when vCenter used as a glance
42  # backend launch may takes up to 1 minute.
43 @@ -133,7 +142,7 @@ end
44  # return true if image has been uploaded and active
45  def check_image(image)
46    list_of_images = image_list
47 -  if list_of_images[:exit_code] == 0 && list_of_images[:images].include?(image['img_name'] => "active")
48 +  if list_of_images[:exit_code] == 0 && list_of_images[:images].select { |k,v| k == image['img_name'] and v[:status] == "active" }
49      return true
50    end
51    return false
52 @@ -157,6 +166,43 @@ def delete_image(image_name)
53    [ stdout, return_code ]
54  end
55  
56 +# For each disk image try to find a kernel and initramfs images and
57 +# attach then to it via kernel_id and ramdisk_id glance properties.
58 +def connect_dependant_images(images)
59 +  # for each image
60 +  # get image id from glance
61 +  img_list = image_list
62 +  return_code = img_list[:exit_code]
63 +  if return_code == 0
64 +    images.each do |image|
65 +      img_list[:images].each do |k,v|
66 +        if k == image['img_name']
67 +          image['id'] = v[:id]
68 +        end
69 +      end
70 +    end
71 +    # for each image that is not in [aki, ari]
72 +    images.each do |image|
73 +      next if ['aki', 'ari'].include?(image['disk_format'])
74 +      images.each do |i|
75 +        # find aki/ari image whose name starts with this image's name
76 +        if i['img_name'].start_with?(image['img_name'])
77 +          ret = 0
78 +          if i['disk_format'] == 'aki'
79 +            _, ret = update_image(image['id'], 'property',
80 +                                        "kernel_id=#{i['id']}")
81 +          elsif i['disk_format'] == 'ari'
82 +            _, ret = update_image(image['id'], 'property',
83 +                                        "ramdisk_id=#{i['id']}")
84 +          end
85 +          return_code += ret
86 +        end
87 +      end
88 +    end
89 +  end
90 +  return return_code
91 +end
92 +
93  ########################
94  
95  wait_for_glance
96 @@ -167,7 +213,8 @@ test_vm_images.each do |image|
97    # retry upload 5 times with a 1 minute sleep between tries
98    5.times.each do |retries|
99      if upload_image(image)
100 -      success = true
101 +      if connect_dependant_images(test_vm_images) == 0
102 +        success = true
103        break
104      end
105      sleep 60