Updated armband
[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  | 52 +++++++++++++++++++++-
12  1 file changed, 50 insertions(+), 2 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..a619f3f 100755
16 --- a/deployment/puppet/osnailyfacter/modular/astute/upload_cirros.rb
17 +++ b/deployment/puppet/osnailyfacter/modular/astute/upload_cirros.rb
18 @@ -52,11 +52,11 @@ def image_list
19    stdout = `glance --verbose image-list`
20    return_code = $?.exitstatus
21 -  images = []
22 +  images = Hash[]
23    stdout.split("\n").each do |line|
24      fields = line.split('|').map { |f| f.chomp.strip }
25      next if fields[1] == 'ID'
26      next unless fields[2]
27 -    images << {fields[2] => fields[6]}
28 +    images[fields[2]] = { :id => fields[1], :status => fields[6] }
29    end
30    {:images => images, :exit_code => return_code}
31  end
32 @@ -78,6 +78,16 @@ EOF
33    [ stdout, return_code ]
34  end
35  
36 +# Calls glance update-image with a given property and value
37 +# Supported properties: 'kernel-id', 'ramdisk-id'
38 +def update_image(image_id, property, value)
39 +  command = "/usr/bin/openstack image set --#{property}=#{value} #{image_id}"
40 +  puts command
41 +  stdout = `#{command}`
42 +  return_code = $?.exitstatus
43 +  [ stdout, return_code ]
44 +end
45 +
46  # check if Glance is online
47  # waited until the glance is started because when vCenter used as a glance
48  # backend launch may takes up to 1 minute.
49 @@ -133,7 +142,7 @@ end
50  # return true if image has been uploaded and active
51  def check_image(image)
52    list_of_images = image_list
53 -  if list_of_images[:exit_code] == 0 && list_of_images[:images].include?(image['img_name'] => "active")
54 +  if list_of_images[:exit_code] == 0 && list_of_images[:images].select { |k,v| k == image['img_name'] and v[:status] == "active" }.count > 0
55      return true
56    end
57    return false
58 @@ -142,7 +151,7 @@ end
59  # the first one
60  def cleanup_image(image)
61    list_of_images = image_list
62 -  unless list_of_images[:images].select { |img_hash| img_hash.key?(image['img_name']) }.empty?
63 +  unless list_of_images[:images].select { |img_hash, v| img_hash == image['img_name'] }.count == 0
64      delete_image(image['img_name'])
65    end
66  end
67 @@ -157,6 +166,41 @@ def delete_image(image_name)
68    [ stdout, return_code ]
69  end
70  
71 +# For each disk image try to find a kernel and initramfs images and
72 +# attach then to it via kernel_id and ramdisk_id glance properties.
73 +def connect_dependant_images(images)
74 +  # for each image
75 +  # get image id from glance
76 +  img_list = image_list
77 +  return_code = img_list[:exit_code]
78 +  if return_code == 0
79 +    images.each do |image|
80 +      img_list[:images].each do |k,v|
81 +        if k == image['img_name']
82 +          image['id'] = v[:id]
83 +        end
84 +      end
85 +    end
86 +    # for each image that is not in [aki, ari]
87 +    images.each do |image|
88 +      next if ['aki', 'ari'].include?(image['disk_format'])
89 +      images.each do |i|
90 +        # find aki/ari image whose name starts with this image's name
91 +        if i['img_name'].start_with?(image['img_name'])
92 +          ret = 0
93 +          if i['disk_format'] == 'aki'
94 +            _, ret = update_image(image['id'], 'kernel-id', i['id'])
95 +          elsif i['disk_format'] == 'ari'
96 +            _, ret = update_image(image['id'], 'ramdisk-id', i['id'])
97 +          end
98 +          return_code += ret
99 +        end
100 +      end
101 +    end
102 +  end
103 +  return return_code
104 +end
105 +
106  ########################
107  
108  wait_for_glance
109 @@ -180,6 +226,8 @@ if errors > 0
110      cleanup_image(image)
111    end
112    exit 1
113 +elsif connect_dependant_images(test_vm_images) > 0
114 +  exit 2
115  end
116
117  exit 0