From fbea1c0823070eb899e8d5ed7628b71abf28a364 Mon Sep 17 00:00:00 2001 From: tiagom62 Date: Sat, 7 Oct 2017 01:58:41 -0400 Subject: [PATCH] Code dedupe, general cleanup and added a verbose option for debugging. (#4173) --- snipeit.sh | 329 +++++++++++++++++++---------------------------------- 1 file changed, 117 insertions(+), 212 deletions(-) diff --git a/snipeit.sh b/snipeit.sh index 4aa49078c5..78ab2cc925 100755 --- a/snipeit.sh +++ b/snipeit.sh @@ -8,7 +8,6 @@ # install process for Debian and CentOS # # based distributions. I assume you will be # # installing as a subdomain on a fresh OS install. # -# Right now I'm not going to worry about SMTP setup # # # # Feel free to modify, but please give # # credit where it's due. Thanks! # @@ -28,9 +27,9 @@ fi clear name="snipeit" +verbose="false" hostname="$(hostname)" fqdn="$(hostname --fqdn)" -ans=default hosts=/etc/hosts spin[0]="-" @@ -50,8 +49,7 @@ progress () { done } -#Used for Debian and Ubuntu -vhenvfile () { +setvhdebian () { find /etc/apache2/mods-enabled -maxdepth 1 -name 'rewrite.load' >/dev/null 2>&1 apachefile=/etc/apache2/sites-available/$name.conf { @@ -71,22 +69,37 @@ vhenvfile () { log "a2ensite $name.conf" } -perms () { - chmod_dirs=( "$webdir/$name/storage" ) - chmod_dirs+=( "$webdir/$name/storage/private_uploads" ) - chmod_dirs+=( "$webdir/$name/public/uploads" ) - #Change permissions on directories - for chmod_dir in "${chmod_dirs[@]}" - do - chmod -R 755 "$chmod_dir" - done +setvhcentos () { + apachefile=/etc/httpd/conf.d/$name.conf + { + echo "" + echo "ServerAdmin webmaster@localhost" + echo " " + echo " Allow From All" + echo " AllowOverride All" + echo " Options +Indexes" + echo " " + echo " DocumentRoot $webdir/$name/public" + echo " ServerName $fqdn" + echo " ErrorLog /var/log/httpd/snipeIT.error.log" + echo " CustomLog /var/log/access.log combined" + echo "" + } >> "$apachefile" } log () { - eval "$@" |& tee -a /var/log/snipeit-install.log >/dev/null 2>&1 + if [ "$verbose" = true ]; then + eval "$@" + else + eval "$@" |& tee -a /var/log/snipeit-install.log >/dev/null 2>&1 + fi } -setenv () { +installsnipeit () { + echo "* Cloning Snipe-IT from github to the web directory." + log "git clone https://github.com/snipe/snipe-it $webdir/$name" + + echo "* Configuring .env file." cp $webdir/$name/.env.example $webdir/$name/.env sed -i '1 i\#Created By Snipe-it Installer' $webdir/$name/.env @@ -96,12 +109,30 @@ setenv () { sed -i 's,^\(DB_USERNAME=\).*,\1'snipeit',' $webdir/$name/.env sed -i 's,^\(DB_PASSWORD=\).*,\1'$mysqluserpw',' $webdir/$name/.env sed -i 's,^\(APP_URL=\).*,\1'http://$fqdn',' $webdir/$name/.env -} -installcomposer () { - cd $webdir/$name/ - curl -sS https://getcomposer.org/installer | php - php composer.phar install --no-dev --prefer-source + echo "* Installing and running composer." + cd $webdir/$name/ + curl -sS https://getcomposer.org/installer | php + php composer.phar install --no-dev --prefer-source + + echo "* Setting permissions." + chmod_dirs=( "$webdir/$name/storage" ) + chmod_dirs+=( "$webdir/$name/storage/private_uploads" ) + chmod_dirs+=( "$webdir/$name/public/uploads" ) + for chmod_dir in "${chmod_dirs[@]}" + do + chmod -R 755 "$chmod_dir" + done + chown -R $ownergroup $webdir/$name + + echo "* Generating the application key." + log "php artisan key:generate --force" + + echo "* Artisan Migrate." + log "php artisan migrate --force" + + echo "* Creating scheduler cron." + (crontab -l ; echo "* * * * * /usr/bin/php $webdir/$name/artisan schedule:run >> /dev/null 2>&1") | crontab - } #CentOS Friendly f(x)s @@ -140,7 +171,7 @@ echo " " echo "" -echo " Welcome to Snipe-IT Inventory Installer for Centos, Debian and Ubuntu!" +echo " Welcome to Snipe-IT Inventory Installer for CentOS, Debian and Ubuntu!" echo "" shopt -s nocasematch case $distro in @@ -171,6 +202,7 @@ fi echo " Setting to $fqdn" echo "" +ans=default until [[ $ans == "yes" ]] || [[ $ans == "no" ]]; do echo -n " Q. Do you want to automatically create the database user password? (y/n) " read setpw @@ -201,6 +233,8 @@ case $distro in if [[ "$version" =~ ^9 ]]; then ##################################### Install for Debian 9 ############################################## webdir=/var/www + ownergroup=www-data:www-data + tzone=$(cat /etc/timezone) echo "* Updating with apt-get update." log "apt-get update" & pid=$! @@ -210,50 +244,31 @@ case $distro in log "apt-get -y upgrade" & pid=$! progress - echo "* Installing httpd, PHP, MariaDB and other requirements." + echo "* Installing Apache httpd, PHP, MariaDB and other requirements." log "DEBIAN_FRONTEND=noninteractive apt-get install -y mariadb-server mariadb-client apache2 libapache2-mod-php php php-mcrypt php-curl php-mysql php-gd php-ldap php-zip php-mbstring php-xml php-bcmath curl git unzip" & pid=$! progress log "a2enmod rewrite" - echo "* Cloning Snipe-IT from github to the web directory." - log "git clone https://github.com/snipe/snipe-it $webdir/$name" & pid=$! - progress - - echo "* Configuring .env file." - tzone=$(cat /etc/timezone) - setenv - echo "* Creating the new virtual host in Apache." - vhenvfile + setvhdebian echo >> $hosts "127.0.0.1 $hostname $fqdn" - a2ensite $name.conf - echo "* Securing MariaDB server."; + echo "* Securing MariaDB." /usr/bin/mysql_secure_installation echo "* Creating MariaDB Database/User." echo "* Please Input your MariaDB root password:" mysql -u root -p --execute="CREATE DATABASE snipeit;GRANT ALL PRIVILEGES ON snipeit.* TO snipeit@localhost IDENTIFIED BY '$mysqluserpw';" - echo "* Installing and running composer." - installcomposer + installsnipeit - echo "* Setting permissions." - perms - chown -R www-data:www-data "/var/www/$name" - - service apache2 restart - - echo "* Generating the application key." - php artisan key:generate --force - - echo "* Artisan Migrate." - php artisan migrate --force + echo "* Restarting Apache httpd." + log "service apache2 restart" else - echo "Unable to Handle Debian Version #. Version Found: " $version + echo "Unsupported Debian version. Version found: " $version return 1 fi ;; @@ -261,6 +276,8 @@ case $distro in if [[ "$version" =~ 1[6-7] ]]; then ##################################### Install for Ubuntu 16-17 ############################################## webdir=/var/www + ownergroup=www-data:www-data + tzone=$(cat /etc/timezone) echo "* Adding MariaDB repository." log "apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8" @@ -268,14 +285,15 @@ case $distro in echo "* Updating with apt-get update." log "apt-get update" & pid=$! - [ -f /var/lib/dpkg/lock ] && rm -f /var/lib/dpkg/lock + #https://wiki.debian.org/Teams/Dpkg/FAQ#Q:_What_can_be_done_when_the_dpkg_lock_is_held.3F + #[ -f /var/lib/dpkg/lock ] && rm -f /var/lib/dpkg/lock progress echo "* Upgrading packages with apt-get upgrade." log "apt-get -y upgrade" & pid=$! progress - echo "* Installing httpd, PHP, MariaDB and other requirements." + echo "* Installing Apache httpd, PHP, MariaDB and other requirements." log "DEBIAN_FRONTEND=noninteractive apt-get install -y mariadb-server mariadb-client apache2 libapache2-mod-php php php-mcrypt php-curl php-mysql php-gd php-ldap php-zip php-mbstring php-xml php-bcmath curl git unzip" & pid=$! progress @@ -283,45 +301,29 @@ case $distro in log "phpenmod mbstring" log "a2enmod rewrite" - echo "* Cloning Snipe-IT from github to the web directory." - log "git clone https://github.com/snipe/snipe-it $webdir/$name" & pid=$! - progress - - echo "* Configuring .env file." - tzone=$(cat /etc/timezone) - setenv - echo "* Creating the new virtual host in Apache." - vhenvfile + setvhdebian - echo "* Starting the MariaDB server."; - service mysql status >/dev/null || service mysql start + echo "* Starting MariaDB." + log "service mysql start" - echo "* Securing MariaDB server."; + echo "* Securing MariaDB." /usr/bin/mysql_secure_installation echo "* Creating MariaDB Database/User." echo "* Please Input your MariaDB root password:" mysql -u root -p --execute="CREATE DATABASE snipeit;GRANT ALL PRIVILEGES ON snipeit.* TO snipeit@localhost IDENTIFIED BY '$mysqluserpw';" - echo "* Installing and running composer." - installcomposer + installsnipeit - echo "* Setting permissions." - perms - chown -R www-data:www-data "/var/www/$name" - - service apache2 restart - - echo "* Generating the application key." - php artisan key:generate --force - - echo "* Artisan Migrate." - php artisan migrate --force + echo "* Restarting Apache httpd." + log "service apache2 restart" elif [[ "$version" =~ 14 ]]; then ##################################### Install for Ubuntu 14 ############################################## webdir=/var/www + ownergroup=www-data:www-data + tzone=$(cat /etc/timezone) echo "* Adding MariaDB and ppa:ondrej/php repositories." log "apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db" @@ -331,14 +333,15 @@ case $distro in echo "* Updating with apt-get update." log "apt-get update" & pid=$! - [ -f /var/lib/dpkg/lock ] && rm -f /var/lib/dpkg/lock + #https://wiki.debian.org/Teams/Dpkg/FAQ#Q:_What_can_be_done_when_the_dpkg_lock_is_held.3F + #[ -f /var/lib/dpkg/lock ] && rm -f /var/lib/dpkg/lock progress echo "* Upgrading packages with apt-get upgrade." log "apt-get -y upgrade" & pid=$! progress - echo "* Installing httpd, PHP, MariaDB and other requirements." + echo "* Installing Apache httpd, PHP, MariaDB and other requirements." log "DEBIAN_FRONTEND=noninteractive apt-get install -y mariadb-server mariadb-client php7.1 php7.1-mcrypt php7.1-curl php7.1-mysql php7.1-gd php7.1-ldap php7.1-zip php7.1-mbstring php7.1-xml php7.1-bcmath curl git unzip" & pid=$! progress @@ -346,53 +349,37 @@ case $distro in log "phpenmod mbstring" log "a2enmod rewrite" - echo "* Cloning Snipe-IT from github to the web directory." - log "git clone https://github.com/snipe/snipe-it $webdir/$name" & pid=$! - progress - - echo "* Configuring .env file." - tzone=$(cat /etc/timezone) - setenv - echo "* Creating the new virtual host in Apache." - vhenvfile + setvhdebian - echo "* Starting the MariaDB server."; - service mysql status >/dev/null || service mysql start + echo "* Starting MariaDB." + log "service mysql start" - echo "* Securing MariaDB server."; + echo "* Securing MariaDB." /usr/bin/mysql_secure_installation echo "* Creating MariaDB Database/User." echo "* Please Input your MariaDB root password:" mysql -u root -p --execute="CREATE DATABASE snipeit;GRANT ALL PRIVILEGES ON snipeit.* TO snipeit@localhost IDENTIFIED BY '$mysqluserpw';" - echo "* Installing and running composer." - installcomposer + installsnipeit - echo "* Setting permissions." - perms - chown -R www-data:www-data "/var/www/$name" - - service apache2 restart - - echo "* Generating the application key." - php artisan key:generate --force - - echo "* Artisan Migrate." - php artisan migrate --force + echo "* Restarting Apache httpd." + log "service apache2 restart" else - echo "Unable to Handle Ubuntu Version #. Version Found: " $version + echo "Unsupported Ubuntu version. Version found: " $version return 1 fi ;; centos) if [[ "$version" =~ ^6 ]]; then - ##################################### Install for Centos/Redhat 6 ############################################## + ##################################### Install for CentOS/Redhat 6 ############################################## webdir=/var/www/html + ownergroup=apache:apache + tzone=$(grep ZONE /etc/sysconfig/clock | tr -d '"' | sed 's/ZONE=//g'); - echo "* Adding IUS, epel-release and MariaDB repositories."; + echo "* Adding IUS, epel-release and MariaDB repositories." mariadbRepo=/etc/yum.repos.d/MariaDB.repo touch "$mariadbRepo" { @@ -408,7 +395,7 @@ case $distro in log "yum -y install https://centos6.iuscommunity.org/ius-release.rpm" log "rpm --import /etc/pki/rpm-gpg/IUS-COMMUNITY-GPG-KEY" - echo "* Installing httpd, PHP, MariaDB and other requirements."; + echo "* Installing Apache httpd, PHP, MariaDB and other requirements." PACKAGES="httpd mariadb-server git unzip php71u php71u-mysqlnd php71u-bcmath php71u-cli php71u-common php71u-embedded php71u-gd php71u-mbstring php71u-mcrypt php71u-ldap php71u-json php71u-simplexml" for p in $PACKAGES;do @@ -420,15 +407,11 @@ case $distro in fi done; - echo "* Cloning Snipe-IT from github to the web directory."; - log "git clone https://github.com/snipe/snipe-it $webdir/$name" + echo "* Setting MariaDB to start on boot and starting MariaDB." + log "chkconfig mysql on" + log "/sbin/service mysql start" - # Make mariaDB start on boot and restart the daemon - echo "* Starting the MariaDB server."; - chkconfig mysql on - /sbin/service mysql start - - echo "* Securing MariaDB server."; + echo "* Securing MariaDB." /usr/bin/mysql_secure_installation echo "* Creating MariaDB Database/User." @@ -437,45 +420,11 @@ case $distro in #Create the new virtual host in Apache and enable rewrite echo "* Creating the new virtual host in Apache." - apachefile=/etc/httpd/conf.d/$name.conf + setvhcentos - { - echo "" - echo "" - echo "" - echo "" - echo "ServerAdmin webmaster@localhost" - echo " " - echo " Allow From All" - echo " AllowOverride All" - echo " Options +Indexes" - echo " " - echo " DocumentRoot $webdir/$name/public" - echo " ServerName $fqdn" - echo " ErrorLog /var/log/httpd/snipeIT.error.log" - echo " CustomLog /var/log/access.log combined" - echo "" - } >> "$apachefile" - - echo "* Setting up hosts file."; + echo "* Setting up hosts file." echo >> $hosts "127.0.0.1 $hostname $fqdn" - # Make apache start on boot and restart the daemon - echo "* Starting the apache server."; - chkconfig httpd on - /sbin/service httpd start - - echo "* Configuring .env file." - tzone=$(grep ZONE /etc/sysconfig/clock | tr -d '"' | sed 's/ZONE=//g'); - setenv - - echo "* Installing and running composer." - installcomposer - - echo "* Setting permissions." - perms - chown -R apache:apache $webdir/$name - /sbin/service iptables status >/dev/null 2>&1 if [ $? = 0 ]; then echo "* Configuring iptables." @@ -484,24 +433,24 @@ case $distro in service iptables save fi - service httpd restart + installsnipeit - echo "* Generating the application key." - php artisan key:generate --force - - echo "* Artisan Migrate." - php artisan migrate --force + echo "* Setting Apache httpd to start on boot and starting service." + log "chkconfig httpd on" + log "/sbin/service httpd start" elif [[ "$version" =~ ^7 ]]; then - ##################################### Install for Centos/Redhat 7 ############################################## + ##################################### Install for CentOS/Redhat 7 ############################################## webdir=/var/www/html + ownergroup=apache:apache + tzone=$(timedatectl | gawk -F'[: ]' ' $9 ~ /zone/ {print $11}'); - echo "* Adding IUS, epel-release and MariaDB repositories."; + echo "* Adding IUS, epel-release and MariaDB repositories." log "yum -y install wget epel-release" log "yum -y install https://centos7.iuscommunity.org/ius-release.rpm" log "rpm --import /etc/pki/rpm-gpg/IUS-COMMUNITY-GPG-KEY" - echo "* Installing httpd, PHP, MariaDB and other requirements."; + echo "* Installing Apache httpd, PHP, MariaDB and other requirements." PACKAGES="httpd mariadb-server git unzip php71u php71u-mysqlnd php71u-bcmath php71u-cli php71u-common php71u-embedded php71u-gd php71u-mbstring php71u-mcrypt php71u-ldap php71u-json php71u-simplexml" for p in $PACKAGES;do @@ -513,15 +462,11 @@ case $distro in fi done; - echo "* Cloning Snipe-IT from github to the web directory."; - log "git clone https://github.com/snipe/snipe-it $webdir/$name" + echo "* Setting MariaDB to start on boot and starting MariaDB." + log "systemctl enable mariadb.service" + log "systemctl start mariadb.service" - #Make mariaDB start on boot and restart the daemon - echo "* Starting the MariaDB server."; - systemctl enable mariadb.service - systemctl start mariadb.service - - echo "* Securing MariaDB server."; + echo "* Securing MariaDB." /usr/bin/mysql_secure_installation echo "* Creating MariaDB Database/User." @@ -531,46 +476,13 @@ case $distro in #TODO make sure the apachefile doesnt exist isnt already in there #Create the new virtual host in Apache and enable rewrite echo "* Creating the new virtual host in Apache." - apachefile="/etc/httpd/conf.d/$name.conf" - - { - - echo "" - echo "" - echo "LoadModule rewrite_module modules/mod_rewrite.so" - echo "" - echo "" - echo "ServerAdmin webmaster@localhost" - echo " " - echo " Allow From All" - echo " AllowOverride All" - echo " Options +Indexes" - echo " " - echo " DocumentRoot $webdir/$name/public" - echo " ServerName $fqdn" - echo " ErrorLog /var/log/httpd/snipeIT.error.log" - echo " CustomLog /var/log/access.log combined" - echo "" - } >> "$apachefile" + setvhcentos #TODO make sure this isnt already in there - echo "* Setting up hosts file."; + echo "* Setting up hosts file." echo >> $hosts "127.0.0.1 $hostname $fqdn" - echo "* Starting the apache server."; - systemctl enable httpd.service - systemctl restart httpd.service - - echo "* Configuring .env file." - tzone=$(timedatectl | gawk -F'[: ]' ' $9 ~ /zone/ {print $11}'); - setenv - - echo "* Installing and running composer." - installcomposer - - echo "* Setting permissions." - perms - chown -R apache:apache $webdir/$name + installsnipeit #Check if SELinux is enforcing if [ "$(getenforce)" == "Enforcing" ]; then @@ -581,19 +493,12 @@ case $distro in chcon -R -h -t httpd_sys_script_rw_t $webdir/$name/ fi + echo "* Setting Apache httpd to start on boot and starting service." + log "systemctl enable httpd.service" log "systemctl restart httpd.service" - echo "* Generating the application key." - php artisan key:generate --force - - echo "* Artisan Migrate." - php artisan migrate --force - - echo "* Creating scheduler cron." - (crontab -l ; echo "* * * * * /usr/bin/php $webdir/$name/artisan schedule:run >> /dev/null 2>&1") | crontab - - else - echo "Unable to Handle Centos Version #. Version Found: " $version + echo "Unsupported CentOS version. Version found: " $version return 1 fi esac