modules/mu/mommacat/naming.rb in cloud-mu-3.1.6 vs modules/mu/mommacat/naming.rb in cloud-mu-3.2.0

- old
+ new

@@ -17,10 +17,20 @@ # MommaCat is in charge of managing metadata about resources we've created, # as well as orchestrating amongst them and bootstrapping nodes outside of # the normal synchronous deploy sequence invoked by *mu-deploy*. class MommaCat + # Lookup table to translate the word "habitat" back to its + # provider-specific jargon + HABITAT_SYNONYMS = { + "AWS" => "account", + "CloudFormation" => "account", + "Google" => "project", + "Azure" => "subscription", + "VMWare" => "sddc" + } + # Given a cloud provider's native descriptor for a resource, make some # reasonable guesses about what the thing's name should be. def self.guessName(desc, resourceclass, cloud_id: nil, tag_value: nil) if desc.respond_to?(:tags) and desc.tags.is_a?(Array) and @@ -45,10 +55,78 @@ try end end + # Given a piece of a BoK resource descriptor Hash, come up with shorthand + # strings to give it a name for human readers. If nothing reasonable can be + # extracted, returns nil. + # @param obj [Hash] + # @param array_of [String] + # @param habitat_translate [String] + # @return [Array<String,nil>] + def self.getChunkName(obj, array_of = nil, habitat_translate: nil) + return [nil, nil] if obj.nil? + if [String, Integer, Boolean].include?(obj.class) + return [obj, nil] + end + obj_type = array_of || obj['type'] + obj_name = obj['name'] || obj['id'] || obj['mu_name'] || obj['cloud_id'] + + name_string = if obj_name + if obj_type + "#{obj_type}[#{obj_name}]" + else + obj_name.dup + end + else + found_it = nil + using = nil + ["entity", "role"].each { |subtype| + if obj[subtype] and obj[subtype].is_a?(Hash) + found_it = if obj[subtype]["id"] + obj[subtype]['id'].dup + elsif obj[subtype]["type"] and obj[subtype]["name"] + "#{obj[subtype]['type']}[#{obj[subtype]['name']}]" + end + break + end + } + found_it + end + if name_string + name_string.gsub!(/\[.+?\](\[.+?\]$)/, '\1') + if habitat_translate and HABITAT_SYNONYMS[habitat_translate] + name_string.sub!(/^habitats?\[(.+?)\]/i, HABITAT_SYNONYMS[habitat_translate]+'[\1]') + end + end + + location_list = [] + + location = if obj['project'] + obj['project'] + elsif obj['habitat'] and (obj['habitat']['id'] or obj['habitat']['name']) + obj['habitat']['name'] || obj['habitat']['id'] + else + hab_str = nil + ['projects', 'habitats'].each { |key| + + if obj[key] and obj[key].is_a?(Array) + location_list = obj[key].sort.map { |p| + (p["name"] || p["id"]).gsub(/^.*?[^\/]+\/([^\/]+)$/, '\1') + } + hab_str = location_list.join(", ") + name_string.gsub!(/^.*?[^\/]+\/([^\/]+)$/, '\1') if name_string + break + end + } + hab_str + end + + [name_string, location, location_list] + end + # Generate a three-character string which can be used to unique-ify the # names of resources which might potentially collide, e.g. Windows local # hostnames, Amazon Elastic Load Balancers, or server pool instances. # @return [String]: A three-character string consisting of two alphnumeric # characters (uppercase) and one number. @@ -216,20 +294,21 @@ # Make sure the given node has proper DNS entries, /etc/hosts entries, # SSH config entries, etc. # @param server [MU::Cloud::Server]: The {MU::Cloud::Server} we'll be setting up. # @param sync_wait [Boolean]: Whether to wait for DNS to fully synchronize before returning. - def self.nameKitten(server, sync_wait: false) + def self.nameKitten(server, sync_wait: false, no_dns: false) node, config, _deploydata = server.describe mu_zone = nil # XXX GCP! - if MU::Cloud::AWS.hosted? and !MU::Cloud::AWS.isGovCloud? + if !no_dns and MU::Cloud::AWS.hosted? and !MU::Cloud::AWS.isGovCloud? zones = MU::Cloud::DNSZone.find(cloud_id: "platform-mu") mu_zone = zones.values.first if !zones.nil? end + if !mu_zone.nil? - MU::Cloud::DNSZone.genericMuDNSEntry(name: node, target: server.canonicalIP, cloudclass: MU::Cloud::Server, sync_wait: sync_wait) + MU::Cloud::DNSZone.genericMuDNSEntry(name: node.gsub(/[^a-z0-9!"\#$%&'\(\)\*\+,\-\/:;<=>\?@\[\]\^_`{\|}~\.]/, '-').gsub(/--|^-/, ''), target: server.canonicalIP, cloudclass: MU::Cloud::Server, sync_wait: sync_wait) else MU::Master.addInstanceToEtcHosts(server.canonicalIP, node) end ## TO DO: Do DNS registration of "real" records as the last stage after the groomer completes