Class: GitPusshuTen::Commands::User

Inherits:
Base
  • Object
show all
Defined in:
lib/gitpusshuten/commands/user.rb

Instance Attribute Summary

Attributes inherited from Base

#cli, #command, #configuration, #environment, #hooks, #perform_hooks

Instance Method Summary collapse

Methods inherited from Base

#c, #command_object, description, #e, #error, example, #g, #git, #help, #local, long_description, #message, #perform!, #perform_hooks!, #post_perform!, #pre_perform!, #prompt_for_root_password!, #prompt_for_user_password!, #r, #requires_user_existence!, #silent, #standard, usage, #validate!, #warning, #y, #yes?

Constructor Details

#initialize(*objects) ⇒ User

Returns a new instance of User.



14
15
16
17
18
19
20
# File 'lib/gitpusshuten/commands/user.rb', line 14

def initialize(*objects)
  super

  @command = cli.arguments.shift

  help if command.nil? or e.name.nil?
end

Instance Method Details

#configure_user!Object

Configures the user. Overwrites all current configurations (if any exist)



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/gitpusshuten/commands/user.rb', line 178

def configure_user!
  message "Configuring #{y(c.user)}."

  ##
  # If the user has an SSH key and it hasn't been installed
  # on the server under the current user then it'll go ahead and install it
  if e.has_ssh_key? and not e.ssh_key_installed?
    perform_install_ssh_key!
  end

  ##
  # Configure .bashrc
  Spinner.return :message => "Configuring #{y('.bashrc')}.." do
    e.execute_as_root("echo -e \"export RAILS_ENV=production\nsource /etc/profile\" > '#{File.join(c.path, '.bashrc')}'")
    g('Done!')
  end

  ##
  # Creating .gemrc
  Spinner.return :message => "Configuring #{y('.gemrc')}.." do
    e.download_packages!(e.home_dir)
    e.execute_as_root("cd #{e.home_dir}; cat gitpusshuten-packages/modules/rvm/gemrc > .gemrc")
    e.clean_up_packages!(e.home_dir)
    g('Done!')
  end

  ##
  # Add user to sudoers file if not already in sudo'ers
  if not e.user_in_sudoers?
    Spinner.return :message => "Adding #{y(c.user)} to sudo-ers.." do
      e.add_user_to_sudoers!
      g('Done!')
    end
  end

  ##
  # Checks to see if the RVM group exists.
  # If it does exist, perform RVM specific tasks.
  Spinner.return :message => "Searching for #{y('RVM')} (Ruby version Manager).." do
    @rvm_found = e.directory?("/usr/local/rvm")
    if @rvm_found
      g("Found!")
    else
      y("Not found, skipping.")
    end
  end
  if @rvm_found
    Spinner.return :message => "Adding #{y(c.user)} to the #{y('RVM')} group.." do
      e.execute_as_root("usermod -G rvm '#{c.user}'")
      g('Done!')
    end
  end

  ##
  # Installs the .gitconfig and minimum configuration
  # if the configuration file does not exist.
  Spinner.return :message => "Configuring #{y('Git')} for #{y(c.user)}." do
    e.install_gitconfig!
    e.install_pushand!
    g('Done!')
  end

  ##
  # Ensure home directory ownership is set to the user
  Spinner.return :message => "Setting permissions.." do
    e.execute_as_root("chown -R #{c.user}:#{c.user} '#{e.home_dir}'")
    g('Done!')
  end

  message "Finished configuring #{y(c.user)}."
end

#ensure_common_dependencies_are_installed!Object

Installs common dependencies that applications typically use for XML parsing and image manipulation



172
173
174
# File 'lib/gitpusshuten/commands/user.rb', line 172

def ensure_common_dependencies_are_installed!
  e.install!('libxml2-dev libxslt1-dev imagemagick')
end

#ensure_git_installed!Object

Ensures that Git is installed on the remote server



165
166
167
# File 'lib/gitpusshuten/commands/user.rb', line 165

def ensure_git_installed!
  e.install!('git-core')
end

#perform_add!Object

Sets up a new UNIX user and configures it based on the .gitpusshuten/config.rb



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gitpusshuten/commands/user.rb', line 24

def perform_add!
  if not e.user_exists? # prompts root
    message "It looks like #{y(c.user)} does not yet exist."
    message "Would you like to add #{y(c.user)} to #{y(c.application)} (#{y(c.ip)})?"
    if yes?
      message "Adding #{y(c.user)} to #{y(c.ip)}.."
      if e.add_user!
        message "Successfully added #{y(c.user)} to #{y(c.application)} (#{y(c.ip)})!"
      else
        error "Failed to add user #{y(c.user)} to #{y(c.application)} (#{y(c.ip)})."
        error "An error occurred. Is your configuration file properly configured?"
        exit
      end
    end
  else
    error "User #{y(c.user)} already exists."
    error "If you want to remove this user, run the following command:"
    standard "\n\s\s#{y("heavenly user remove from #{e.name}")}\n\n"
    error "If you just want to reconfigure the user without removing it, run the following ommand:"
    standard "\n\s\s#{(y("heavenly user reconfigure for #{e.name}"))}"
    exit
  end

  ##
  # Install Git if it isn't installed yet
  Spinner.return :message => "Ensuring #{y('Git')} is installed.." do
    ensure_git_installed!
    g('Done!')
  end

  ##
  # Install common application dependencies like xmllib and imagemagick
  Spinner.return :message => "Ensuring #{y('common application dependencies')} are installed.." do
    ensure_common_dependencies_are_installed!
    g('Done!')
  end

  ##
  # Configures the user
  configure_user!

  ##
  # Finished adding user!
  message "Finished adding and configuring #{y(c.user)}!"
  message "You should now be able to push your application to #{y(c.application)} at #{y(c.ip)}."
end

#perform_install_root_ssh_key!Object

Installs the ssh key for the root user



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/gitpusshuten/commands/user.rb', line 146

def perform_install_root_ssh_key!
  unless e.has_ssh_key?
    error "Could not find ssh key in #{y(e.ssh_key_path)}"
    error "To create one, run: #{y('ssh-keygen -t rsa')}"
    exit
  end

  unless e.root_ssh_key_installed? # prompts root
    Spinner.return :message => "Installing #{"SSH Key".color(:yellow)}.." do
      e.install_root_ssh_key!
      g("Done!")
    end
  else
    message "Your ssh has already been installed for #{y('root')} at #{y(c.ip)}."
  end
end

#perform_install_ssh_key!Object

Installs the ssh key for the application user



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/gitpusshuten/commands/user.rb', line 127

def perform_install_ssh_key!
  unless e.has_ssh_key?
    error "Could not find ssh key in #{y(e.ssh_key_path)}"
    error "To create one, run: #{y('ssh-keygen -t rsa')}"
    exit
  end

  unless e.ssh_key_installed? # prompts root
    Spinner.return :message => "Installing #{"SSH Key".color(:yellow)}.." do
      e.install_ssh_key!
      g("Done!")
    end
  else
    message "Your ssh has already been installed for #{y(c.user)} at #{y(c.ip)}."
  end
end

#perform_login!Object

Returns a string which can be used to login the user



110
111
112
113
114
115
116
117
# File 'lib/gitpusshuten/commands/user.rb', line 110

def perform_login!
  if not e.user_exists?
    error "Cannot login, #{y(c.user)} does not exist."
    exit
  end

  puts "ssh #{c.user}@#{c.ip} -p #{c.port}"
end

#perform_login_root!Object

Returns a string which can be used to login as root



121
122
123
# File 'lib/gitpusshuten/commands/user.rb', line 121

def 
  puts "ssh root@#{c.ip} -p #{c.port}"
end

#perform_reconfigure!Object

Reconfigures the user without removing it or any applications



98
99
100
101
102
103
104
105
106
# File 'lib/gitpusshuten/commands/user.rb', line 98

def perform_reconfigure!
  if e.user_exists? # prompts root
    configure_user!
  else
    error "User #{y(c.user)} does not exist at #{y(c.ip)}."
    error "If you want to add #{y(c.user)}, run the following command:"
    standard "\n\s\s#{y("heavenly user add to #{e.name}")}"
  end
end

#perform_remove!Object

Removes the user and home directory



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/gitpusshuten/commands/user.rb', line 73

def perform_remove!
  if e.user_exists?
    warning "Are you #{y('SURE')} you want to remove #{y(c.user)}?"
    warning "Doing so will also remove #{y(e.home_dir)}, in which the #{y('applications')} of #{y(c.user)} are."
    if yes?
      warning "Are you #{y('REALLY')} sure?"
      if yes?
        message "Removing user #{y(c.user)} from #{y(c.ip)}."
        if e.remove_user!
          e.execute_as_root("rm -rf '#{e.home_dir}'")
          message "User #{y(c.user)} has been removed from #{y(c.ip)}."
        else
          error "Failed to remove user #{y(c.user)} from #{y(c.ip)}."
          error "An error occurred. Is your configuration file properly configured?"
          exit
        end
      end
    end
  else
    error "User #{y(c.user)} does not exist at #{y(c.ip)}."
  end
end