Class: VagrantPlugins::CommandDns::Action::Route53::Create

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-command-dns/action/route53/create.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Create

Returns a new instance of Create.



10
11
12
13
# File 'lib/vagrant-command-dns/action/route53/create.rb', line 10

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new('vagrant_command_dns::action::route53::create')
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
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
# File 'lib/vagrant-command-dns/action/route53/create.rb', line 15

def call(env)
  if env[:machine].state.id != :running
    raise Errors::MachineState, state: 'running'
  end

  env[:record_map].each do |hostname, ip|
    if env[:machine].config.dns.route53_skip_aliases.include?(hostname)
      env[:ui].info(I18n.t('vagrant_command_dns.command.route53.skip_alias', hostname: hostname))
      next
    else
      @logger.info("Checking for existing '#{hostname}' Route53 record...")

      record_name = hostname + '.' unless hostname.end_with?('.')

      begin
        zone = env[:route53].zones.get(env[:machine].config.dns.route53_zone_id)
        record = zone.records.get(record_name)
      rescue Excon::Errors::SocketError
        env[:ui].error(I18n.t('vagrant_command_dns.command.route53.fog_error',
                              message: 'Unable to reach AWS. Are you connected to the internet?'))
      rescue Fog::DNS::AWS::Error => err
        env[:ui].error(I18n.t('vagrant_command_dns.command.route53.fog_error',
                         message: err.message))
      end

      if record.nil? || record.attributes[:name] != record_name
        @logger.info('Creating Route53 record...')
        begin
          new_record = zone.records.new({
            :value => ip,
            :name  => record_name,
            :type  => 'A',
            :ttl   => '60'
          })
          new_record.save
        rescue Fog::DNS::AWS::Error => err
          env[:ui].error(I18n.t('vagrant_command_dns.command.route53.fog_error',
                           message: err.message))
        end
        env[:ui].info(I18n.t('vagrant_command_dns.command.route53.create_success',
                        ip: ip, hostname: hostname))

      elsif record.attributes[:value][0] == ip
        env[:ui].info(I18n.t('vagrant_command_dns.command.route53.create_exists_match',
                        ip: ip, hostname: hostname))

      elsif record.attributes[:value][0] != ip
        env[:ui].warn(I18n.t('vagrant_command_dns.command.route53.create_exists_conflict',
                        ip: ip, hostname: hostname))

      end
    end
  end

  @app.call(env)
end