From 7eca0912128a3b3aa8a02e083a5cd1ba0874820b Mon Sep 17 00:00:00 2001 From: Daniel Clavijo Coca Date: Fri, 15 Feb 2019 09:50:03 -0600 Subject: [PATCH] F #1684: Make LXD marketplace create VM templates --- .../remotes/linuxcontainers/monitor | 137 ++++++++++-------- 1 file changed, 77 insertions(+), 60 deletions(-) diff --git a/src/market_mad/remotes/linuxcontainers/monitor b/src/market_mad/remotes/linuxcontainers/monitor index f6bcc0a2cc..80ba3182a9 100755 --- a/src/market_mad/remotes/linuxcontainers/monitor +++ b/src/market_mad/remotes/linuxcontainers/monitor @@ -21,22 +21,38 @@ require 'uri' require 'json' require 'base64' require 'rexml/document' +require 'time' #------------------------------------------------------------------------------- # #------------------------------------------------------------------------------- class LinuxContainersMarket + #--------------------------------------------------------------------------- # Default Configuration parameters for the Driver #--------------------------------------------------------------------------- DEFAULTS = { - :url => 'https://images.linuxcontainers.org', - :sizemb => 2560, - :fs => 'ext4', + :url => 'https://images.linuxcontainers.org', + :sizemb => 1024, + :fs => 'ext4', :format => 'raw', - :agent => 'OpenNebula' + :agent => 'OpenNebula' } + TEMPLATE = " +CPU = \"1\" +MEMORY = \"768\" +LXD_SECURITY_PRIVILEGED = \"true\" +GRAPHICS = [ + LISTEN =\"0.0.0.0\", + TYPE =\"vnc\" +] +CONTEXT = [ + NETWORK =\"YES\", + SSH_PUBLIC_KEY =\"$USER[SSH_PUBLIC_KEY]\", + SET_HOSTNAME =\"$NAME\" + ]" + #--------------------------------------------------------------------------- # Configuration varibales # :url of linuxcontainers market place @@ -50,10 +66,7 @@ class LinuxContainersMarket @options.merge!(options) version_path = File.dirname(__FILE__) + '/../../VERSION' - - if File.exists? version_path - @options[:agent] = "OpenNebula #{File.read(version_path)}" - end + @options[:agent] = "OpenNebula #{File.read(version_path)}" if File.exist? version_path end # Get container information @@ -81,48 +94,46 @@ class LinuxContainersMarket req['User-Agent'] = @options[:agent] response = Net::HTTP.start(uri.hostname, uri.port, p_host, p_port, - :use_ssl => uri.scheme == 'https') { |http| + :use_ssl => uri.scheme == 'https') do |http| http.request(req) - } - - if response.is_a? Net::HTTPSuccess - return 0, response.body - else - puts response.code.to_i - puts response.msg - return response.code.to_i, response.msg end + + return 0, response.body if response.is_a? Net::HTTPSuccess + + puts response.code.to_i + puts response.msg + [response.code.to_i, response.msg] end # Get the list of appliances - def get_appliances() + def get_appliances first_level = '/images/' rc, body = get(first_level) return rc, body if rc != 0 - distros = body.scan(/a href="([a-z].*\/)">/) - tree = Hash.new + distros = body.scan(%r{a href="([a-z].*/)">}) + tree = {} distros.each do |distro| rc, body = get(first_level + distro[0]) next if rc != 0 - version = body.scan(/a href="(.*\/)">/) - version.shift #Remove first entry ("Parent Directory") + version = body.scan(%r{a href="(.*/)">}) + version.shift # Remove first entry ("Parent Directory") - version_path = Hash.new + version_path = {} version.each do |version| - path = "#{first_level}#{distro[0]}#{version[0]}amd64/default/" + path = "#{first_level}#{distro[0]}#{version[0]}amd64/default/" rc, body = get(path) next if rc != 0 - release_dates = body.scan(/a href="(.*\/)">/) + release_dates = body.scan(%r{a href="(.*/)">}) - #Previous release_dates array leaves a point in the html page + # Previous release_dates array leaves a point in the html page release_date = release_dates.last[0] version_path[version[0]] = "#{path}#{release_date}rootfs.tar.xz" end @@ -130,41 +141,37 @@ class LinuxContainersMarket tree[distro[0]] = version_path end - appstr = "" + appstr = '' #----------------------------------------------------------------------- # Generate the container app information #----------------------------------------------------------------------- - tree.each { |distro, value| - value.each { |version, path| + tree.each do |distro, value| + value.each do |version, path| + source = app_url(path) + description = 'Downloaded from https://images.linuxcontainers.org' + name = "#{distro[0...-1]}_#{version[0...-1]} - LXD" - id = "-1" - source = app_url(path) + data = { + 'NAME' => name, 'SOURCE' => source.to_s, 'FORMAT' => 'raw', + 'IMPORT_ID' => '-1', 'ORIGIN_ID' => '-1', 'TYPE' => 'IMAGE', + 'PUBLISHER' => 'linuxcontainers.org', 'VERSION' => '1.0', + 'DESCRIPTION' => description, 'REGTIME' => app_time(path), + 'TAGS' => '', 'SIZE' => @options[:sizemb] + } - description = "Downloaded from https://images.linuxcontainers.org" + tmpl = '' - tmpl = "" + data.each {|key, value| print_var(tmpl, key, value) } - print_var(tmpl, "NAME", "#{distro[0...-1]}_#{version[0...-1]} - LXD") - print_var(tmpl, "SOURCE", "#{source}") - print_var(tmpl, "IMPORT_ID", id) - print_var(tmpl, "ORIGIN_ID", "-1") - print_var(tmpl, "TYPE", "IMAGE") - print_var(tmpl, "PUBLISHER", "linuxcontainers.org") - print_var(tmpl, "FORMAT", "raw") - print_var(tmpl, "DESCRIPTION", description) - print_var(tmpl, "VERSION", "1.0") - print_var(tmpl, "TAGS", "") - print_var(tmpl, "REGTIME", "1523362012") - print_var(tmpl, "SIZE", @options[:sizemb]) + tmpl64 = '' + print_var(tmpl64, 'DRIVER', 'raw') + print_var(tmpl, 'APPTEMPLATE64', Base64.strict_encode64(tmpl64)) + print_var(tmpl, 'VMTEMPLATE64', Base64.strict_encode64(TEMPLATE)) - tmpl64 = "" - print_var(tmpl64, "DRIVER", "raw") - print_var(tmpl, "APPTEMPLATE64", Base64::strict_encode64(tmpl64)) - - appstr << "APP=\"#{Base64::strict_encode64(tmpl)}\"\n" - } - } + appstr << "APP=\"#{Base64.strict_encode64(tmpl)}\"\n" + end + end appstr end @@ -183,6 +190,16 @@ class LinuxContainersMarket "#{@options[:fs]}&format=#{@options[:format]}\\\"" end + # Returns build date based on image path + def app_time(path) + m1 = 'amd64/default/./' + m2 = '/rootfs.tar.xz' + + buildate = path[/#{m1}(.*?)#{m2}/m, 1] + buildate = DateTime.strptime(buildate, '%Y%m%d_%H:%M') + buildate.to_time.to_i + end + # Print variable in an APP template def print_var(str, name, val) return if val.nil? @@ -190,6 +207,7 @@ class LinuxContainersMarket str << "#{name}=\"#{val}\"\n" end + end ################################################################################ @@ -201,17 +219,16 @@ end begin options = {} - drv_message = Base64::decode64(ARGV[0]) - + drv_message = Base64.decode64(ARGV[0]) doc = REXML::Document.new(drv_message).root - set_option(options, doc, :url, 'MARKETPLACE/TEMPLATE/ENDPOINT') - set_option(options, doc, :sizemb, 'MARKETPLACE/TEMPLATE/IMAGE_SIZE_MB') - set_option(options, doc, :fs, 'MARKETPLACE/TEMPLATE/FILESYSTEM') - set_option(options, doc, :format, 'MARKETPLACE/TEMPLATE/FORMAT') + pre = 'MARKETPLACE/TEMPLATE' + + data = { :url => "#{pre}/ENDPOINT", :sizemb => "#{pre}/IMAGE_SIZE_MB", + :fs => "#{pre}/FILESYSTEM", :format => "#{pre}/FORMAT" } + + data.each {|key, value| set_option(options, doc, key, value) } puts LinuxContainersMarket.new(options).get_appliances - rescue Exception end -