Skip to content

Instantly share code, notes, and snippets.

@jcpowermac
Created October 9, 2014 12:30
Show Gist options
  • Select an option

  • Save jcpowermac/32176ceaa93d17297c9a to your computer and use it in GitHub Desktop.

Select an option

Save jcpowermac/32176ceaa93d17297c9a to your computer and use it in GitHub Desktop.
diff --git a/core/image_service/base.rb b/core/image_service/base.rb
index a38368d..e94bc93 100644
--- a/core/image_service/base.rb
+++ b/core/image_service/base.rb
@@ -94,7 +94,7 @@ module ProjectHanlon
logger.error e.message
return cleanup_on_failure(create_mount_success, create_imagepath_success, e.message)
end
-
+puts ">>>>> in base.rb add_image"
umount
[true, '']
end
diff --git a/core/image_service/vmware_hypervisor.rb b/core/image_service/vmware_hypervisor.rb
index 9e95357..ba8b39b 100644
--- a/core/image_service/vmware_hypervisor.rb
+++ b/core/image_service/vmware_hypervisor.rb
@@ -19,7 +19,9 @@ module ProjectHanlon
resp = super(src_image_path, lcl_image_path, extra)
if resp[0]
success, result_string = verify(lcl_image_path)
+puts "success: #{success}"
unless success
+puts "unless success"
logger.error result_string
return [false, result_string]
end
@@ -41,11 +43,21 @@ module ProjectHanlon
return [false, "ISO file structure is invalid"]
end
# and check some parameters from the files extracted from the ISO
+puts "before if"
+
+puts "image_path: #{image_path}"
+ osl_exist = File.exist?("#{image_path}/vmware-esx-base-osl.txt")
+ boot_cfg = File.exist?("#{image_path}/boot.cfg")
+puts "vmware-esx-base-osl.txt_exist: #{osl_exist}"
+puts "boot_cfg: #{boot_cfg}"
if File.exist?("#{image_path}/vmware-esx-base-osl.txt") && File.exist?("#{image_path}/boot.cfg")
begin
- @esxi_version = File.read("#{image_path}/vmware-esx-base-osl.txt").split("\n")[2].gsub("\r","")
- @boot_cfg = File.read("#{image_path}/boot.cfg")
+ @esxi_version = File.read("#{image_path}/vmware-esx-base-osl.txt", :encoding => 'ISO-8859-1').split("\n")[2].gsub("\r","")
+ @boot_cfg = File.read("#{image_path}/boot.cfg", :encoding => 'ISO-8859-1')
+puts "boot_cfg: #{@boot_cfg}"
+puts "esxi_version: #{@esxi_version}"
if @esxi_version && @boot_cfg
+puts "in if, so they actualy exist"
return [true, '']
end
# if we got here, could read the files but there wasn't anything in one or
@@ -55,10 +67,11 @@ module ProjectHanlon
[false, "Missing 'boot_cfg' in ISO"]
rescue => e
logger.debug e
+puts e.message
[false, e.message]
end
- [true, '']
else
+puts "does not look like an esxi iso"
logger.error "Does not look like an ESXi ISO"
[false, "Does not look like an ESXi ISO"]
end
diff --git a/core/model/base.rb b/core/model/base.rb
index 09bf228..6402f85 100644
--- a/core/model/base.rb
+++ b/core/model/base.rb
@@ -21,6 +21,7 @@ module ProjectHanlon
attr_accessor :counter
attr_accessor :log
attr_accessor :req_metadata_hash
+attr_accessor :opt_metadata_hash
# init
# @param hash [Hash]
@@ -32,6 +33,7 @@ module ProjectHanlon
@noun = "model"
@description = "Base model template"
@req_metadata_hash = {}
+@opt_metadata_hash = {}
@callback = {}
@current_state = :init
@node = nil
@@ -227,6 +229,7 @@ module ProjectHanlon
end
def web_create_metadata(provided_metadata)
+puts "DEBUG: web_create_metadata"
missing_metadata = []
rmd = req_metadata_hash
rmd.each_key do
@@ -249,9 +252,65 @@ module ProjectHanlon
end
end
- def cli_create_metadata
+def yaml_read_metadata(yaml_metadata_hash)
+#temp = metadata_hash.reject { |key| req_metadata_hash.keys.include?(key) }
+#pp ">>> temp #{temp.inspect}"
+#pp ">>> req_metadata_hash.keys #{req_metadata_hash.keys.inspect}"
+
+
+req_meta_vals = yaml_metadata_hash.select{ |key| req_metadata_hash.keys.include?(key) }
+
+req_meta_vals.each{ |key,value|
+ metadata = map_keys_to_symbols(req_metadata_hash[key])
+ key = key.to_sym if !key.is_a?(Symbol)
+#puts ">>> key = #{key}, value = #{value}"
+#puts ">>> validation = #{metadata[:validation]}"
+
+flag = set_metadata_value(key, value, metadata[:validation])
+if !flag
+ raise ProjectHanlon::Error::Slice::InvalidModelMetadata, "Invalid Metadata [#{key}:#{value}]"
+end
+
+#unless set_metadata_value(key, value, metadata[:validation])
+#flag = set_metadata_value(key, value, metadata[:validation])
+#puts ">>> flag #{flag}"
+#end
+}
+
+#puts ">>> req_meta_vals #{req_meta_vals.inspect}"
+
+optional_vals = yaml_metadata_hash.reject{ |key| req_metadata_hash.keys.include?(key) }
+
+
+optional_vals.each{ |key,value|
+#puts "before map_keys_to_symbols"
+#puts "opt_metadata_hash[#{key}] == #{opt_metadata_hash[key]}"
+ metadata = map_keys_to_symbols( opt_metadata_hash[key] )
+#puts "after"
+ key = key.to_sym if !key.is_a?(Symbol)
+ set_metadata_value(key, value, metadata[:validation])
+}
+
+#puts " >>> optional_vals #{optional_vals.inspect}"
+
+
+#pp ">>> symbol_keys #{metadata_hash.inspect}"
+
+temp_keys = req_metadata_hash.keys - yaml_metadata_hash.keys
+#puts ">>> temp_keys #{temp_keys.inspect}"
+
+end
+
+
+
+ def cli_create_metadata(yaml_metadata_hash)
puts "--- Building Model (#{name}): #{label}\n".yellow
- req_metadata_hash.each_key { |key|
+remaining_keys = yaml_read_metadata(yaml_metadata_hash)
+#puts ">>> result_metadata_hash #{remaining_keys.inspect}"
+
+# req_metadata_hash.each_key { |key|
+remaining_keys.each { |key|
+
metadata = map_keys_to_symbols(req_metadata_hash[key])
key = key.to_sym if !key.is_a?(Symbol)
flag = false
@@ -294,17 +353,43 @@ module ProjectHanlon
}
tmp
end
-
- def set_metadata_value(key, value, validation)
- regex = Regexp.new(validation)
- if regex =~ value
- self.instance_variable_set(key.to_sym, value)
- true
- else
- false
- end
+def set_metadata_value(key, value, validation)
+puts "DEBUG(#{self.name}): self.instance_variable_set(#{key.to_sym}, #{value})"
+ if value.is_a?(Array)
+ self.instance_variable_set(key.to_sym, value)
+ true
+ else
+ regex = Regexp.new(validation)
+ if regex =~ value
+ self.instance_variable_set(key.to_sym, value)
+ true
+ else
+ false
+ end
+ end
end
+#def set_metadata_value(key, value, validation)
+# regex = Regexp.new(validation)
+# if regex =~ value
+# self.instance_variable_set(key.to_sym, value)
+# true
+# else
+# false
+# end
+# end
+
+
+# def set_metadata_value(key, value, validation)
+# regex = Regexp.new(validation)
+ # if regex =~ value
+ # self.instance_variable_set(key.to_sym, value)
+ # true
+ # else
+ # false
+ # end
+ # end
+
def skip_quit_option
"(" + "SKIP".white + " to skip, " + "QUIT".red + " to cancel)"
end
diff --git a/core/model/vmware_esxi.rb b/core/model/vmware_esxi.rb
index d99fe8d..f16042c 100644
--- a/core/model/vmware_esxi.rb
+++ b/core/model/vmware_esxi.rb
@@ -47,6 +47,8 @@ module ProjectHanlon
@vcenter_name = nil
@vcenter_datacenter_path = nil
@vcenter_cluster_path = nil
+
+ @packages = nil
# Metadata
@req_metadata_hash = {
"@esx_license" => { :default => "",
@@ -99,7 +101,9 @@ module ProjectHanlon
:example => "ntp.hanlon.example.local",
:validation => '^[\w.]{3,}$',
:required => true,
- :description => "NTP server for node" },
+ :description => "NTP server for node" }
+ }
+@opt_metadata_hash = {
"@vcenter_name" => { :default => "",
:example => "vcenter01",
:validation => '^[\w.-]{3,}$',
@@ -114,7 +118,13 @@ module ProjectHanlon
:example => "Cluster01",
:validation => '^[a-zA-Z\d-]{3,}$',
:required => false,
- :description => "Optional for broker use: the vCenter Cluster to place ESXi node in" }
+ :description => "Optional for broker use: the vCenter Cluster to place ESXi node in" },
+"@packages" => { :default => "",
+ :example => "",
+ :validation => '',
+ :required => false,
+ :description => "Optional for broker use: the vCenter Cluster to place ESXi node in" }
+
}
diff --git a/core/slice/model.rb b/core/slice/model.rb
index daf30f5..34e6972 100644
--- a/core/slice/model.rb
+++ b/core/slice/model.rb
@@ -65,7 +65,16 @@ module ProjectHanlon
:description => 'The image UUID to use for the new model.',
:uuid_is => 'not_allowed',
:required => true
+ },
+ { :name => :optional_yaml,
+ :default => false,
+ :short_form => '-o',
+ :long_form => '--option YAML_FILE',
+ :description => 'Use optional yaml file to create model',
+ :uuid_is => 'not_allowed',
+ :required => false
}
+
],
:update => [
{ :name => :label,
@@ -161,6 +170,7 @@ module ProjectHanlon
# load the appropriate option items for the subcommand we are handling
option_items = command_option_data(:add)
command_hash = Hash[*@command_array]
+puts ">>> command_hash - #{command_hash.inspect}"
template_name = command_hash["-t"] || command_hash["--template"]
option_items = option_items.map { |option|
option[:name] == :image_uuid ? (option[:required] = false; option) : option
@@ -169,18 +179,42 @@ module ProjectHanlon
# subcommand (this method will return a UUID value, if present, and the
# options map constructed from the @commmand_array)
tmp, options = parse_and_validate_options(option_items, "hanlon model add (options...)", :require_all)
+#puts ">>> tmp - #{tmp.inspect}"
+#puts ">>> options - #{options.inspect}"
+
includes_uuid = true if tmp && tmp != "add"
# check for usage errors (the boolean value at the end of this method
# call is used to indicate whether the choice of options from the
# option_items hash must be an exclusive choice)
check_option_usage(option_items, options, includes_uuid, false)
+
+optional_yaml_file = options[:optional_yaml]
+#puts ">>> options_yaml_file #{optional_yaml_file}"
+
template = options[:template]
label = options[:label]
image_uuid = options[:image_uuid]
# use the arguments passed in to create a new model
model = get_model_using_template_name(options[:template])
raise ProjectHanlon::Error::Slice::InputError, "Invalid model template [#{options[:template]}] " unless model
- model.cli_create_metadata
+if optional_yaml_file
+metadata_hash = YAML.load(File.read(optional_yaml_file))
+#puts ">>> metadata_hash = #{metadata_hash.inspect}"
+
+
+# we need to modify the hash map from yaml to include the @ symbol to match req_metadata_hash keys
+
+metadata_hash.keys.each{|k| metadata_hash.store("@#{k}", metadata_hash.delete(k))}
+
+
+model.cli_create_metadata(metadata_hash)
+#metadata_hash.each { |key,value|
+#puts ">>> key = #{key}"
+#puts ">>> val = #{value}"
+#}
+else
+ model.cli_create_metadata({})
+end
# setup the POST (to create the requested policy) and return the results
uri = URI.parse @uri_string
body_hash = {
@@ -190,9 +224,20 @@ module ProjectHanlon
"req_metadata_hash" => model.req_metadata_hash
}
model.req_metadata_hash.each { |key, md_hash_value|
+puts "DEBUG(): req_metadata_has key == #{key}"
value = model.instance_variable_get(key)
body_hash[key] = value
}
+
+model.opt_metadata_hash.each { |key, md_hash_value|
+ value = model.instance_variable_get(key)
+
+puts "DEBUG(): opt_metadata_hash key == #{key}, value == #{value}"
+#puts "DEBUG: opt_metadata_hash key == #{key}"
+ body_hash[key] = value
+
+}
+
json_data = body_hash.to_json
result, response = hnl_http_post_json_data(uri, json_data, true)
if response.instance_of?(Net::HTTPBadRequest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment