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.



38
39
40
41
42
43
44
45
# File 'lib/lsync/controller.rb', line 38

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



134
135
136
# File 'lib/lsync/controller.rb', line 134

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

Instance Attribute Details

#serverObject (readonly)

The current server.



48
49
50
# File 'lib/lsync/controller.rb', line 48

def server
  @server
end

Instance Method Details

#==(other) ⇒ Object



126
127
128
# File 'lib/lsync/controller.rb', line 126

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

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

Run a command on the given server using this shell.



103
104
105
106
107
108
109
110
# File 'lib/lsync/controller.rb', line 103

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

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

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



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/lsync/controller.rb', line 112

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

		result = task.wait

		unless result.exitstatus == 0
			raise ShellScriptError.new("Command #{command.inspect} failed: #{result.exitstatus}", result.exitstatus)
		end

		return task.output.read
	end
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/lsync/controller.rb', line 130

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

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/lsync/controller.rb', line 50

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])
		
		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.inspect} on #{@server}"
			
			connection.send_object([:script, command, data])
		elsif options[:remote]
			@logger.info "Running script #{command.inspect} on #{@server}"
			
			data = File.read(command[0])
			connection.send_object([:script, command, data])
		else
			@logger.info "Running command #{command.inspect} 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
			task.wait
		end
	end
end