Skip to content

Commit

Permalink
reimplement command line interface with GLI
Browse files Browse the repository at this point in the history
  • Loading branch information
mikegreiling committed Apr 23, 2015
1 parent 74b33d8 commit 10175e7
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 33 deletions.
1 change: 1 addition & 0 deletions aws-eni.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency "gli", "~> 2.5"
spec.add_dependency "aws-sdk", "~> 2.0"

spec.add_development_dependency "bundler", "~> 1.7"
Expand Down
137 changes: 104 additions & 33 deletions bin/aws-eni
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
#!/usr/bin/env ruby

require 'gli'
require 'aws-eni'

begin
case ARGV[0]
include GLI::App

program_desc 'Manage and sync local network config with AWS Elastic Network Interfaces'

version Aws::ENI::VERSION

subcommand_option_handling :normal
arguments :strict
sort_help :manually

# global options

desc 'Display all system commands and warnings'
switch [:v,:verbose], negatable: false

pre do |opt|
Aws::ENI::IFconfig.verbose = opt[:verbose]
true
end

# commands

# list command
when 'list'
Aws::ENI.list(ARGV[1]).each do |interface|
desc 'List current interface configuration'
long_desc %{
List information about a set of interfaces including interface id, interface
name, MAC address, and a list of primary and secondary ip addresses along with
any public ips associated with them.
Use the optional filter argument to limit the listing to interfaces with a
matching name, interface id, subnet id, or MAC address (default 'all').
}
arg 'filter | all (default)', :optional
command [:list,:ls] do |c|
c.action do |global,opts,args|
args.delete('all')
Aws::ENI.list(args.first).each do |interface|
print "#{interface[:name]}:"
print "\tID #{interface[:interface_id]}"
print " HWaddr #{interface[:hwaddr]}"
Expand All @@ -21,44 +51,85 @@ begin
end
end
puts "\ninterface config is out of sync" if Aws::ENI.configure(nil, dry_run: true) > 0
end
end

desc 'Configure network interfaces'
long_desc %{
Syncronize configuration for a set of interfaces to match their configuration
on AWS by managing secondary ips, routes, and rules.
# sync command
when 'config', 'configure'
if Aws::ENI.configure(ARGV[1]) != 0
Use the optional filter argument to limit this action to interfaces with a
matching name, interface id, subnet id, or MAC address (default 'all').
}
arg 'filter | all (default)', :optional
command [:config,:conf] do |c|
c.action do |global,opts,args|
args.delete('all')
if Aws::ENI.configure(args.first) != 0
puts 'synchronized interface config'
else
puts 'network interface config already in sync'
end
end
end

# sync command
when 'deconfig', 'deconfigure'
Aws::ENI.deconfigure(ARGV[1])
desc 'Remove custom configuration from network interfaces'
long_desc %{
Remove custom configuration for a set of interfaces removing any custom ips,
routes, and rules previously added (the 'eth0' primary ip is always left
untouched for safety).
# add command
when 'add'
@add = Aws::ENI.add(ARGV[1], ARGV[2])
puts "added #{@add['device']} with private ip #{@add['private_ip']}"
Use the optional filter argument to limit this action to interfaces with a
matching name, interface id, subnet id, or MAC address (default 'all').
}
arg 'filter | all (default)', :optional
command [:deconfig,:deconf] do |c|
c.action do |global,opts,args|
args.delete('all')
Aws::ENI.deconfigure(args.first)
end
end

# remove command
when 'remove'
@remove = Aws::ENI.remove(ARGV[1], ARGV[2], ARGV[3])
if @remove['release'] == true
puts "removed #{@remove['private_ip']} and dissociated #{remove['public_ip']} from #{@remove['device']}"
else
puts "removed #{@remove['private_ip']} from #{@remove['device']}"
end
desc 'Enable network interface'
long_desc %{
Enable one or more network interfaces (similar to 'ifup').
# assoc command
when 'associate'
@assoc = Aws::ENI.assoc(ARGV[1], ARGV[2])
puts "associated #{@assoc['private_ip']} => #{@assoc['public_ip']}"
Use the optional filter argument to limit this action to interfaces with a
matching name, interface id, subnet id, or MAC address (default 'all').
}
arg 'filter | all (default)', :optional
command [:enable,:up] do |c|
c.action do |global,opts,args|
args.delete('all')
Aws::ENI.IFconfig.filter(args.first).each(&:enable)
end
end

else
abort "Unknown command: #{ARGV[0]}"
desc 'Disable network interface'
long_desc %{
Disable one or more network interfaces (similar to 'ifdown').
Use the optional filter argument to limit this action to interfaces with a
matching name, interface id, subnet id, or MAC address (default 'all').
}
arg 'filter | all (default)', :optional
command [:disable,:down] do |c|
c.action do |global,opts,args|
args.delete('all')
Aws::ENI.IFconfig.filter(args.first).each(&:disable)
end
end

# error handling

# handle library exceptions
rescue Aws::ENI::Error => e
abort "Error: " << e.message
on_error do |exception|
if Aws::ENI::PermissionError === exception
warn "error: This action requires super-user privileges (try sudo)"
false
else
true
end
end

exit run(ARGV)

0 comments on commit 10175e7

Please sign in to comment.