class LSync::ServerController

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

Attributes

server[R]

The current server.

Public Class Methods

new(script, logger, server) click to toggle source
Calls superclass method LSync::BasicController::new
# File lib/lsync/controller.rb, line 69
def initialize(script, logger, server)
        super(script, logger)

        @server = server

        @connection = nil
        @platform = nil
end

Public Instance Methods

==(other) click to toggle source
# File lib/lsync/controller.rb, line 165
def ==(other)
        @server == other.server
end
exec(command, options = {}, &block) click to toggle source

Run a command on the given server using this shell.

# 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 = {}) click to toggle source
# 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
method_missing(name, *args, &block) click to toggle source
# File lib/lsync/controller.rb, line 173
def method_missing(name, *args, &block)
        @server.send(name, *args, &block)
end
respond_to?(name) click to toggle source
Calls superclass method
# File lib/lsync/controller.rb, line 169
def respond_to?(name)
        @server.respond_to?(name) || super(name)
end
run(command, options = {}) { |task| ... } click to toggle source
# 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