cli_tool 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17d8d9e6d914296940ccbd28dd3eabb488dc7b46
4
- data.tar.gz: 026adfb4294b909858ab0ff98c9d3c89d4a1f53a
3
+ metadata.gz: 5d6bebdc51c5d3fa303521a576030ef09a2a2f1e
4
+ data.tar.gz: 44314ab4226493adced0d5300540c55de8194801
5
5
  SHA512:
6
- metadata.gz: 66bdb094264f87a4928d380b04e5244472dce78b85aef3c047be6400b88645e887612ba8d7abedc1958c8053a7ce246ffbe4724b47476d7234378d7f6f4e2532
7
- data.tar.gz: 8250abdc7bbfb6d3d6bf5ee8fed267b0598d1e1dba65b205072695689a15f5e0c7897f813d07e9f53c8317cdefb73ded1db51ae36a66685d113429d94431e2ce
6
+ metadata.gz: c4f62e534448942a0f7ef307b7c3be7b37807ca658692f89c7df97a11db30de081c6ec5eb82fdc3a6f0408df5e922e92edcc1e2351d636d64124e8bfffa5b57d
7
+ data.tar.gz: 0f5cc7400c20e4c3732aba67f733873d89843eff4f17901a362617e021518d72dfc22a65dd8d1217b12abdda98eb4c838b35553f26bc48854408397de2f12ec0
@@ -0,0 +1,130 @@
1
+ module CliTool
2
+ module Remote
3
+ class WaitingForSSH < StandardError; end;
4
+ class Failure < StandardError; end;
5
+
6
+ class Script < String
7
+ def <<(script)
8
+ script = script.strip + ';' unless script.match(/;$/)
9
+ script = Script.new(script)
10
+ super("#{script}\n")
11
+ end
12
+
13
+ def prepend(script)
14
+ script = script.strip + ';' unless script.match(/;$/)
15
+ script = Script.new(script)
16
+ super("#{script}\n")
17
+ end
18
+ end
19
+
20
+ def self.included(base)
21
+ base.__send__(:include, ::CliTool::StdinOut)
22
+ base.__send__(:include, ::CliTool::OptionParser)
23
+ base.__send__(:include, ::CliTool::SoftwareDependencies)
24
+ base.extend(ClassMethods)
25
+ base.software(:ssh, :nc)
26
+ base.options host: :required,
27
+ identity: :required,
28
+ debug: :none,
29
+ user: {
30
+ dependency: :required,
31
+ default: %x{whoami}.strip
32
+ },
33
+ port: {
34
+ dependency: :required,
35
+ default: '22'
36
+ }
37
+ end
38
+
39
+ def script(script = nil, sudo = false, sudouser = nil)
40
+ @_script ||= Script.new
41
+ return Script.new(@_script.strip) if script.nil?
42
+ return @_script = Script.new if script == :reset
43
+ @_script << (sudo == :sudo ? %{sudo su -l -c "#{script.strip.gsub('"','\"')}" #{sudouser}} : script).strip
44
+ end
45
+
46
+ def script_exec
47
+ wait4ssh
48
+
49
+ command =[ "ssh -t -t" ]
50
+ command << "-I #{@identity}" if @identity
51
+ command << "-p #{@port}" if @port
52
+ command << "#{@user}@#{@host}"
53
+ command << "<<-SCRIPT\n#{script}\nexit;\nSCRIPT"
54
+ command = command.join(' ')
55
+ script :reset
56
+
57
+ puts("Running Remotely:\n#{command}\n", [:blue, :white_bg])
58
+
59
+ system(command)
60
+
61
+ unless $?.success?
62
+ raise Failure, "Error running \"#{command}\" on #{@host} exited with code #{$?.to_i}."
63
+ end
64
+ end
65
+
66
+ def aptget(action = :update, *software)
67
+ software = software.map(&:to_s).join(' ')
68
+ script "apt-get #{action} -q -y --force-yes #{software}", :sudo
69
+ end
70
+
71
+ def aptkeyadd(*keys)
72
+ keys = keys.map(&:to_s).join(' ')
73
+ script "apt-key adv --keyserver keyserver.ubuntu.com --recv-keys #{keys}", :sudo
74
+ end
75
+
76
+ def restart
77
+ script :reset
78
+ script "shutdown -r now &", :sudo
79
+ script_exec
80
+
81
+ # Let the server shutdown
82
+ sleep 5
83
+ end
84
+
85
+ def adduser(user, system = false)
86
+ script "adduser --disabled-password --quiet --gecos '' #{user}".squeeze(' '), :sudo
87
+ end
88
+
89
+ def wait4ssh
90
+ _retry ||= false
91
+ %x{nc -z #{@host} #{@port}}
92
+ raise WaitingForSSH unless $?.success?
93
+ puts("\nSSH is now available!", :green) if _retry
94
+ rescue WaitingForSSH
95
+ print "Waiting for ssh..." unless _retry
96
+ _retry = true
97
+ print '.'
98
+ sleep 2
99
+ retry
100
+ end
101
+
102
+ module ClassMethods
103
+
104
+ # Create the options array
105
+ def software(*soft)
106
+ @@software ||= []
107
+
108
+ # If no software dependencies were passed then return
109
+ return @@software.uniq unless soft
110
+
111
+ # Find missing software
112
+ missing = []
113
+ soft.each do |app|
114
+ %x{which #{app}}
115
+ missing << app unless $?.success?
116
+ end
117
+
118
+ # Raise if there were any missing software's
119
+ unless missing.empty?
120
+ missing = missing.join(', ')
121
+ raise CliTool::MissingDependencies,
122
+ %{The required software packages "#{missing}" could not be found in your $PATH.}
123
+ end
124
+
125
+ # Append to the software list
126
+ @@software = @@software.concat(soft).uniq
127
+ end
128
+ end
129
+ end
130
+ end
@@ -1,3 +1,3 @@
1
1
  module CliTool
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/cli_tool.rb CHANGED
@@ -2,6 +2,7 @@ require "cli_tool/version"
2
2
  require "cli_tool/stdin_out"
3
3
  require "cli_tool/option_parser"
4
4
  require "cli_tool/software_dependencies"
5
+ require "cli_tool/remote"
5
6
 
6
7
  module CliTool
7
8
  # Your code goes here...
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cli_tool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelly Becker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-24 00:00:00.000000000 Z
11
+ date: 2013-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,6 +71,7 @@ files:
71
71
  - cli_tool.gemspec
72
72
  - lib/cli_tool.rb
73
73
  - lib/cli_tool/option_parser.rb
74
+ - lib/cli_tool/remote.rb
74
75
  - lib/cli_tool/software_dependencies.rb
75
76
  - lib/cli_tool/stdin_out.rb
76
77
  - lib/cli_tool/version.rb