cli_tool 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/cli_tool/remote.rb +64 -30
- data/lib/cli_tool/version.rb +1 -1
- metadata +2 -2
data/lib/cli_tool/remote.rb
CHANGED
@@ -13,12 +13,13 @@ module CliTool
|
|
13
13
|
attr_accessor :environment
|
14
14
|
attr_accessor :indent
|
15
15
|
|
16
|
-
def initialize
|
17
|
-
@environment = {}
|
16
|
+
def initialize(indent = 2, opts = {})
|
17
|
+
@environment = opts[:environment] || {}
|
18
18
|
@commands = []
|
19
19
|
@apt = {}
|
20
20
|
@remote_installed = 0
|
21
|
-
@indent =
|
21
|
+
@indent = indent
|
22
|
+
@sudo = opts[:sudo] || false
|
22
23
|
end
|
23
24
|
alias reset initialize
|
24
25
|
|
@@ -106,15 +107,31 @@ module CliTool
|
|
106
107
|
if?((exist ? '' : '! ') + %{-f "#{file}"}, &block)
|
107
108
|
end
|
108
109
|
|
109
|
-
def directory_exist(directory, exist = true, &block)
|
110
|
-
if?((exist ? '' : '! ') + %{-d "#{
|
110
|
+
def directory_exist?(directory, exist = true, &block)
|
111
|
+
if?((exist ? '' : '! ') + %{-d "#{directory}"}, &block)
|
112
|
+
end
|
113
|
+
|
114
|
+
def for_in(value, source, &block)
|
115
|
+
for_script = "for #{value} in #{source}\n#{"\t" * (@indent + 2)}do\n\n"
|
116
|
+
for_script << Script.new(@indent + 2, environment: @environment).__send__(:instance_exec, &block).to_s(env: false)
|
117
|
+
for_script << "\n#{"\t" * @indent}done"
|
118
|
+
@commands << for_script << ''
|
119
|
+
self
|
120
|
+
end
|
121
|
+
|
122
|
+
def if?(condition, &block)
|
123
|
+
if_script = "if [ #{condition} ]\n#{"\t" * (@indent + 2)}then\n\n"
|
124
|
+
if_script << Script.new(@indent + 2, environment: @environment).__send__(:instance_exec, &block).to_s(env: false)
|
125
|
+
if_script << "\n#{"\t" * @indent}fi"
|
126
|
+
@commands << if_script << ''
|
127
|
+
self
|
111
128
|
end
|
112
129
|
|
113
130
|
##########
|
114
131
|
#= Exec =#
|
115
132
|
##########
|
116
133
|
|
117
|
-
def exec(script,
|
134
|
+
def exec(script, use_sudo = false, sudo_user = :root)
|
118
135
|
if File.exist?(script)
|
119
136
|
script = File.read(script)
|
120
137
|
end
|
@@ -126,19 +143,44 @@ module CliTool
|
|
126
143
|
script = script.map { |x| x.gsub(/#{indents}/, '') }.join("\n")
|
127
144
|
end
|
128
145
|
|
129
|
-
#
|
130
|
-
if
|
131
|
-
|
132
|
-
|
133
|
-
|
146
|
+
# Use sudo if requested
|
147
|
+
if use_sudo && ! @sudo
|
148
|
+
return sudo(sudo_user) do
|
149
|
+
exec(script)
|
150
|
+
end
|
134
151
|
end
|
135
152
|
|
136
|
-
@commands << script.rstrip
|
153
|
+
@commands << script.rstrip
|
154
|
+
self
|
155
|
+
end
|
156
|
+
|
157
|
+
def sudo(user = :root, &block)
|
158
|
+
sudo_script = %{sudo su -c "/bin/bash -i -s --" #{user} <<-SCRIPT\n}
|
159
|
+
sudo_script << Script.new(@indent + 2, sudo: true, environment: @environment).__send__(:instance_exec, &block).to_s(heredoc: true)
|
160
|
+
sudo_script << "\n#{"\t" * @indent}SCRIPT"
|
161
|
+
@commands << sudo_script << ''
|
137
162
|
self
|
138
163
|
end
|
139
164
|
|
140
|
-
def to_s(
|
141
|
-
|
165
|
+
def to_s(opts = {})
|
166
|
+
env = opts[:env] == false ? [] : (get_environment_exports << '')
|
167
|
+
result = env.concat(@commands).map! do |line|
|
168
|
+
("\t" * @indent) + line
|
169
|
+
end
|
170
|
+
|
171
|
+
# Join the commands
|
172
|
+
result = result.join("\n")
|
173
|
+
|
174
|
+
if @indent < 3
|
175
|
+
result = result.gsub('$', '\$')
|
176
|
+
result << "\n"
|
177
|
+
end
|
178
|
+
|
179
|
+
if opts[:heredoc] == true
|
180
|
+
result = result.gsub('$', '\\\\\$')
|
181
|
+
end
|
182
|
+
|
183
|
+
result
|
142
184
|
end
|
143
185
|
|
144
186
|
private
|
@@ -163,15 +205,10 @@ module CliTool
|
|
163
205
|
self
|
164
206
|
end
|
165
207
|
|
166
|
-
def
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
def get_environment_exports(indent = 0)
|
172
|
-
@environment.reduce([]) { |out, (key, val)|
|
173
|
-
out << %{export #{(' ' * indent)}#{key.upcase}=#{val}}
|
174
|
-
}.join("\n") << "\n"
|
208
|
+
def get_environment_exports
|
209
|
+
@environment.reduce([]) do |out, (key, val)|
|
210
|
+
out << "export #{key.upcase}=#{val}"
|
211
|
+
end
|
175
212
|
end
|
176
213
|
end
|
177
214
|
|
@@ -294,15 +331,16 @@ module CliTool
|
|
294
331
|
end
|
295
332
|
|
296
333
|
def remote_exec!(script)
|
297
|
-
ssh_cmd =[ 'ssh
|
334
|
+
ssh_cmd =[ 'ssh' ]
|
298
335
|
ssh_cmd << "-i #{@identity}" if @identity
|
299
336
|
ssh_cmd << "-p #{@port}" if @port
|
300
337
|
ssh_cmd << "#{@username}@#{@host}"
|
301
|
-
ssh_cmd << "/bin/bash -s"
|
338
|
+
ssh_cmd << "/bin/bash -i -s --"
|
339
|
+
ssh_cmd << "<<-EOF\n#{script}\nexit;\nEOF"
|
302
340
|
|
303
341
|
# Show debug script
|
304
342
|
if self.debug
|
305
|
-
pretty_cmd = ssh_cmd.
|
343
|
+
pretty_cmd = ssh_cmd.join(' ')
|
306
344
|
message = "About to run remote process over ssh on #{@username}@#{@host}:#{@port}"
|
307
345
|
puts message, :blue
|
308
346
|
puts '#' * message.length, :blue
|
@@ -313,10 +351,6 @@ module CliTool
|
|
313
351
|
sleep 2
|
314
352
|
end
|
315
353
|
|
316
|
-
#ssh_cmd << %{"#{script.gsub(/"/, '\\\1')}"}
|
317
|
-
ssh_cmd << "<<-SCRIPT\n#{script}\nexit;\nSCRIPT"
|
318
|
-
ssh_cmd << %{| grep -v 'stdin: is not a tty'}
|
319
|
-
|
320
354
|
puts "Running Script", [:blue, :italic]
|
321
355
|
|
322
356
|
# Run command if a connection is available
|
data/lib/cli_tool/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cli_tool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-02-
|
12
|
+
date: 2014-02-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|