invoiceninja-debianinstaller/setupninja.sh

665 lines
24 KiB
Bash

#!/bin/bash
#SETTINGS
passlen=22
emailforle="putyouremailaddresshere@domain.com"
#FUNCTIONS
function errorcheck {
if [[ $1 == 0 ]] || [[ -z $1 ]]; then
echo "SUCCESS: $2" >> "$origpath/setupninja.log"
else
if [[ $3 == "EXIT" ]]; then
echo "ERROR: $2 (EXIT CODE: $1)" >> "$origpath/setupninja.log"
echo "Install Failed! View $origpath/setupninja.log"
exit 1
else
echo "ERROR: $2" >> "$origpath/setupninja.log"
echo "JSON: $3" >> "$origpath/setupninja.log"
echo "Error with last step: Please check $origpath/setupninja.log"
echo "Able to proceed..."
fi
fi
}
function generatepassword {
passcharacterset=('A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z' 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z' '1' '2' '3' '4' '5' '6' '7' '8' '9' '0')
temp=""
test=""
for ((n=0; n<$passlen; n++)); do
passcharacterset=($(shuf -e ${passcharacterset[@]}))
if [[ $test == ${passcharacterset[0]} ]]; then
pass="$temp${passcharacterset[1]}"
test=${passcharacterset[1]}
else
pass="$temp${passcharacterset[0]}"
test=${passcharacterset[0]}
fi
temp=$pass
done
echo $pass
}
function generatedbname {
dbcharacterset=('1' '2' '3' '4' '5' '6' '7' '8' '9' '0')
temp="ninja"
for ((n=0; n<5; n++)); do
dbcharacterset=($(shuf -e ${dbcharacterset[@]}))
name="$temp${dbcharacterset[0]}"
temp=$name
done
echo $name
}
function getipaddress {
size=$( cat /proc/net/dev | wc -l )
((size++))
for ((n=4; n<$size; n++)); do
temp=($( cat /proc/net/dev | head -n $n | tail -1 ))
netdev[$((n-4))]=${temp[0]}
done
size=${#netdev[@]}
c=0
for ((n=0; n<$size; n++)); do
temp=$( ip a | grep ${netdev[$n]} | grep "state UP" )
if [[ ! -z $temp ]]; then
netdevup[$c]=${netdev[$n]}
((c++))
fi
done
size=${#netdevup[@]}
if [[ $size == 0 ]]; then
errorcheck 1 "No interfaces are connected" "EXIT"
elif [[ $size == 1 ]]; then
ipaddress=$( ip -o -4 addr list ${netdevup[0]:0:-1} | awk '{print $4}' | cut -d/ -f1 )
else
echo "Available Interfaces..."
for ((n=0; n<$size; n++)); do
echo "$n ${netdevup[$n]:0:-1} $( ip -o -4 addr list ${netdevup[$n]:0:-1} | awk '{print $4}' | cut -d/ -f1 )"
done
echo -n "Which interface do you want to use? (choose 0-$((size-1)) | default is 0) "
read reply
if [[ $reply == "" ]]; then
reply=0
fi
ipaddress=$( ip -o -4 addr list ${netdevup[$reply]:0:-1} | awk '{print $4}' | cut -d/ -f1 )
fi
}
function gathersettings {
#GATHER SETTINGS
getipaddress
echo "IP address is $ipaddress..."
echo -n "What is the FQDN for the server? "
read fqdn
echo -n "Must an SLL certificate be used? (y/n)"
read reply
if [[ $reply == "y" ]]; then
ishttps=true
else
ishttps=false
fi
echo -n "Enter a password for MariaDB root (blank for random password): "
read mysqlpass
if [[ $mysqlpass == "" ]]; then
echo "Generating a random password..."
mysqlpass=$( generatepassword )
fi
echo -n "Enter a name for the Invoice Ninja database (blank for 'ninja') "
read appdbname
if [[ $appdbname == "" ]]; then
appdbname=$( generatedbname )
fi
echo -n "Enter a name for the Invoice Ninja database user (blank for same as database name) "
read appdbuser
if [[ $appdbuser == "" ]]; then
appdbuser=$appdbname
fi
echo -n "Enter a password for Invoice Ninja database (blank for random password): "
read appdbpass
if [[ $appdbpass == "" ]]; then
echo "Generating a random password..."
appdbpass=$( generatepassword )
fi
echo -n "What is the FQDN for the mailserver? "
read mailhost
echo -n "Is the mailserver using SSL? (y/n) "
read reply
if [[ $reply == "y" ]]; then
mailssl=true
else
mailssl=false
fi
echo -n "What is the port for the mailserver? (leave blank for 465) "
read mailport
if [[ $mailport == "" ]]; then
mailport="465"
fi
echo -n "What is the username for the mailserver? "
read mailuser
echo -n "What is the password for the mailserver? "
read mailpass
echo -n "What is the from address for the mailserver? (leave blank if same as username) "
read mailfrom
if [[ $mailfrom == "" ]]; then
mailfrom=$mailuser
fi
echo -n "What is the sender name for the mailserver? "
read mailname
echo
buildjson
}
function buildjson {
echo -n "{\"ipaddress\":\"$ipaddress\",\"fqdn\":\"$fqdn\",\"ishttps\":$ishttps,\"mysqlpass\":\"$mysqlpass\",\"appdbname\":\"$appdbname\",\"appdbuser\":\"$appdbuser\",\"appdbpass\":\"$appdbpass\",\"mailhost\":\"$mailhost\",\"mailssl\":$mailssl,\"mailport\":\"$mailport\",\"mailuser\":\"$mailuser\",\"mailpass\":\"$mailpass\",\"mailfrom\":\"$mailfrom\",\"mailname\":\"$mailname\"}" > "$origpath/setupninja.json"
}
function applysettings {
echo "Reading JSON manifest..."
ipaddress=$( jq -r '.ipaddress' "$origpath/setupninja.json" )
if [[ $ipaddress == "null" ]]; then
getipaddress
fi
fqdn=$( jq -r '.fqdn' "$origpath/setupninja.json" )
if [[ $fqdn == "null" ]]; then
errorcheck 1 "Invalid JSON file" "EXIT"
fi
ishttps=$( jq -r '.ishttps' "$origpath/setupninja.json" )
if [[ $ishttps == "null" ]]; then
ishttps=true
fi
mysqlpass=$( jq -r '.mysqlpass' "$origpath/setupninja.json" )
if [[ $mysqlpass == "null" ]]; then
mysqlpass=$( generatepassword )
fi
appdbname=$( jq -r '.appdbname' "$origpath/setupninja.json" )
if [[ $appdbname == "null" ]]; then
appdbname=$( generatedbname )
fi
appdbuser=$( jq -r '.appdbuser' "$origpath/setupninja.json" )
if [[ $appdbuser == "null" ]]; then
appdbuser=$appdbname
fi
appdbpass=$( jq -r '.appdbpass' "$origpath/setupninja.json" )
if [[ $appdbpass == "null" ]]; then
appdbpass=$( generatepassword )
fi
mailhost=$( jq -r '.mailhost' "$origpath/setupninja.json" )
if [[ $mailhost == "null" ]]; then
errorcheck 1 "Invalid JSON file" "EXIT"
fi
mailssl=$( jq -r '.mailssl' "$origpath/setupninja.json" )
if [[ $mailssl == "null" ]]; then
mailssl=true
fi
mailport=$( jq -r '.mailport' "$origpath/setupninja.json" )
if [[ $mailport == "null" ]]; then
mailport="465"
fi
mailuser=$( jq -r '.mailuser' "$origpath/setupninja.json" )
if [[ $mailuser == "null" ]]; then
errorcheck 1 "Invalid JSON file" "EXIT"
fi
mailpass=$( jq -r '.mailpass' "$origpath/setupninja.json" )
if [[ $mailpass == "null" ]]; then
errorcheck 1 "Invalid JSON file" "EXIT"
fi
mailfrom=$( jq -r '.mailfrom' "$origpath/setupninja.json" )
if [[ $mailfrom == "null" ]]; then
mailfrom=$mailuser
fi
mailname=$( jq -r '.mailname' "$origpath/setupninja.json" )
if [[ $mailname == "null" ]]; then
errorcheck 1 "Invalid JSON file" "EXIT"
fi
echo
buildjson
}
function sethostname {
#SETTING UP HOSTNAME
echo "Setting up hostname..."
hostnamectl set-hostname ${fqdn%%.*} > /dev/null 2>&1
errorcheck $? "define hostname" "EXIT"
echo "$ipaddress $fqdn ${fqdn%%.*}" >> /etc/hosts
errorcheck $? "define FQDN" "EXIT"
echo "1" > "$origpath/setupninja.step"
echo
}
function initsetup {
#INITIAL SETUP
echo "Updating System..."
apt -y update > /dev/null 2>&1
errorcheck $? "apt -y update" "EXIT"
apt -y upgrade > /dev/null 2>&1
errorcheck $? "apt -y upgrade" "EXIT"
echo "2" > "$origpath/setupninja.step"
echo
}
function gostandard {
#INSTALLING STANDING REQUIREMENTS
echo "Installing packages to do install..."
apt -y install jq extrepo curl screen rsync unzip > /dev/null 2>&1
errorcheck $? "apt -y install extrepo curl screen rsync unzip" "EXIT"
echo "3" > "$origpath/setupninja.step"
echo
}
function gosury {
#ENABLE SURY FOR PHP
echo "Setting up Sury Repo for PHP..."
extrepo enable sury > /dev/null 2>&1
errorcheck $? "extrepo enable sury" "EXIT"
apt -y update > /dev/null 2>&1
errorcheck $? "apt -y update" "EXIT"
echo "4" > "$origpath/setupninja.step"
echo
}
function goapache {
#INSTALL APACHE2
echo "Installing Apache2..."
apt -y install apache2 > /dev/null 2>&1
errorcheck $? "apt -y install apache2" "EXIT"
echo "5" > "$origpath/setupninja.step"
echo
}
function gomariadb {
#INSTALL MARIADB
echo "Installing MariaDB..."
apt -y install mariadb-server mariadb-client > /dev/null 2>&1
errorcheck $? "apt -y install mariadb-server mariadb-client" "EXIT"
echo "6" > "$origpath/setupninja.step"
echo
}
function gophp {
#INSTALL PHP
echo "Install PHP8.2..."
apt -y install php8.2-{fpm,soap,bcmath,common,imagick,mysql,gmp,curl,intl,mbstring,xmlrpc,gd,xml,cli,zip,bz2,fpm} libapache2-mod-php8.2 libapache2-mod-fcgid > /dev/null 2>&1
errorcheck $? "apt -y install php8.2-{fpm,soap,bcmath,common,imagick,mysql,gmp,curl,intl,mbstring,xmlrpc,gd,xml,cli,zip,bz2,fpm} libapache2-mod-php8.2 libapache2-mod-fcgid" "EXIT"
echo "7" > "$origpath/setupninja.step"
echo
}
function confapachephp {
#CONFIGURE APACHE2 AND PHP
echo "Configuring Apache2 and PHP-FPM..."
systemctl stop apache2 > /dev/null 2>&1
errorcheck $? "systemctl stop apache2" "EXIT"
a2dismod php8.2 > /dev/null 2>&1
errorcheck $? "a2dismod php8.2" "EXIT"
a2dismod mpm_prefork > /dev/null 2>&1
errorcheck $? "a2dismod mpm_prefork" "EXIT"
a2enmod mpm_event proxy proxy_fcgi setenvif rewrite > /dev/null 2>&1
errorcheck $? "a2enmod mpm_event proxy proxy_fcgi setenvif rewrite" "EXIT"
a2enconf php8.2-fpm > /dev/null 2>&1
errorcheck $? "a2enconf php8.2-fpm" "EXIT"
systemctl restart apache2 > /dev/null 2>&1
errorcheck $? "systemctl restart apache2" "EXIT"
echo "8" > "$origpath/setupninja.step"
echo
}
function confmariadb {
#CONFIGURE MARIADB
echo "Hardening MariaDB..."
systemctl enable mariadb > /dev/null 2>&1
errorcheck $? "systemctl enable mariadb" "EXIT"
systemctl start mariadb > /dev/null 2>&1
errorcheck $? "systemctl start mariadb" "EXIT"
echo "UPDATE mysql.global_priv SET priv=json_set(priv, '$.password_last_changed', UNIX_TIMESTAMP(), '$.plugin', 'mysql_native_password', '$.authentication_string', 'invalid', '$.auth_or', json_array(json_object(), json_object('plugin', 'unix_socket'))) WHERE User='root';" | mysql
errorcheck $? "UPDATE mysql.global_priv SET priv=json_set(priv, '$.password_last_changed', UNIX_TIMESTAMP(), '$.plugin', 'mysql_native_password', '$.authentication_string', 'invalid', '$.auth_or', json_array(json_object(), json_object('plugin', 'unix_socket'))) WHERE User='root';" "EXIT"
echo "FLUSH PRIVILEGES;" | mysql
errorcheck $? "FLUSH PRIVILEGES;" "EXIT"
echo "UPDATE mysql.global_priv SET priv=json_set(priv, '$.plugin', 'mysql_native_password', '$.authentication_string', PASSWORD('basic_single_escape \"$mysqlpass\"')) WHERE User='root';" | mysql
errorcheck $? "UPDATE mysql.global_priv SET priv=json_set(priv, '$.plugin', 'mysql_native_password', '$.authentication_string', PASSWORD('basic_single_escape \"$mysqlpass\"')) WHERE User='root';" "EXIT"
echo "DELETE FROM mysql.global_priv WHERE User='';" | mysql
errorcheck $? "DELETE FROM mysql.global_priv WHERE User='';" "EXIT"
echo "DELETE FROM mysql.global_priv WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');" | mysql
errorcheck $? "DELETE FROM mysql.global_priv WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');" "EXIT"
echo "DROP DATABASE IF EXISTS test;" | mysql
errorcheck $? "DROP DATABASE IF EXISTS test;" "EXIT"
echo "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'" | mysql
errorcheck $? "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'" "EXIT"
echo "FLUSH PRIVILEGES;" | mysql
errorcheck $? "FLUSH PRIVILEGES;" "EXIT"
echo "9" > "$origpath/setupninja.step"
echo
}
function ninjadb {
#SETUP DATABASE FOR NINJA
echo "Setting up database for Invoice Ninja..."
echo "CREATE DATABASE $appdbname; CREATE USER $appdbuser@localhost IDENTIFIED BY '$appdbpass'; GRANT ALL PRIVILEGES ON $appdbname.* TO $appdbuser@localhost; FLUSH PRIVILEGES;" | mysql
errorcheck $? "CREATE DATABASE $appdbname; CREATE USER $appdbuser@localhost IDENTIFIED BY '$appdbpass'; GRANT ALL PRIVILEGES ON $appdbname.* TO $appdbuser@localhost; FLUSH PRIVILEGES;" "EXIT"
echo "10" > "$origpath/setupninja.step"
echo
}
function downloadninja {
#DOWNLOAD INVOICE NINJA
if [[ -f "$origpath/invoiceninja.zip" ]]; then
rm -f "$origpath/invoiceninja.zip"
fi
appversion=$(curl -s https://api.github.com/repos/invoiceninja/invoiceninja/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//')
errorcheck $? "curl -s https://api.github.com/repos/invoiceninja/invoiceninja/releases/latest" "EXIT"
echo "Downloading Invoice Ninja version $appversion..."
wget -q "https://github.com/invoiceninja/invoiceninja/releases/download/v$appversion/invoiceninja.zip"
errorcheck $? "wget -q "https://github.com/invoiceninja/invoiceninja/releases/download/v$appversion/invoiceninja.zip"" "EXIT"
echo "11" > "$origpath/setupninja.step"
echo
}
function unzipninja {
#EXTRACTING INVOICE NINJA
echo "Unzipping files..."
if [[ -d "/var/www/$appdbname" ]]; then
rm -R "/var/www/$appdbname"
fi
mkdir "/var/www/$appdbname"
unzip invoiceninja.zip -d /var/www/$appdbname > /dev/null 2>&1
errorcheck $? "unzip invoiceninja.zip -d /var/www/$appdbname" "EXIT"
echo "12" > "$origpath/setupninja.step"
echo
}
function confninja {
#PRECONFIGURNG INVOICE NINJA
echo "Creating .env file..."
chown -R www-data:www-data /var/www/$appdbname > /dev/null 2>&1
chmod -R 755 /var/www/$appdbname > /dev/null 2>&1
cd /var/www/$appdbname
if [[ -f .env ]]; then
rm -f .env
fi
touch .env
while read -r line; do
if [[ ${line:0:7} == "APP_URL" ]]; then
if [[ ishttps == true ]]; then
echo "APP_URL=\"https://$fqdn/public\"" >> .env
else
echo "APP_URL=\"http://$fqdn/public\"" >> .env
fi
elif [[ ${line:0:11} == "DB_DATABASE" ]]; then
echo "DB_DATABASE=\"$appdbname\"" >> .env
elif [[ ${line:0:11} == "DB_USERNAME" ]]; then
echo "DB_USERNAME=\"$appdbuser\"" >> .env
elif [[ ${line:0:11} == "DB_PASSWORD" ]]; then
echo "DB_PASSWORD=\"$appdbpass\"" >> .env
elif [[ ${line:0:9} == "MAIL_HOST" ]]; then
echo "MAIL_HOST=\"$mailhost\"" >> .env
elif [[ ${line:0:9} == "MAIL_PORT" ]]; then
echo "MAIL_HOST=\"$mailport\"" >> .env
elif [[ ${line:0:13} == "MAIL_USERNAME" ]]; then
echo "MAIL_USERNAME=\"$mailuser\"" >> .env
elif [[ ${line:0:13} == "MAIL_PASSWORD" ]]; then
echo "MAIL_PASSWORD=\"$mailpass\"" >> .env
elif [[ ${line:0:17} == "MAIL_FROM_ADDRESS" ]]; then
echo "MAIL_FROM_ADDRESS=\"$mailfrom\"" >> .env
elif [[ ${line:0:14} == "MAIL_FROM_NAME" ]]; then
echo "MAIL_FROM_NAME=\"$mailname\"" >> .env
elif [[ ${line:0:15} == "MAIL_ENCRYPTION" ]]; then
if [[ $mailssl == true ]]; then
echo "MAIL_ENCRYPTION=\"ssl\"" >> .env
else
echo $line >> .env
fi
elif [[ ${line:0:11} == "REQUIRE_HTTPS" ]]; then
if [[ ishttps == true ]]; then
echo "REQUIRE_HTTPS=\"true\"" >> .env
else
echo "REQUIRE_HTTPS=\"false\"" >> .env
fi
else
echo $line >> .env
fi
done < .env.example
chown www-data:www-data .env > /dev/null 2>&1
echo "13" > "$origpath/setupninja.step"
echo
}
function vhostninja {
#SETTING UP VHOST
echo "Creating Apache2 VHost and Disabling the default VHost..."
if [[ -f /etc/apache2/sites-available/$appdbname.conf ]]; then
rm -f /etc/apache2/sites-available/$appdbname.conf
fi
touch /etc/apache2/sites-available/$appdbname.conf
echo "<VirtualHost *:80>" >> /etc/apache2/sites-available/$appdbname.conf
echo " ServerName $fqdn" >> /etc/apache2/sites-available/$appdbname.conf
echo " DocumentRoot /var/www/$appdbname" >> /etc/apache2/sites-available/$appdbname.conf
echo " <Directory /var/www/$appdbname>" >> /etc/apache2/sites-available/$appdbname.conf
echo " DirectoryIndex index.php" >> /etc/apache2/sites-available/$appdbname.conf
echo " Options +FollowSymLinks" >> /etc/apache2/sites-available/$appdbname.conf
echo " AllowOverride All" >> /etc/apache2/sites-available/$appdbname.conf
echo " Require all granted" >> /etc/apache2/sites-available/$appdbname.conf
echo " </Directory>" >> /etc/apache2/sites-available/$appdbname.conf
echo "" >> /etc/apache2/sites-available/$appdbname.conf
echo " <FilesMatch \.php$>" >> /etc/apache2/sites-available/$appdbname.conf
echo " SetHandler \"proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost\"" >> /etc/apache2/sites-available/$appdbname.conf
echo " </FilesMatch>" >> /etc/apache2/sites-available/$appdbname.conf
echo "" >> /etc/apache2/sites-available/$appdbname.conf
echo " ErrorLog ${APACHE_LOG_DIR}/$appdbname_error.log" >> /etc/apache2/sites-available/$appdbname.conf
echo " CustomLog ${APACHE_LOG_DIR}/$appdbname_access.log combined" >> /etc/apache2/sites-available/$appdbname.conf
echo "</VirtualHost>" >> /etc/apache2/sites-available/$appdbname.conf
a2ensite $appdbname.conf > /dev/null 2>&1
errorcheck $? "a2ensite $appdbname.conf" "EXIT"
a2dissite 000-default.conf > /dev/null 2>&1
errorcheck $? "a2dissite 000-default.conf" "EXIT"
systemctl restart apache2 > /dev/null 2>&1
errorcheck $? "systemctl restart apache2" "EXIT"
echo "14" > "$origpath/setupninja.step"
echo
}
function gossl {
#SETUP LET'S ENCRYPT SSL
if [[ $ishttps == true ]]; then
echo "Installing Let's Encrypt Certificate..."
a2enmod ssl > /dev/null 2>&1
errorcheck $? "a2enmod ssl > /dev/null" "EXIT"
systemctl restart apache2 > /dev/null 2>&1
errorcheck $? "systemctl restart apache2" "EXIT"
apt -y install certbot python3-certbot-apache > /dev/null 2>&1
errorcheck $? "apt -y install certbot python3-certbot-apache" "EXIT"
certbot --apache -d $fqdn --agree-tos -m $emailforle --redirect > /dev/null 2>&1
errorcheck $? "certbot --apache -d $fqdn --agree-tos -m $emailforle --redirect" "EXIT"
echo "15" > "$origpath/setupninja.step"
echo
fi
}
function goweb {
#INSTALL VIA WEB
echo "Configuring Invoice Ninja via Web Interface..."
mailnameforurl=$( echo $mailname | sed 's/ /+/g' )
mailuserforurl=$( echo $mailuser | sed 's/@/%40/g' )
mailfromforurl=$( echo $mailfrom | sed 's/@/%40/g' )
if [[ $ishttps == true ]]; then
temp=($( curl -s https://$fqdn/public/setup | grep "_token" ))
else
temp=($( curl -s http://$fqdn/public/setup | grep "_token" ))
fi
token=${temp[3]:7:-1}
echo "Token: $token"
echo "Generating password for admin account..."
adminpass=$( generatepassword )
echo -n "Test PDF: "
if [[ $ishttps == true ]]; then
testpdf=$( curl -s -X POST "https://$fqdn/public/setup/check_pdf" )
else
testpdf=$( curl -s -X POST "http://$fqdn/public/setup/check_pdf" )
fi
if [[ $testpdf == '{"url":""}' ]]; then
echo "Successful!"
errorcheck 0 "Test PDF Creation" "SUCCESS"
else
echo "ERROR!"
errorcheck 1 "Test PDF Creation" "$testpdf"
fi
echo -n "Test DB: "
if [[ $ishttps == true ]]; then
testdb=$( curl -s -X POST "https://$fqdn/public/setup/check_db" -H "Content-Type: application/json" -d "{\"db_host\":\"localhost\",\"db_port\":\"3306\",\"db_database\":\"$appdbname\",\"db_username\":\"$appdbuser\",\"db_password\":\"$appdbpass\"}" )
else
testdb=$( curl -s -X POST "http://$fqdn/public/setup/check_db" -H "Content-Type: application/json" -d "{\"db_host\":\"localhost\",\"db_port\":\"3306\",\"db_database\":\"$appdbname\",\"db_username\":\"$appdbuser\",\"db_password\":\"$appdbpass\"}" )
fi
if [[ $testdb == '[]' ]]; then
echo "Successful!"
errorcheck 0 "Test DB Settings" "SUCCESS"
else
echo "ERROR!"
errorcheck 1 "Test DB Settings" "$testdb"
fi
echo -n "Test Mail: "
if [[ $ishttps == true ]]; then
testmail=$( curl -s -X POST "https://$fqdn/public/setup/check_mail" -H "Content-Type: application/json" -d "{\"mail_driver\":\"smtp\",\"mail_name\":\"$mailname\",\"mail_address\":\"$mailfrom\",\"mail_username\":\"$mailuser\",\"mail_host\":\"$mailhost\",\"mail_port\":\"$mailport\",\"encryption\":\"ssl\",\"mail_password\":\"$mailpass\"}" )
else
testmail=$( curl -s -X POST "http://$fqdn/public/setup/check_mail" -H "Content-Type: application/json" -d "{\"mail_driver\":\"smtp\",\"mail_name\":\"$mailname\",\"mail_address\":\"$mailfrom\",\"mail_username\":\"$mailuser\",\"mail_host\":\"$mailhost\",\"mail_port\":\"$mailport\",\"encryption\":\"ssl\",\"mail_password\":\"$mailpass\"}" )
fi
if [[ $testmail == '[]' ]]; then
echo "Successful!"
errorcheck 0 "Test Email Settings" "SUCCESS"
else
echo "ERROR!"
errorcheck 1 "Test Email Settings" "$testmail"
fi
echo "Submit Everything"
if [[ $ishttps == true ]]; then
testsubmit=$( curl -s -X POST "https://$fqdn/public/setup" -H "Content-Type: application/x-www-form-urlencoded" -d "_token=$token&url=http%3A%2F%2F$fqdn%2Fpublic&db_driver=MySQL&db_host=localhost&db_port=3306&db_database=$appdbname&db_username=$appdbuser&db_password=$appdbpass&mail_driver=smtp&mail_name=$mailnameforurl&mail_address=$mailfromforurl&mail_username=$mailuserforurl&mail_host=$mailhost&mail_port=$mailport&encryption=ssl&mail_password=$mailpass&first_name=System&last_name=Admin&email=$mailfromforurl&password=$adminpass&terms_of_service=on&privacy_policy=on" )
else
testsubmit=$( curl -s -X POST "http://$fqdn/public/setup" -H "Content-Type: application/x-www-form-urlencoded" -d "_token=$token&url=http%3A%2F%2F$fqdn%2Fpublic&db_driver=MySQL&db_host=localhost&db_port=3306&db_database=$appdbname&db_username=$appdbuser&db_password=$appdbpass&mail_driver=smtp&mail_name=$mailnameforurl&mail_address=$mailfromforurl&mail_username=$mailuserforurl&mail_host=$mailhost&mail_port=$mailport&encryption=ssl&mail_password=$mailpass&first_name=System&last_name=Admin&email=$mailfromforurl&password=$adminpass&terms_of_service=on&privacy_policy=on" )
fi
echo "16" > "$origpath/setupninja.step"
echo
}
function gocron {
#SETUP CRONJOBS
echo "Creating Cron Jobs..."
echo "#Invoice Ninja Cron Jobs" >> /etc/crontab
echo "0 6 * * * www-data /usr/bin/php /var/www/$appdbname/artisan ninja:send-recurring > /dev/null" >> /etc/crontab
echo "0 6 * * * www-data /usr/bin/php /var/www/$appdbname/artisan ninja:send-reminders > /dev/null" >> /etc/crontab
echo "* * * * * www-data /usr/bin/php /var/www/$appdbname/artisan schedule:run >> /dev/null 2>&1" >> /etc/crontab
echo "17" > "$origpath/setupninja.step"
echo
}
function goreport {
#FINISHED
cd $origpath
echo "MariaDB root password: $mysqlpass" >> "setupninja.log"
echo "Database: $appdbname" >> "setupninja.log"
echo "Username: $appdbuser" >> "setupninja.log"
echo "Password: $appdbpass" >> "setupninja.log"
echo "URL: $fqdn/public" >> "setupninja.log"
echo "Login: $mailfrom" >> "setupninja.log"
echo "Password: $adminpass" >> "setupninja.log"
echo "FINISHED!!!"
echo "MariaDB root password: $mysqlpass"
echo "Database: $appdbname"
echo "Username: $appdbuser"
echo "Password: $appdbpass"
echo "URL: $fqdn/public"
echo "Login: $mailfrom"
echo "Password: $adminpass"
echo "This is all saved in $origpath/setupninja.log"
echo "18" > "$origpath/setupninja.step"
echo
}
function gohousekeeping {
#HOUSEKEEPING
echo "Cleaning up around the house..."
apt -y autoremove > /dev/null 2>&1
apt -y clean > /dev/null 2>&1
rm -f "$origpath/invoiceninja.zip" > /dev/null 2>&1
rm -f "$origpath/setupninja.sh" > /dev/null 2>&1
echo "19" > "$origpath/setupninja.step"
echo
}
#CHECK IF JSON EXISTS AND LOG EXISTS
apt -y install jq > /dev/null 2>&1
origpath=$(pwd)
step="0"
if [[ -f "$origpath/setupninja.log" ]]; then
step=$( cat "$origpath/setupninja.step" )
echo "STARTING AGAIN FROM STEP $step" >> "$origpath/setupninja.log"
echo "Starting again from step $step"
echo
else
touch "$origpath/setupninja.log"
fi
if [[ -f "$origpath/setupninja.json" ]]; then
applysettings
echo "0" > "$origpath/setupninja.step"
else
touch "$origpath/setupninja.json"
gathersettings
fi
if [[ $step -lt "1" ]]; then
sethostname
fi
if [[ $step -lt "2" ]]; then
initsetup
fi
if [[ $step -lt "3" ]]; then
gostandard
fi
if [[ $step -lt "4" ]]; then
gosury
fi
if [[ $step -lt "5" ]]; then
goapache
fi
if [[ $step -lt "6" ]]; then
gomariadb
fi
if [[ $step -lt "7" ]]; then
gophp
fi
if [[ $step -lt "8" ]]; then
confapachephp
fi
if [[ $step -lt "9" ]]; then
confmariadb
fi
if [[ $step -lt "10" ]]; then
ninjadb
fi
if [[ $step -lt "11" ]]; then
downloadninja
fi
if [[ $step -lt "12" ]]; then
unzipninja
fi
if [[ $step -lt "13" ]]; then
confninja
fi
if [[ $step -lt "14" ]]; then
vhostninja
fi
if [[ $step -lt "15" ]]; then
gossl
fi
if [[ $step -lt "16" ]]; then
goweb
fi
if [[ $step -lt "17" ]]; then
gocron
fi
if [[ $step -lt "18" ]]; then
goreport
fi
if [[ $step -lt "19" ]]; then
gohousekeeping
else
echo "Nothing to do!"
fi
exit 0