akaer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+
20
+ # RubyMine
21
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the akaer gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ source "http://rubygems.org"
8
+
9
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,80 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ akaer (0.1.0)
5
+ cowtech-extensions (~> 2.1.0)
6
+ cowtech-lib (~> 1.9.8.0)
7
+ hashie (~> 1.2.0)
8
+ rainbow (~> 1.1.0)
9
+
10
+ GEM
11
+ remote: http://rubygems.org/
12
+ specs:
13
+ actionpack (3.2.6)
14
+ activemodel (= 3.2.6)
15
+ activesupport (= 3.2.6)
16
+ builder (~> 3.0.0)
17
+ erubis (~> 2.7.0)
18
+ journey (~> 1.0.1)
19
+ rack (~> 1.4.0)
20
+ rack-cache (~> 1.2)
21
+ rack-test (~> 0.6.1)
22
+ sprockets (~> 2.1.3)
23
+ activemodel (3.2.6)
24
+ activesupport (= 3.2.6)
25
+ builder (~> 3.0.0)
26
+ activesupport (3.2.6)
27
+ i18n (~> 0.6)
28
+ multi_json (~> 1.0)
29
+ builder (3.0.0)
30
+ cowtech-extensions (2.1.0)
31
+ actionpack (~> 3.0)
32
+ tzinfo (~> 0.3.33)
33
+ cowtech-lib (1.9.8.1)
34
+ jeweler
35
+ open4
36
+ diff-lcs (1.1.3)
37
+ erubis (2.7.0)
38
+ git (1.2.5)
39
+ hashie (1.2.0)
40
+ hike (1.2.1)
41
+ i18n (0.6.0)
42
+ jeweler (1.8.4)
43
+ bundler (~> 1.0)
44
+ git (>= 1.2.5)
45
+ rake
46
+ rdoc
47
+ journey (1.0.4)
48
+ json (1.7.3)
49
+ multi_json (1.3.6)
50
+ open4 (1.3.0)
51
+ rack (1.4.1)
52
+ rack-cache (1.2)
53
+ rack (>= 0.4)
54
+ rack-test (0.6.1)
55
+ rack (>= 1.0)
56
+ rainbow (1.1.4)
57
+ rake (0.9.2.2)
58
+ rdoc (3.12)
59
+ json (~> 1.4)
60
+ rspec (2.10.0)
61
+ rspec-core (~> 2.10.0)
62
+ rspec-expectations (~> 2.10.0)
63
+ rspec-mocks (~> 2.10.0)
64
+ rspec-core (2.10.1)
65
+ rspec-expectations (2.10.0)
66
+ diff-lcs (~> 1.1.3)
67
+ rspec-mocks (2.10.1)
68
+ sprockets (2.1.3)
69
+ hike (~> 1.2)
70
+ rack (~> 1.0)
71
+ tilt (~> 1.1, != 1.3.0)
72
+ tilt (1.3.3)
73
+ tzinfo (0.3.33)
74
+
75
+ PLATFORMS
76
+ ruby
77
+
78
+ DEPENDENCIES
79
+ akaer!
80
+ rspec (~> 2.10.0)
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # akaer
2
+
3
+ A small utility to add aliases to network interfaces.
4
+ http://github.com/ShogunPanda/akaer
5
+
6
+ ## Description
7
+
8
+ Akaer is a small utility that adds and remove aliases to your network interfaces. This is useful in web development.
9
+
10
+ ## Basic usage
11
+
12
+ 1. Install the gem:
13
+
14
+ `gem install akaer`
15
+
16
+ 2. Run the application:
17
+
18
+ `akaer`
19
+
20
+ **You're done!**
21
+
22
+ ## Advanced usage
23
+
24
+ Just type `akaer --help` and you'll see all available options.
25
+
26
+ ## Configuration
27
+
28
+ By defaults, Akaer uses a configuration file in `~/.akaer_config`, but you can change the path using the `--config` switch.
29
+
30
+ The file is a plain Ruby file with a single `config` object that supports the following directives.
31
+
32
+ * `interface`: The network interface to manage. `lo0` by default.
33
+ * `address`: A specific list of aliases to manage.
34
+ * `start-address`: The address to start sequential address. `10.0.0.1` by default. Not used if `addresses` is specified.
35
+ * `aliases`: The number of sequential addresses to add. 5 by default.
36
+ * `log_file`: The default log file. By default it logs to standard output.
37
+ * `log_level`: The default log level. Valid values are from 0 to 5 where 0 means "all messages".
38
+
39
+ ## Remarks
40
+
41
+ Akaer is tightly coupled with the UNIX tools, so it won't work for Windows.
42
+
43
+ ## Contributing to akaer
44
+
45
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
46
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
47
+ * Fork the project
48
+ * Start a feature/bugfix branch
49
+ * Commit and push until you are happy with your contribution
50
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
51
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
52
+
53
+ ## Copyright
54
+
55
+ Copyright (C) 2012 and above Shogun <[shogun_panda@me.com](mailto:shogun_panda@me.com)>.
56
+ Licensed under the MIT license, which can be found at [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php).
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the akaer gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "bundler/gem_tasks"
8
+ require "rspec/core/rake_task"
9
+
10
+ RSpec::Core::RakeTask.new("spec")
11
+
12
+ desc "Run all specs with rcov"
13
+ RSpec::Core::RakeTask.new("spec:coverage") do |t|
14
+ t.rcov_opts = %q[--exclude "spec"]
15
+ end
data/akaer.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the akaer gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "./lib/akaer/version"
8
+
9
+ Gem::Specification.new do |gem|
10
+ gem.name = "akaer"
11
+ gem.version = Akaer::Version::STRING
12
+ gem.authors = ["Shogun"]
13
+ gem.email = ["shogun_panda@me.com"]
14
+ gem.homepage = "http://github.com/ShogunPanda/akaer"
15
+ gem.summary = %q{A small utility to add aliases to network interfaces.}
16
+ gem.description = %q{A small utility to add aliases to network interfaces.}
17
+
18
+ gem.rubyforge_project = "akaer"
19
+ gem.files = `git ls-files`.split("\n")
20
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ gem.require_paths = ["lib"]
23
+
24
+ gem.add_dependency("cowtech-extensions", "~> 2.1.0")
25
+ gem.add_dependency("cowtech-lib", "~> 1.9.8.0")
26
+ gem.add_dependency("hashie", "~> 1.2.0")
27
+ gem.add_dependency("rainbow", "~> 1.1.0")
28
+
29
+ gem.add_development_dependency("rspec", "~> 2.10.0")
30
+ end
31
+
32
+
data/bin/akaer ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+ #
4
+ # This file is part of the akaer gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
5
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
6
+
7
+ require "rubygems"
8
+ require "akaer"
9
+ Akaer::Application.start
@@ -0,0 +1,120 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the akaer gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "cowtech-lib"
8
+ require "ipaddr"
9
+
10
+ class Fixnum
11
+ def i(num = 3)
12
+ self.to_s.rjust(num, "0")
13
+ end
14
+ end
15
+
16
+ module Akaer
17
+ class Application < Cowtech::Lib::Script
18
+ def self.start
19
+ self.new
20
+ end
21
+
22
+ def initialize
23
+ Akaer::Logger.start_time = Time.now
24
+ super(:args => {:name => "Interface aliases", :version => 1.0, :description => "A small utility to add aliases to network interfaces.", :usage => "Usage: #{ARGV[0]} [OPTIONS] [add|remove]"})
25
+ end
26
+
27
+ def add_options
28
+ @options_parser << [
29
+ {:name => "configuration", :short => "-c", :long => "--configuration", :type => :string, :help => "Configuration file. By default is the standard output", :meta => "FILE", :required => false, :default => "~/.akaer_config"},
30
+ {:name => "interface", :short => "-i", :long => "--interface", :type => :string, :help => "Interface", :meta => "INTERFACE", :required => false, :default => "lo0"},
31
+ {:name => "addresses", :short => "-A", :long => "--addresses", :type => :list, :help => "Addresses to add", :required => false, :default => []},
32
+ {:name => "start-address", :short => "-s", :long => "--start-address", :type => :string, :help => "Starting address (not used if --addresses is specified).", :meta => "ADDRESS", :required => false, :default => "10.0.0.1"},
33
+ {:name => "aliases", :short => "-S", :long => "--aliases", :type => :int, :help => "Number of aliases to add", :meta => "NUMBER", :required => false, :default => 5},
34
+ {:name => "log-file", :short => "-l", :long => "--log-file", :type => :string, :help => "Log file. By default is the standard output", :meta => "FILE", :required => false, :default => "STDOUT"},
35
+ {:name => "log-level", :short => "-L", :long => "--log-level", :type => :int, :help => "The default log level. Valid values are from 0 to 5 where 0 means \"all messages\".", :meta => "NUMBER", :required => false, :default => Logger::INFO},
36
+ {:name => "dry-run", :short => "-n", :long => "--dry-run", :type => :bool, :help => "Do not really execute actions."},
37
+ {:name => "quiet", :short => "-q", :long => "--quiet", :type => :bool, :help => "Do not show any message", :required => false, :default => false}
38
+ ]
39
+ end
40
+
41
+ def create_logger(file, level = Logger::INFO)
42
+ file = case file
43
+ when "STDOUT" then $stdout
44
+ when "STDERR" then $stderr
45
+ else file
46
+ end
47
+
48
+ rv = Akaer::Logger.new(file)
49
+ rv.level = level.to_i
50
+ rv
51
+ end
52
+
53
+ def execute_command(interface, action, ip, prefix = "")
54
+ prefix += " " if !prefix.blank?
55
+ @logger.info("#{prefix}#{(action == "add" ? "Adding" : "Removing").bright} alias #{ip.bright} #{action == "add" ? "to" : "from"} interface #{interface.bright}.")
56
+ system("sudo ifconfig #{interface} #{action == "add" ? "alias" : "-alias"} #{ip}")
57
+ end
58
+
59
+ def run
60
+ @logger = self.create_logger(@options_parser["log-file"], @options_parser["log-level"])
61
+
62
+ command = (@options_parser.args[0] || "add").downcase
63
+ aliases = []
64
+ target_address = ""
65
+
66
+ # Parse configuration file
67
+ configuration = Akaer::Configuration.load(@options_parser["configuration"], @logger)
68
+
69
+ # Now merge with the options
70
+ @options_parser[].each_pair do |option, value|
71
+ key = option.to_s.gsub("-", "_")
72
+ configuration[key] = value if configuration.has_key?(key) && @options_parser.provided?(option)
73
+ end
74
+
75
+ # Instantiate the logger
76
+ logger = self.create_logger(configuration.log_file, configuration.log_level)
77
+
78
+ # Create address
79
+ if !["add", "remove"].include?(command) then
80
+ @logger.fatal("\"Please choose an action between \"add\" or \"remove\".")
81
+ abort
82
+ end
83
+
84
+ begin
85
+ if configuration.addresses.blank? then
86
+ target_address = configuration.start_address
87
+ temp = IPAddr.new(target_address)
88
+
89
+ [configuration.aliases, 1].max.times do
90
+ aliases << temp.to_s
91
+ temp = temp.succ
92
+ target_address = temp.to_s
93
+ end
94
+ else
95
+ configuration.addresses.each do |t|
96
+ target_address = t
97
+ aliases << IPAddr.new(target_address)
98
+ end
99
+ end
100
+ rescue ArgumentError => e
101
+ logger.fatal("\"#{target_address}\" is not a valid IP address.")
102
+ abort
103
+ end
104
+
105
+ if !@options_parser["dry-run"] then
106
+ total = aliases.length
107
+ rj = total.to_s.length
108
+ aliases.each_with_index do |a, i|
109
+ prefix = "[#{(i + 1).to_s.rjust(rj, "0")}/#{total}]"
110
+ if !self.execute_command(configuration.interface, command, a, prefix) then
111
+ logger.fatal("#{command == "add" ? "Adding" : "Removing"} alias #{a} failed.")
112
+ abort
113
+ end
114
+ end
115
+ else
116
+ @logger.info("I will #{command.bright} #{aliases.length == 1 ? "this alias" : "these aliases"} #{command == "add" ? "to" : "from"} interface #{configuration.interface.bright}: #{aliases.collect {|a| a.bright}.join(", ")}.")
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the akaer gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Akaer
8
+ class Configuration < Hashie::Dash
9
+ property :interface, :default => "lo0"
10
+ property :addresses, :default => []
11
+ property :start_address, :default => "10.0.0.1"
12
+ property :aliases, :default => 5
13
+ property :log_file, :default => "STDOUT"
14
+ property :log_level, :default => Logger::INFO
15
+
16
+ def self.load(file = nil, logger = nil)
17
+ if logger.blank? then
18
+ logger ||= Akaer::Logger.new($stderr)
19
+ logger.level = Logger::INFO
20
+ end
21
+
22
+ rv = self.new
23
+ if file.present? then
24
+ begin
25
+ # Open the file
26
+ path = Pathname.new(File.expand_path(file)).realpath
27
+ logger.debug("Using configuration file #{path}.")
28
+
29
+ rv.tap do |config|
30
+ eval(File.read(path))
31
+ end
32
+ rescue Errno::ENOENT, LoadError
33
+ rescue Exception
34
+ raise Akaer::Errors::InvalidConfiguration.new("Config file #{file.bright} is not valid.")
35
+ end
36
+ end
37
+
38
+ rv
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the akaer gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Akaer
8
+ module Errors
9
+ class InvalidConfiguration < ArgumentError
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the akaer gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Akaer
8
+ class Logger < ::Logger
9
+ mattr_accessor :start_time
10
+
11
+ def initialize(logdev, shift_age = 0, shift_size = 1048576)
12
+ super(logdev, shift_age, shift_size)
13
+
14
+ self.formatter = Proc.new {|severity, datetime, progname, msg|
15
+ color = case severity
16
+ when "DEBUG" then :cyan
17
+ when "INFO" then :green
18
+ when "WARN" then :yellow
19
+ when "ERROR" then :red
20
+ when "FATAL" then :magenta
21
+ else nil
22
+ end
23
+
24
+ header = ("[%s T+%0.5f]" %[datetime.strftime("%Y/%b/%d %H:%M:%S"), datetime.to_f - @@start_time.to_f, msg]).bright
25
+ header = header.color(color) if color.present?
26
+ log = "%s %s\n" % [header, msg]
27
+ }
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the akaer gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Akaer
8
+ module Version
9
+ MAJOR = 0
10
+ MINOR = 1
11
+ PATCH = 0
12
+
13
+ STRING = [MAJOR, MINOR, PATCH].compact.join(".")
14
+ end
15
+ end
data/lib/akaer.rb ADDED
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the akaer gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "cowtech-extensions"
8
+ require "cowtech-lib"
9
+ require "ipaddr"
10
+ require "hashie"
11
+ require "rainbow"
12
+ require "pathname"
13
+
14
+ Sickill::Rainbow.enabled = true
15
+
16
+ require "akaer/version" if !defined?(Akaer::Version)
17
+ require "akaer/errors"
18
+ require "akaer/logger"
19
+ require "akaer/configuration"
20
+ require "akaer/application"
21
+
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: akaer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Shogun
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cowtech-extensions
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.1.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: cowtech-lib
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.9.8.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.9.8.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: hashie
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.2.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.2.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: rainbow
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.1.0
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.1.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 2.10.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 2.10.0
94
+ description: A small utility to add aliases to network interfaces.
95
+ email:
96
+ - shogun_panda@me.com
97
+ executables:
98
+ - akaer
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - .gitignore
103
+ - Gemfile
104
+ - Gemfile.lock
105
+ - README.md
106
+ - Rakefile
107
+ - akaer.gemspec
108
+ - bin/akaer
109
+ - lib/akaer.rb
110
+ - lib/akaer/application.rb
111
+ - lib/akaer/configuration.rb
112
+ - lib/akaer/errors.rb
113
+ - lib/akaer/logger.rb
114
+ - lib/akaer/version.rb
115
+ homepage: http://github.com/ShogunPanda/akaer
116
+ licenses: []
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ segments:
128
+ - 0
129
+ hash: 2748666871543433517
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ segments:
137
+ - 0
138
+ hash: 2748666871543433517
139
+ requirements: []
140
+ rubyforge_project: akaer
141
+ rubygems_version: 1.8.24
142
+ signing_key:
143
+ specification_version: 3
144
+ summary: A small utility to add aliases to network interfaces.
145
+ test_files: []