Class: Server

Inherits:
Object show all
Includes:
DataMapper::Resource
Defined in:
lib/fabric/server.rb

Instance Method Summary collapse

Instance Method Details

#account_exists_for?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

def (user)
  self.execute_command("id #{user.name}")

  if self.output =~ /uid=/
    true
  else
    false
  end
end

#accountsObject



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

def accounts
  return [] unless self.role
  @accounts ||= self.role.users.collect { |user| Account.create(:user => user, :server => self) }
end

#accounts_to_addObject



97
98
99
100
101
# File 'lib/fabric/server.rb', line 97

def accounts_to_add
  self.role.users.select do |user|
    not self.(user)
  end
end

#accounts_to_removeObject



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

def accounts_to_remove
  narrate "Checking for dead accounts"
  users = User.all

  users.reject do |user|
    self.(user) and self.(user)
  end
end

#create_group(group) ⇒ Object



112
113
114
# File 'lib/fabric/server.rb', line 112

def create_group(group)
  self.execute_command("sudo /usr/sbin/groupadd #{group.name}")
end

#delete_account_for(user) ⇒ Object



75
76
77
78
# File 'lib/fabric/server.rb', line 75

def (user)
  narrate "Deleting dead account for #{user.name}"
  self.execute_command("sudo /usr/sbin/userdel --remove #{user.name}")
end

#errorsObject



52
53
54
# File 'lib/fabric/server.rb', line 52

def errors
  self.captures.first(:name => 'errors').contents
end

#execute_command(command) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fabric/server.rb', line 22

def execute_command(command)
  self.clear_captures!
  
  narrate "executing: #{command}"

  self.connection.open_channel do |channel, success|
    # Works without this with passwordless sudo locally - with it on, errors end up in on_data, not on_extended_data, annoyingly.
    channel.request_pty

    channel.exec command do |ch, success|

      # "on_extended_data" is called when the process writes something to stderr
      ch.on_extended_data do |c, type, data|
        self.capture :errors, data
      end

      # "on_data" is called when the process writes something to stdout
      ch.on_data do |c, data|
        self.capture :output, data
      end
    end
  end

  self.connection.loop
end

#install_accounts!Object



80
81
82
83
84
85
86
87
88
# File 'lib/fabric/server.rb', line 80

def install_accounts!
  narrate "Installing accounts"
  self.accounts.each do ||
    .add_user
    .add_ssh_directory
    .write_ssh_key
    .add_to_groups
  end
end

#is_running_plesk?Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
124
# File 'lib/fabric/server.rb', line 116

def is_running_plesk?
  self.execute_command("if [ -d /usr/local/psa ]; then echo 'plesk'; else echo 'not plesk'; fi")

  case self.output.strip
    when 'plesk' then true
    when 'not plesk' then false
    else raise "Couldn't detect if this server is running plesk."
  end
end

#narrate_asObject



18
19
20
# File 'lib/fabric/server.rb', line 18

def narrate_as
  self.host
end

#outputObject



48
49
50
# File 'lib/fabric/server.rb', line 48

def output
  self.captures.first(:name => 'output').contents
end

#remove_dead_accountsObject



90
91
92
93
94
95
# File 'lib/fabric/server.rb', line 90

def remove_dead_accounts
  accounts_to_remove.each do |user|
    self.(user)
  end
  narrate "Dead accounts removed"
end

#should_account_exist_for?(user) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/fabric/server.rb', line 61

def (user)
  self.accounts.include?(user)
end