modules/mu/mommacat.rb in cloud-mu-3.0.2 vs modules/mu/mommacat.rb in cloud-mu-3.1.0
- old
+ new
@@ -283,11 +283,11 @@
if !File.exist?(deploy_dir+"/private_key")
@private_key, @public_key = createDeployKey
end
MU.log "Creating deploy secret for #{MU.deploy_id}"
@deploy_secret = Password.random(256)
- if !@original_config['scrub_mu_isms']
+ if !@original_config['scrub_mu_isms'] and !@no_artifacts
credsets.each_pair { |cloud, creds|
creds.uniq!
cloudclass = Object.const_get("MU").const_get("Cloud").const_get(cloud)
creds.each { |credentials|
cloudclass.writeDeploySecret(@deploy_id, @deploy_secret, credentials: credentials)
@@ -659,13 +659,13 @@
# Balancers).
# @param name [String]: The shorthand name of the resource, usually the value of the "name" field in an Mu resource declaration.
# @param max_length [Integer]: The maximum length of the resulting resource name.
# @param need_unique_string [Boolean]: Whether to forcibly append a random three-character string to the name to ensure it's unique. Note that this behavior will be automatically invoked if the name must be truncated.
# @param scrub_mu_isms [Boolean]: Don't bother with generating names specific to this deployment. Used to generate generic CloudFormation templates, amongst other purposes.
- # @param allowed_chars [Regexp]: A pattern of characters that are legal for this resource name, such as +/[a-zA-Z0-9-]/+
+ # @param disallowed_chars [Regexp]: A pattern of characters that are illegal for this resource name, such as +/[^a-zA-Z0-9-]/+
# @return [String]: A full name string for this resource
- def getResourceName(name, max_length: 255, need_unique_string: false, use_unique_string: nil, reuse_unique_string: false, scrub_mu_isms: @original_config['scrub_mu_isms'], allowed_chars: nil)
+ def getResourceName(name, max_length: 255, need_unique_string: false, use_unique_string: nil, reuse_unique_string: false, scrub_mu_isms: @original_config['scrub_mu_isms'], disallowed_chars: nil)
if name.nil?
raise MuError, "Got no argument to MU::MommaCat.getResourceName"
end
if @appname.nil? or @environment.nil? or @timestamp.nil? or @seed.nil?
MU.log "getResourceName: Missing global deploy variables in thread #{Thread.current.object_id}, using bare name '#{name}' (appname: #{@appname}, environment: #{@environment}, timestamp: #{@timestamp}, seed: #{@seed}, deploy_id: #{@deploy_id}", MU::WARN, details: caller
@@ -684,23 +684,26 @@
basename = @appname.upcase + "-" + @environment.upcase + "-" + @timestamp + "-" + @seed.upcase + "-" + name.upcase
if scrub_mu_isms
basename = @appname.upcase + "-" + @environment.upcase + name.upcase
end
- subchar = if allowed_chars
- if !"-".match(allowed_chars)
- if "_".match(allowed_chars)
+ subchar = if disallowed_chars
+ if "-".match(disallowed_chars)
+ if !"_".match(disallowed_chars)
"_"
else
""
end
else
"-"
end
end
- basename.gsub!(allowed_chars, subchar) if allowed_chars
+ if disallowed_chars
+ basename.gsub!(disallowed_chars, subchar) if disallowed_chars
+ end
+ attempts = 0
begin
if (basename.length + reserved) > max_length
MU.log "Stripping name down from #{basename}[#{basename.length.to_s}] (reserved: #{reserved.to_s}, max_length: #{max_length.to_s})", MU::DEBUG
if basename == @appname.upcase + "-" + @seed.upcase + "-" + name.upcase
# If we've run out of stuff to strip, truncate what's left and
@@ -709,21 +712,23 @@
# hostnames.
basename = name.upcase + "-" + @appname.upcase
basename.slice!((max_length-(reserved+3))..basename.length)
basename.sub!(/-$/, "")
basename = basename + "-" + @seed.upcase
- basename.gsub!(allowed_chars, subchar) if allowed_chars
+ basename.gsub!(disallowed_chars, subchar) if disallowed_chars
else
# If we have to strip anything, assume we've lost uniqueness and
# will have to compensate with #genUniquenessString.
need_unique_string = true
reserved = 4
basename.sub!(/-[^-]+-#{@seed.upcase}-#{Regexp.escape(name.upcase)}$/, "")
basename = basename + "-" + @seed.upcase + "-" + name.upcase
- basename.gsub!(allowed_chars, subchar) if allowed_chars
+ basename.gsub!(disallowed_chars, subchar) if disallowed_chars
end
end
+ attempts += 1
+ raise MuError, "Failed to generate a reasonable name getResourceName(#{name}, max_length: #{max_length.to_s}, need_unique_string: #{need_unique_string.to_s}, use_unique_string: #{use_unique_string.to_s}, reuse_unique_string: #{reuse_unique_string.to_s}, scrub_mu_isms: #{scrub_mu_isms.to_s}, disallowed_chars: #{disallowed_chars})" if attempts > 10
end while (basename.length + reserved) > max_length
# Finally, apply our short random differentiator, if it's needed.
if need_unique_string
# Preferentially use a requested one, if it's not already in use.
@@ -744,11 +749,11 @@
}
end
else
muname = basename
end
- muname.gsub!(allowed_chars, subchar) if allowed_chars
+ muname.gsub!(disallowed_chars, subchar) if disallowed_chars
return muname
end
@@ -1666,17 +1671,21 @@
# Fun times: if we specified a habitat, which we may also have done by
# its shorthand sibling name, let's... call ourselves first to make sure
# we're fishing for the right thing.
if habitat
- MU.log indent+"findLitterMate(#{argstring}): Attempting to resolve habitat name #{habitat}", loglevel
- realhabitat = findLitterMate(type: "habitat", name: habitat, debug: debug, credentials: credentials, indent: indent+" ")
- if realhabitat and realhabitat.mu_name
- MU.log indent+"findLitterMate: Resolved habitat name #{habitat} to #{realhabitat.mu_name}", loglevel, details: [realhabitat.mu_name, realhabitat.cloud_id, realhabitat.config.keys]
- habitat = realhabitat.cloud_id
- elsif debug
- MU.log indent+"findLitterMate(#{argstring}): Failed to resolve habitat name #{habitat}", MU::WARN
+ if habitat.is_a?(MU::Config::Ref) and habitat.id
+ habitat = habitat.id
+ else
+ MU.log indent+"findLitterMate(#{argstring}): Attempting to resolve habitat name #{habitat}", loglevel
+ realhabitat = findLitterMate(type: "habitat", name: habitat, debug: debug, credentials: credentials, indent: indent+" ")
+ if realhabitat and realhabitat.mu_name
+ MU.log indent+"findLitterMate: Resolved habitat name #{habitat} to #{realhabitat.mu_name}", loglevel, details: [realhabitat.mu_name, realhabitat.cloud_id, realhabitat.config.keys]
+ habitat = realhabitat.cloud_id
+ elsif debug
+ MU.log indent+"findLitterMate(#{argstring}): Failed to resolve habitat name #{habitat}", MU::WARN
+ end
end
end
@kitten_semaphore.synchronize {
@@ -1688,11 +1697,11 @@
end
MU.log indent+"START findLitterMate(#{argstring}), caller: #{caller[2]}", loglevel, details: @kittens[type].keys.map { |hab| hab.to_s+": "+@kittens[type][hab].keys.join(", ") }
matches = []
@kittens[type].each { |habitat_group, sib_classes|
- next if habitat and habitat_group != habitat
+ next if habitat and habitat_group != habitat and !habitat_group.nil?
sib_classes.each_pair { |sib_class, data|
virtual_name = nil
if !has_multiples and data and !data.is_a?(Hash) and data.config and data.config.is_a?(Hash) and data.config['virtual_name'] and name == data.config['virtual_name']
virtual_name = data.config['virtual_name']
@@ -1738,9 +1747,10 @@
MU.log indent+"(credentials.nil? or data.credentials.nil? or credentials[#{credentials.class.name}:#{credentials}] == data.credentials[#{data.credentials.class.name}:#{data.credentials}])", loglevel, details: (credentials.nil? or data.credentials.nil? or credentials == data.credentials).to_s
if (name.nil? or sib_class == name.to_s or virtual_name == name.to_s) and
(cloud_id.nil? or cloud_id.to_s == data_cloud_id) and
(credentials.nil? or data.credentials.nil? or credentials.to_s == data.credentials.to_s)
+ MU.log indent+"OUTER MATCH PASSED, NEED !created_only (#{created_only.to_s}) or !data_cloud_id.nil? (#{data_cloud_id})", loglevel, details: (cloud_id.nil? or cloud_id == data_cloud_id).to_s
if !created_only or !data_cloud_id.nil?
MU.log indent+"SINGLE MATCH findLitterMate(#{argstring})", loglevel, details: [data.mu_name, data_cloud_id, data.config.keys]
matches << data
end
end