Skip to content

Instantly share code, notes, and snippets.

@davit
Last active March 25, 2016 11:21
Show Gist options
  • Save davit/c82181cbe54799526222 to your computer and use it in GitHub Desktop.
Save davit/c82181cbe54799526222 to your computer and use it in GitHub Desktop.
An interactive command line drupal 8 installer
#!/bin/bash
# Interactive drupal installation
### Allow only root users to use the script
if [ "$EUID" -ne 0 ]; then
echo "Script must be run as a root."
exit 1
fi
#### Get user name in case script is run with 'sudo'
script_name=$(basename $0)
promter=" > "
### Define error codes
PERM_DENIED=10
NOT_WRITBALE=11
INVALID_USER=12
UNSUPP_OS=13
MISSING_DEPEND=14
INSTALL_ERROR=15
trap 'echo script has been terminated. Cleaning up and exitting; cleanup_script $LINENO $BASH_COMMAND; \
echo You can check the logs in /tmp/${script_name}.log; exit 0' SIGINT SIGQUIT SIGTERM TERM
### Set default drupal variables
temp_dir=/tmp
context_root_default="default-${RANDOM}"
site_name_default="Default Site Name"
site_user_default="admin"
site_pass_default="admin"
db_user_default="root"
db_name_default="drupaldb${RANDOM}"
# List of themes with available sub-themes
available_sub_themes="bartik bootstrap"
### FUNCTIONS BEGIN ###
### Custom Utils Begin ###
function os_type {
sys_info=`uname -a`
case "$sys_info" in
*ARCH*)
echo "arch"
;;
*Ubuntu*)
echo "ubuntu"
;;
*Debian*)
echo "debian"
;;
*CYGWIN*|MINGW32*|MSYS*)
echo "windows"
;;
*Darwin)
echo "mac"
;;
*)
echo "other"
;;
esac
}
function command_exists {
type "$1" &> /dev/null ;
}
function to_machine_name {
local my_string="$1"
local str_lower="${my_string,,}"
local machine_name="${str_lower// /_}"
echo $machine_name
}
### Custom Utils End ###
### Script Related Begin ###
function cleanup_variables {
unset temp_dir
unset install_path
unset context_root
unset site_name
unset site_user
unset site_password
unset db_name
unset db_user
unset db_password
}
function cleanup_script {
echo "[$(date +"%T")] script: $script_name was terminated: line: $1, command was '$2'" | tee -a /tmp/${script_name}.log
if [ -d "$temp_full_install_path" ]; then
echo "Removing context root folder: ${temp_full_install_path}..."
rm -r $temp_full_install_path
fi
if [ ! -z "$db_name" ]; then
echo "Dropping database ${db_name}..."
mysql -u"$db_user" -p"$db_password" -e "DROP DATABASE IF EXISTS $db_name"
fi
}
function print_prompter {
message="$1"
echo
echo " $message"
echo -n "$promter"
}
function exit_script {
message="$1"
code="$2"
echo "$message"
exit "$code"
}
function confirm {
while true; do
read -p "$1 " yn
case $yn in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
"" ) return 0;;
* ) echo "Please answer yes or no.";;
esac
done
}
### Script Related End ###
### Script Depdencencies Begin ###
function install_composer {
cd /tmp
if ! command_exists "curl"; then
echo "curl not installed, falling back to php..."
php -r "readfile('https://getcomposer.org/installer');" | php
else
curl -sS https://getcomposer.org/installer | php
fi
mv composer.phar /usr/local/bin/composer
export PATH="$HOME/.composer/vendor/bin:$PATH"
source ~/.bashrc
}
function install_drush {
cd /tmp
wget http://files.drush.org/drush.phar
chmod 755 drush.phar
mv drush.phar /usr/local/bin/drush
}
function install_sqlite {
wget http://www.sqlite.org/sqlite-autoconf-3070603.tar.gz
cd sqlite-autoconf-3070603
./configure
make
make install
if [ $? -neq 0 ]; then
exit_script "Something went wrong while installing sqlite. Check errors above. Exitting..." "$INSTALL_ERROR"
fi
cd -
}
### Script Depdencencies End ###
function install_modules {
print_prompter "List drupal modules separated by space"
read modules
echo "Installing modules: ${modules// /, }..."
drush en -y $modules
}
### Drupal Related Begin ###
function install_drupal_console {
curl https://drupalconsole.com/installer -L -o drupal.phar
mv drupal.phar drupal
chmod +x drupal
mv drupal /usr/local/bin
drupal init
}
function install_theme {
print_prompter "Enter name of the theme:"
read theme
local theme="${theme,,}"
echo "Enabling theme: ${theme}..."
drush en -y $theme
}
function set_theme_default {
drush -y config-set system.theme default $1
}
function create_bartik_subtheme {
echo "Crating Bartik sub-theme..."
}
function create_bootstrap_subtheme {
local default_name="Theme Default Name"
local default_description="Theme Default Description"
local default_machine_name="$(to_machine_name $default_name)"
print_prompter "Enter name of the sub-theme (e.g \"My Theme\")[$default_name]:"
read name
if [ -z "$name" ]; then
name="$default_name"
machine_name="$default_machine_name"
else
machine_name=$(to_machine_name "$name")
fi
print_prompter "Enter description of the sub-theme [$default_description]:"
read description
if [ -z "$description" ]; then
description="$default_description"
fi
echo "Theme machine name: $machine_name"
echo
echo "Moving into themes folder..."
cd themes
cdn=contrib/bootstrap/starterkits/cdn
echo "Copying $cdn to custom/${machine_name}..."
cp -r $cdn custom/$machine_name
echo "Moving into custom/${machine_name}..."
cd custom/$machine_name
echo "Renaming files that begin with and replacing occurences of "THEMENAME"..."
find -maxdepth 3 -name "THEMENAME.*" \
-exec bash -c 'file={} ; sed -i "s/THEMENAME/'"$machine_name"'/g" $file ; mv $file ${file//THEMENAME/'"$machine_name"'} ' \;
echo "Renaming ${machine_name}.starterkit.yml to ${machine_name}.info.yml..."
mv ${machine_name}.starterkit.yml ${machine_name}.info.yml
local info_file="${machine_name}.info.yml"
echo "Set sub-theme name in ${info_file}..."
sed -i "/^name:/c\name: '$name'" $info_file
echo "Set sub-theme description in ${info_file}..."
sed -i "/^description:/c\ndescription: '$description'" $info_file
if [ ! -e js ]; then
echo "'js' directory doesn't exist. Creating 'js' directory..."
mkdir js
fi
echo
if confirm "Do you want to set $name as the default theme? (y)"; then
drush en $machine_name
set_theme_default $machine_name
fi
echo "Moving back to ${temp_full_install_path}..."
cd ${temp_full_install_path}
}
function create_common_folders {
temp_full_install_path=$1
echo "Moving into ${temp_full_install_path}..."
cd $temp_full_install_path
echo "Creating ${temp_full_install_path}/modules/contrib folder..."
mkdir modules/contrib
echo "Creating ${temp_full_install_path}/modules/custom folder..."
mkdir modules/custom
echo "Creating ${temp_full_install_path}/themes/contrib folder..."
mkdir themes/contrib
echo "Creating ${temp_full_install_path}/themes/custom folder..."
mkdir themes/custom
}
### Drupal Related End ###
function set_default_os_variables {
case "$(os_type)" in
"windows")
exit_script " Script currently cannot be run on Windows :( exiting..." "$UNSUPP_OS"
;;
"arch")
inst_path_default=/srv/http
echo " OS: Arch Linux. Setting default installation path to ${inst_path_default}"
;;
"ubuntu"|"debian")
inst_path_default=/var/www
echo -n " OS type: Ubuntu. Setting default installation path to ${inst_path_default}"
esac
}
function check_dependences {
echo "Checking if apache is installed..."
if ! command_exists "apachectl"; then
echo "Apache not installed. Exitting..."
exit_script "Apache is either not installed or not properly configured. Please install/fix it and rerun the script." "$MISSING_DEPEND"
fi
echo "Checking if composer is installed..."
if ! command_exists "composer"; then
echo "Composer isn't installed. Installing composer..."
install_composer
fi
echo "Checking if drush is installed..."
if ! command_exists "drush"; then
echo "Drush isn't installed. Installing drush..."
install_drush
fi
# echo "Checking if curl is installed..."
# if ! command_exists "curl"; then
# echo "curl not installed. Exitting..."
# exit_script "Curl isn't installed. Please install it and rerun the script." "$MISSING_DEPEND"
# fi
echo "Checking if sqlite is installed..."
if ! command_exists "sqlite3"; then
echo "Sqlite not installed. Installing sqlite..."
install_sqlite
fi
echo "Checking if drupal console is installed..."
if ! command_exists drupal; then
echo "Drupal console isn't installed. Installing..."
install_drupal_console
fi
}
### FUNCTIONS END ###
### SCRIPTS BEGIN HERE ###
# Notify or install if any depdendencies are missing
echo "Entering $temp_dir folder..."
cd $temp_dir
check_dependences
echo " Setting OS specific variables..."
set_default_os_variables
print_prompter "Enter installation path [$inst_path_default]:"
read install_path
if [ -z "$install_path" ]; then
install_path="$inst_path_default"
else
while [ ! -d $install_path ]; do
print_prompter "Folder not found: ${install_path}. Try again:"
read install_path
done
fi
print_prompter "Enter context root name [$context_root_default]:"
read context_root
if [ -z "$context_root" ]; then
context_root="$context_root_default"
fi
# Set full installation path
temp_full_install_path="$temp_dir/$context_root"
full_install_path="${install_path}/$context_root"
print_prompter "Enter site name [$site_name_default]:"
read site_name
if [ -z "$site_name" ]; then
site_name="$site_name_default"
fi
print_prompter "Enter site user [$site_user_default]:"
read site_user
if [ -z "$site_user" ]; then
site_user="$site_user_default"
fi
print_prompter "Enter site password [$site_pass_default]:"
stty -echo
read site_password
stty echo
if [ -z "$site_password" ]; then
site_password="$site_pass_default"
fi
echo
print_prompter "Enter database user [$db_user_default]:"
read db_user
if [ -z "$db_user" ]; then
db_user=$db_user_default
fi
print_prompter "Enter database password:"
stty -echo
read db_password
stty echo
if [ -z $db_password ]; then
print_prompter "[WARNING] Database password is required! Enter database password:"
stty -echo
read db_password
stty echo
else
while ! mysql -u root -p$db_password -e ";" ; do
print_prompter "Try again"
stty -echo
read db_password
stty echo
done
fi
echo
print_prompter "Enter database name [$db_name_default]:"
read db_name
if [ -z "$db_name" ]; then
db_name=$db_name_default
fi
echo
echo "Downloadin drupal..."
drush dl drupal
echo "Renaming drupal folder to ${context_root}..."
mv drupal-8* "$context_root"
echo "Entering ${temp_full_install_path}..."
cd $temp_full_install_path
echo "Installing drupal..."
drush -y --notify site-install --site-name="$site_name" --account-name="$site_user" --account-pass="$site_password" \
--db-url=mysql://${db_user}:${db_password}@localhost/${db_name}
echo "Setting up common drupal folders..."
create_common_folders $temp_full_install_path
echo
### Check if drupal modules are requested
if confirm "Would you like to install drupal modules? (y/n):"; then
install_modules
fi
echo
### Check if a new theme is requested
if confirm "Would you like to enable or download some theme? (y/n):"; then
install_theme
if [ $? -eq 0 ]; then
theme_lower="${theme,,}"
# Check if script supports creating enabled theme sub-theme creation
if [[ "$available_sub_themes" =~ "$theme_lower" ]]; then
echo
if confirm "Script is able to crete $theme sub-theme for you, would you like that? (y/n):" ; then
echo
create_"${theme_lower}"_subtheme
fi
fi
fi
fi
echo "Enabling debug mode..."
drupal site:mode dev
#echo "Clearing drupal cache..."
#drush cr
echo "Moving $temp_full_install_path to ${install_path}..."
mv $temp_full_install_path $install_path || echo "Something went wrong while moving the installed drupal to $inst_full_path" -1
cd $full_install_path
echo "Making sites/default/files writable..."
chmod 777 -R sites/default/files || echo "Failed to set writable permissions to the sites/default/files folder. Please you set them."
echo
echo "Congrats, you've completed full installation! Check the info below:"
echo
echo "Current directory: $PWD"
echo "Installation folder: $context_root"
echo "Site name: $site_name"
echo "Site user name: $site_user"
echo "Site password: $site_password"
echo "Database user: $db_user"
echo "Database name: $db_name"
echo "Theme enabled: $theme"
echo
echo "Unsetting variables..."
cleanup_variables
echo
echo "OK, get to work now!"
stty echo
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment