installer automation? (#5433)

* cli

* less spaces!

* rename functions

* move progress spinner variables into progress ()

* $hosts was used in one place

* missed this one

* make testing snipeit.sh easier
This commit is contained in:
tiagom62 2018-04-29 09:09:46 -04:00 committed by snipe
parent d6d498bc8f
commit 6ad3d40216
2 changed files with 557 additions and 453 deletions

68
Vagrantfile vendored Normal file
View file

@ -0,0 +1,68 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :
SNIPEIT_SH_URL= "https://raw.githubusercontent.com/snipe/snipe-it/master/snipeit.sh"
NETWORK_BRIDGE= "en0: Wi-Fi (AirPort)"
Vagrant.configure("2") do |config|
config.vm.define "xenial" do |xenial|
xenial.vm.box = "ubuntu/xenial64"
xenial.vm.hostname = 'xenial'
xenial.vm.network "public_network", bridge: NETWORK_BRIDGE
xenial.vm.provision :shell, :inline => "wget #{SNIPEIT_SH_URL}"
xenial.vm.provision :shell, :inline => "chmod 755 snipeit.sh"
end
config.vm.define "trusty" do |trusty|
trusty.vm.box = "ubuntu/trusty32"
trusty.vm.hostname = 'trusty'
trusty.vm.network "public_network", bridge: NETWORK_BRIDGE
trusty.vm.provision :shell, :inline => "wget #{SNIPEIT_SH_URL}"
trusty.vm.provision :shell, :inline => "chmod 755 snipeit.sh"
end
config.vm.define "centos7" do |centos7|
centos7.vm.box = "centos/7"
centos7.vm.hostname = 'centos7'
centos7.vm.network "public_network", bridge: NETWORK_BRIDGE
centos7.vm.provision :shell, :inline => "sudo yum -y update"
centos7.vm.provision :shell, :inline => "yum install -y wget"
centos7.vm.provision :shell, :inline => "wget #{SNIPEIT_SH_URL}"
centos7.vm.provision :shell, :inline => "chmod 755 snipeit.sh"
end
config.vm.define "centos6" do |centos6|
centos6.vm.box = "centos/6"
centos6.vm.hostname = 'centos6'
centos6.vm.network "public_network", bridge: NETWORK_BRIDGE
centos6.vm.provision :shell, :inline => "sudo yum -y update"
centos6.vm.provision :shell, :inline => "wget #{SNIPEIT_SH_URL}"
centos6.vm.provision :shell, :inline => "chmod 755 snipeit.sh"
end
config.vm.define "jessie" do |jessie|
jessie.vm.box = "debian/jessie64"
jessie.vm.hostname = 'debian8'
jessie.vm.network "public_network", bridge: NETWORK_BRIDGE
jessie.vm.provision :shell, :inline => "wget #{SNIPEIT_SH_URL}"
jessie.vm.provision :shell, :inline => "chmod 755 snipeit.sh"
end
config.vm.define "stretch" do |stretch|
stretch.vm.box = "debian/stretch64"
stretch.vm.hostname = 'debian9'
stretch.vm.network "public_network", bridge: NETWORK_BRIDGE
stretch.vm.provision :shell, :inline => "wget #{SNIPEIT_SH_URL}"
stretch.vm.provision :shell, :inline => "chmod 755 snipeit.sh"
end
config.vm.define "fedora25" do |fedora25|
fedora25.vm.box = "fedora/25-cloud-base"
fedora25.vm.hostname = 'fedora25'
fedora25.vm.network "public_network", bridge: NETWORK_BRIDGE
fedora25.vm.provision :shell, :inline => "dnf -y update"
fedora25.vm.provision :shell, :inline => "dnf -y install wget"
fedora25.vm.provision :shell, :inline => "wget #{SNIPEIT_SH_URL}"
fedora25.vm.provision :shell, :inline => "chmod 755 snipeit.sh"
end
end

156
snipeit.sh Normal file → Executable file
View file

@ -1,4 +1,11 @@
#!/bin/bash #!/bin/bash
#/ Usage: sniepit [-vh]
#/
#/ Install Snipe-IT open source asset management.
#/
#/ OPTIONS:
#/ -v | --verbose Enable verbose output.
#/ -h | --help Show this message.
###################################################### ######################################################
# Snipe-It Install Script # # Snipe-It Install Script #
@ -13,6 +20,43 @@
# credit where it's due. Thanks! # # credit where it's due. Thanks! #
###################################################### ######################################################
# Parse arguments
while true; do
case "$1" in
-h|--help)
show_help=true
shift
;;
-v|--verbose)
set -x
verbose=true
shift
;;
-*)
echo "Error: invalid argument: '$1'" 1>&2
exit 1
;;
*)
break
;;
esac
done
print_usage () {
grep '^#/' <"$0" | cut -c 4-
exit ${1:-1}
}
if [ -n "$show_help" ]; then
print_usage
else
for x in "$@"; do
if [ "$x" = "--help" ] || [ "$x" = "-h" ]; then
print_usage
fi
done
fi
# ensure running as root # ensure running as root
if [ "$(id -u)" != "0" ]; then if [ "$(id -u)" != "0" ]; then
#Debian doesnt have sudo if root has a password. #Debian doesnt have sudo if root has a password.
@ -27,17 +71,15 @@ fi
clear clear
name="snipeit" name="snipeit"
verbose="false"
hostname="$(hostname)" hostname="$(hostname)"
fqdn="$(hostname --fqdn)" fqdn="$(hostname --fqdn)"
hosts=/etc/hosts
spin[0]="-"
spin[1]="\\"
spin[2]="|"
spin[3]="/"
progress () { progress () {
spin[0]="-"
spin[1]="\\"
spin[2]="|"
spin[3]="/"
echo -n " " echo -n " "
while kill -0 "$pid" > /dev/null 2>&1; do while kill -0 "$pid" > /dev/null 2>&1; do
for i in "${spin[@]}"; do for i in "${spin[@]}"; do
@ -49,21 +91,21 @@ progress () {
} }
log () { log () {
if [ "$verbose" = true ]; then if [ -n "$verbose" ]; then
eval "$@" eval "$@" |& tee -a /var/log/snipeit-install.log
else else
eval "$@" |& tee -a /var/log/snipeit-install.log >/dev/null 2>&1 eval "$@" |& tee -a /var/log/snipeit-install.log >/dev/null 2>&1
fi fi
} }
installpackages () { install_packages () {
case $distro in case $distro in
ubuntu|debian) ubuntu|debian)
for p in $PACKAGES; do for p in $PACKAGES; do
if dpkg -s "$p" >/dev/null 2>&1; then if dpkg -s "$p" >/dev/null 2>&1; then
echo " * $p already installed" echo " * $p already installed"
else else
echo " * Installing $p ... " echo " * Installing $p"
log "DEBIAN_FRONTEND=noninteractive apt-get install -y $p" log "DEBIAN_FRONTEND=noninteractive apt-get install -y $p"
fi fi
done; done;
@ -73,7 +115,7 @@ installpackages () {
if yum list installed "$p" >/dev/null 2>&1; then if yum list installed "$p" >/dev/null 2>&1; then
echo " * $p already installed" echo " * $p already installed"
else else
echo " * Installing $p ... " echo " * Installing $p"
log "yum -y install $p" log "yum -y install $p"
fi fi
done; done;
@ -83,7 +125,7 @@ installpackages () {
if dnf list installed "$p" >/dev/null 2>&1; then if dnf list installed "$p" >/dev/null 2>&1; then
echo " * $p already installed" echo " * $p already installed"
else else
echo " * Installing $p ... " echo " * Installing $p"
log "dnf -y install $p" log "dnf -y install $p"
fi fi
done; done;
@ -91,7 +133,7 @@ installpackages () {
esac esac
} }
createvh () { create_virtualhost () {
{ {
echo "<VirtualHost *:80>" echo "<VirtualHost *:80>"
echo " <Directory $webdir/$name/public>" echo " <Directory $webdir/$name/public>"
@ -106,7 +148,7 @@ createvh () {
} >> "$apachefile" } >> "$apachefile"
} }
installsnipeit () { install_snipeit () {
echo "* Creating MariaDB Database/User." echo "* Creating MariaDB Database/User."
echo "* Please Input your MariaDB root password:" 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';" mysql -u root -p --execute="CREATE DATABASE snipeit;GRANT ALL PRIVILEGES ON snipeit.* TO snipeit@localhost IDENTIFIED BY '$mysqluserpw';"
@ -148,7 +190,7 @@ installsnipeit () {
(crontab -l ; echo "* * * * * /usr/bin/php $webdir/$name/artisan schedule:run >> /dev/null 2>&1") | crontab - (crontab -l ; echo "* * * * * /usr/bin/php $webdir/$name/artisan schedule:run >> /dev/null 2>&1") | crontab -
} }
openfirewalld () { set_firewall () {
if [ "$(firewall-cmd --state)" == "running" ]; then if [ "$(firewall-cmd --state)" == "running" ]; then
echo "* Configuring firewall to allow HTTP traffic only." echo "* Configuring firewall to allow HTTP traffic only."
log "firewall-cmd --zone=public --add-port=http/tcp --permanent" log "firewall-cmd --zone=public --add-port=http/tcp --permanent"
@ -156,7 +198,7 @@ openfirewalld () {
fi fi
} }
configureselinux () { set_selinux () {
#Check if SELinux is enforcing #Check if SELinux is enforcing
if [ "$(getenforce)" == "Enforcing" ]; then if [ "$(getenforce)" == "Enforcing" ]; then
echo "* Configuring SELinux." echo "* Configuring SELinux."
@ -167,15 +209,15 @@ configureselinux () {
fi fi
} }
sethostfile () { set_hosts () {
echo "* Setting up hosts file." echo "* Setting up hosts file."
echo >> $hosts "127.0.0.1 $hostname $fqdn" echo >> /etc/hosts "127.0.0.1 $hostname $fqdn"
} }
if [[ -f /etc/lsb-release || -f /etc/debian_version ]]; then if [[ -f /etc/lsb-release || -f /etc/debian_version ]]; then
distro="$(lsb_release -s -i)" distro="$(lsb_release -is)"
version="$(lsb_release -s -r)" version="$(lsb_release -rs)"
codename="$(lsb_release -c -s)" codename="$(lsb_release -cs)"
elif [ -f /etc/os-release ]; then elif [ -f /etc/os-release ]; then
distro="$(. /etc/os-release && echo $ID)" distro="$(. /etc/os-release && echo $ID)"
version="$(. /etc/os-release && echo $VERSION_ID)" version="$(. /etc/os-release && echo $VERSION_ID)"
@ -275,23 +317,22 @@ case $distro in
echo "* Installing Apache httpd, PHP, MariaDB and other requirements." echo "* Installing Apache httpd, PHP, MariaDB and other requirements."
PACKAGES="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" PACKAGES="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"
installpackages install_packages
echo "* Configuring Apache." echo "* Configuring Apache."
createvh create_virtualhost
log "a2enmod rewrite" log "a2enmod rewrite"
log "a2ensite $name.conf" log "a2ensite $name.conf"
sethostfile set_hosts
echo "* Securing MariaDB." echo "* Securing MariaDB."
/usr/bin/mysql_secure_installation /usr/bin/mysql_secure_installation
installsnipeit install_snipeit
echo "* Restarting Apache httpd." echo "* Restarting Apache httpd."
log "service apache2 restart" log "service apache2 restart"
elif [[ "$version" =~ ^8 ]]; then elif [[ "$version" =~ ^8 ]]; then
##################################### Install for Debian 8 ############################################## ##################################### Install for Debian 8 ##############################################
webdir=/var/www webdir=/var/www
@ -315,23 +356,22 @@ case $distro in
echo "* Installing Apache httpd, PHP, MariaDB and other requirements." echo "* Installing Apache httpd, PHP, MariaDB and other requirements."
PACKAGES="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" PACKAGES="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"
installpackages install_packages
echo "* Configuring Apache." echo "* Configuring Apache."
createvh create_virtualhost
log "a2enmod rewrite" log "a2enmod rewrite"
log "a2ensite $name.conf" log "a2ensite $name.conf"
sethostfile set_hosts
echo "* Securing MariaDB." echo "* Securing MariaDB."
/usr/bin/mysql_secure_installation /usr/bin/mysql_secure_installation
installsnipeit install_snipeit
echo "* Restarting Apache httpd." echo "* Restarting Apache httpd."
log "service apache2 restart" log "service apache2 restart"
else else
echo "Unsupported Debian version. Version found: $version" echo "Unsupported Debian version. Version found: $version"
exit 1 exit 1
@ -357,16 +397,16 @@ case $distro in
echo "* Installing Apache httpd, PHP, MariaDB and other requirements." echo "* Installing Apache httpd, PHP, MariaDB and other requirements."
PACKAGES="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" PACKAGES="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"
installpackages install_packages
echo "* Configuring Apache." echo "* Configuring Apache."
createvh create_virtualhost
log "phpenmod mcrypt" log "phpenmod mcrypt"
log "phpenmod mbstring" log "phpenmod mbstring"
log "a2enmod rewrite" log "a2enmod rewrite"
log "a2ensite $name.conf" log "a2ensite $name.conf"
sethostfile set_hosts
echo "* Starting MariaDB." echo "* Starting MariaDB."
log "service mysql start" log "service mysql start"
@ -374,11 +414,10 @@ case $distro in
echo "* Securing MariaDB." echo "* Securing MariaDB."
/usr/bin/mysql_secure_installation /usr/bin/mysql_secure_installation
installsnipeit install_snipeit
echo "* Restarting Apache httpd." echo "* Restarting Apache httpd."
log "service apache2 restart" log "service apache2 restart"
elif [[ "$version" =~ 14 ]]; then elif [[ "$version" =~ 14 ]]; then
##################################### Install for Ubuntu 14 ############################################## ##################################### Install for Ubuntu 14 ##############################################
webdir=/var/www webdir=/var/www
@ -400,16 +439,16 @@ case $distro in
echo "* Installing Apache httpd, PHP, MariaDB and other requirements." echo "* Installing Apache httpd, PHP, MariaDB and other requirements."
PACKAGES="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" PACKAGES="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"
installpackages install_packages
echo "* Configuring Apache." echo "* Configuring Apache."
createvh create_virtualhost
log "phpenmod mcrypt" log "phpenmod mcrypt"
log "phpenmod mbstring" log "phpenmod mbstring"
log "a2enmod rewrite" log "a2enmod rewrite"
log "a2ensite $name.conf" log "a2ensite $name.conf"
sethostfile set_hosts
echo "* Starting MariaDB." echo "* Starting MariaDB."
log "service mysql start" log "service mysql start"
@ -417,11 +456,10 @@ case $distro in
echo "* Securing MariaDB." echo "* Securing MariaDB."
/usr/bin/mysql_secure_installation /usr/bin/mysql_secure_installation
installsnipeit install_snipeit
echo "* Restarting Apache httpd." echo "* Restarting Apache httpd."
log "service apache2 restart" log "service apache2 restart"
else else
echo "Unsupported Ubuntu version. Version found: $version" echo "Unsupported Ubuntu version. Version found: $version"
exit 1 exit 1
@ -453,21 +491,21 @@ case $distro in
echo "* Installing Apache 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 php71u-process" 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 php71u-process"
installpackages install_packages
echo "* Configuring Apache." echo "* Configuring Apache."
createvh create_virtualhost
echo "* Setting MariaDB to start on boot and starting MariaDB." echo "* Setting MariaDB to start on boot and starting MariaDB."
log "chkconfig mysql on" log "chkconfig mysql on"
log "/sbin/service mysql start" log "/sbin/service mysql start"
sethostfile set_hosts
echo "* Securing MariaDB." echo "* Securing MariaDB."
/usr/bin/mysql_secure_installation /usr/bin/mysql_secure_installation
installsnipeit install_snipeit
if /sbin/service iptables status >/dev/null 2>&1; then if /sbin/service iptables status >/dev/null 2>&1; then
echo "* Configuring iptables." echo "* Configuring iptables."
@ -479,7 +517,6 @@ case $distro in
echo "* Setting Apache httpd to start on boot and starting service." echo "* Setting Apache httpd to start on boot and starting service."
log "chkconfig httpd on" log "chkconfig httpd on"
log "/sbin/service httpd start" log "/sbin/service httpd start"
elif [[ "$version" =~ ^7 ]]; then elif [[ "$version" =~ ^7 ]]; then
##################################### Install for CentOS/Redhat 7 ############################################## ##################################### Install for CentOS/Redhat 7 ##############################################
webdir=/var/www/html webdir=/var/www/html
@ -494,12 +531,12 @@ case $distro in
echo "* Installing Apache 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 php71u-process" 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 php71u-process"
installpackages install_packages
echo "* Configuring Apache." echo "* Configuring Apache."
createvh create_virtualhost
sethostfile set_hosts
echo "* Setting MariaDB to start on boot and starting MariaDB." echo "* Setting MariaDB to start on boot and starting MariaDB."
log "systemctl enable mariadb.service" log "systemctl enable mariadb.service"
@ -508,16 +545,15 @@ case $distro in
echo "* Securing MariaDB." echo "* Securing MariaDB."
/usr/bin/mysql_secure_installation /usr/bin/mysql_secure_installation
installsnipeit install_snipeit
openfirewalld set_firewall
configureselinux set_selinux
echo "* Setting Apache httpd to start on boot and starting service." echo "* Setting Apache httpd to start on boot and starting service."
log "systemctl enable httpd.service" log "systemctl enable httpd.service"
log "systemctl restart httpd.service" log "systemctl restart httpd.service"
else else
echo "Unsupported CentOS version. Version found: $version" echo "Unsupported CentOS version. Version found: $version"
exit 1 exit 1
@ -532,12 +568,12 @@ case $distro in
echo "* Installing Apache httpd, PHP, MariaDB and other requirements." echo "* Installing Apache httpd, PHP, MariaDB and other requirements."
PACKAGES="httpd mariadb-server git unzip php php-mysqlnd php-bcmath php-cli php-common php-embedded php-gd php-mbstring php-mcrypt php-ldap php-json php-simplexml" PACKAGES="httpd mariadb-server git unzip php php-mysqlnd php-bcmath php-cli php-common php-embedded php-gd php-mbstring php-mcrypt php-ldap php-json php-simplexml"
installpackages install_packages
echo "* Configuring Apache." echo "* Configuring Apache."
createvh create_virtualhost
sethostfile set_hosts
echo "* Setting MariaDB to start on boot and starting MariaDB." echo "* Setting MariaDB to start on boot and starting MariaDB."
log "systemctl enable mariadb.service" log "systemctl enable mariadb.service"
@ -546,11 +582,11 @@ case $distro in
echo "* Securing MariaDB." echo "* Securing MariaDB."
/usr/bin/mysql_secure_installation /usr/bin/mysql_secure_installation
installsnipeit install_snipeit
openfirewalld set_firewall
configureselinux set_selinux
echo "* Setting Apache httpd to start on boot and starting service." echo "* Setting Apache httpd to start on boot and starting service."
log "systemctl enable httpd.service" log "systemctl enable httpd.service"