Class: Server
- Includes:
- DataMapper::Resource
- Defined in:
- lib/fabric/server.rb
Instance Method Summary collapse
- #account_exists_for?(user) ⇒ Boolean
- #accounts ⇒ Object
- #accounts_to_add ⇒ Object
- #accounts_to_remove ⇒ Object
- #create_group(group) ⇒ Object
- #delete_account_for(user) ⇒ Object
- #errors ⇒ Object
- #execute_command(command) ⇒ Object
- #install_accounts! ⇒ Object
- #is_running_plesk? ⇒ Boolean
- #narrate_as ⇒ Object
- #output ⇒ Object
- #remove_dead_accounts ⇒ Object
- #should_account_exist_for?(user) ⇒ Boolean
Instance Method Details
#account_exists_for?(user) ⇒ Boolean
65 66 67 68 69 70 71 72 73 |
# File 'lib/fabric/server.rb', line 65 def account_exists_for?(user) self.execute_command("id #{user.name}") if self.output =~ /uid=/ true else false end end |
#accounts ⇒ Object
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_add ⇒ Object
97 98 99 100 101 |
# File 'lib/fabric/server.rb', line 97 def accounts_to_add self.role.users.select do |user| not self.account_exists_for?(user) end end |
#accounts_to_remove ⇒ Object
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.should_account_exist_for?(user) and self.account_exists_for?(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 delete_account_for(user) narrate "Deleting dead account for #{user.name}" self.execute_command("sudo /usr/sbin/userdel --remove #{user.name}") end |
#errors ⇒ Object
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 |account| account.add_user account.add_ssh_directory account.write_ssh_key account.add_to_groups end end |
#is_running_plesk? ⇒ 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_as ⇒ Object
18 19 20 |
# File 'lib/fabric/server.rb', line 18 def narrate_as self.host end |
#output ⇒ Object
48 49 50 |
# File 'lib/fabric/server.rb', line 48 def output self.captures.first(:name => 'output').contents end |
#remove_dead_accounts ⇒ Object
90 91 92 93 94 95 |
# File 'lib/fabric/server.rb', line 90 def remove_dead_accounts accounts_to_remove.each do |user| self.delete_account_for(user) end narrate "Dead accounts removed" end |
#should_account_exist_for?(user) ⇒ Boolean
61 62 63 |
# File 'lib/fabric/server.rb', line 61 def should_account_exist_for?(user) self.accounts.include?(user) end |