From 470274279b0516280c05d20623298843fa4087de Mon Sep 17 00:00:00 2001 From: Saeed Vaziry Date: Sun, 25 Feb 2024 17:16:11 +0100 Subject: [PATCH] drop mysql and redis and use sqlite for Vito itself to optimize the resources --- .env.example | 22 ------- .env.prod | 25 +------- .env.testing | 27 -------- .github/workflows/code-style.yml | 3 - .github/workflows/tests.yml | 21 +------ Dockerfile | 17 +----- config/database.php | 5 +- ..._15_090830_create_firewall_rules_table.php | 2 +- ..._21_210213_update_firewall_rules_table.php | 4 +- docker/standalone/start.sh | 16 +---- install/install.sh | 28 +-------- phpunit.xml | 4 +- tests/Feature/NotificationChannelsTest.php | 61 ++++++++++--------- tests/Feature/ServerKeysTest.php | 8 ++- 14 files changed, 53 insertions(+), 190 deletions(-) delete mode 100755 .env.testing diff --git a/.env.example b/.env.example index bf21e04..239fb2f 100755 --- a/.env.example +++ b/.env.example @@ -4,26 +4,7 @@ APP_KEY= APP_DEBUG=true APP_URL=http://vito.test -LOG_CHANNEL=stack -LOG_LEVEL=debug - -DB_CONNECTION=mysql -DB_HOST=127.0.0.1 -DB_PORT=3306 -DB_DATABASE=vito -DB_USERNAME=root -DB_PASSWORD= - -BROADCAST_DRIVER=null -CACHE_DRIVER=file FILESYSTEM_DRIVER=local -QUEUE_CONNECTION=default -SESSION_DRIVER=database -SESSION_LIFETIME=120 - -REDIS_HOST=127.0.0.1 -REDIS_PASSWORD=null -REDIS_PORT=6379 MAIL_MAILER=smtp MAIL_HOST= @@ -33,6 +14,3 @@ MAIL_PASSWORD=null MAIL_ENCRYPTION=null MAIL_FROM_ADDRESS=null MAIL_FROM_NAME="${APP_NAME}" - -SSH_PUBLIC_KEY_NAME=ssh-public.key -SSH_PRIVATE_KEY_NAME=ssh-private.pem diff --git a/.env.prod b/.env.prod index 8b39156..b18d155 100755 --- a/.env.prod +++ b/.env.prod @@ -1,29 +1,9 @@ APP_NAME=Vito APP_ENV=production APP_KEY= -APP_DEBUG=false -APP_URL= +APP_URL=http://vito.test -LOG_CHANNEL=stack -LOG_LEVEL=debug - -DB_CONNECTION=mysql -DB_HOST=127.0.0.1 -DB_PORT=3306 -DB_DATABASE= -DB_USERNAME= -DB_PASSWORD= - -BROADCAST_DRIVER=null -CACHE_DRIVER=file FILESYSTEM_DRIVER=local -QUEUE_CONNECTION=default -SESSION_DRIVER=database -SESSION_LIFETIME=120 - -REDIS_HOST=127.0.0.1 -REDIS_PASSWORD=null -REDIS_PORT=6379 MAIL_MAILER=smtp MAIL_HOST= @@ -33,6 +13,3 @@ MAIL_PASSWORD=null MAIL_ENCRYPTION=null MAIL_FROM_ADDRESS=null MAIL_FROM_NAME="${APP_NAME}" - -SSH_PUBLIC_KEY_NAME=ssh-public.key -SSH_PRIVATE_KEY_NAME=ssh-private.pem diff --git a/.env.testing b/.env.testing deleted file mode 100755 index caee8cd..0000000 --- a/.env.testing +++ /dev/null @@ -1,27 +0,0 @@ -APP_NAME=Vito -APP_ENV=local -APP_KEY=base64:d9kZW60V4lFEw2SPn6UiJ0cfi04v80EWP0GZ6kzoxNg= -APP_DEBUG=true -APP_URL=http://localhost:2080 - -LOG_CHANNEL=stack -LOG_LEVEL=debug - -DB_CONNECTION=mysql -DB_HOST=127.0.0.1 -DB_PORT=3306 -DB_DATABASE=vito_test -DB_USERNAME=root -DB_PASSWORD= - -BROADCAST_DRIVER=null -CACHE_DRIVER=array -FILESYSTEM_DRIVER=local -QUEUE_CONNECTION=database -SESSION_DRIVER=array -SESSION_LIFETIME=120 - -MAIL_MAILER=array - -SSH_PUBLIC_KEY_NAME=ssh-public.key -SSH_PRIVATE_KEY_NAME=ssh-private.pem diff --git a/.github/workflows/code-style.yml b/.github/workflows/code-style.yml index 7a77dbb..6920a07 100644 --- a/.github/workflows/code-style.yml +++ b/.github/workflows/code-style.yml @@ -1,10 +1,7 @@ name: code-style on: - push: pull_request: - schedule: - - cron: '0 0 * * *' jobs: code-style: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 42b2f12..cab6151 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -2,26 +2,14 @@ name: tests on: push: + branches: + - 1.x pull_request: - schedule: - - cron: '0 0 * * *' jobs: tests: runs-on: ubuntu-22.04 - services: - mysql: - image: mysql - env: - MYSQL_DATABASE: test_db - MYSQL_USER: user - MYSQL_PASSWORD: password - MYSQL_ROOT_PASSWORD: rootpassword - ports: - - 3306:3306 - options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 - strategy: fail-fast: true matrix: @@ -49,8 +37,3 @@ jobs: - name: Run test suite run: php artisan test - env: - DB_HOST: 127.0.0.1 - DB_DATABASE: test_db - DB_USERNAME: user - DB_PASSWORD: password diff --git a/Dockerfile b/Dockerfile index ec01afe..d3becdf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,9 +4,6 @@ WORKDIR /var/www/html ENV DEBIAN_FRONTEND noninteractive -RUN echo "mysql-server mysql-server/root_password password password" | debconf-set-selections -RUN echo "mysql-server mysql-server/root_password_again password password" | debconf-set-selections - # upgrade RUN apt clean && apt update && apt update && apt upgrade -y && apt autoremove -y @@ -22,23 +19,13 @@ RUN apt update \ python2 dnsutils librsvg2-bin fswatch wget \ && add-apt-repository ppa:ondrej/php -y \ && apt update \ - && apt install -y php8.1 php8.1-fpm php8.1-mbstring php8.1-mysql php8.1-mcrypt php8.1-gd php8.1-xml \ + && apt install -y php8.1 php8.1-fpm php8.1-mbstring php8.1-mcrypt php8.1-gd php8.1-xml \ php8.1-curl php8.1-gettext php8.1-zip php8.1-bcmath php8.1-soap php8.1-redis COPY docker/standalone/php.ini /etc/php/8.1/cli/conf.d/99-vito.ini # composer RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer -# mysql -RUN wget -c https://dev.mysql.com/get/mysql-apt-config_0.8.22-1_all.deb \ - && mkdir -p /etc/apt/keyrings \ - && apt clean \ - && apt update \ - && dpkg -i mysql-apt-config_0.8.22-1_all.deb \ - && apt install mysql-server -y - -RUN service mysql stop - # app COPY . /var/www/html RUN rm -rf /var/www/html/vendor @@ -60,6 +47,6 @@ COPY docker/standalone/supervisord.conf /etc/supervisor/conf.d/supervisord.conf COPY docker/standalone/start.sh /start.sh RUN chmod +x /start.sh -EXPOSE 80 3306 +EXPOSE 80 CMD ["/start.sh"] diff --git a/config/database.php b/config/database.php index 833bcc8..03641e9 100644 --- a/config/database.php +++ b/config/database.php @@ -15,7 +15,7 @@ | */ - 'default' => env('DB_CONNECTION', 'mysql'), + 'default' => env('DB_CONNECTION', 'sqlite'), /* |-------------------------------------------------------------------------- @@ -37,8 +37,7 @@ 'sqlite' => [ 'driver' => 'sqlite', - 'url' => env('DATABASE_URL'), - 'database' => env('DB_DATABASE', database_path('database.sqlite')), + 'database' => storage_path(env('DB_DATABASE', 'database.sqlite')), 'prefix' => '', 'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true), ], diff --git a/database/migrations/2021_07_15_090830_create_firewall_rules_table.php b/database/migrations/2021_07_15_090830_create_firewall_rules_table.php index ac13e58..92020a8 100755 --- a/database/migrations/2021_07_15_090830_create_firewall_rules_table.php +++ b/database/migrations/2021_07_15_090830_create_firewall_rules_table.php @@ -15,7 +15,7 @@ public function up(): void $table->string('protocol'); $table->integer('port'); $table->ipAddress('source')->default('0.0.0.0'); - $table->tinyInteger('mask')->default(0); + $table->string('mask')->nullable(); $table->text('note')->nullable(); $table->string('status')->default('creating'); $table->timestamps(); diff --git a/database/migrations/2023_07_21_210213_update_firewall_rules_table.php b/database/migrations/2023_07_21_210213_update_firewall_rules_table.php index 5ac104a..2e31c43 100644 --- a/database/migrations/2023_07_21_210213_update_firewall_rules_table.php +++ b/database/migrations/2023_07_21_210213_update_firewall_rules_table.php @@ -7,7 +7,9 @@ { public function up(): void { - DB::statement('ALTER TABLE firewall_rules MODIFY mask varchar(10) null'); + if (DB::getDriverName() === 'mysql') { + DB::statement('ALTER TABLE firewall_rules MODIFY mask varchar(10) null'); + } } public function down(): void diff --git a/docker/standalone/start.sh b/docker/standalone/start.sh index 9c9ee93..bba6759 100644 --- a/docker/standalone/start.sh +++ b/docker/standalone/start.sh @@ -8,19 +8,7 @@ PASSWORD=${PASSWORD:-"password"} # Check if the flag file does not exist, indicating a first run if [ ! -f "$INIT_FLAG" ]; then - echo "First run of the container. Initializing MySQL..." - - # Start MySQL temporarily - service mysql start - - # Wait for MySQL to start up completely (may need to adjust the sleep duration) - sleep 3 - - # Create Database - mysql -u root -p`echo password` -e "CREATE DATABASE IF NOT EXISTS vito CHARACTER SET utf8 COLLATE utf8_general_ci;" - - # Change Password - mysql -u root -p`echo password` -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$DB_PASSWORD'; FLUSH PRIVILEGES;" + echo "Initializing..." # Generate SSH keys openssl genpkey -algorithm RSA -out /var/www/html/storage/ssh-private.pem @@ -31,8 +19,6 @@ if [ ! -f "$INIT_FLAG" ]; then touch "$INIT_FLAG" fi -service mysql start - service php8.1-fpm start service nginx start diff --git a/install/install.sh b/install/install.sh index b616eb8..c86e060 100644 --- a/install/install.sh +++ b/install/install.sh @@ -100,14 +100,11 @@ if ! echo "${V_NGINX_CONFIG}" | tee /etc/nginx/nginx.conf; then fi service nginx start -# redis -apt install redis-server -y - # php export V_PHP_VERSION="8.1" add-apt-repository ppa:ondrej/php -y apt update -apt install -y php${V_PHP_VERSION} php${V_PHP_VERSION}-fpm php${V_PHP_VERSION}-mbstring php${V_PHP_VERSION}-mysql php${V_PHP_VERSION}-mcrypt php${V_PHP_VERSION}-gd php${V_PHP_VERSION}-xml php${V_PHP_VERSION}-curl php${V_PHP_VERSION}-gettext php${V_PHP_VERSION}-zip php${V_PHP_VERSION}-bcmath php${V_PHP_VERSION}-soap php${V_PHP_VERSION}-redis +apt install -y php${V_PHP_VERSION} php${V_PHP_VERSION}-fpm php${V_PHP_VERSION}-mbstring php${V_PHP_VERSION}-mcrypt php${V_PHP_VERSION}-gd php${V_PHP_VERSION}-xml php${V_PHP_VERSION}-curl php${V_PHP_VERSION}-gettext php${V_PHP_VERSION}-zip php${V_PHP_VERSION}-bcmath php${V_PHP_VERSION}-soap php${V_PHP_VERSION}-redis if ! sed -i "s/www-data/${V_USERNAME}/g" /etc/php/${V_PHP_VERSION}/fpm/pool.d/www.conf; then echo 'Error installing PHP' && exit 1 fi @@ -120,23 +117,6 @@ service php${V_PHP_VERSION}-fpm restart curl -sS https://getcomposer.org/installer -o composer-setup.php php composer-setup.php --install-dir=/usr/local/bin --filename=composer -# database -export V_MARIADB_VERSION="10.3" -export V_DB_USER="vito" -export V_DB_NAME="vito" -export V_DB_PASS=$(openssl rand -base64 12) -wget -c https://dev.mysql.com/get/mysql-apt-config_0.8.22-1_all.deb -dpkg -i mysql-apt-config_0.8.22-1_all.deb -apt update -apt install mysql-server -y -service mysql enable -service mysql start -mysql -e "CREATE DATABASE IF NOT EXISTS ${V_DB_NAME} CHARACTER SET utf8 COLLATE utf8_general_ci" -mysql -e "CREATE USER IF NOT EXISTS '${V_DB_USER}'@'localhost' IDENTIFIED BY '${V_DB_PASS}'" -mysql -e "FLUSH PRIVILEGES" -mysql -e "GRANT ALL PRIVILEGES ON ${V_DB_NAME}.* TO '${V_DB_USER}'@'localhost'" -mysql -e "FLUSH PRIVILEGES" - # setup website export V_SSL=${V_SSL:-1} export COMPOSER_ALLOW_SUPERUSER=1 @@ -196,9 +176,6 @@ else export V_URL="http://${V_DOMAIN}" fi sed -i "s|APP_URL=.*|APP_URL=${V_URL}|" /home/${V_USERNAME}/${V_DOMAIN}/.env -sed -i "s|DB_DATABASE=.*|DB_DATABASE=${V_DB_NAME}|" /home/${V_USERNAME}/${V_DOMAIN}/.env -sed -i "s|DB_USERNAME=.*|DB_USERNAME=${V_DB_USER}|" /home/${V_USERNAME}/${V_DOMAIN}/.env -sed -i "s|DB_PASSWORD=.*|DB_PASSWORD=${V_DB_PASS}|" /home/${V_USERNAME}/${V_DOMAIN}/.env php artisan key:generate php artisan storage:link php artisan migrate --force @@ -249,9 +226,6 @@ php artisan icons:cache echo "🎉 Congratulations!" echo "✅ SSH User: ${V_USERNAME}" echo "✅ SSH Password: ${V_PASSWORD}" -echo "✅ DB Name: ${V_DB_NAME}" -echo "✅ DB Username: ${V_DB_USER}" -echo "✅ DB Password: ${V_DB_PASS}" echo "✅ Admin Email: ${V_ADMIN_EMAIL}" echo "✅ Admin Password: ${V_ADMIN_PASSWORD}" echo "✅ URL: http://${V_DOMAIN}" diff --git a/phpunit.xml b/phpunit.xml index 66fee18..2d8b792 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -11,12 +11,12 @@ + + - - diff --git a/tests/Feature/NotificationChannelsTest.php b/tests/Feature/NotificationChannelsTest.php index 50a3d76..c75c932 100644 --- a/tests/Feature/NotificationChannelsTest.php +++ b/tests/Feature/NotificationChannelsTest.php @@ -27,13 +27,13 @@ public function test_add_email_channel(): void ->call('add') ->assertSuccessful(); - $this->assertDatabaseHas('notification_channels', [ - 'provider' => NotificationChannel::EMAIL, - 'data' => cast_to_json([ - 'email' => 'email@example.com', - ]), - 'connected' => 1, - ]); + /** @var \App\Models\NotificationChannel $channel */ + $channel = \App\Models\NotificationChannel::query() + ->where('provider', NotificationChannel::EMAIL) + ->first(); + + $this->assertEquals('email@example.com', $channel->data['email']); + $this->assertTrue($channel->connected); } public function test_add_slack_channel(): void @@ -49,13 +49,13 @@ public function test_add_slack_channel(): void ->call('add') ->assertSuccessful(); - $this->assertDatabaseHas('notification_channels', [ - 'provider' => NotificationChannel::SLACK, - 'data' => cast_to_json([ - 'webhook_url' => 'https://hooks.slack.com/services/123/token', - ]), - 'connected' => 1, - ]); + /** @var \App\Models\NotificationChannel $channel */ + $channel = \App\Models\NotificationChannel::query() + ->where('provider', NotificationChannel::SLACK) + ->first(); + + $this->assertEquals('https://hooks.slack.com/services/123/token', $channel->data['webhook_url']); + $this->assertTrue($channel->connected); } public function test_add_discord_channel(): void @@ -71,15 +71,18 @@ public function test_add_discord_channel(): void ->call('add') ->assertSuccessful(); - $this->assertDatabaseHas('notification_channels', [ - 'provider' => NotificationChannel::DISCORD, - 'data' => cast_to_json([ - 'webhook_url' => 'https://discord.com/api/webhooks/123/token', - ]), - 'connected' => 1, - ]); + /** @var \App\Models\NotificationChannel $channel */ + $channel = \App\Models\NotificationChannel::query() + ->where('provider', NotificationChannel::DISCORD) + ->first(); + + $this->assertEquals('https://discord.com/api/webhooks/123/token', $channel->data['webhook_url']); + $this->assertTrue($channel->connected); } + /* + * @TODO fix json comparison + */ public function test_add_telegram_channel(): void { $this->actingAs($this->user); @@ -94,14 +97,14 @@ public function test_add_telegram_channel(): void ->call('add') ->assertSuccessful(); - $this->assertDatabaseHas('notification_channels', [ - 'provider' => NotificationChannel::TELEGRAM, - 'data' => cast_to_json([ - 'chat_id' => '123', - 'bot_token' => 'token', - ]), - 'connected' => 1, - ]); + /** @var \App\Models\NotificationChannel $channel */ + $channel = \App\Models\NotificationChannel::query() + ->where('provider', NotificationChannel::TELEGRAM) + ->first(); + + $this->assertEquals('123', $channel->data['chat_id']); + $this->assertEquals('token', $channel->data['bot_token']); + $this->assertTrue($channel->connected); } public function test_see_channels_list(): void diff --git a/tests/Feature/ServerKeysTest.php b/tests/Feature/ServerKeysTest.php index 726b4de..0e4ce9c 100644 --- a/tests/Feature/ServerKeysTest.php +++ b/tests/Feature/ServerKeysTest.php @@ -28,7 +28,9 @@ public function test_see_server_keys() 'public_key' => 'public-key-content', ]); - $this->server->sshKeys()->attach($sshKey); + $this->server->sshKeys()->attach($sshKey, [ + 'status' => SshKeyStatus::ADDED, + ]); Livewire::test(ServerKeysList::class, ['server' => $this->server]) ->assertSeeText('My first key'); @@ -46,7 +48,9 @@ public function test_delete_ssh_key() 'public_key' => 'public-key-content', ]); - $this->server->sshKeys()->attach($sshKey); + $this->server->sshKeys()->attach($sshKey, [ + 'status' => SshKeyStatus::ADDED, + ]); Livewire::test(ServerKeysList::class, ['server' => $this->server]) ->set('deleteId', $sshKey->id)