Class: Dnsync::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/dnsync/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Cli

Returns a new instance of Cli.



17
18
19
20
# File 'lib/dnsync/cli.rb', line 17

def initialize(argv)
  @args         = argv.dup
  @program_name = File.basename($0)
end

Instance Attribute Details

#program_nameObject (readonly)

Returns the value of attribute program_name.



15
16
17
# File 'lib/dnsync/cli.rb', line 15

def program_name
  @program_name
end

Instance Method Details

#callObject



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dnsync/cli.rb', line 22

def call
  Configlet.prefix = 'dnsync'
  Configlet.munge(:noop) { |v| v == "true" }
  Configlet.munge(:monitor_frequency) { |v| v.present? ? v.to_i : v }

  Scrolls.single_line_exceptions = true

  read_env_from_file(File.expand_path("~/.dnsync.env"))
  read_env_from_file(File.expand_path("../../../.env", __FILE__))
  read_env_from_file('.env')

  opts = OptionParser.new do |opts|
    opts.banner = "usage: #{program_name} [options] <command> [<args>]"

    opts.separator ""
    opts.separator commands_help

    opts.separator ""
    opts.separator "Options:"

    opts.on("--dnsimple-email=EMAIL", "DNSimple email address") do |v|
      Configlet[:dnsimple_email] = v
    end
    opts.on("--dnsimple-token=TOKEN", "DNSimple token") do |v|
      Configlet[:dnsimple_token] = v
    end
    opts.on("--nsone-token=TOKEN", "NSONE token") do |v|
      Configlet[:nsone_token] = v
    end
    opts.on("--domain=DOMAIN", "Domain to synchronize") do |v|
      Configlet[:domain] = v
    end
    opts.on("--monitor-frequency=FREQUENCY", "Frequency to check DNSimple for updates") do |v|
      Configlet[:monitor_frequency] = v
    end
    opts.on("--status-port=PORT", "Port to run status HTTP server on") do |v|
      Configlet[:status_port] = v
    end
    opts.on("--noop", "Don't do any write operations") do |v|
      Configlet[:noop] = v.to_s
    end
    opts.on("-h", "--help", "This help message") do
      puts opts
      exit(1)
    end
  end
  @args = opts.order(@args)
  
  case command = @args.shift
  when 'dump'
    dump
  when 'diff'
    diff
  when 'sync'
    sync
  when 'monitor'
    monitor
  else
    puts "#{program_name}: '#{command}' is not a command. see '#{program_name} --help'."
    exit(1)
  end
  
  exit(0)
end

#commands_helpObject



87
88
89
90
91
92
93
94
# File 'lib/dnsync/cli.rb', line 87

def commands_help
  unindent("    The available commands are:\n         sync         Perform a one-time synchronization from DNSimple to NSONE\n         monitor      Perform continual synchronization from DNSimple to NSONE\n\n  EOF\nend\n")

#diffObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/dnsync/cli.rb', line 110

def diff
  nsone = Nsone.new(Configlet[:nsone_token], Configlet[:domain])
  dnsimple = Dnsimple.new(Configlet[:dnsimple_email], 
    Configlet[:dnsimple_token], Configlet[:domain])
  
  diff = ZoneDifference.new(nsone.zone, dnsimple.zone,
    %w(NS SOA))

  puts " ---- added ---- "
  pp diff.added
  
  puts " ---- removed ---- "
  pp diff.removed
  
  puts " ---- changed ---- "
  pp diff.changed
end

#dumpObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dnsync/cli.rb', line 96

def dump
  case command = @args.shift
  when 'nsone'
    nsone = Nsone.new(Configlet[:nsone_token], Configlet[:domain])
    records = nsone.zone
  else
    dnsimple = Dnsimple.new(Configlet[:dnsimple_email], 
      Configlet[:dnsimple_token], Configlet[:domain])
    records = dnsimple.zone
  end
  
  pp records
end

#monitorObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/dnsync/cli.rb', line 159

def monitor
  nsone = Nsone.new(Configlet[:nsone_token], Configlet[:domain])
  dnsimple = Dnsimple.new(Configlet[:dnsimple_email],
    Configlet[:dnsimple_token], Configlet[:domain])

  updater = RecurringZoneUpdater.new(dnsimple, nsone,
    Configlet[:monitor_frequency] || 10)
  updater.start

  if status_port = Configlet[:status_port]
    puts "Starting status server on #{status_port}"
    status = HttpStatus.new(status_port, updater)
    status.start
  end

  rd, wr = IO.pipe
  Thread.new do
    # Wait for a signal
    rd.read(1)
    updater.stop

    if status
      status.stop
    end
  end

  %w(QUIT HUP INT TERM).each do |sig|
    Signal.trap(sig) { wr.write('x') }
  end

  updater.join
  if status
    status.join
  end
end

#syncObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/dnsync/cli.rb', line 128

def sync
  nsone = Nsone.new(Configlet[:nsone_token], Configlet[:domain])
  dnsimple = Dnsimple.new(Configlet[:dnsimple_email],
    Configlet[:dnsimple_token], Configlet[:domain])

  nsone_zone, dnsimple_zone = nil

  Scrolls.log(:zone => Configlet[:domain], :from => :nsone) do
    nsone_zone = nsone.zone
  end

  Scrolls.log(:zone => Configlet[:domain], :from => :dnsimple) do
    dnsimple_zone = dnsimple.zone
  end

  diff = ZoneDifference.new(nsone_zone, dnsimple_zone,
    %w(NS SOA))

  if Configlet[:noop]
    puts "Would be: Adding: #{diff.added.length} Updating: #{diff.changed.length} Removing: #{diff.removed.length}"
  else
    updater = ZoneUpdater.new(diff, nsone)

    Scrolls.log(:zone => Configlet[:domain], :action => :updating, :to => :nsone,
      :adding => diff.added.length, :updating => diff.changed.length,
      :removing => diff.removed.length) do
      updater.call
    end
  end
end