Class: EY::Serverside::Server

Inherits:
Struct show all
Includes:
LoggedOutput
Defined in:
lib/engineyard-serverside/server.rb

Defined Under Namespace

Classes: DuplicateHostname

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LoggedOutput

#debug, #info, logfile, logfile=, #logged_system, verbose=, verbose?, #verbose?

Methods inherited from Struct

json_create, #to_json

Constructor Details

#initialize(*fields) ⇒ Server

Returns a new instance of Server.



15
16
17
18
# File 'lib/engineyard-serverside/server.rb', line 15

def initialize(*fields)
  super
  self.roles = self.roles.map { |r| r.to_sym } if self.roles
end

Instance Attribute Details

#default_task=(value) ⇒ Object (writeonly)

Sets the attribute default_task

Parameters:

  • value

    the value to set the attribute default_task to.



20
21
22
# File 'lib/engineyard-serverside/server.rb', line 20

def default_task=(value)
  @default_task = value
end

#hostnameObject

Returns the value of attribute hostname

Returns:

  • (Object)

    the current value of hostname



6
7
8
# File 'lib/engineyard-serverside/server.rb', line 6

def hostname
  @hostname
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



6
7
8
# File 'lib/engineyard-serverside/server.rb', line 6

def name
  @name
end

#rolesObject

Returns the value of attribute roles

Returns:

  • (Object)

    the current value of roles



6
7
8
# File 'lib/engineyard-serverside/server.rb', line 6

def roles
  @roles
end

#userObject

Returns the value of attribute user

Returns:

  • (Object)

    the current value of user



6
7
8
# File 'lib/engineyard-serverside/server.rb', line 6

def user
  @user
end

Class Method Details

.add(server_hash) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/engineyard-serverside/server.rb', line 49

def self.add(server_hash)
  hostname = server_hash[:hostname]
  if by_hostname(hostname)
    raise DuplicateHostname.new(hostname)
  end
  server = new(hostname, server_hash[:roles], server_hash[:name], server_hash[:user])
  @all << server
  server
end

.allObject



41
42
43
# File 'lib/engineyard-serverside/server.rb', line 41

def self.all
  @all
end

.by_hostname(hostname) ⇒ Object



45
46
47
# File 'lib/engineyard-serverside/server.rb', line 45

def self.by_hostname(hostname)
  all.find{|s| s.hostname == hostname}
end

.currentObject



59
60
61
# File 'lib/engineyard-serverside/server.rb', line 59

def self.current
  all.find {|s| s.local? }
end

.from_roles(*want_roles) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/engineyard-serverside/server.rb', line 22

def self.from_roles(*want_roles)
  want_roles = want_roles.flatten.compact.map{|r| r.to_sym}
  return all if !want_roles || want_roles.include?(:all) || want_roles.empty?

  all.select do |s|
    !(s.roles & want_roles).empty?
  end
end

.load_all_from_array(server_hashes) ⇒ Object



35
36
37
38
39
# File 'lib/engineyard-serverside/server.rb', line 35

def self.load_all_from_array(server_hashes)
  server_hashes.each do |instance_hash|
    add(instance_hash)
  end
end

.resetObject



63
64
65
# File 'lib/engineyard-serverside/server.rb', line 63

def self.reset
  @all = []
end

Instance Method Details

#local?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/engineyard-serverside/server.rb', line 72

def local?
  hostname == 'localhost'
end

#roleObject



31
32
33
# File 'lib/engineyard-serverside/server.rb', line 31

def role
  roles.first
end

#run(command) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/engineyard-serverside/server.rb', line 82

def run(command)
  if local?
    logged_system(command)
  else
    logged_system(ssh_command + " " + Escape.shell_command(["#{user}@#{hostname}", command]))
  end
end

#ssh_commandObject



90
91
92
# File 'lib/engineyard-serverside/server.rb', line 90

def ssh_command
  "ssh -i #{ENV['HOME']}/.ssh/internal -o StrictHostKeyChecking=no -o PasswordAuthentication=no"
end

#sync_directory(directory) ⇒ Object



76
77
78
79
80
# File 'lib/engineyard-serverside/server.rb', line 76

def sync_directory(directory)
  return if local?
  run "mkdir -p #{directory}"
  logged_system(%|rsync --delete -aq -e "#{ssh_command}" #{directory}/ #{user}@#{hostname}:#{directory}|)
end