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
69 70 71 72 73 74 75 76 |
# File 'lib/fabric/server.rb', line 69 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
100 101 102 103 104 |
# File 'lib/fabric/server.rb', line 100 def accounts_to_add self.role.users.select do |user| not self.account_exists_for?(user) end end |
#accounts_to_remove ⇒ Object
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.account_exists_for?(user) and not self.should_account_exist_for?(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 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
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 |account| account.add_user account.add_ssh_directory account.write_ssh_key account.add_to_groups end end |
#is_running_plesk? ⇒ 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_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
93 94 95 96 97 98 |
# File 'lib/fabric/server.rb', line 93 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 64 65 66 67 |
# File 'lib/fabric/server.rb', line 61 def should_account_exist_for?(user) self.accounts.each do |account| return true if account.user == user end false end |