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)


69
70
71
72
73
74
75
76
# File 'lib/fabric/server.rb', line 69

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



100
101
102
103
104
# File 'lib/fabric/server.rb', line 100

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

#accounts_to_removeObject



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

def accounts_to_remove
  narrate "Checking for dead accounts"
  users = User.all.select do |user|
    self.(user) and not self.(user)
  end
end

#create_group(group) ⇒ Object



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

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

#delete_account_for(user) ⇒ Object



78
79
80
81
# File 'lib/fabric/server.rb', line 78

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



83
84
85
86
87
88
89
90
91
# File 'lib/fabric/server.rb', line 83

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)


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

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



93
94
95
96
97
98
# File 'lib/fabric/server.rb', line 93

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
64
65
66
67
# File 'lib/fabric/server.rb', line 61

def (user)
  self.accounts.each do ||
    return true if .user == user
  end
  
  false
end