Преглед на файлове

Shell scripts + Dashboard Controller + fix

Andrea Pollastri преди 5 години
родител
ревизия
f43ef6b118

+ 4 - 21
app/Http/Controllers/DashboardController.php

@@ -1,30 +1,13 @@
 <?php
 <?php
 
 
 namespace App\Http\Controllers;
 namespace App\Http\Controllers;
-
-use Illuminate\Http\Request;
 use App\Server;
 use App\Server;
 
 
-class DashboardController extends Controller
-{
-    /**
-     * Create a new controller instance.
-     *
-     * @return void
-     */
-    public function __construct()
-    {
-        $this->middleware('auth');
-    }
+class DashboardController extends Controller {
 
 
-    /**
-     * Show the application dashboard.
-     *
-     * @return \Illuminate\Contracts\Support\Renderable
-     */
-    public function index()
-    {
-        $servers = Server::where('complete', 1)->get();
+    public function index() {
+        $servers = Server::where('complete', 2)->orderBy('name')->get();
         return view('dashboard', compact('servers'));
         return view('dashboard', compact('servers'));
     }
     }
+
 }
 }

+ 45 - 0
app/Http/Controllers/RemoteController.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace App\Http\Controllers;
+use Illuminate\Support\Facades\Http;
+use App\Server;
+
+class RemoteController extends Controller {
+
+    public function start($servercode) {
+        $server = Server::where('servercode', $servercode)->where('complete', 0)->value('servercode');
+        if(!$servercode) {
+            return abort(403);
+        }
+        $server = Server::where('servercode', $servercode)->update(['complete' => 1]);
+        return 'OK';
+    }
+
+    public function finalize($servercode) {
+        $servercode = Server::where('servercode', $servercode)->where('complete', 1)->value('servercode');
+        if(!$servercode) {
+            return abort(403);
+        }
+        $server = Server::where('servercode', $servercode)->update(['complete' => 2]);
+        return 'OK';
+    }
+
+    public function ping($servercode) {
+        $server = Server::where('servercode', $servercode)->where('complete', 2)->get()->first();
+        if(!$server) {
+            return abort(403);
+        }
+        $response = Http::get('http://'.$server->ip.'/ping_'.$server->servercode.'.php');
+        return $response->status();
+    }
+
+    public function status($servercode) {
+        $server = Server::where('servercode', $servercode)->where('complete', 2)->get()->first();
+        if(!$server) {
+            return abort(403);
+        }
+        $response = Http::get('http://'.$server->ip.'/ping_'.$server->servercode.'.php');
+        return $response->body();
+    }
+
+}

+ 3 - 2
database/migrations/2020_04_27_000002_create_applications_table.php

@@ -21,9 +21,10 @@ class CreateApplicationsTable extends Migration
             $table->string('username');
             $table->string('username');
             $table->string('password');
             $table->string('password');
             $table->string('dbpass');
             $table->string('dbpass');
-            $table->string('appcode')->index();
+            $table->string('basepath')->nullable()->default('public');
+            $table->string('php')->default('7.4');
             $table->text('nginx')->nullable();
             $table->text('nginx')->nullable();
-            $table->string('basepath')->nullable();
+            $table->string('appcode')->index();
             $table->timestamps();
             $table->timestamps();
         });
         });
 
 

+ 21 - 12
routes/web.php

@@ -2,21 +2,30 @@
 
 
 use Illuminate\Support\Facades\Route;
 use Illuminate\Support\Facades\Route;
 
 
-/*
-|--------------------------------------------------------------------------
-| Web Routes
-|--------------------------------------------------------------------------
-|
-| Here is where you can register web routes for your application. These
-| routes are loaded by the RouteServiceProvider within a group which
-| contains the "web" middleware group. Now create something great!
-|
-*/
-
 Route::get('/', function () {
 Route::get('/', function () {
     return redirect('/dashboard');
     return redirect('/dashboard');
 });
 });
 
 
 Auth::routes(['register' => false]);
 Auth::routes(['register' => false]);
 
 
-Route::get('/dashboard', 'DashboardController@index');
+Route::group(['prefix' => 'tools'], function () use ($router) {
+    Route::get('/start/{servercode}','ApisController@start');
+    Route::get('/finalize/{servercode}','ApisController@finalize');
+    Route::get('/status/{servercode}','ApisController@status');
+    Route::get('/ping/{servercode}','ApisController@ping');
+});
+
+Route::group(['prefix' => 'sh'], function () use ($router) {
+    Route::get('/go/{servercode}','ShellController@install');
+    Route::get('/ha/{servercode}','ShellController@hostadd');
+    Route::get('/hd/{servercode}','ShellController@hostdel');
+    Route::get('/hm/{servercode}','ShellController@hostmod');
+    Route::get('/hg/{servercode}','ShellController@hostget');
+    Route::get('/pw/{servercode}','ShellController@passwd');
+    Route::get('/st/{servercode}','ShellController@status');
+    Route::get('/dy/{servercode}','ShellController@deploy');
+});
+
+Route::group(['middleware' => 'auth'], function () use ($router) {
+    Route::get('/dashboard', 'DashboardController@index');
+});

+ 34 - 0
storage/app/scripts/deploy.sh

@@ -0,0 +1,34 @@
+#!/bin/bash
+
+######### REMOTE GIT CONFIGURATION #########
+GITHUB="andreapollastri/cipi.git" #Choose your Github private/public repo
+BRANCH="master" #Choose your repository branch here
+
+######### DO NOT CHANGE ANYTHING IN THIS AREA #########
+SSH_KEY="/home/###CIPI-USER###/git/deploy"
+WORK_TREE="/home/###CIPI-USER###/web"
+GIT_DIR="/home/###CIPI-USER###/git/deploy.git"
+chmod 600 $SSH_KEY
+eval $(ssh-agent -s)
+ssh-add $SSH_KEY
+REPO="git@github.com:$GITHUB"
+if [ -d "$GIT_DIR" ]; then
+    cd $WORK_TREE
+    git --work-tree=$WORK_TREE --git-dir=$GIT_DIR fetch
+    git --work-tree=$WORK_TREE --git-dir=$GIT_DIR fetch origin --tags --force
+    git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f $BRANCH
+    git --work-tree=$WORK_TREE --git-dir=$GIT_DIR merge origin/$BRANCH
+else
+    git init --bare $GIT_DIR
+    rm -rf $WORK_TREE
+    mkdir $WORK_TREE
+    git --work-tree=$WORK_TREE --git-dir=$GIT_DIR remote add origin $REPO
+    git --work-tree=$WORK_TREE --git-dir=$GIT_DIR fetch
+    git --work-tree=$WORK_TREE --git-dir=$GIT_DIR fetch origin --tags --force
+    git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f $BRANCH
+    git --work-tree=$WORK_TREE --git-dir=$GIT_DIR merge origin/$BRANCH
+fi
+######################################################
+
+######### POST DEPLOY SCRIPTS HERE #########
+#Example: composer update

+ 139 - 0
storage/app/scripts/hostadd.sh

@@ -0,0 +1,139 @@
+#!/usr/bin/env bash
+
+DBROOT=???
+IP=???
+
+BASE_PATH=
+USER_SHELL=/bin/bash
+
+# Check if user is root
+if [ $(id -u) != "0" ]; then
+    echo "Error: You must be root to run this script."
+    exit 1
+fi
+
+while [ -n "$1" ] ; do
+            case $1 in
+            -d | --domain )
+                    shift
+                    DOMAIN=$1
+                    ;;
+            -u | --user )
+                    shift
+                    USER_NAME=$1
+                    ;;
+            -p | --pass )
+                    shift
+                    PASSWORD=$1
+                    ;;
+            -dbp | --dbpass )
+                    shift
+                    DBPASS=$1
+                    ;;
+            -b |  --base )
+                    shift
+                    BASE_PATH=$1
+                    ;;
+            -r |  --remote )
+                    shift
+                    REMOTE=$1
+                    ;;
+            -r |  --appcode )
+                    shift
+                    APPCODE=$1
+                    ;;
+            * )
+                    echo "ERROR: Unknown option: $1"
+                    exit -1
+                    ;;
+            esac
+            shift
+done
+
+#CREATE USER
+isUserExits() {
+    grep $1 /etc/passwd > /dev/null
+    [ $? -eq 0 ] && return $TRUE || return $FALSE
+}
+if(!isUserExits $USER_NAME)
+    then
+        sudo useradd -m -s $USER_SHELL -d /home/$USER_NAME -G www-data $USER_NAME
+        echo "$USER_NAME:$PASSWORD"|chpasswd
+        sudo chmod o-r /home/$USER_NAME
+    else
+        echo "Error: Retry!"
+        exit 1
+fi
+
+if [ $BASE_PATH != "" ]; then
+    WELCOME=/home/$USER_NAME/web/$BASE_PATH/index.php
+else
+    WELCOME=/home/$USER_NAME/web/index.php
+fi
+sudo touch $WELCOME
+sudo cat > "$WELCOME" <<EOF
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+    <head>
+        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+        <title>Coming soon...</title>
+        <style type="text/css">
+            body {
+                text-align: center;
+                background: #f0f0f0;
+                font-family: Arial, Helvetica, sans-serif;
+                font-size: 48px;
+                font-weight: bold;
+            }
+            h2.c1 {
+                margin-top: 60px;
+                color: #444;
+                font-size: 32px;
+                font-weight: lighter;
+            }
+        </style>
+    </head>
+    <body>
+        <h2 class="c1">
+            Coming soon...
+        </h2>
+    </body>
+</html>
+EOF
+
+#VIRTUALHOST
+HOST="'wget -qO- http://$REMOTE/sh/hg/'"
+NGINX=/etc/nginx/sites-available/$USER_NAME.conf
+sudo touch $NGINX
+sudo cat > "$NGINX" <<EOF
+    $HOST
+EOF
+sudo dos2unix $NGINX
+sudo ln -s $NGINX /etc/nginx/sites-enabled/
+sudo systemctl restart nginx.service
+
+#MYSQL
+DBNAME=$USER_NAME
+DBUSER=$USER_NAME
+/usr/bin/mysql -u root -p$DBROOT <<EOF
+CREATE DATABASE IF NOT EXISTS $DBNAME;
+CREATE USER $DBUSER@'localhost' IDENTIFIED BY '$DBPASS';
+GRANT USAGE ON *.* TO '$DBUSER'@'localhost' IDENTIFIED BY '$DBPASS' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
+GRANT ALL PRIVILEGES ON $DBNAME.* TO $DBUSER@'localhost';
+EOF
+
+#RESUME
+clear
+echo "###CIPI###Ok"
+
+#GIT
+sudo mkdir /home/$USER_NAME/git/
+sudo cp /cipi/github /home/$USER_NAME/git/deploy
+sudo cp /cipi/github.pub /home/$USER_NAME/git/deploy.pub
+sudo cp /cipi/deploy.sh /home/$USER_NAME/git/deploy.sh
+sudo rpl -q "###CIPI-USER###" "$USER_NAME" /home/$USER_NAME/git/deploy.sh
+sudo chown -R $USER_NAME:$USER_NAME /home/$USER_NAME/git/
+sudo chown -R $USER_NAME:$USER_NAME /home/$USER_NAME/web/
+
+#PERMISSIONS
+chown -R $USER_NAME:$USER_NAME /home/$USER_NAME

+ 39 - 0
storage/app/scripts/hostdel.sh

@@ -0,0 +1,39 @@
+#!/usr/bin/env bash
+
+USER_NAME=
+DBROOT=???
+
+while [ -n "$1" ] ; do
+    case $1 in
+    -u | --user* )
+            shift
+            USER_NAME=$1
+            ;;
+    * )
+            echo "ERROR: Unknown option: $1"
+            exit -1
+            ;;
+    esac
+    shift
+done
+
+#LINUX USER
+sudo userdel -r $USER_NAME
+
+#MYSQL USER AND DB
+/usr/bin/mysql -u root -p$DBROOT <<EOF
+DROP DATABASE $USER_NAME;
+DROP USER '$USER_NAME'@'localhost';
+EOF
+
+#SSL & CRON
+sudo unlink /etc/cron.d/certbot_renew_$USER_NAME.crontab
+sudo crontab -u $USER_NAME -r
+
+#NGINX
+sudo unlink /etc/nginx/sites-enabled/$USER_NAME.conf
+sudo unlink /etc/nginx/sites-available/$USER_NAME.conf
+sudo systemctl restart nginx.service
+
+clear
+echo "###CIPI###Ok"

+ 26 - 18
storage/app/scripts/install.sh

@@ -36,17 +36,13 @@ fi
 
 
 #ROOT Check
 #ROOT Check
 if [ "$(id -u)" = "0" ]; then
 if [ "$(id -u)" = "0" ]; then
-
     clear
     clear
     echo "Running as root :)"
     echo "Running as root :)"
     sleep 2s
     sleep 2s
-
 else
 else
-
     clear
     clear
     echo -e "You have to run this script as root. In AWS digit 'sudo -s'"
     echo -e "You have to run this script as root. In AWS digit 'sudo -s'"
     exit 1
     exit 1
-
 fi
 fi
 
 
 
 
@@ -74,16 +70,16 @@ REMOTEURL=???
 sudo apt-get update
 sudo apt-get update
 sudo apt-get -y install curl wget
 sudo apt-get -y install curl wget
 
 
-curl --request GET --url $REMOTEURL/server/api/start/$SERVERCODE
+curl --request GET --url $REMOTEURL/remote/start/$SERVERCODE
 
 
 sudo mkdir /cipi/
 sudo mkdir /cipi/
 sudo mkdir /cipi/html/
 sudo mkdir /cipi/html/
-wget $REMOTEURL/scripts/deploy/$SERVERCODE/  -O /cipi/deploy.sh
-wget $REMOTEURL/scripts/hostadd/$SERVERCODE/ -O /cipi/host-add.sh
-wget $REMOTEURL/scripts/hostdel/$SERVERCODE/ -O /cipi/host-del.sh
-wget $REMOTEURL/scripts/hostssl/$SERVERCODE/ -O /cipi/ssl.sh
-wget $REMOTEURL/scripts/passwd/$SERVERCODE/  -O /cipi/passwd.sh
-wget $REMOTEURL/scripts/status/$SERVERCODE/  -O /cipi/status.sh
+wget $REMOTEURL/sh/dy/$SERVERCODE/ -O /cipi/deploy.sh
+wget $REMOTEURL/sh/ha/$SERVERCODE/ -O /cipi/host-add.sh
+wget $REMOTEURL/sh/hm/$SERVERCODE/ -O /cipi/host-mod.sh
+wget $REMOTEURL/sh/hd/$SERVERCODE/ -O /cipi/host-del.sh
+wget $REMOTEURL/sh/pw/$SERVERCODE/ -O /cipi/passwd.sh
+wget $REMOTEURL/sh/st/$SERVERCODE/ -O /cipi/status.sh
 sudo chmod o-r /cipi
 sudo chmod o-r /cipi
 
 
 clear
 clear
@@ -137,8 +133,8 @@ sudo chmod o-r /cipi
 sudo dos2unix /cipi/deploy.sh
 sudo dos2unix /cipi/deploy.sh
 sudo dos2unix /cipi/passwd.sh
 sudo dos2unix /cipi/passwd.sh
 sudo dos2unix /cipi/host-add.sh
 sudo dos2unix /cipi/host-add.sh
+sudo dos2unix /cipi/host-mod.sh
 sudo dos2unix /cipi/host-del.sh
 sudo dos2unix /cipi/host-del.sh
-sudo dos2unix /cipi/ssl.sh
 
 
 shopt -s expand_aliases
 shopt -s expand_aliases
 alias ll='ls -alF'
 alias ll='ls -alF'
@@ -428,7 +424,7 @@ echo "User creation..."
 sleep 3s
 sleep 3s
 
 
 sudo useradd -m -s /bin/bash cipi
 sudo useradd -m -s /bin/bash cipi
-echo "cipi:$PASS"|chpasswd
+echo "$USER:$PASS"|chpasswd
 sudo usermod -aG sudo cipi
 sudo usermod -aG sudo cipi
 
 
 clear
 clear
@@ -437,11 +433,24 @@ sleep 3s
 echo -e "\n"
 echo -e "\n"
 
 
 
 
-#WELCOME PAGE
+#CIPI PAGES
 clear
 clear
-echo "Welcome page creation..."
+echo "Cipi pages creation..."
 sleep 3s
 sleep 3s
 
 
+PING=/var/www/ping_$SERVERCODE.php
+sudo touch $PING
+sudo cat > "$PING" <<EOF
+    UP!
+EOF
+
+STATUS=/var/www/stats_$SERVERCODE.php
+sudo touch $STATUS
+sudo cat > "$STATUS" <<EOF
+    <?php
+    echo exec("sh /cipi/status.sh");
+EOF
+
 WELCOME=/var/www/html/index.php
 WELCOME=/var/www/html/index.php
 sudo touch $WELCOME
 sudo touch $WELCOME
 sudo cat > "$WELCOME" <<EOF
 sudo cat > "$WELCOME" <<EOF
@@ -610,13 +619,12 @@ sudo cat > "$WELCOME" <<EOF
 </html>
 </html>
 EOF
 EOF
 clear
 clear
-echo "Welcome page creation: OK!"
+echo "Cipi pages creation: OK!"
 sleep 3s
 sleep 3s
 echo -e "\n"
 echo -e "\n"
 
 
 
 
 
 
-
 #END
 #END
 clear
 clear
 echo "Cipi installation is finishing. Wait..."
 echo "Cipi installation is finishing. Wait..."
@@ -630,7 +638,7 @@ crontab -l | { cat; echo "5 4 * * sun DEBIAN_FRONTEND=noninteractive DEBIAN_PRIO
 
 
 sudo systemctl restart nginx.service
 sudo systemctl restart nginx.service
 
 
-curl --request GET --url $REMOTEURL/server/api/finalize/$SERVERCODE
+curl --request GET --url $REMOTEURL/remote/finalize/$SERVERCODE
 
 
 clear
 clear
 echo "Cipi installation has been completed... Wait for your data!"
 echo "Cipi installation has been completed... Wait for your data!"

+ 43 - 0
storage/app/scripts/passwd.sh

@@ -0,0 +1,43 @@
+#!/bin/bash
+
+# Check if user is root
+if [ $(id -u) != "0" ]; then
+echo "Error: You must be root to run this script."
+exit 1
+fi
+
+while [ -n "$1" ] ; do
+  case $1 in
+  -u | --user )
+      shift
+      USER=$1
+      ;;
+  -p | --pass )
+      shift
+      PASS=$1
+      ;;
+  -dbp | --dbpass )
+      shift
+      DBPASS=$1
+      ;;
+  -dbop | --dboldpass )
+      shift
+      DBOLDPASS=$1
+      ;;
+  * )
+      echo "ERROR: Unknown option: $1"
+      exit -1
+      ;;
+  esac
+  shift
+done
+
+#CHANGE LINUX USER PASSWORD
+echo "$USER:$PASS"| sudo chpasswd
+
+#CHANGE MYSQL PASSWORD
+sudo mysqladmin -u $USER -p$DBOLDPASS password $DBPASS
+
+#RESUME
+clear
+echo "###CIPI###Ok"

+ 1 - 0
storage/app/scripts/status.sh

@@ -0,0 +1 @@
+echo "`LC_ALL=C top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}'`%;`free -m | awk '/Mem:/ { printf("%3.1f%%", $3/$2*100) }'`;`df -h / | awk '/\// {print $(NF-1)}'`"