Class: EY::Serverside::Server

Inherits:
Struct
  • Object
show all
Defined in:
lib/engineyard-serverside/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#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

.from_hash(server_hash) ⇒ Object



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

def self.from_hash(server_hash)
  new(server_hash[:hostname], Set.new(server_hash[:roles].map{|r|r.to_sym}), server_hash[:name], server_hash[:user])
end

.known_hosts_fileObject

Make a known hosts tempfile to absorb host fingerprints so we don’t show

Warning: Permanently added 'xxx' (RSA) to the list of known hosts.

for every ssh command. (even with StrictHostKeyChecking=no, the warning output is annoying)



80
81
82
# File 'lib/engineyard-serverside/server.rb', line 80

def self.known_hosts_file
  @known_hosts_file ||= Tempfile.new('ey-ss-known-hosts')
end

Instance Method Details

#authorityObject



11
12
13
# File 'lib/engineyard-serverside/server.rb', line 11

def authority
  "#{user}@#{hostname}"
end

#command_on_server(prefix, cmd, &block) ⇒ Object



60
61
62
63
64
# File 'lib/engineyard-serverside/server.rb', line 60

def command_on_server(prefix, cmd, &block)
  command = block ? block.call(self, cmd.dup) : cmd
  command = "#{prefix} #{Escape.shell_command([command])}"
  local? ? command : remote_command(command)
end

#inspectObject



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

def inspect
  name_s = name && ":#{name}"
  "#{hostname}(#{role}#{name_s})"
end

#local?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/engineyard-serverside/server.rb', line 33

def local?
  hostname == 'localhost'
end

#matches_roles?(set) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/engineyard-serverside/server.rb', line 24

def matches_roles?(set)
  (roles & set).any?
end

#remote_command(command) ⇒ Object



70
71
72
# File 'lib/engineyard-serverside/server.rb', line 70

def remote_command(command)
  ssh_command + Escape.shell_command(["#{user}@#{hostname}", command])
end

#roleObject



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

def role
  roles.to_a.first
end

#run(command) {|local? ? command : remote_command(command)| ... } ⇒ Object

Yields:



66
67
68
# File 'lib/engineyard-serverside/server.rb', line 66

def run(command)
  yield local? ? command : remote_command(command)
end

#scp_command(local_file, remote_file) ⇒ Object



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

def scp_command(local_file, remote_file)
  Escape.shell_command([
    'scp',
    '-i', "#{ENV['HOME']}/.ssh/internal",
    "-o", "StrictHostKeyChecking=no",
    "-o", "UserKnownHostsFile=#{self.class.known_hosts_file.path}",
    "-o", "PasswordAuthentication=no",
    local_file,
    "#{authority}:#{remote_file}",
  ])
end

#ssh_commandObject



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

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

#sync_directory_command(directory) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/engineyard-serverside/server.rb', line 37

def sync_directory_command(directory)
  return nil if local?
  [
    remote_command("mkdir -p #{directory}"),
    # File mod times aren't important during deploy, and
    # -a (archive mode) sets --times which causes problems.
    # -a is equivalent to -rlptgoD. We remove the -t, and add -q.
    Escape.shell_command(%w[rsync --delete -rlpgoDq -e] + [ssh_command, "#{directory}/", "#{user}@#{hostname}:#{directory}"])
  ].join(' && ')
end