From 53807c9633c53c4294ceabf7fe6d0441c5cb55ee Mon Sep 17 00:00:00 2001
From: Michel Roegl-Brunner
 <73236783+michelroegl-brunner@users.noreply.github.com>
Date: Mon, 2 Dec 2024 09:05:12 +0100
Subject: [PATCH] SnipeIT install Script (#538)

* Bare Metal install Script for SnipeIT

* Fixed Copyright Header

* Worked in all requested Changes and also added the Update function.
Per my testing all is working as intendet.
---
 ct/snipeit.sh              |  94 ++++++++++++++++++++++++++++++++
 install/snipeit-install.sh | 107 +++++++++++++++++++++++++++++++++++++
 json/snipeit.json          |  31 +++++++++++
 3 files changed, 232 insertions(+)
 create mode 100644 ct/snipeit.sh
 create mode 100644 install/snipeit-install.sh
 create mode 100644 json/snipeit.json

diff --git a/ct/snipeit.sh b/ct/snipeit.sh
new file mode 100644
index 000000000..19045ed0d
--- /dev/null
+++ b/ct/snipeit.sh
@@ -0,0 +1,94 @@
+#!/usr/bin/env bash
+source <(curl -s https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/build.func)
+
+#Copyright (c) 2021-2024 community-scripts ORG
+# Author: Michel Roegl-Brunner (michelroegl-brunner)
+# License: MIT
+# https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
+function header_info {
+clear
+cat <<"EOF"
+   _____       _                  __________
+  / ___/____  (_)___  ___        /  _/_  __/
+  \__ \/ __ \/ / __ \/ _ \______ / /  / /   
+ ___/ / / / / / /_/ /  __/_____// /  / /    
+/____/_/ /_/_/ .___/\___/     /___/ /_/     
+            /_/                             
+EOF
+}
+header_info
+echo -e "Loading..."
+APP="SnipeIT"
+
+var_disk="4"
+var_cpu="2"
+var_ram="2048"
+var_os="debian"
+var_version="12"
+variables
+color
+catch_errors
+
+function default_settings() {
+  CT_TYPE="1"
+  PW=""
+  CT_ID=$NEXTID
+  HN=$NSAPP
+  DISK_SIZE="$var_disk"
+  CORE_COUNT="$var_cpu"
+  RAM_SIZE="$var_ram"
+  BRG="vmbr0"
+  NET="dhcp"
+  GATE=""
+  APT_CACHER=""
+  APT_CACHER_IP=""
+  DISABLEIP6="no"
+  MTU=""
+  SD=""
+  NS=""
+  MAC=""
+  VLAN=""
+  SSH="no"
+  VERB="no"
+  echo_default
+}
+
+function update_script() {
+header_info
+check_container_storage
+check_container_resources
+if [[ ! -d /opt/snipe-it ]]; then msg_error "No ${APP} Installation Found!"; exit; fi
+msg_info "Updating ${APP} LXC"
+apt-get update &>/dev/null
+apt-get -y upgrade &>/dev/null
+mv /opt/snipe-it /opt/snipe-it-backup
+cd /opt
+RELEASE=$(curl -s https://api.github.com/repos/snipe/snipe-it/releases/latest | grep "tag_name" | awk '{print substr($2, 3, length($2)-4) }')
+wget -q "https://github.com/snipe/snipe-it/archive/refs/tags/v${RELEASE}.zip" &>/dev/null
+unzip -q v${RELEASE}.zip
+mv snipe-it-${RELEASE} /opt/snipe-it
+cp /opt/snipe-it-backup/.env /opt/snipe-it/.env
+cp -r /opt/snipe-it-backup/public/uploads/ /opt/snipe-it/public/uploads/
+cp -r /opt/snipe-it-backup/storage/private_uploads /opt/snipe-it/storage/private_uploads
+cd /opt/snipe-it/
+export COMPOSER_ALLOW_SUPERUSER=1
+composer install --no-dev --prefer-source &>/dev/null
+composer dump-autoload &>/dev/null
+php artisan migrate --force &>/dev/null
+php artisan config:clear &>/dev/null
+php artisan route:clear &>/dev/null
+php artisan cache:clear &>/dev/null
+php artisan view:clear &>/dev/null
+chown -R www-data: /opt/snipe-it
+chmod -R 755 /opt/snipe-it
+rm -rf /opt/v${RELEASE}.zip
+rm -rf /opt/snipe-it-backup
+msg_ok "Updated ${APP} LXC"
+exit
+}
+
+start
+build_container
+description
+
+msg_ok "Completed Successfully!\n"
diff --git a/install/snipeit-install.sh b/install/snipeit-install.sh
new file mode 100644
index 000000000..897feb8a5
--- /dev/null
+++ b/install/snipeit-install.sh
@@ -0,0 +1,107 @@
+#!/usr/bin/env bash
+
+
+#Copyright (c) 2021-2024 community-scripts ORG
+# Author: Michel Roegl-Brunner (michelroegl-brunner)
+# License: MIT
+# https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
+
+
+source /dev/stdin <<<"$FUNCTIONS_FILE_PATH"
+color
+verb_ip6
+catch_errors
+setting_up_container
+network_check
+update_os
+
+msg_info "Installing Dependencies"
+$STD apt-get install -y \
+  curl \
+  composer \
+  git \
+  sudo \
+  mc \
+  nginx \
+  php8.2-{bcmath,common,ctype,curl,fileinfo,fpm,gd,iconv,intl,mbstring,mysql,soap,xml,xsl,zip,cli} \
+  mariadb-server 
+msg_ok "Installed Dependencies"
+
+msg_info "Setting up database"
+DB_NAME=snipeit_db
+DB_USER=snipeit
+DB_PASS=$(openssl rand -base64 18 | tr -dc 'a-zA-Z0-9' | head -c13)
+mysql -u root -e "CREATE DATABASE $DB_NAME;"
+mysql -u root -e "CREATE USER '$DB_USER'@'localhost' IDENTIFIED WITH mysql_native_password AS PASSWORD('$DB_PASS');"
+mysql -u root -e "GRANT ALL ON $DB_NAME.* TO '$DB_USER'@'localhost'; FLUSH PRIVILEGES;"
+{
+    echo "SnipeIT-Credentials"
+    echo "SnipeIT Database User: $DB_USER"
+    echo "SnipeIT Database Password: $DB_PASS"
+    echo "SnipeIT Database Name: $DB_NAME"
+} >> ~/snipeit.creds
+msg_ok "Set up database"
+
+msg_info "Installing Snipe-IT"
+cd /opt
+RELEASE=$(curl -s https://api.github.com/repos/snipe/snipe-it/releases/latest | grep "tag_name" | awk '{print substr($2, 3, length($2)-4) }')
+wget -q "https://github.com/snipe/snipe-it/archive/refs/tags/v${RELEASE}.zip"
+unzip -q v${RELEASE}.zip
+mv snipe-it-${RELEASE} /opt/snipe-it
+
+cd /opt/snipe-it
+cp .env.example .env
+IPADDRESS=$(hostname -I | awk '{print $1}')
+
+sed -i -e "s|^APP_URL=.*|APP_URL=http://$IPADDRESS|" \
+       -e "s|^DB_DATABASE=.*|DB_DATABASE=$DB_NAME|" \
+       -e "s|^DB_USERNAME=.*|DB_USERNAME=$DB_USER|" \
+       -e "s|^DB_PASSWORD=.*|DB_PASSWORD=$DB_PASS|" .env
+
+chown -R www-data: /opt/snipe-it
+chmod -R 755 /opt/snipe-it
+
+
+export COMPOSER_ALLOW_SUPERUSER=1
+$STD composer update --no-plugins --no-scripts
+$STD composer install --no-dev --prefer-source --no-plugins --no-scripts
+
+$STD php artisan key:generate --force
+msg_ok "Installed SnipeIT"
+
+msg_info "Creating Service"
+
+cat <<EOF >/etc/nginx/conf.d/snipeit.conf
+server {
+        listen 80;
+        root /opt/snipe-it/public;
+        server_name $IPADDRESS; 
+        index index.php;
+                
+        location / {
+                try_files \$uri \$uri/ /index.php?\$query_string;
+        }
+        
+        location ~ \.php\$ {
+                include fastcgi.conf;
+                include snippets/fastcgi-php.conf;
+                fastcgi_pass unix:/run/php/php8.2-fpm.sock;
+                fastcgi_split_path_info ^(.+\.php)(/.+)\$;
+                fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
+                include fastcgi_params;
+        }
+}
+EOF
+
+systemctl reload nginx
+msg_ok "Configured Service"
+
+
+motd_ssh
+customize
+
+msg_info "Cleaning up"
+rm -rf /opt/v${RELEASE}.zip
+$STD apt-get -y autoremove
+$STD apt-get -y autoclean
+msg_ok "Cleaned"
diff --git a/json/snipeit.json b/json/snipeit.json
new file mode 100644
index 000000000..e202bc3ad
--- /dev/null
+++ b/json/snipeit.json
@@ -0,0 +1,31 @@
+{
+    "name": "SnipeIT",
+    "slug": "snipeit",
+    "categories": [
+        8
+    ],
+    "date_created": "2024-25-11",
+    "type": "ct",
+    "updateable": false,
+    "privileged": false,
+    "interface_port": null,
+    "documentation": null,
+    "website": "https://snipeitapp.com/",
+    "logo": "https://github.com/snipe/snipe-it/blob/master/public/img/logo.png",
+    "description": "This is a FOSS project for asset management in IT Operations. Knowing who has which laptop, when it was purchased in order to depreciate it correctly, handling software licenses, etc.",
+    "install_methods":{
+            "type": "default",
+            "script": "ct/snipeit.sh",
+            "resources": {
+                "cpu": "2",
+                "ram": "2048",
+                "hdd": "4",
+                "os": "debian",
+                "version": "12"
+            }
+        },
+    "default_credentials": {
+        "username": null,
+        "password": null
+    }
+}