Class: Fhcap::Tasks::Misc::CreateDNSRecord

Inherits:
TaskBase
  • Object
show all
Defined in:
lib/fhcap/tasks/misc/create_dns_record.rb

Instance Attribute Summary collapse

Attributes inherited from TaskBase

#config, #options, #thor, #verbose

Instance Method Summary collapse

Methods inherited from TaskBase

#ask_config, #color_pad, #exit_with_error, #set_color, #table_header, #table_row, #with_progress

Methods included from KnifeHelper

#chef_server_config_for, #chef_server_config_hash_for, #chef_server_names, #knife_config, #knife_config_dir, #knife_config_file_for

Methods included from ProvidersHelper

#provider_availability_zones, #provider_config, #provider_credentials, #provider_for, #provider_names, #provider_names_for, #provider_regions, #provider_type, #providers_config

Methods included from ReposHelper

#find_cluster, #find_cluster_pwds, #find_cluster_pwds_with_repo, #find_cluster_with_repo, #find_data_bag, #find_data_bag_item, #find_environment, #find_repo_item, #find_role, #get_cookbook_deps, #get_cookbook_meta, #get_cookbook_versions, #get_cookbooks, #get_entry_dependencies, #get_modified_cookbooks, #is_dirty?, #modified?, #repo_cfg, #repo_clusters_dir, #repo_cookbook_paths, #repo_dir, #repo_names, #repo_paths, #repos_config, #repos_dir, #run_inside_repo_dir

Methods included from FhcapHelper

#cluster_template_names

Constructor Details

#initialize(options) ⇒ CreateDNSRecord

Returns a new instance of CreateDNSRecord.



15
16
17
18
19
20
21
# File 'lib/fhcap/tasks/misc/create_dns_record.rb', line 15

def initialize(options)
  super
  @domain = options[:domain]
  @alias_target = options[:'alias-target']
  @ipaddress = options[:ipaddress]
  @ttl = options[:ttl] || 300
end

Instance Attribute Details

#alias_target ⇒ Object (readonly)

Returns the value of attribute alias_target.



13
14
15
# File 'lib/fhcap/tasks/misc/create_dns_record.rb', line 13

def alias_target
  @alias_target
end

#domain ⇒ Object (readonly)

Returns the value of attribute domain.



13
14
15
# File 'lib/fhcap/tasks/misc/create_dns_record.rb', line 13

def domain
  @domain
end

#ipaddress ⇒ Object (readonly)

Returns the value of attribute ipaddress.



13
14
15
# File 'lib/fhcap/tasks/misc/create_dns_record.rb', line 13

def ipaddress
  @ipaddress
end

#ttl ⇒ Object (readonly)

Returns the value of attribute ttl.



13
14
15
# File 'lib/fhcap/tasks/misc/create_dns_record.rb', line 13

def ttl
  @ttl
end

Instance Method Details

#run ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fhcap/tasks/misc/create_dns_record.rb', line 23

def run
  thor.say "CreateDNSRecord: name = #{domain}, ipaddress = #{ipaddress}, ttl = #{ttl}", :yellow

  host_domain = PublicSuffix.parse(domain).domain
  provider = provider_for(host_domain)

  if provider
    begin
      cfg = provider_config(provider)
      region = provider_regions(provider).keys.first
      creds = cfg[:credentials]
      client = Aws::Route53::Client.new(
          region: region,
          access_key_id: creds[:'aws-access-key'],
          secret_access_key: creds[:'aws-secret-key']
      )
      resp = client.list_hosted_zones
      zone = resp.hosted_zones.reject { |z| !(z.name =~ /#{host_domain}/) }.first

      if zone
        thor.say "Found zone id: #{zone.id}, name: #{zone.name}"
        resp = client.list_resource_record_sets({
                                                    hosted_zone_id: zone.id,
                                                    start_record_name: domain,
                                                    start_record_type: "A",
                                                    max_items: 1
                                                })
        record_set = resp.resource_record_sets.reject { |z| !(z.name =~ /^#{domain}/) }.first
        if record_set
          thor.say "Found existing record set, id: name: #{record_set.name}, type: #{record_set.type}, record_set: #{record_set.resource_records || 'None'}, alias_target: #{record_set.alias_target || 'None'}\n"
        else
          change_request = {
              :action => 'CREATE',
              :resource_record_set => {
                  :name => domain,
                  :type => "A"
              }
          }

          if @alias_target
            change_request[:resource_record_set].merge!(
                {
                    alias_target: {
                        evaluate_target_health: false
                    }.merge!(@alias_target)
                }
            )
          else
            change_request[:resource_record_set].merge!(
                {
                    :ttl => ttl,
                    :resource_records => [{:value => ipaddress}]
                }
            )
          end

          client.change_resource_record_sets({
                                                 :hosted_zone_id => zone.id,
                                                 :change_batch => {
                                                     :changes => [change_request]
                                                 }
                                             })
        end
      else
        thor.say_status 'error', "Can't get hosted zone for '#{host_domain}'", :red
      end
    rescue Aws::Route53::Errors::ServiceError => e
      thor.say_status 'error', e.message, :red
    end
  else
    thor.say_status 'error', "No DNS provider configured for '#{domain}'", :red
  end
end