Class: LSync::Server

Inherits:
Object
  • Object
show all
Includes:
EventHandler
Defined in:
lib/lsync/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventHandler

#abort!, #fire, #on, #try

Constructor Details

#initialize(host, options = {}) ⇒ Server

Returns a new instance of Server.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lsync/server.rb', line 28

def initialize(host, options = {})
	@options = options
 
	@host = host
	@root = "/"

	@platform = nil

	@shell = Shells::SSH.new

	@enabled = true

	@roles = Set.new
end

Instance Attribute Details

#hostObject

The host name (e.g. DNS entry) for the given server



47
48
49
# File 'lib/lsync/server.rb', line 47

def host
  @host
end

#platformObject

The platform of the server, e.g. linux, used for executing actions.



53
54
55
# File 'lib/lsync/server.rb', line 53

def platform
  @platform
end

#rolesObject

The roles that dictate how the server fits into the overall infratstructure.



44
45
46
# File 'lib/lsync/server.rb', line 44

def roles
  @roles
end

#rootObject

The root path on the server in which all other directories will be relative to.



50
51
52
# File 'lib/lsync/server.rb', line 50

def root
  @root
end

#shellObject

The shell to use to connect to the server.



56
57
58
# File 'lib/lsync/server.rb', line 56

def shell
  @shell
end

Instance Method Details

#connection_string(directory) ⇒ Object

Give a general connection string (e.g “host:/directory” or “/directory” if local).



66
67
68
69
70
71
72
# File 'lib/lsync/server.rb', line 66

def connection_string(directory)
	if local?
		return full_path(directory)
	else
		return @host + ":" + full_path(directory).to_cmd
	end
end

#full_path(directory = "./") ⇒ Object

Give the full path for a particular subdirectory.



59
60
61
62
63
# File 'lib/lsync/server.rb', line 59

def full_path(directory = "./")
	p = File.expand_path(directory.to_s, @root)

	return Pathname.new(p).cleanpath.normalize_trailing_slash
end

#local?Boolean

Checks if the host resolves to the local machine.

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/lsync/server.rb', line 79

def local?
	return true if @host == "localhost"

	hostname = Socket.gethostname

	begin
		hostname = Socket.gethostbyname(hostname)[0]
	rescue SocketError
		puts $!
	end

	return @host == hostname
end

#role?(role) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/lsync/server.rb', line 74

def role?(role)
	@roles.include?(role) || @roles.include?(:all) || role == :any
end

#to_sObject

String representation of the server for logging.



94
95
96
# File 'lib/lsync/server.rb', line 94

def to_s
	"#{@host}:#{full_path}"
end