Skip to content

Instantly share code, notes, and snippets.

@tjhanley
Created September 22, 2011 16:38
Show Gist options
  • Save tjhanley/1235283 to your computer and use it in GitHub Desktop.
Save tjhanley/1235283 to your computer and use it in GitHub Desktop.
Rubber Capistrano Task to create/delete users across your cluster.
# This will create a user with a default password of #{username}123
# then the first time they connect they are prompted to change their password
# once the user is created in your local ./tmp directory you'll have the
# private and public keys for the new user
desc "Create a user on the servers"
require "cocaine"
task :create_server_user do
set(:user_to_add, Capistrano::CLI.ui.ask("USERNAME you'd like to add: ") )
key_file = File.join(File.dirname(__FILE__), '../../tmp', user_to_add)
puts key_file
line = Cocaine::CommandLine.new('ssh-keygen',"-q -t dsa -N '' -f :file", :file => key_file);
puts line.command
line.run
key_path = "/home/#{user_to_add}/.ssh"
rubber.sudo_script 'create_server_user', <<-ENDSCRIPT
# add the user for running app server with
if ! id #{user_to_add} &> /dev/null; then useradd -m #{user_to_add}; fi
mkdir -p #{key_path}
# add ssh keys for user
if [[ ! -f /home/#{user_to_add}/.ssh/id_dsa ]]; then ssh-keygen -q -t dsa -N '' -f /home/#{user_to_add}/.ssh/id_dsa; fi
echo #{user_to_add}:#{user_to_add}123 | chpasswd
chage -d 0 #{user_to_add}
ENDSCRIPT
upload("#{key_file}.pub","#{key_path}/authorized_keys")
end
desc "Delete a user on the server"
task :delete_server_user do
set(:user_to_del, Capistrano::CLI.ui.ask("USERNAME you'd like to delete: ") )
rubber.sudo_script 'delete_server_user', <<-ENDSCRIPT
if id #{user_to_del} &> /dev/null; then userdel -f #{user_to_del}; fi
ENDSCRIPT
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment