Class: LSync::ServerController

Inherits:
BasicController show all
Defined in:
lib/lsync/controller.rb

Overview

The server controller provides event handlers with a unified interface for dealing with servers and associated actions.

Instance Attribute Summary collapse

Attributes inherited from BasicController

#logger, #script

Instance Method Summary collapse

Constructor Details

#initialize(script, logger, server) ⇒ ServerController

Returns a new instance of ServerController.


69
70
71
72
73
74
75
76
# File 'lib/lsync/controller.rb', line 69

def initialize(script, logger, server)
	super(script, logger)

	@server = server

	@connection = nil
	@platform = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object


173
174
175
# File 'lib/lsync/controller.rb', line 173

def method_missing(name, *args, &block)
	@server.send(name, *args, &block)
end

Instance Attribute Details

#serverObject (readonly)

The current server.


79
80
81
# File 'lib/lsync/controller.rb', line 79

def server
  @server
end

Instance Method Details

#==(other) ⇒ Object


165
166
167
# File 'lib/lsync/controller.rb', line 165

def ==(other)
	@server == other.server
end

#exec(command, options = {}, &block) ⇒ Object

Run a command on the given server using this shell.


142
143
144
145
146
147
148
149
# File 'lib/lsync/controller.rb', line 142

def exec(command, options = {}, &block)
	unless @server.local?
		command = @server.shell.connection_command(@server) + ["--"] + command
	end

	@logger.info "Executing #{command.to_cmd} on #{@server}"
	RExec::Task.open(command, options, &block)
end

#exec!(command, options = {}) ⇒ Object


151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/lsync/controller.rb', line 151

def exec!(command, options = {})
	exec(command, options) do |task|
		task.input.close

		result = task.wait

		unless result.exitstatus == 0
			raise CommandFailure.new(command, result.exitstatus)
		end

		return task.output.read
	end
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)

169
170
171
# File 'lib/lsync/controller.rb', line 169

def respond_to?(name)
	@server.respond_to?(name) || super(name)
end

#run(command, options = {}) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/lsync/controller.rb', line 81

def run(command, options = {})
	task = nil
	
	root ||= options[:root] || @server.root
	
	if String === command
		command = [command]
	end
	
	begin
		connection, task = @server.shell.connect(@server)
		connection.send_object([:chdir, root])
		
		# Convert all arguments to strings for execution.
		# For some reason, the parent process hangs if you don't have this.. need to investigate further.
		command = command.collect{|arg| arg.to_s}
		
		if options[:script]
			data = command[0]
			
			command = command.dup
			
			# Descriptive name can be provided by options[:script].
			case options[:script]
			when String
				command[0] = options[:script]
			else
				command[0] = "script"
			end
			
			@logger.info "Running script #{command.to_cmd} on #{@server}"
			
			connection.send_object([:script, command, data])
		elsif options[:remote]
			@logger.info "Running script #{command.to_cmd} on #{@server}"
			
			data = File.read(command[0])
			connection.send_object([:script, command, data])
		else
			@logger.info "Running command #{command.to_cmd} on #{@server}"
			connection.send_object([:exec, command])
		end
		
		if block_given?
			yield task
		else
			LSync::log_task(task, @logger)
		end
	ensure
		if task
			# task.stop
			result = task.wait
			
			unless result.exitstatus == 0
				raise CommandFailure.new(command, result.exitstatus)
			end
		end
	end
end