Module: OvhDnsup::Cli
- Defined in:
- lib/ovh_dnsup/cli.rb
Overview
The command-line interface.
Class Method Summary collapse
- .authorize ⇒ Object
-
.get_interface_address(interface:, version: nil) ⇒ Object
Get the IP address of an interface.
-
.interfaces ⇒ Object
Lists all local interfaces and their IP addresses.
- .list ⇒ Object
- .load_config ⇒ Object
- .login ⇒ Object
- .logout ⇒ Object
- .register ⇒ Object
-
.run ⇒ Object
Run the command-line interface.
- .save_config ⇒ Object
- .sessions ⇒ Object
- .unregister ⇒ Object
- .update ⇒ Object
- .usage ⇒ Object
Class Method Details
.authorize ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/ovh_dnsup/cli.rb', line 188 def self. parser = OptionParser.new do |opts| opts. = 'Usage: ovh_dnsup authorize domain subdomain token_file' end parser.parse! if ARGV.length != 3 puts 'Invalid number of arguments' puts parser return end domain, subdomain, token_file = ARGV load_config map = @api.subdomain_type_map(domain, subdomain) if map.empty? puts "Subdomain has no A or AAAA record." return end up = DomainUpdater.new(endpoint: @api.endpoint, application_key: @api.application_key, application_secret: @api.application_secret) up.login(domain: domain, subdomain: subdomain, type_map: map) File.open(token_file, 'w') do |of| of.write(up.state.to_json) end end |
.get_interface_address(interface:, version: nil) ⇒ Object
Get the IP address of an interface
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/ovh_dnsup/cli.rb', line 333 def self.get_interface_address(interface:, version: nil) ipv4 = [] ipv6 = [] Socket.getifaddrs.each do |ifaddr| if ifaddr.name == interface if ifaddr.addr.ipv4? ipv4.push(ifaddr.addr.ip_address) end if ifaddr.addr.ipv6? ipv6.push(ifaddr.addr.ip_address) end end end # filter local addresses ipv6.select! { |addr| !(/^(fc|fd|fe8|fe9|fea|feb)/ =~ addr) } if !ipv6.empty? && (version == nil || version == 6) return ipv6[0] elsif !ipv4.empty? && (version == nil || version == 4) return ipv4[0] else raise 'No suitable address found.' end end |
.interfaces ⇒ Object
Lists all local interfaces and their IP addresses.
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/ovh_dnsup/cli.rb', line 360 def self.interfaces parser = OptionParser.new do |opts| opts. = 'Usage: ovh_dnsup interfaces' end parser.parse! if ARGV.length != 0 puts 'Invalid number of arguments' puts parser return end interfaces = Hash.new { |h, k| h[k] = [] } Socket.getifaddrs.each do |a| if a.addr.ip? interfaces[a.name].push(a.addr) end end interfaces.each do |k,v| puts k v.each { |i| puts " #{i.ip_address}" } end end |
.list ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/ovh_dnsup/cli.rb', line 173 def self.list parser = OptionParser.new do |opts| opts. = 'Usage: ovh_dnsup list domain' end parser.parse! if ARGV.length != 1 puts 'Invalid number of arguments' puts parser return end load_config @api.list_zone ARGV[0] end |
.load_config ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/ovh_dnsup/cli.rb', line 59 def self.load_config begin conf = File.open(@conf_fn, 'r') { |fp| JSON.parse(fp.read) } rescue puts "Could not load configuration" conf = {} end @api = DomainManager.new(state: conf) end |
.login ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/ovh_dnsup/cli.rb', line 142 def self.login parser = OptionParser.new do |opts| opts. = 'Usage: ovh_dnsup login' end parser.parse! if ARGV.length != 0 puts 'Invalid number of arguments' puts parser return end load_config @api.login save_config end |
.logout ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/ovh_dnsup/cli.rb', line 158 def self.logout parser = OptionParser.new do |opts| opts. = 'Usage: ovh_dnsup logout' end parser.parse! if ARGV.length != 0 puts 'Invalid number of arguments' puts parser return end load_config @api.logout end |
.register ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ovh_dnsup/cli.rb', line 73 def self.register parser = OptionParser.new do |opts| opts. = 'Usage: ovh_dnsup register [options]' end parser.parse! if ARGV.length != 0 puts 'Invalid number of arguments' puts parser return end print "Endpoint (#{OvhApi.endpoints.join(', ')}): " endpoint = gets.strip if !OvhApi.endpoint_url(endpoint) puts 'Invalid endpoint' return end puts " Please visit\n\n \#{OvhApi.endpoint_url(endpoint)}createApp/\n\n and create an application. Afterwards, come back and answer the\n questions below.\n EOS\n\n print 'Application key: '\n application_key = gets().strip\n print 'Application secret: '\n application_secret = gets().strip\n\n @api = DomainManager.new(endpoint: endpoint,\n application_key: application_key,\n application_secret: application_secret)\n save_config\nend\n" |
.run ⇒ Object
Run the command-line interface.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ovh_dnsup/cli.rb', line 24 def self.run @conf_fn = File.join(Dir.home, '.ovh_dnsup.conf') command = ARGV.shift if %w{register unregister login logout list authorize update interfaces sessions}.include? command self.send command.to_sym() else usage puts "Invalid command #{command}" end end |
.save_config ⇒ Object
69 70 71 |
# File 'lib/ovh_dnsup/cli.rb', line 69 def self.save_config File.open(@conf_fn, 'w') { |fp| fp.write(@api.state.to_json) } end |
.sessions ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/ovh_dnsup/cli.rb', line 303 def self.sessions load_config params = {} parser = OptionParser.new do |opts| opts. = 'Usage: ovh_dnsup sessions' opts.on('-a', '--all', 'List all sessions') opts.on('-d', '--delete ID', 'Delete session') opts.on('--delete-all', 'Delte all sessions') end parser.parse!(into: params) if ARGV.length != 0 puts 'Invalid number of arguments' puts parser return end if params[:delete] @api.delete_session(params[:delete].to_i) elsif params["delete-all".to_sym] @api.delete_all_sessions() else @api.list_sessions(all: params[:all]) end end |
.unregister ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/ovh_dnsup/cli.rb', line 113 def self.unregister load_config params = {} parser = OptionParser.new do |opts| opts. = "Usage: ovh_dnsup unregister [-a|--all]" opts.on('-a', '--all', 'Unregister all applications.') end parser.parse!(into: params) if ARGV.length != 0 puts 'Invalid number of arguments' puts parser return end puts 'All sessions of unregistered applications become invalid. To continue, please type "YES"' if gets.strip != 'YES' puts 'Abort' return end @api.unregister(all: params[:all]) if params[:all] puts "All applications have been unregistered" else puts "This application has been unregistered" end end |
.update ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/ovh_dnsup/cli.rb', line 218 def self.update = "Usage: ovh_dnsup update [options] token_file" params = {} parser = OptionParser.new do |opts| opts. = opts.on("--ipv6", "Force IPv6") opts.on("--ipv4", "Force IPv4") opts.on("--ip IP", "The IP address to set") opts.on("--if INTERFACE", "The interface") opts.on("-d", "--daemon", "Run continuously") opts.on("-v", "--verbose", "Run verbosely") opts.on("--delay SECONDS", "Time in seconds between checks") end parser.parse!(into: params) if ARGV.length != 1 puts "Invalid number of arguments" puts parser return end token_file, = ARGV delay = params[:delay] ? params[:delay] : 60 if params[:ipv6] version = 6 elsif params[:ipv4] version = 4 end if !params[:ip] && !params[:if] puts "Either provide IP address of interface name" return end if params[:ip] && params[:if] puts "You cannot provide IP and interface!" return end if params[:daemon] && !params[:if] puts "Daemon mode requires an interface to be given" return end state = File.open(token_file, 'r') { |fp| JSON.parse(fp.read) } @up = DomainUpdater.new(state: state) ip = nil running = true while running begin if params[:ip] new_ip = params[:ip] else new_ip = get_interface_address(interface: params[:if], version: version) end if new_ip != ip puts "Setting new IP #{new_ip}" if params[:verbose] @up.update new_ip ip = new_ip if !params[:daemon] running = false end else puts "IP not changed" if params[:verbose] end rescue Interrupt puts "Interrupted" if params[:verbose] running = false rescue Exception => e puts "WARNING: #{e.to_s}" end begin sleep delay if running rescue Interrupt running = false end end end |
.usage ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/ovh_dnsup/cli.rb', line 38 def self.usage puts " Usage: ovh_dnsup command [arguments...]\n\n OVH DNS updater\n\n Commands:\n\n register Register application with the API.\n unregister Unregister (the) application(s).\n login Login to manage the DNS updaters.\n logout Logout.\n list List a DNS zone.\n authorize Authorize a DNS updater.\n update Perform DNS updates.\n sessions List all authorized updaters.\n interfaces List the local network interfaces.\n\n EOS\nend\n" |