Class: Inprovise::Infrastructure::Node
- Inherits:
-
Target
- Object
- Target
- Inprovise::Infrastructure::Node
show all
- Defined in:
- lib/inprovise/node.rb
Instance Attribute Summary collapse
Attributes inherited from Target
#config, #name
Class Method Summary
collapse
Instance Method Summary
collapse
-
#binary_exists?(bin) ⇒ Boolean
-
#cat(path) ⇒ Object
-
#channel ⇒ Object
-
#copy(from, to) ⇒ Object
-
#delete(path) ⇒ Object
-
#directory?(path) ⇒ Boolean
-
#disconnect! ⇒ Object
-
#download(from, to) ⇒ Object
-
#echo(arg) ⇒ Object
-
#env(var) ⇒ Object
-
#exists?(path) ⇒ Boolean
-
#file?(path) ⇒ Boolean
-
#for_dir(path) ⇒ Object
-
#for_user(new_user, user_key = nil) ⇒ Object
-
#group(path) ⇒ Object
-
#hash_for(path) ⇒ Object
-
#helper ⇒ Object
-
#initialize(name, config = {}) ⇒ Node
constructor
-
#log ⇒ Object
-
#log_to(log) ⇒ Object
-
#mkdir(path) ⇒ Object
-
#move(from, to) ⇒ Object
-
#owner(path) ⇒ Object
-
#permissions(path) ⇒ Object
-
#prepare_connection_for_user!(new_user) ⇒ Object
-
#run(cmd, opts = {}) ⇒ Object
generic command execution.
-
#set_owner(path, user, group = nil) ⇒ Object
-
#set_permissions(path, perm) ⇒ Object
-
#sudo(cmd, opts = {}) ⇒ Object
-
#to_json(*a) ⇒ Object
-
#to_s ⇒ Object
-
#upload(from, to) ⇒ Object
Methods inherited from Target
#add_target, #add_to, #get, #includes?, #remove_from, #remove_target, #set, #targets, #targets_with_config
Constructor Details
#initialize(name, config = {}) ⇒ Node
Returns a new instance of Node.
11
12
13
14
15
16
17
18
19
|
# File 'lib/inprovise/node.rb', line 11
def initialize(name, config={})
@host = config[:host] || name
@user = config[:user] || 'root'
@channel = nil
@helper = nil
@history = []
@user_nodes = {}
super(name, config)
end
|
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
9
10
11
|
# File 'lib/inprovise/node.rb', line 9
def host
@host
end
|
#user ⇒ Object
Returns the value of attribute user.
9
10
11
|
# File 'lib/inprovise/node.rb', line 9
def user
@user
end
|
Instance Method Details
#binary_exists?(bin) ⇒ Boolean
170
171
172
173
174
175
|
# File 'lib/inprovise/node.rb', line 170
def binary_exists?(bin)
log.execute("BINARY_EXISTS?: #{bin}") if Inprovise.verbosity > 0
rc = helper.binary_exists?(bin)
log.execute("BINARY_EXISTS?: #{rc}") if Inprovise.verbosity > 0
rc
end
|
#cat(path) ⇒ Object
84
85
86
87
88
89
|
# File 'lib/inprovise/node.rb', line 84
def cat(path)
log.execute("CAT: #{path}") if Inprovise.verbosity > 0
out = helper.cat(path)
log.execute("CAT: #{out}") if Inprovise.verbosity > 0
out
end
|
#channel ⇒ Object
21
22
23
|
# File 'lib/inprovise/node.rb', line 21
def channel
@channel ||= Inprovise::CmdChannel.open(self, config[:channel])
end
|
#copy(from, to) ⇒ Object
124
125
126
127
|
# File 'lib/inprovise/node.rb', line 124
def copy(from, to)
log.execute("COPY: #{from} #{to}") if Inprovise.verbosity > 0
helper.copy(from, to)
end
|
#delete(path) ⇒ Object
134
135
136
137
|
# File 'lib/inprovise/node.rb', line 134
def delete(path)
log.execute("DELETE: #{path}") if Inprovise.verbosity > 0
helper.delete(path)
end
|
#directory?(path) ⇒ Boolean
117
118
119
120
121
122
|
# File 'lib/inprovise/node.rb', line 117
def directory?(path)
log.execute("DIRECTORY?: #{path}") if Inprovise.verbosity > 0
rc = helper.directory?(path)
log.execute("DIRECTORY?: #{rc}") if Inprovise.verbosity > 0
rc
end
|
#disconnect! ⇒ Object
29
30
31
32
33
|
# File 'lib/inprovise/node.rb', line 29
def disconnect!
@user_nodes.each_value {|n| n.disconnect! }
@channel.close if @channel
self
end
|
#download(from, to) ⇒ Object
63
64
65
66
|
# File 'lib/inprovise/node.rb', line 63
def download(from, to)
log.execute("DOWLOAD: #{to} <= #{from}") if Inprovise.verbosity > 0
helper.download(from, to)
end
|
#echo(arg) ⇒ Object
70
71
72
73
74
75
|
# File 'lib/inprovise/node.rb', line 70
def echo(arg)
log.execute("ECHO: #{arg}") if Inprovise.verbosity > 0
out = helper.echo(arg)
log.execute("ECHO: #{out}") if Inprovise.verbosity > 0
out
end
|
#env(var) ⇒ Object
77
78
79
80
81
82
|
# File 'lib/inprovise/node.rb', line 77
def env(var)
log.execute("ENV: #{var}") if Inprovise.verbosity > 0
val = helper.env(var)
log.execute("ENV: #{val}") if Inprovise.verbosity > 0
val
end
|
#exists?(path) ⇒ Boolean
103
104
105
106
107
108
|
# File 'lib/inprovise/node.rb', line 103
def exists?(path)
log.execute("EXISTS?: #{path}") if Inprovise.verbosity > 0
rc = helper.exists?(path)
log.execute("EXISTS?: #{rc}") if Inprovise.verbosity > 0
rc
end
|
#file?(path) ⇒ Boolean
110
111
112
113
114
115
|
# File 'lib/inprovise/node.rb', line 110
def file?(path)
log.execute("FILE?: #{path}") if Inprovise.verbosity > 0
rc = helper.file?(path)
log.execute("FILE?: #{rc}") if Inprovise.verbosity > 0
rc
end
|
#for_dir(path) ⇒ Object
196
197
198
199
200
201
|
# File 'lib/inprovise/node.rb', line 196
def for_dir(path)
user_key = "#{self.user}:#{path}"
return @user_nodes[user_key] if @user_nodes[user_key]
new_node = self.dup
new_node.prepare_connection_for_user!(self.user)
end
|
#for_user(new_user, user_key = nil) ⇒ Object
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/inprovise/node.rb', line 185
def for_user(new_user, user_key=nil)
new_user = new_user.to_s
return self if self.user == new_user
user_key ||= new_user
return @user_nodes[user_key] if @user_nodes[user_key]
new_node = self.dup
new_node.prepare_connection_for_user!(new_user)
@user_nodes[user_key] = new_node
new_node
end
|
#group(path) ⇒ Object
158
159
160
161
162
163
|
# File 'lib/inprovise/node.rb', line 158
def group(path)
log.execute("GROUP: #{path}") if Inprovise.verbosity > 0
group = helper.group(path)
log.execute("OWNER: #{group}") if Inprovise.verbosity > 0
group
end
|
#hash_for(path) ⇒ Object
91
92
93
94
95
96
|
# File 'lib/inprovise/node.rb', line 91
def hash_for(path)
log.execute("HASH_FOR: #{path}") if Inprovise.verbosity > 0
hsh = helper.hash_for(path)
log.execute("HASH_FOR: #{hsh}") if Inprovise.verbosity > 0
hsh
end
|
#helper ⇒ Object
25
26
27
|
# File 'lib/inprovise/node.rb', line 25
def helper
@helper ||= Inprovise::CmdHelper.get(self, config[:helper])
end
|
#log ⇒ Object
177
178
179
|
# File 'lib/inprovise/node.rb', line 177
def log
@log ||= Inprovise::Logger.new(self, nil)
end
|
#log_to(log) ⇒ Object
181
182
183
|
# File 'lib/inprovise/node.rb', line 181
def log_to(log)
@log = log
end
|
#mkdir(path) ⇒ Object
98
99
100
101
|
# File 'lib/inprovise/node.rb', line 98
def mkdir(path)
log.execute("MKDIR: #{path}") if Inprovise.verbosity > 0
helper.mkdir(path)
end
|
#move(from, to) ⇒ Object
129
130
131
132
|
# File 'lib/inprovise/node.rb', line 129
def move(from, to)
log.execute("MOVE: #{from} #{to}") if Inprovise.verbosity > 0
helper.move(from, to)
end
|
#owner(path) ⇒ Object
151
152
153
154
155
156
|
# File 'lib/inprovise/node.rb', line 151
def owner(path)
log.execute("OWNER: #{path}") if Inprovise.verbosity > 0
owner = helper.owner(path)
log.execute("OWNER: #{owner}") if Inprovise.verbosity > 0
owner
end
|
#permissions(path) ⇒ Object
139
140
141
142
143
144
|
# File 'lib/inprovise/node.rb', line 139
def permissions(path)
log.execute("PERMISSIONS: #{path}") if Inprovise.verbosity > 0
perm = helper.permissions(path)
log.execute("PERMISSIONS: #{'%o' % perm}") if Inprovise.verbosity > 0
perm
end
|
#prepare_connection_for_user!(new_user) ⇒ Object
203
204
205
206
207
208
209
210
|
# File 'lib/inprovise/node.rb', line 203
def prepare_connection_for_user!(new_user)
@user = new_user
@channel = nil
@helper = nil
@user_nodes = {}
@history = []
@log = Inprovise::Logger.new(self, @log.task) if @log
end
|
#run(cmd, opts = {}) ⇒ Object
generic command execution
37
38
39
40
41
42
43
44
|
# File 'lib/inprovise/node.rb', line 37
def run(cmd, opts={})
log.execute("RUN: #{cmd}") if Inprovise.verbosity > 0
if should_run?(cmd, opts)
really_run(cmd, opts)
else
cached_run(cmd, opts)
end
end
|
#set_owner(path, user, group = nil) ⇒ Object
165
166
167
168
|
# File 'lib/inprovise/node.rb', line 165
def set_owner(path, user, group=nil)
log.execute("SET_OWNER: #{path} #{user}#{group ? " #{group}" : ''}") if Inprovise.verbosity > 0
helper.set_owner(path, user, group)
end
|
#set_permissions(path, perm) ⇒ Object
146
147
148
149
|
# File 'lib/inprovise/node.rb', line 146
def set_permissions(path, perm)
log.execute("SET_PERMISSIONS: #{path} #{'%o' % perm}") if Inprovise.verbosity > 0
helper.set_permissions(path, perm)
end
|
#sudo(cmd, opts = {}) ⇒ Object
46
47
48
49
50
51
52
53
54
|
# File 'lib/inprovise/node.rb', line 46
def sudo(cmd, opts={})
log.execute("SUDO: #{cmd}") if Inprovise.verbosity > 0
opts = opts.merge({:sudo => true})
if should_run?(cmd, opts)
really_run(cmd, opts)
else
cached_run(cmd, opts)
end
end
|
#to_json(*a) ⇒ Object
225
226
227
228
229
230
231
232
233
|
# File 'lib/inprovise/node.rb', line 225
def to_json(*a)
{
JSON.create_id => self.class.name,
:data => {
:name => name,
:config => safe_config
}
}.to_json(*a)
end
|
#to_s ⇒ Object
212
213
214
|
# File 'lib/inprovise/node.rb', line 212
def to_s
"#{name}(#{user}@#{host})"
end
|
#upload(from, to) ⇒ Object
58
59
60
61
|
# File 'lib/inprovise/node.rb', line 58
def upload(from, to)
log.execute("UPLOAD: #{from} => #{to}") if Inprovise.verbosity > 0
helper.upload(from, to)
end
|