awt 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ba28acfb7c2b085b5932197f7a2145c82bb74085
4
+ data.tar.gz: fb4d005cf06fda90dc483f721fa6d8de189c7126
5
+ SHA512:
6
+ metadata.gz: 82cfc32ee11022ee12b2699091c821ffedc00e5bee4e976ad70a50f8b6d858699d66258e1fffa137107be491664a4f114913200c0201907fb2aa8d7279d341fe
7
+ data.tar.gz: b6b16cfaff66874e4769e4c790408f33f2a3504bf08813ed423ed5f01cfc8f0531c55dba80694fc1a0432465ab7f13d8c23e3b598751bdb521c50584b7c57deb
@@ -0,0 +1 @@
1
+ repo_token: CGVbpVsIe3uUFN0JPt8LuHB3bCWngmEOw
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ gemfile:
5
+ - Gemfile
6
+ script: bundle exec rake spec
7
+ notifications:
8
+ mails:
9
+ - i2bskn@gmail.com
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in awt.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'coveralls', require: false
8
+ gem 'simplecov', require: false
9
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 i2bskn
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,71 @@
1
+ # Awt
2
+
3
+ [![Build Status](https://travis-ci.org/i2bskn/awt.png)](https://travis-ci.org/i2bskn/awt)
4
+ [![Coverage Status](https://coveralls.io/repos/i2bskn/awt/badge.png)](https://coveralls.io/r/i2bskn/awt)
5
+ [![Code Climate](https://codeclimate.com/github/i2bskn/awt.png)](https://codeclimate.com/github/i2bskn/awt)
6
+
7
+ Awt is cli tool for system administration.
8
+
9
+ ## Requirements
10
+
11
+ * Ruby 2.0.0
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'awt'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install awt
26
+
27
+ ## Usage
28
+
29
+ Awt to run looking Awtfile from a task that is specified in the argument.
30
+
31
+ #### Awtfile
32
+
33
+ Awtfile be placed in the home directory or the current directory tree.
34
+ It can be specified on the command line if you want to place other location.
35
+
36
+ `server` can also be specified on the command line argument.
37
+ `server` required if you do not specify on the command line argument.
38
+
39
+ The following example of Awtfile.
40
+
41
+ ```ruby
42
+ server "hostname", user: "awt", port: 22, key: "/path/to/id_rsa"
43
+
44
+ task :task_name do
45
+ run "do something"
46
+ end
47
+ ```
48
+
49
+ #### Execute
50
+
51
+ Awt task executable as follows:
52
+
53
+ ```
54
+ $ bundle exec awt task_name
55
+ ```
56
+
57
+ Options that can be specified:
58
+
59
+ * `-H host1,host2` => Required if you do not specify on the command line argument.
60
+ * `-u user` => Specify user name. Default is current user name.
61
+ * `-p port` => Specify port number. Default is 22.
62
+ * `-i identity_file` => Specify identity file. Default is `~/.ssh/id_rsa`.
63
+ * `-f file` => `/path/to/Autfile`.
64
+
65
+ ## Contributing
66
+
67
+ 1. Fork it
68
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
70
+ 4. Push to the branch (`git push origin my-new-feature`)
71
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ desc "Run all specs"
5
+ RSpec::Core::RakeTask.new(:spec) do |t|
6
+ t.rspec_opts = ["-c", "-fs"]
7
+ end
8
+
9
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'awt/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "awt"
8
+ spec.version = Awt::VERSION
9
+ spec.authors = ["i2bskn"]
10
+ spec.email = ["i2bskn@gmail.com"]
11
+ spec.description = %q{Awt is cli tool for system administration.}
12
+ spec.summary = %q{Awt is cli tool for system administration.}
13
+ spec.homepage = "https://github.com/i2bskn/awt"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "net-ssh"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
data/bin/awt ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ path = File.expand_path("../../lib", __FILE__)
4
+ $:.unshift(path) unless $:.include?(path)
5
+
6
+ require "optparse"
7
+
8
+ require "awt"
9
+ require "awt/dsl"
10
+
11
+ include Awt::DSL
12
+
13
+ options = {}
14
+ OptionParser.new do |opt|
15
+ opt.on("-H VAL") {|v| options[:hosts] = v.split(",")}
16
+ opt.on("-u VAL") {|v| options[:user] = v}
17
+ opt.on("-p VAL") {|v| options[:port] = v.to_i}
18
+ opt.on("-i VAL") {|v| options[:key] = v}
19
+ opt.on("-f VAL") {|v| options[:task_file] = v}
20
+
21
+ begin
22
+ opt.parse!(ARGV)
23
+ rescue => e
24
+ puts e
25
+ exit 1
26
+ end
27
+ end
28
+
29
+ task_name = ARGV.first.to_sym
30
+ hosts = options.delete(:hosts) || []
31
+ task_file = options.delete(:task_file) || task_find
32
+
33
+ hosts.each do |host|
34
+ server host, options
35
+ end
36
+
37
+ unless task_file.nil?
38
+ load task_file
39
+ $AWT_TASKS[task_name].exec($AWT_TARGETS)
40
+ end
@@ -0,0 +1,6 @@
1
+ require "net/ssh"
2
+
3
+ require "awt/printer"
4
+ require "awt/server"
5
+ require "awt/task"
6
+ require "awt/version"
@@ -0,0 +1,31 @@
1
+ module Awt
2
+ module DSL
3
+ $AWT_TASKS = {}
4
+ $AWT_TARGETS = []
5
+
6
+ def task(name, &block)
7
+ task = Task.new &block
8
+ $AWT_TASKS.store(name.to_sym, task)
9
+ end
10
+
11
+ def server(host, user: nil, port: 22, key: "~/.ssh/id_rsa")
12
+ $AWT_TARGETS << Awt::Server.new(host: host, user: user, port: port, key: key)
13
+ end
14
+
15
+ def task_find(path = File.expand_path("."))
16
+ task_file = "Awtfile"
17
+ file = File.join(path, task_file)
18
+
19
+ if File.exists?(file)
20
+ file
21
+ else
22
+ if path == "/"
23
+ file = File.join(ENV["HOME"], task_file)
24
+ File.exists?(file) ? file : nil
25
+ else
26
+ task_find(File.expand_path("..", path))
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,17 @@
1
+ module Awt
2
+ class Printer
3
+ attr_accessor :host
4
+
5
+ def initialize(host)
6
+ @host = host
7
+ end
8
+
9
+ def method_missing(action, *args)
10
+ if action.to_s =~ /print_(.+)/
11
+ puts "[#{@host}] #{$1}: #{args.first}"
12
+ else
13
+ super
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ module Awt
2
+ class Server
3
+ attr_accessor :host, :user, :options
4
+
5
+ def initialize(host: "localhost", port: 22, user: nil, key: "~/.ssh/id_rsa")
6
+ @host = host
7
+ @user = user.nil? ? ENV["USER"] : user
8
+ @options = {port: port, keys: File.expand_path(key)}
9
+ @printer = Printer.new(@host)
10
+ end
11
+
12
+ def run(cmd)
13
+ @printer.host = @host if @printer.host != @host
14
+ Net::SSH.start(@host, @user, @options) do |ssh|
15
+ @printer.print_run cmd
16
+ out = ssh.exec!(cmd)
17
+ @printer.print_out out
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ module Awt
2
+ class Task
3
+ def initialize(&block)
4
+ @block = block
5
+ end
6
+
7
+ def exec(targets)
8
+ targets.each do |target|
9
+ target.instance_eval &@block
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Awt
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,57 @@
1
+ require "spec_helper"
2
+
3
+ describe Awt::DSL do
4
+ class Klass
5
+ include Awt::DSL
6
+ end
7
+
8
+ let(:klass) {Klass.new}
9
+
10
+ describe "#task" do
11
+ before do
12
+ klass.task :example do
13
+ "example"
14
+ end
15
+ end
16
+
17
+ after {$AWT_TASKS = {}}
18
+
19
+ it "should define $AWT_TASKS" do
20
+ expect($AWT_TASKS).not_to be_nil
21
+ end
22
+
23
+ it "should add task" do
24
+ expect($AWT_TASKS).not_to be_empty
25
+ end
26
+ end
27
+
28
+ describe "#server" do
29
+ before do
30
+ klass.server "example.com", user: "awt", port: 30022, key: "/path/to/id_rsa"
31
+ end
32
+
33
+ after {$AWT_TARGETS = []}
34
+
35
+ it "should define $AWT_TARGETS" do
36
+ expect($AWT_TARGETS).not_to be_nil
37
+ end
38
+
39
+ it "should add target" do
40
+ expect($AWT_TARGETS).not_to be_empty
41
+ end
42
+ end
43
+
44
+ describe "#task_find" do
45
+ let(:path) {klass.task_find("/home/i2bskn")}
46
+
47
+ it "returns task file path" do
48
+ File.should_receive(:exists?).and_return(true)
49
+ expect(path).to eq("/home/i2bskn/Awtfile")
50
+ end
51
+
52
+ it "returns nil if task file not found" do
53
+ File.should_receive(:exists?).at_least(2).and_return(false)
54
+ expect(path).to be_nil
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ describe Awt::Printer do
4
+ let(:printer) {Awt::Printer.new("example.com")}
5
+
6
+ describe "#initialize" do
7
+ it "should set specified host" do
8
+ expect(printer.host).to eq("example.com")
9
+ end
10
+ end
11
+
12
+ describe "#method_missing" do
13
+ context "with print_* method" do
14
+ it "should print message" do
15
+ expect(
16
+ capture(:stdout){
17
+ printer.print_run "command"
18
+ }.chomp
19
+ ).to eq("[example.com] run: command")
20
+ end
21
+ end
22
+
23
+ context "with unknown method" do
24
+ it "should throw exception" do
25
+ expect {
26
+ printer.unknown_method "command"
27
+ }.to raise_error
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,78 @@
1
+ require "spec_helper"
2
+
3
+ describe Awt::Server do
4
+ describe "#initialize" do
5
+ context "with default params" do
6
+ let(:server) {Awt::Server.new}
7
+
8
+ it "should set default host" do
9
+ expect(server.host).to eq("localhost")
10
+ end
11
+
12
+ it "should set default port" do
13
+ expect(server.options[:port]).to eq(22)
14
+ end
15
+
16
+ it "should set default user" do
17
+ expect(server.user).to eq(ENV["USER"])
18
+ end
19
+
20
+ it "should set default key" do
21
+ expect(server.options[:keys]).to eq(File.expand_path("~/.ssh/id_rsa"))
22
+ end
23
+
24
+ it "should set printer" do
25
+ expect(server.instance_eval{@printer}.is_a? Awt::Printer).to be_true
26
+ end
27
+ end
28
+
29
+ context "with specified params" do
30
+ it "should set specified host" do
31
+ server = Awt::Server.new(host: "example.com")
32
+ expect(server.host).to eq("example.com")
33
+ end
34
+
35
+ it "should set specified port" do
36
+ server = Awt::Server.new(port: 30022)
37
+ expect(server.options[:port]).to eq(30022)
38
+ end
39
+
40
+ it "should set specified user" do
41
+ server = Awt::Server.new(user: "awt")
42
+ expect(server.user).to eq("awt")
43
+ end
44
+
45
+ it "should set specified key" do
46
+ server = Awt::Server.new(key: "/path/to/id_rsa")
47
+ expect(server.options[:keys]).to eq("/path/to/id_rsa")
48
+ end
49
+ end
50
+ end
51
+
52
+ describe "#run" do
53
+ let(:server) {Awt::Server.new}
54
+ let(:ssh) {double("Net::SSH mock").as_null_object}
55
+
56
+ before do
57
+ Net::SSH.stub(:start).and_yield(ssh)
58
+ Awt::Printer.any_instance.stub(:print_run)
59
+ Awt::Printer.any_instance.stub(:print_out)
60
+ end
61
+
62
+ after {server.run("example")}
63
+
64
+ it "should call Awt::Printer#print_*" do
65
+ Awt::Printer.any_instance.should_receive(:print_run)
66
+ Awt::Printer.any_instance.should_receive(:print_out)
67
+ end
68
+
69
+ it "should call Net::SSH::Connection::Session#exec" do
70
+ ssh.should_receive(:exec!)
71
+ end
72
+
73
+ it "should re-set host of Printer" do
74
+ Awt::Printer.any_instance.should_receive(:host=)
75
+ server.host = "example.com"
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ describe Awt::Task do
4
+ class Target
5
+ def run(cmd)
6
+ cmd
7
+ end
8
+ end
9
+
10
+ let(:task) {Awt::Task.new {run "example"}}
11
+ let(:target) {Target.new}
12
+
13
+ describe "#initialize" do
14
+ it "should set Proc object" do
15
+ expect(task.instance_eval{@block}.is_a? Proc).to be_true
16
+ end
17
+ end
18
+
19
+ describe "#exec" do
20
+ it "should execute target method" do
21
+ target.should_receive(:run)
22
+ task.exec([target])
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ require "simplecov"
2
+ require "coveralls"
3
+ Coveralls.wear!
4
+
5
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
6
+ SimpleCov::Formatter::HTMLFormatter,
7
+ Coveralls::SimpleCov::Formatter
8
+ ]
9
+
10
+ SimpleCov.start do
11
+ add_filter "spec"
12
+ add_filter ".bundle"
13
+ end
14
+
15
+ require "awt"
16
+ require "awt/dsl"
17
+
18
+ support_files = File.join(File.expand_path("..", __FILE__), "support/*.rb")
19
+ Dir[support_files].each {|f| require f}
20
+
21
+ RSpec.configure do |config|
22
+ config.order = "random"
23
+ config.include(SpecUtils)
24
+ end
@@ -0,0 +1,15 @@
1
+ require "stringio"
2
+
3
+ module SpecUtils
4
+ def capture(stream)
5
+ begin
6
+ stream = stream.to_s
7
+ eval "$#{stream} = StringIO.new"
8
+ yield
9
+ result = eval("$#{stream}").string
10
+ ensure
11
+ eval("$#{stream} = #{stream.upcase}")
12
+ end
13
+ result
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: awt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - i2bskn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-ssh
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Awt is cli tool for system administration.
70
+ email:
71
+ - i2bskn@gmail.com
72
+ executables:
73
+ - awt
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .coveralls.yml
78
+ - .gitignore
79
+ - .travis.yml
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - awt.gemspec
85
+ - bin/awt
86
+ - lib/awt.rb
87
+ - lib/awt/dsl.rb
88
+ - lib/awt/printer.rb
89
+ - lib/awt/server.rb
90
+ - lib/awt/task.rb
91
+ - lib/awt/version.rb
92
+ - spec/awt/dsl_spec.rb
93
+ - spec/awt/printer_spec.rb
94
+ - spec/awt/server_spec.rb
95
+ - spec/awt/task_spec.rb
96
+ - spec/spec_helper.rb
97
+ - spec/support/spec_utils.rb
98
+ homepage: https://github.com/i2bskn/awt
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.1.9
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Awt is cli tool for system administration.
122
+ test_files:
123
+ - spec/awt/dsl_spec.rb
124
+ - spec/awt/printer_spec.rb
125
+ - spec/awt/server_spec.rb
126
+ - spec/awt/task_spec.rb
127
+ - spec/spec_helper.rb
128
+ - spec/support/spec_utils.rb