Skip to content

Instantly share code, notes, and snippets.

@d70rr3s
Last active March 14, 2018 18:52
Show Gist options
  • Select an option

  • Save d70rr3s/2b4f2ab24cd1d5a5dac3bcf45274402e to your computer and use it in GitHub Desktop.

Select an option

Save d70rr3s/2b4f2ab24cd1d5a5dac3bcf45274402e to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Installer script
# -----------------------------------------------
# This script is responsible for creating proper
# directories and files and the installation of
# ansible package any other dependencies needed
# and executing provitioning playbook.
# -----------------------------------------------
echo \ \ \ \
echo "========================================";
echo "Welcome to 'You are not prepare script'!";
echo "========================================";
echo \ \ \ \
echo "This is the launcher script for https://github.com/d70rr3s/ubuntu-dev-playbook " \ ;
echo "Ansible playbook, read the README file for more information." \ ;
echo \ \ \ \
# Check if the script is being run with proper
# privileges (root).
if [ $(id -u) -ne 0 ]; then
echo "> Please run this script as root";
exit 1;
fi
# Ask for username to set proper ownership
# of files and directories.
username='';
while [ -z "$username" ]; do
read -p "> Please enter your username: " username
done;
# Sanitize username
# First, strip underscores
CLEAN=${username//_/};
# Next, replace spaces with underscores
CLEAN=${CLEAN// /_};
# Now, clean out anything that's not alphanumeric or an underscore
username=${CLEAN//[^a-zA-Z0-9_]/};
echo \ \ \ \
# Validates if is a valid username.
if [[ $(id "$username") =~ .*uid.* ]]; then
echo "> Hello $username, standby for installation.";
else
echo "> The user $username does not exits.";
exit 1;
fi
echo \ \
echo "----------------------------------------";
echo " Directory structure ";
echo "----------------------------------------";
# .temp directory will hold any temporal files
# without the need of using OS temp directory.
if [ -d "/home/$username/.temp" ]; then
echo "> Directory /home/$username/.temp already exists! Skipping."
else
su -c "mkdir -p /home/$username/.temp" $username;
echo "> Directory /home/$username/.temp done!";
fi
# .installer directory will hold the playbook files.
if [ -d "/home/$username/.installer" ]; then
echo "> Directory /home/$username/.installer already exists! Skipping."
else
su -c "mkdir -p /home/$username/.installer" $username;
echo "> Directory /home/$username/.installer done!";
fi
echo \
echo "----------------------------------------";
echo " Registering repos ";
echo "----------------------------------------";
add-apt-repository ppa:ansible/ansible -y;
echo \
echo "----------------------------------------";
echo " Updating registry ";
echo "----------------------------------------";
apt-get update;
echo \
echo "----------------------------------------";
echo " Installing cURL ";
echo "----------------------------------------";
apt-get install curl -y
echo \
echo "----------------------------------------";
echo " Installing Ansible ";
echo "----------------------------------------";
apt-get install ansible -y;
echo \
echo "----------------------------------------";
echo " Configure Ansible ";
echo "----------------------------------------";
chmod -R 0755 "/home/$username/.ansible";
chmod -R 0644 "/home/$username/.ansible_galaxy";
chown -R "$username:$username" "/home/$username/.ansible";
chown -R "$username:$username" "/home/$username/.ansible_galaxy";
echo \
echo "----------------------------------------";
echo " Downloading playbook ";
echo "----------------------------------------";
# Delete previous downloads before proceed.
if [ -e "/home/$username/.temp/ubuntu-dev-playbook.zip" ]; then
rm -f "/home/$username/.temp/ubuntu-dev-playbook.zip"
fi
if [ -d "~/.temp/ubuntu-dev-playbook-master" ]; then
rm -rf "~/.temp/ubuntu-dev-playbook-master"
fi
# Download the playbook from GitHub master branch.
su -c "curl -L https://github.com/d70rr3s/ubuntu-dev-playbook/archive/master.zip -k -o ~/.temp/ubuntu-dev-playbook.zip" $username;
if [ ! -e "/home/$username/.temp/ubuntu-dev-playbook.zip" ]; then
echo "> The playbook could not be downloaded. Exting...";
exit 1;
else
su -c "unzip ~/.temp/ubuntu-dev-playbook.zip -d ~/.temp/ >/dev/null 2>&1" $username;
# @FIXME For some reason unzip command decompress the file not only in destination but also in PWD.
if [ -d "/home/$username/ubuntu-dev-playbook-master" ]; then
rm -rf "/home/$username/ubuntu-dev-playbook-master";
fi
su -c "rsync -rvaEz --delete ~/.temp/ubuntu-dev-playbook-master/* ~/.installer/ >/dev/null 2>&1" $username;
rm -rf "/home/$username/.temp/ubuntu-dev-playbook-master";
echo \
echo "> Playbook downloaded!" \ ;
fi
# Install playbook requirements (roles).
su -c "ansible-galaxy install -r /home/$username/.installer/requirements.yml" $username;
echo \
# Prompt the user for install execution.
while true; do
read -p "Do you wish to execute the playbook installation? [y/n]" execute_installer
case $execute_installer in
[Yy]* ) echo \ ; break;;
[Nn]* ) echo "Aborting installer."; "You can run it manually by executing: ansible-playbook -i \"localhost,\" -c local -K /home/$username/.installer/playbook.yml"; cd $parent_dir; exit 2;;
* ) echo "Please answer yes or no.";;
esac
done
echo "----------------------------------------";
echo " Configure playbook ";
echo "----------------------------------------";
echo \
# Prompt for user email.
user_email='';
while [ -z "$user_email" ]; do
read -p "> Please enter your email address: " user_email
done;
# Prompt for user full name.
full_name='';
while [ -z "$full_name" ]; do
read -p "> Please enter your full name: " full_name
done;
echo "From now on, you will be known as: $full_name <$user_email>" \ \
echo "The following packages and binaries will be installed:" \
echo " - htop" \
echo " - gparted" \
echo " - liberror-perl" \
echo " - libqt5x11extras5" \
echo " - libsdl1.2debian" \
echo " - python-pip" \
echo " - pulseaudio" \
echo " - meld" \
echo " - vim" \
echo " - synapse" \
echo " - autojump" \
echo " - battery-monitor" \
echo " - indicator-keylock" \
echo " - fzf" \
echo " - google-chrome-stable" \
echo " - putty" \
echo " - terminator" \
echo " - filezilla" \
echo " - toolboxapp" \
echo \ ;
echo "The following languages and tools will be installed:" \
echo " - git" \
echo " - php" \
echo " - composer" \
echo " - java" \
echo " - vagrant" \
echo " - nfs" \
echo " - oh-my-zsh" \
echo \ ;
# Extra tools.
options=("docker" "virtualbox" "nodejs" "ruby" "sublime" "atom" "yarn")
installed_extras_menu() {
echo "Select any of the following extra tools to be installed:"
for i in ${!options[@]}; do
printf "%3d%s) %s\n" $((i+1)) "${choices[i]:- }" "${options[i]}"
done
[[ "$msg" ]] && echo "$msg"; :
}
prompt="Check an option (again to uncheck, ENTER when done): "
while installed_extras_menu && read -rp "$prompt" num && [[ "$num" ]]; do
[[ "$num" != *[![:digit:]]* ]] &&
(( num > 0 && num <= ${#options[@]} )) ||
{ msg="Invalid option: $num"; continue; }
((num--)); msg="${options[num]} was ${choices[num]:+un}checked"
[[ "${choices[num]}" ]] && choices[num]="" || choices[num]="+"
done
# Save selected elements in Ansible array format.
installed_extras="";
printf ""; selected_options=""
for i in ${!options[@]}; do
[[ "${choices[i]}" ]] && { installed_extras="$installed_extras${options[i]},"; selected_options=""; }
done
installed_extras=$(sed "s/,]/]/" <<< "[$installed_extras]");
# Print back selected elements.
printf "You selected:"; msg=" nothing"
for i in ${!options[@]}; do
[[ "${choices[i]}" ]] && { printf " %s" "${options[i]}"; msg=""; }
done
echo "$msg";
echo \ \ \ \
echo "----------------------------------------";
echo " Executing playbook ";
echo "----------------------------------------";
echo \
echo "ansible-playbook -i \"localhost,\" -c local -K /home/$username/.installer/playbook.yml --extra-vars \"current_user=$username git_fullname='$full_name' git_email='$user_email' installed_extras=$installed_extras\"" \ ;
ansible-playbook -i "localhost," -c local -K "/home/$username/.installer/playbook.yml" --extra-vars "current_user=$username git_fullname='$full_name' git_email='$user_email' installed_extras=$installed_extras";
echo \
echo "You should reboot system to some changes to take effect.";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment