diff --git a/.docker/compose.yml b/.docker/compose.yml index 65988b3..c81be83 100644 --- a/.docker/compose.yml +++ b/.docker/compose.yml @@ -1,47 +1,36 @@ services: - php: + webserver: build: - context: "services/php" - dockerfile: "Dockerfile.${PHP_VERSION}" + context: "${STACK_DIR}" + dockerfile: "${STACK_DIR}/.docker/services/webserver/version/${PHP_VERSION}/Dockerfile" args: uid: "${PHP_UID}" - image: "${PROJECT}_php" - container_name: "${PROJECT}_php" + image: "${PROJECT}_webserver" + container_name: "${PROJECT}_webserver" + ports: + - "${SERVICE_PORT}:80" volumes: - - ./services/php/config/conf.d:/usr/local/etc/php/conf.d:ro - - ../service:/var/www/html + # Log + - "${STACK_DIR}/logs/nginx:/var/log/nginx" + # PHP + - "${STACK_DIR}/.docker/services/webserver/config/conf.d:/usr/local/etc/php/conf.d:ro" + # Web + - "${STACK_DIR}/www:/var/www/html" environment: MYSQL_USER: ${MYSQL_USER} MYSQL_PASSWORD: ${MYSQL_PASSWORD} - nginx: - build: - context: "services/nginx" - image: "${PROJECT}_nginx" - container_name: "${PROJECT}_nginx" - ports: - - "${SERVICE_PORT}:80" - - "${PMA_PORT}:8080" - volumes: - - ../service:/var/www/html - - ./services/nginx/config/conf.d:/etc/nginx/conf.d - - ../logs/nginx:/var/log/nginx - - phpmyadmin-data:/var/www/phpmyadmin - depends_on: - - php - database: build: context: "services/database" image: "${PROJECT}_database" container_name: "${PROJECT}_database" - #restart: // always;unless-stopped ports: - "${DB_PORT}:3306" volumes: - - ./services/database/config/conf.d/my.cnf:/etc/mysql/conf.d/my.cnf - - database-data:/var/lib/mysql + - "${STACK_DIR}/.docker/services/database/config/conf.d/my.cnf:/etc/mysql/conf.d/my.cnf" + - "database-data:/var/lib/mysql" environment: MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} MYSQL_DATABASE: ${MYSQL_DATABASE} @@ -53,21 +42,23 @@ services: context: "services/phpmyadmin" image: "${PROJECT}_phpmyadmin" container_name: "${PROJECT}_phpmyadmin" - volumes: - - phpmyadmin-data:/var/www/html + ports: + - "${PMA_PORT}:80" environment: - PMA_HOST: database + - PMA_HOST=database + depends_on: + - database + profiles: ["phpmyadmin"] mailhog: build: context: "services/mailhog" image: "${PROJECT}_mailhog" container_name: "${PROJECT}_mailhog" - #restart: always ports: - "${MAILHOG_PORT}:8025" + profiles: ["mailhog"] # Volumes volumes: database-data: - phpmyadmin-data: \ No newline at end of file diff --git a/.docker/services/database/Dockerfile b/.docker/services/database/Dockerfile index 30d16c5..6fc9412 100644 --- a/.docker/services/database/Dockerfile +++ b/.docker/services/database/Dockerfile @@ -1 +1 @@ -FROM mysql:5.7 \ No newline at end of file +FROM mysql:8.0 \ No newline at end of file diff --git a/.docker/services/database/config/conf.d/my.cnf b/.docker/services/database/config/conf.d/my.cnf index c2084bc..1e85908 100644 --- a/.docker/services/database/config/conf.d/my.cnf +++ b/.docker/services/database/config/conf.d/my.cnf @@ -1,5 +1,4 @@ [mysqld] collation-server = utf8mb4_unicode_ci character-set-server = utf8mb4 -sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" default_time_zone = Europe/Rome \ No newline at end of file diff --git a/.docker/services/nginx/Dockerfile b/.docker/services/nginx/Dockerfile deleted file mode 100644 index 9683860..0000000 --- a/.docker/services/nginx/Dockerfile +++ /dev/null @@ -1,9 +0,0 @@ -#==================== Setting up stage ====================# - -# Create image based on the official nginx - Alpine image -FROM nginx:1.19-alpine - -# Expose the listening port -EXPOSE 80 - -CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/.docker/services/nginx/config/conf.d/phpmyadmin.conf b/.docker/services/nginx/config/conf.d/phpmyadmin.conf deleted file mode 100644 index 76a2682..0000000 --- a/.docker/services/nginx/config/conf.d/phpmyadmin.conf +++ /dev/null @@ -1,15 +0,0 @@ -server { - listen 8080; - listen [::]:8080; - server_name _; - root /var/www/phpmyadmin; - index index.php; - - location ~* \.php$ { - fastcgi_pass phpmyadmin:9000; - root /var/www/html; - include fastcgi_params; - fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; - fastcgi_param SCRIPT_NAME $fastcgi_script_name; - } -} \ No newline at end of file diff --git a/.docker/services/nginx/config/example.php.conf b/.docker/services/nginx/config/example.php.conf deleted file mode 100644 index a5074c3..0000000 --- a/.docker/services/nginx/config/example.php.conf +++ /dev/null @@ -1,18 +0,0 @@ -server { - listen 80 default_server; - listen [::]:80; - server_name _; - root /var/www/html; - index index.php; - - location / { - try_files $uri $uri/ =404; - } - - location ~* \.php$ { - fastcgi_pass php:9000; - include fastcgi_params; - fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; - fastcgi_param SCRIPT_NAME $fastcgi_script_name; - } -} \ No newline at end of file diff --git a/.docker/services/php/config/conf.d/php.ini b/.docker/services/webserver/config/conf.d/php.ini similarity index 72% rename from .docker/services/php/config/conf.d/php.ini rename to .docker/services/webserver/config/conf.d/php.ini index 09be8cd..e0b0f39 100644 --- a/.docker/services/php/config/conf.d/php.ini +++ b/.docker/services/webserver/config/conf.d/php.ini @@ -13,18 +13,6 @@ extension=zip.so ; http://php.net/date.timezone date.timezone = "Europe/Rome" -; Xdebug -; See https://xdebug.org/docs/all_settings - -;PHPStorm -[Xdebug] -xdebug.remote_enable=1 -xdebug.idekey=PHPSTORM -xdebug.profiler_enable=0 -xdebug.max_nesting_level=700 -xdebug.remote_host=192.168.0.1 # your ip -xdebug.remote_port=9000 - ; INTL Extension [intl] intl.default_locale = diff --git a/.docker/services/php/config/conf.d/www.conf b/.docker/services/webserver/config/conf.d/www.conf similarity index 98% rename from .docker/services/php/config/conf.d/www.conf rename to .docker/services/webserver/config/conf.d/www.conf index 75c76ca..300129d 100644 --- a/.docker/services/php/config/conf.d/www.conf +++ b/.docker/services/webserver/config/conf.d/www.conf @@ -17,7 +17,7 @@ group = www-data ; specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. -listen = nginx:9000 +listen = 127.0.0.0:9000 ; Choose how the process manager will control the number of child processes. @@ -72,4 +72,4 @@ pm.min_spare_servers = 1 ; Note: Mandatory when pm is set to 'dynamic' pm.max_spare_servers = 3 -;--------------------- \ No newline at end of file +;--------------------- diff --git a/.docker/services/nginx/config/conf.d/php.conf b/.docker/services/webserver/config/sites-available/default.nginx similarity index 93% rename from .docker/services/nginx/config/conf.d/php.conf rename to .docker/services/webserver/config/sites-available/default.nginx index 6db7615..f1adf97 100644 --- a/.docker/services/nginx/config/conf.d/php.conf +++ b/.docker/services/webserver/config/sites-available/default.nginx @@ -18,10 +18,10 @@ server { location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; - fastcgi_pass php:9000; + fastcgi_pass 127.0.0.0:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } -} \ No newline at end of file +} diff --git a/.docker/services/webserver/entrypoint.sh b/.docker/services/webserver/entrypoint.sh new file mode 100644 index 0000000..9609463 --- /dev/null +++ b/.docker/services/webserver/entrypoint.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +set -e + +# PHP > Start services +service memcached start +php-fpm -D +nginx -g 'daemon off;' diff --git a/.docker/services/php/Dockerfile.php56 b/.docker/services/webserver/version/php56/Dockerfile similarity index 70% rename from .docker/services/php/Dockerfile.php56 rename to .docker/services/webserver/version/php56/Dockerfile index 341508f..3a794fe 100644 --- a/.docker/services/php/Dockerfile.php56 +++ b/.docker/services/webserver/version/php56/Dockerfile @@ -66,16 +66,6 @@ RUN echo "Install PHP Intl" \ && docker-php-ext-enable intl \ && rm -rf /tmp/* -# Install xdebug -RUN echo "Install xDebug" \ - && pecl install xdebug-2.5.5 \ - && docker-php-ext-enable xdebug - -# Install redis -RUN echo "Install Redis" \ - && pecl install redis-2.2.8 \ - && docker-php-ext-enable redis - # Install sendmailer for Mailhog RUN curl --location --output /usr/local/bin/mhsendmail https://github.com/mailhog/mhsendmail/releases/download/v0.2.0/mhsendmail_linux_amd64 \ && chmod +x /usr/local/bin/mhsendmail @@ -87,20 +77,43 @@ RUN apt-get remove -y libmcrypt-dev libicu-dev libpng-dev libjpeg-dev libfreetyp && rm -rf /var/lib/apt/lists/* \ && rm -rf /tmp/* +# ------------------------------------------------------- +# SERVE STAGE +# ------------------------------------------------------- + +# Get API Base Image +FROM base-stage AS serve-stage + +# Arguments defined in compose.yml +ARG uid + +# Install serve dependencies +RUN apt-get update \ + && apt-get upgrade -y \ + && apt-get -y --no-install-recommends install \ + nginx \ + memcached \ + ssl-cert + +# Clean up, try to reduce image size +RUN apt-get autoremove -y \ + && apt-get clean all \ + && rm -rf /var/lib/apt/lists/* \ + && rm -rf /tmp/* -# Add user for web application -RUN groupadd -g $uid www -RUN useradd -u $uid -ms /bin/bash -g www www +# Generate ssl certificate +RUN make-ssl-cert generate-default-snakeoil --force-overwrite -# Copy existing application directory contents -COPY . /var/www/html +# Copy nginx config +COPY .docker/services/webserver/config/sites-available/default.nginx /etc/nginx/sites-available/default -# Copy existing application directory permissions -COPY --chown=www:www . /var/www/html +# Copy entrypoint +COPY .docker/services/webserver/entrypoint.sh /usr/local/bin/ +RUN chmod +x /usr/local/bin/entrypoint.sh +RUN ln -s /usr/local/bin/entrypoint.sh / -# Change current user to www -USER www +# Specify the entrypoint +ENTRYPOINT ["entrypoint.sh"] -# Expose port 9000 and start php-fpm server -EXPOSE 9000 -CMD ["php-fpm"] \ No newline at end of file +# Set working dir +WORKDIR /var/www/html \ No newline at end of file diff --git a/.docker/services/php/Dockerfile.php74 b/.docker/services/webserver/version/php74/Dockerfile similarity index 60% rename from .docker/services/php/Dockerfile.php74 rename to .docker/services/webserver/version/php74/Dockerfile index 1d99d44..57d8797 100644 --- a/.docker/services/php/Dockerfile.php74 +++ b/.docker/services/webserver/version/php74/Dockerfile @@ -1,8 +1,9 @@ -# Create image based on the official PHP-FMP image -FROM php:7.4-fpm-buster +# ------------------------------------------------------- +# BASE STAGE +# ------------------------------------------------------- -# Arguments defined in docker-compose.yml -ARG uid +# Create image based on the official PHP-FMP image +FROM php:7.4-fpm-buster AS base-stage # Update the base libraries RUN apt-get -y update --fix-missing && apt-get upgrade -y @@ -35,7 +36,7 @@ RUN apt-get -y --no-install-recommends install --fix-missing \ libmemcached-dev \ zlib1g-dev -# Other PHP5.6 Extensions +# Other PHP Extensions RUN echo "Installing PHP extensions" && \ docker-php-ext-install pdo_mysql \ pdo_sqlite \ @@ -50,7 +51,7 @@ RUN echo "Installing PHP extensions" && \ docker-php-ext-configure gd --with-freetype=/usr/include --with-jpeg=/usr/include && \ docker-php-ext-install gd -# Other PHP5.6 Extensions +# Other PHP Extensions RUN echo "Enabling PHP extensions" && \ docker-php-ext-enable pdo_mysql pdo_sqlite mysqli zip @@ -61,16 +62,6 @@ RUN echo "Install PHP Intl" \ && docker-php-ext-enable intl \ && rm -rf /tmp/* -# Install xdebug -RUN echo "Install xDebug" \ - && pecl install xdebug-2.9.2 \ - && docker-php-ext-enable xdebug - -# Install redis -RUN echo "Install Redis" \ - && pecl install redis-5.3.4 \ - && docker-php-ext-enable redis - # Install sendmailer for Mailhog RUN curl --location --output /usr/local/bin/mhsendmail https://github.com/mailhog/mhsendmail/releases/download/v0.2.0/mhsendmail_linux_amd64 \ && chmod +x /usr/local/bin/mhsendmail @@ -82,20 +73,43 @@ RUN apt-get remove -y libmcrypt-dev libicu-dev libpng-dev libjpeg-dev libfreetyp && rm -rf /var/lib/apt/lists/* \ && rm -rf /tmp/* +# ------------------------------------------------------- +# SERVE STAGE +# ------------------------------------------------------- + +# Get API Base Image +FROM base-stage AS serve-stage + +# Arguments defined in compose.yml +ARG uid + +# Install serve dependencies +RUN apt-get update \ + && apt-get upgrade -y \ + && apt-get -y --no-install-recommends install \ + nginx \ + memcached \ + ssl-cert + +# Clean up, try to reduce image size +RUN apt-get autoremove -y \ + && apt-get clean all \ + && rm -rf /var/lib/apt/lists/* \ + && rm -rf /tmp/* -# Add user for web application -RUN groupadd -g $uid www -RUN useradd -u $uid -ms /bin/bash -g www www +# Generate ssl certificate +RUN make-ssl-cert generate-default-snakeoil --force-overwrite -# Copy existing application directory contents -COPY . /var/www/html +# Copy nginx config +COPY .docker/services/webserver/config/sites-available/default.nginx /etc/nginx/sites-available/default -# Copy existing application directory permissions -COPY --chown=www:www . /var/www/html +# Copy entrypoint +COPY .docker/services/webserver/entrypoint.sh /usr/local/bin/ +RUN chmod +x /usr/local/bin/entrypoint.sh +RUN ln -s /usr/local/bin/entrypoint.sh / -# Change current user to www -USER www +# Specify the entrypoint +ENTRYPOINT ["entrypoint.sh"] -# Expose port 9000 and start php-fpm server -EXPOSE 9000 -CMD ["php-fpm"] +# Set working dir +WORKDIR /var/www/html diff --git a/.docker/services/php/Dockerfile.php8 b/.docker/services/webserver/version/php8/Dockerfile similarity index 67% rename from .docker/services/php/Dockerfile.php8 rename to .docker/services/webserver/version/php8/Dockerfile index f579da2..f75d3c3 100644 --- a/.docker/services/php/Dockerfile.php8 +++ b/.docker/services/webserver/version/php8/Dockerfile @@ -56,16 +56,6 @@ RUN echo "Install PHP Intl" \ && docker-php-ext-enable intl \ && rm -rf /tmp/* -# Install xdebug -RUN echo "Install xDebug" \ - && pecl install xdebug-3.1.5 \ - && docker-php-ext-enable xdebug - -# Install redis -RUN echo "Install Redis" \ - && pecl install redis-5.3.4 \ - && docker-php-ext-enable redis - # Install sendmailer for Mailhog RUN curl --location --output /usr/local/bin/mhsendmail https://github.com/mailhog/mhsendmail/releases/download/v0.2.0/mhsendmail_linux_amd64 \ && chmod +x /usr/local/bin/mhsendmail @@ -77,20 +67,43 @@ RUN apt-get remove -y libmcrypt-dev libicu-dev libpng-dev libjpeg-dev libfreetyp && rm -rf /var/lib/apt/lists/* \ && rm -rf /tmp/* +# ------------------------------------------------------- +# SERVE STAGE +# ------------------------------------------------------- + +# Get API Base Image +FROM base-stage AS serve-stage + +# Arguments defined in compose.yml +ARG uid + +# Install serve dependencies +RUN apt-get update \ + && apt-get upgrade -y \ + && apt-get -y --no-install-recommends install \ + nginx \ + memcached \ + ssl-cert + +# Clean up, try to reduce image size +RUN apt-get autoremove -y \ + && apt-get clean all \ + && rm -rf /var/lib/apt/lists/* \ + && rm -rf /tmp/* -# Add user for web application -RUN groupadd -g $uid www -RUN useradd -u $uid -ms /bin/bash -g www www +# Generate ssl certificate +RUN make-ssl-cert generate-default-snakeoil --force-overwrite -# Copy existing application directory contents -COPY . /var/www/html +# Copy nginx config +COPY .docker/services/webserver/config/sites-available/default.nginx /etc/nginx/sites-available/default -# Copy existing application directory permissions -COPY --chown=www:www . /var/www/html +# Copy entrypoint +COPY .docker/services/webserver/entrypoint.sh /usr/local/bin/ +RUN chmod +x /usr/local/bin/entrypoint.sh +RUN ln -s /usr/local/bin/entrypoint.sh / -# Change current user to www -USER www +# Specify the entrypoint +ENTRYPOINT ["entrypoint.sh"] -# Expose port 9000 and start php-fpm server -EXPOSE 9000 -CMD ["php-fpm"] +# Set working dir +WORKDIR /var/www/html diff --git a/.gitignore b/.gitignore index d590c33..11ebf50 100644 --- a/.gitignore +++ b/.gitignore @@ -3,16 +3,16 @@ .vscode # CONF -# .docker/web/conf.d/php.conf +services # LOGS logs/* !logs/database/.gitkeep -!logs/web/.gitkeep +!logs/nginx/.gitkeep -# SERVICES -service -!service/.gitkeep +# WWW +www +!www/.gitkeep #ENV .env \ No newline at end of file diff --git a/LICENSE b/LICENSE index 766f020..3701350 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 Luca Pattocchio +Copyright (c) 2021 GDRCD Organization Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index e1f6d52..c5e4290 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,42 @@ -# GDRCD-Stack +# GDRCD > Stack -Lo scopo di GDRCD-Stack è di fornire un basilare ambiente di sviluppo per GDRCD, il principale engine Open Source di Giochi di Ruolo play-by-chat, -fornendo all'utilizzatore tutti gli strumenti necessari per la sua realizzazione. +Lo scopo di questo strumento è di fornire un basilare ambiente di sviluppo per [GDRCD](https://github.com/GDRCD/GDRCD), lo Script per la creazione di Giochi di Ruolo "Play by Chat" su browser, e dotare l'utilizzatore di tutti gli strumenti necessari per la sua realizzazione. -Questa soluzione è stata pensata prevalentemente per lo sviluppo in locale, tuttavia può essere utilizzato anche per un ambiente di produzione. +Questa soluzione è stata pensata prevalentemente per lo sviluppo in locale, si sconsiglia l'uso in ambienti di produzione se non si hanno le capacità necessarie per comprenderne il funzionamento. ## Requisiti -Per utilizzare lo stack presente in questa repository, occorre aver installato sul proprio terminale Docker e GIT. +Per utilizzare lo ***stack*** presente in questa repository, occorre aver installato sul proprio terminale Docker e GIT. Di seguito, gli indirizzi con i riferimenti per l'installazione di questi strumenti: - [Docker Desktop (o Docker Toolbox)](https://www.docker.com/products/docker-desktop) - [GIT](https://git-scm.com/downloads) -Occorre avere un minimo di dimestichezza con il terminale (come può essere Powershell, la Bash di Linux o il Terminale di macOS) per poter utilizzare lo stack. +Occorre avere un minimo di dimestichezza con il terminale per poter utilizzare lo ***stack***. + +>N.B.: Su sistemi Linux e macOS si utilizza la shell Bash, mentre su Windows è necessario utilizzare WSL (Windows Subsystem for Linux), poiché **lo stack non è compatibile con PowerShell**. ## Installazione -Per utilizzare GDRCD-stack, clona il progetto in una qualsiasi cartella sul tuo PC. Puoi farlo scaricando l'intero progetto compresso in una cartella zip oppure utilizzando GIT ed eseguendo il seguente comando: +Per utilizzare ***stack***, clona il progetto in una qualsiasi cartella sul tuo PC. Puoi farlo scaricando l'intero progetto compresso in una cartella zip oppure utilizzando GIT ed eseguendo il seguente comando: ```shell -git clone https://github.com/GDRCD/GDRCD-stack.git +git clone https://github.com/GDRCD/stack.git ``` -Il comando salverà in una cartella `gdrcd-stack` l'intera struttura dello stack. Puoi inserirlo in una cartella a tuo piacimento, aggiungendo in fondo al comando sopra indicato il nome della cartella desiderata (qualora non sia presente, la crea in automatico). +Il comando salverà in una cartella `stack` l'intera struttura. Puoi inserirlo in una cartella a tuo piacimento, aggiungendo in fondo al comando sopra indicato il nome della cartella desiderata (qualora non sia presente, la crea in automatico). -Una volta terminato il salvataggio dei file dello stack, occorre inserire il proprio progetto dentro la cartella `service`. Può essere fatto manualmente, così come attraverso `git`. +Una volta terminato il salvataggio dei file dello stack, occorre inserire il proprio progetto dentro la cartella [`www`](www ). Può essere fatto manualmente, così come attraverso `git`. Per chi sta iniziando con un nuovo progetto di GDRCD, sarà sufficiente eseguire i seguenti comandi: ```shell -cd service # ci si sposta nella cartella del progetto -git clone https://github.com/GDRCD/GDRCD.git # clona il progetto GDRCD +cd www +git clone https://github.com/GDRCD/GDRCD.git ``` -A questo punto, copiare il file `sample.env` in un nuovo file `.env` e compilare le varie variabili presenti. +> N.B.: potrebbe accadere che questo passaggio dia errore per via della presenza del file `.gitkeep`. Nel caso si può tranquillamente rimuovere quest'ultimo e ritentare lo scarico del progetto. + +A questo punto, copiare il file [`sample.env`](sample.env ) in un nuovo file [`.env`](.env ) e compilare le varie variabili presenti. È molto importante specificare quale versione di PHP si desidera utilizzare, popolando la variabile `PHP_VERSION` con il numero di versione desiderato tra quelle disponibili. Di seguito le versioni attualmente supportate, con i relativi riferimenti: @@ -41,60 +44,265 @@ Di seguito le versioni attualmente supportate, con i relativi riferimenti: - PHP 7.4 (php74) - PHP 8.0 (php8) -Puoi cambiare la versione PHP utilizzata in qualsiasi momento, cambiando la variabile nel file `.env` e ricostruendo lo stack. +> N.B.: Puoi cambiare la versione PHP utilizzata in qualsiasi momento, cambiando la variabile nel file [`.env`](.env ) e ricostruendo lo stack. -Ora il tuo GDRCD-Stack è pronto e funzionante!! +Per proseguire con l'installazione, occorre eseguire il comando di installazione con i permessi di amministratore: +```shell +sudo ./run install +``` -## Utilizzo +>N.B.: è possibile configurare il percorso in cui installare il comando globale aggiungendo l'opzione `-t` dopo [`bin/commands/install`](bin/commands/install ) seguito dal percorso desiderato. Di default, il comando viene installato in [`/usr/local/bin/`](/usr/local/bin/ ). -Per facilitare l'utilizzo dello strumento, è stato predisposto il comando `gdrcd-stack` che raccoglie una serie di comandi utili all'esecuzione delle funzioni primarie dello stack. -Il comando non è altro che un file eseguibile da terminale, motivo per il quale è necessario usare la formula: +Il comando di installazione configura l'ambiente di sviluppo e crea un alias di sistema per il progetto. +L'alias sarà uguale al nome specificato nella variabile PROJECT del file `.env`. +Ad esempio, se nel file `.env` è stato impostato: ```shell -./gdrcd-stack +PROJECT=mygdrcd ``` -Per avviare l'esecuzione dei servizi dello stack, sarà sufficiente eseguire il seguente comando: +Una volta completata l'installazione, sarà possibile utilizzare il comando: +```shell +mygdrcd +``` +al posto di: ```shell -./gdrcd-stack start +./run ``` -In automatico avverrà la compilazione dello stack, processo che si occuperà di costruire i singoli servizi e di istanziarli in appositi container di docker, e l'avvio dei servizi. +Ora il tuo ***stack*** è pronto e funzionante! -La compilazione dello stack può essere effettuata manualmente in qualsiasi momento, attraverso il comando: +## Utilizzo +Per facilitare l'utilizzo dello strumento, è stato predisposto il comando `run` che raccoglie una serie di comandi utili all'esecuzione delle funzioni primarie dello stack. +Il comando non è altro che un file eseguibile da terminale, motivo per il quale è necessario usare la formula: ```shell -./gdrcd-stack build +./run ``` -Ciò può essere utile qualora vengono apportate modifiche allo stack, come ad esempio una modifica alla versione PHP utilizzata, o se si desidera aggiungere nuovi servizi. +Per avviare l'esecuzione dei servizi dello ***stack***, sarà sufficiente eseguire il seguente comando: +```shell +./run start +``` -Per fermare i servizi dello stack, è sufficiente eseguire il seguente comando: +In automatico avverrà la compilazione dello ***stack***, processo che si occuperà di costruire i singoli servizi e di istanziarli in appositi container di docker, e l'avvio dei servizi. +La compilazione dello stack può essere effettuata manualmente in qualsiasi momento, attraverso il comando: ```shell -./gdrcd-stack stop +./run build ``` -Assieme a questi comandi, è stato predisposto anche un comando per rimuovere compleatamente lo stack, in modo da poterne ricominciare da capo: +Ciò può essere utile qualora vengono apportate modifiche allo ***stack***, come ad esempio una modifica alla versione PHP utilizzata, o se si desidera aggiungere nuovi servizi. + +Per fermare i servizi dello ***stack***, è sufficiente eseguire il seguente comando: +```shell +./run stop +``` +Assieme a questi comandi, è stato predisposto anche un comando per rimuovere compleatamente lo ***stack***, in modo da poterne ricominciare da capo: ```shell -./gdrcd-stack clean +./run clean ``` ## Comandi Utili Di seguito i comandi a disposizione: - ```shell -./gdrcd-stack start # avvia lo stack -./gdrcd-stack stop # ferma lo stack -./gdrcd-stack restart # riavvia lo stack -./gdrcd-stack build # compila lo stack -./gdrcd-stack clean # rimuove tutti tutti i servizi generati dallo stack -./gdrcd-stack help # mostra i comandi a disposizione +./run start # avvia lo stack +./run stop # ferma lo stack +./run restart # riavvia lo stack +./run build # compila lo stack +./run clean # rimuove tutti tutti i servizi generati dallo stack +./run help # mostra i comandi a disposizione ``` +## Dettaglio dei Comandi + +`./run start` + +Avvia tutti i container dello stack. + +`./run stop` + +Ferma tutti i container dello stack. + +`./run restart` + +Riavvia tutti i container dello stack. + +`./run build` + +Compila tutti i container dello stack. + +Opzioni disponibili: +- `-f, --force`: Forza la ricompilazione dei container + +`./run clean` + +Rimuove tutti i container dello stack. + +Opzioni disponibili: +- `-v, --volumes`: Rimuove anche i volumi associati ai container + +`./run logs` + +Mostra i log di tutti i container dello stack. + +Opzioni disponibili: +- `-f, --follow`: Segue i log in tempo reale + +`./run recreate` + +Ricrea tutti i container e le reti dello stack. + +Opzioni disponibili: +- `-f, --force`: Forza la ricreazione dei container + +`sudo ./run install` + +Installa lo stack e crea un alias di sistema per il progetto. + +Opzioni disponibili: +- `-t, --target `: Specifica il percorso di installazione del comando globale (default: [`/usr/local/bin/`](/usr/local/bin/ )) +- `-f, --force`: Forza la reinstallazione del comando globale + +`./run service list` + +Elenca tutti i servizi opzionali disponibili. + +`./run service enable ` + +Abilita un servizio opzionale. + +`./run service disable ` + +Disabilita un servizio opzionale. + +`./run database logs` + +Mostra i log del container `database`. + +Opzioni disponibili: +- `-f, --follow`: Segue i log in tempo reale + +`./run database start` + +Avvia il container `database`. + +`./run database stop` + +Ferma il container `database`. + +`./run database restart` + +Riavvia il container `database`. + +`./run database build` + +Compila il container `database`. + +Opzioni disponibili: +- `-f, --force`: Forza la ricompilazione del container + +`./run database attach` + +Accede alla shell del container `database`. + +`./run webserver logs` + +Mostra i log del container `webserver`. + +Opzioni disponibili: +- `-f, --follow`: Segue i log in tempo reale + +`./run webserver start` + +Avvia il container `webserver`. + +`./run webserver stop` + +Ferma il container `webserver`. + +`./run webserver restart` + +Riavvia il container `webserver`. + +`./run webserver build` + +Compila il container `webserver`. + +Opzioni disponibili: +- `-f, --force`: Forza la ricompilazione del container + +`./run webserver attach` + +Accede alla shell del container `webserver`. + +`./run phpmyadmin logs` + +Mostra i log del container `phpmyadmin`. + +Opzioni disponibili: +- `-f, --follow`: Segue i log in tempo reale + +`./run phpmyadmin start` + +Avvia il container `phpmyadmin`. + +`./run phpmyadmin stop` + +Ferma il container `phpmyadmin`. + +`./run phpmyadmin restart` + +Riavvia il container `phpmyadmin`. + +`./run phpmyadmin build` + +Compila il container `phpmyadmin`. + +Opzioni disponibili: +- `-f, --force`: Forza la ricompilazione del container + +`./run phpmyadmin attach` + +Accede alla shell del container `phpmyadmin`. + +`./run mailhog logs` + +Mostra i log del container `mailhog`. + +Opzioni disponibili: +- `-f, --follow`: Segue i log in tempo reale + +`./run mailhog start` + +Avvia il container `mailhog`. + +`./run mailhog stop` + +Ferma il container `mailhog`. + +`./run mailhog restart` + +Riavvia il container `mailhog`. + +`./run mailhog build` + +Compila il container `mailhog`. + +Opzioni disponibili: +- `-f, --force`: Forza la ricompilazione del container + +`./run mailhog attach` + +Accede alla shell del container `mailhog`. + +>N.B.: Se utilizzi l'installazione, non è necessario eseguire ogni volta il comando da eseguibile, ma direttamente dalla shortcut globale. + +>N.B.: Ogni comando dispone di un'opzione `-h` per visualizzare le opzioni disponibili. + ## Servizi Disponibili Lo stack mette a disposizione una serie di servizi, che sono: @@ -104,14 +312,13 @@ Lo stack mette a disposizione una serie di servizi, che sono: - PhpMyAdmin (phpmyadmin) - Mailhog (mailhog) -N.B.: -Il servizio `mailhog` è un servizio di test che permette di visualizzare le email inviate dal sistema e funziona solo in ambiente `dev`. +>N.B.: Il servizio `mailhog` è un servizio di test che permette di visualizzare le email inviate dal sistema e funziona solo in ambiente `dev`. ## Segnalazione bug e richieste di aiuto Prima di aprire una segnalazione bug o una richiesta di aiuto, assicurati che il tuo problema non sia già stato trattato -tra le varie [issues](https://github.com/GDRCD/GDRCD-stack/issues). Se non trovi nulla, puoi aprirne una nuova -[qui](https://github.com/GDRCD/GDRCD-stack/issues/new). +tra le varie [issues](https://github.com/GDRCD/stack/issues). Se non trovi nulla, puoi aprirne una nuova +[qui](https://github.com/GDRCD/stack/issues/new). ## Riferimenti diff --git a/bin/commands/build b/bin/commands/build new file mode 100755 index 0000000..cdcc311 --- /dev/null +++ b/bin/commands/build @@ -0,0 +1,68 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" + +# Save command name +args=("$@") +COMMAND_NAME="${args[0]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "build" + helpify_subtitle "Building all containers"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "build" "Building all containers" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "$COMMAND_NAME" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +build() { + prompt -i "Building all containers..." + dockerCompose rm -s -f && dockerCompose build --no-cache && dockerCompose up -d +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + build +fi \ No newline at end of file diff --git a/bin/commands/clean b/bin/commands/clean new file mode 100755 index 0000000..2fca491 --- /dev/null +++ b/bin/commands/clean @@ -0,0 +1,71 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" + +# Save command name +args=("$@") +COMMAND_NAME="${args[0]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "clean" + helpify_subtitle "Remove all containers"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "clean" "Remove all containers" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "$COMMAND_NAME" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +clean() { + # Preliminary checks + isContainerExist + # + prompt -i "Cleaning all stack stuff..." + dockerCompose rm -s -f; +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + clean +fi \ No newline at end of file diff --git a/bin/commands/database/attach b/bin/commands/database/attach new file mode 100755 index 0000000..28e03b5 --- /dev/null +++ b/bin/commands/database/attach @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} attach" "[OPTIONS...]" + helpify_subtitle "Access to ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container bash"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "attach" "Access to ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container bash" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +attach() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + isContainerRunning "${SERVICE_NAME}" + # + prompt -i "Attaching to ${SERVICE_NAME} container..." + dockerCompose exec -it "${SERVICE_NAME}" bash && return 0; +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + attach +fi \ No newline at end of file diff --git a/bin/commands/database/build b/bin/commands/database/build new file mode 100755 index 0000000..5fae5da --- /dev/null +++ b/bin/commands/database/build @@ -0,0 +1,73 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} build" "[OPTIONS...]" + helpify_subtitle "Build ${SERVICE_NAME:-${CURRENT_DIR_NAME}} image and container"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "build" "Build ${SERVICE_NAME:-${CURRENT_DIR_NAME}} image and container" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +build() { + # + prompt -i "Building ${SERVICE_NAME} image and container..." + dockerCompose rm -s -f "${SERVICE_NAME}" && dockerCompose build --no-cache "${SERVICE_NAME}" && dockerCompose up -d "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + build +fi \ No newline at end of file diff --git a/bin/commands/database/export b/bin/commands/database/export new file mode 100755 index 0000000..f7938db --- /dev/null +++ b/bin/commands/database/export @@ -0,0 +1,125 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# Commands variables +DATABASE_NAME="" +FILE="" +COMPRESS="false" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} export" "[OPTIONS...] DATABASE_NAME [FILE]" + helpify_subtitle "Export a database into a dump file"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" + helpify "-c, --compress" "Export a compressed dump file" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "export" "Export a database into a dump file" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift; + ;; + -c|--compress) + COMPRESS="true"; shift; + ;; + *) + # First argument is the database name + if [[ -z "${DATABASE_NAME}" ]]; then + DATABASE_NAME="$1"; shift; + # Second argument is the file + elif [[ -z "${FILE}" ]]; then + FILE="$1"; shift; + # Other arguments are not allowed + else + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1; + fi + ;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +export() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + isContainerRunning "${SERVICE_NAME}" + + # Check if database name is empty + if [[ -z "${DATABASE_NAME}" ]]; then + prompt -e "Database name is required"; + exit 1; + fi + + # Check if file is empty, set default file name + if [[ -z "${FILE}" ]]; then + FILE="${DATABASE_NAME}.sql" + fi + + # Check if file name is a dump file + if [[ "${FILE}" != *.sql ]]; then + prompt -e "File name must be a dump file (.sql)"; + exit 1; + fi + + # Remove previous file if exists + if [[ -f "${FILE}" ]]; then + rm "${FILE}" + fi + + # + prompt -i "Exporting ${DATABASE_NAME} into ${FILE} file..." + docker exec -i "${PROJECT}"_database mysqldump --routines -uroot -p"${MARIADB_ROOT_PASSWORD}" "${DATABASE_NAME}" > "${FILE}" || exit 1 + + # Execute gzip if compress option is true + if [[ "${COMPRESS}" == "true" ]]; then + prompt -i "Compressing ${FILE} file..." + gzip "${FILE}" + fi +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + export +fi \ No newline at end of file diff --git a/bin/commands/database/import b/bin/commands/database/import new file mode 100755 index 0000000..d0b7742 --- /dev/null +++ b/bin/commands/database/import @@ -0,0 +1,146 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +DATABASE_NAME="" +FILE="" + +# If is set, drop schema before import +need_fresh_import="false" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} import" "[OPTIONS...] DATABASE_NAME FILE" + helpify_subtitle "Import a dump file (.sql, .sql.gz)"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" + helpify "-fd, --force-drop" "Drop schema before import" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "import" "Import a dump file (.sql, .sql.gz)" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift; + ;; + -fd|--force-drop) + need_fresh_import="true"; shift; + ;; + *) + # First argument is the database name + if [[ -z "${DATABASE_NAME}" ]]; then + DATABASE_NAME="$1"; shift; + # Second argument is the file + elif [[ -z "${FILE}" ]]; then + FILE="$1"; shift; + # Other arguments are not allowed + else + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1; + fi + ;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +import() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + isContainerRunning "${SERVICE_NAME}" + + # Check if database name is empty + if [[ -z "${DATABASE_NAME}" ]]; then + prompt -e "Database name is required"; + exit 1; + fi + + # Check if file is empty + if [[ -z "${FILE}" ]]; then + prompt -e "File is required"; + exit 1; + fi + + # Check if file exists + if [[ ! -f "${FILE}" ]]; then + prompt -e "File '${FILE}' does not exist"; + exit 1; + fi + + # Check if file is a dump file + if [[ ! "${FILE}" =~ \.(sql|sql\.gz)$ ]]; then + prompt -e "File '${FILE}' is not a dump file"; + exit 1; + fi + + # Check if file is a gzip file + if [[ "${FILE}" =~ \.gz$ ]]; then + # Check if gzip is installed + if ! command -v gzip &> /dev/null; then + prompt -e "gzip is not installed"; + exit 1; + fi + # Check if gunzip is installed + if ! command -v gunzip &> /dev/null; then + prompt -e "gunzip is not installed"; + exit 1; + fi + # Unzip file + prompt -i "Unzipping file..." + gunzip "${FILE}" || exit 1 + # Remove .gz extension + FILE="${FILE%.*}" + fi + + # Drop schema if needed + if [[ "${need_fresh_import}" == "true" ]]; then + prompt -i "Dropping schema..." + docker exec -i "${PROJECT}"_database mysql -uroot -p"${MARIADB_ROOT_PASSWORD}" -e "DROP DATABASE IF EXISTS ${DATABASE_NAME}; CREATE DATABASE ${DATABASE_NAME};" || return 1 + fi + # Import dump file + prompt -i "Importing dump file to ${DATABASE_NAME} ..." + docker exec -i "${PROJECT}"_database mysql -uroot -p"${MARIADB_ROOT_PASSWORD}" "${DATABASE_NAME}" < "${FILE}" || return 1 +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + import +fi \ No newline at end of file diff --git a/bin/commands/database/logs b/bin/commands/database/logs new file mode 100755 index 0000000..83b6236 --- /dev/null +++ b/bin/commands/database/logs @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} logs" "[OPTIONS...]" + helpify_subtitle "Show ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container's log"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "logs" "Show ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container's log" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +logs() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + isContainerRunning "${SERVICE_NAME}" + # + prompt -i "Getting ${SERVICE_NAME} container logs..." + dockerCompose logs -f -t "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + logs +fi \ No newline at end of file diff --git a/bin/commands/database/refresh b/bin/commands/database/refresh new file mode 100755 index 0000000..c01c17e --- /dev/null +++ b/bin/commands/database/refresh @@ -0,0 +1,95 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# Commands variables +DATABASE_NAME="" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} refresh" "[OPTIONS...] DATABASE_NAME" + helpify_subtitle "Clean all tables in the database" + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "refresh" "Clean all tables in the database" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift; + ;; + *) + # First argument is the database name + if [[ -z "${DATABASE_NAME}" ]]; then + DATABASE_NAME="$1"; shift; + # Other arguments are not allowed + else + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1; + fi + ;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +refresh() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + isContainerRunning "${SERVICE_NAME}" + + # Check if database name is empty + if [[ -z "${DATABASE_NAME}" ]]; then + prompt -e "Database name is required"; + exit 1; + fi + + # Prompt + prompt -i "Refreshing database ${DATABASE_NAME}..." + # Run command + dockerCompose exec "${SERVICE_NAME}" sh -c "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'DROP DATABASE IF EXISTS ${DATABASE_NAME}; CREATE DATABASE ${DATABASE_NAME};'" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + refresh +fi \ No newline at end of file diff --git a/bin/commands/database/restart b/bin/commands/database/restart new file mode 100755 index 0000000..09c078c --- /dev/null +++ b/bin/commands/database/restart @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} restart" "[OPTIONS...]" + helpify_subtitle "Restart ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" + +} + +# Function for showing usage of this command for help +usage4help() { + helpify "restart" "Restart ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +restart() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + # + prompt -i "Restarting ${SERVICE_NAME} container..." + dockerCompose restart "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + restart +fi \ No newline at end of file diff --git a/bin/commands/database/start b/bin/commands/database/start new file mode 100755 index 0000000..3dd0776 --- /dev/null +++ b/bin/commands/database/start @@ -0,0 +1,75 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} start" "[OPTIONS...]" + helpify_subtitle "Start ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "start" "Start ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +start() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + # + prompt -i "Starting ${SERVICE_NAME} container..." + dockerCompose start "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + start +fi \ No newline at end of file diff --git a/bin/commands/database/stop b/bin/commands/database/stop new file mode 100755 index 0000000..a7bae55 --- /dev/null +++ b/bin/commands/database/stop @@ -0,0 +1,75 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} stop" "[OPTIONS...]" + helpify_subtitle "Stop ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "stop" "Stop ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +stop() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + # + prompt -i "Stopping ${SERVICE_NAME} container..." + dockerCompose stop "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + stop +fi \ No newline at end of file diff --git a/bin/commands/install b/bin/commands/install new file mode 100755 index 0000000..49ec88d --- /dev/null +++ b/bin/commands/install @@ -0,0 +1,128 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" + +# Save command name +args=("$@") +COMMAND_NAME="${args[0]}" + +# --------------------------------------------------------------------- +# Messages +# --------------------------------------------------------------------- + +MESSAGE_INSTALL_START="Starting installation..."; +MESSAGE_INSTALL_ALREADY_INSTALLED="Already installed!"; +MESSAGE_INSTALL_SUCCESS="Installation completed!"; +MESSAGE_INSTALL_FAILED="Installation failed!"; + +MESSAGE_TARGET_NOT_WRITABLE="Target path is not writable with the current user, please check the permissions or run the script as root"; +MESSAGE_TARGET_NOT_DIRECTORY="Target path is not a directory, please check the path or run the script as root"; + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +usage() { + helpify_title "install" "[OPTIONS...]" + helpify_subtitle "Install the GDRCD development stack"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" + helpify "-t, --target" "Set the global command installation path" "[/usr/local/bin]" + helpify "-f, --force" "Force reinstallation of global command" +} + +usage4help() { + helpify "install" "Installing the GDRCD development stack" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Set default values +target="/usr/local/bin" +force_reinstall="false" + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" ]]; then + shift; continue + fi + + case "${1}" in + -h|--help) + need_help="true"; shift ;; + -t|--target) + target="${2}"; shift; shift ;; + -f|--force) + force_reinstall="true"; shift ;; + *) + messageUnknownCommand "$COMMAND_NAME" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +install() { + message --magenta "${MESSAGE_INSTALL_START}"; + + # Check if the target path is a directory + if [[ ! -d "$target" ]]; then + message --error "${MESSAGE_TARGET_NOT_DIRECTORY}"; + exit 1; + fi; + + # Check if the target path is writable + if [[ ! -w "$target" ]]; then + message --error "${MESSAGE_TARGET_NOT_WRITABLE}"; + exit 1; + fi; + + # Check if stack is already installed + if [[ -f "${target}/${PROJECT}" ]]; then + if [[ "${force_reinstall}" == "true" ]]; then + rm -f "${target}/${PROJECT}"; + else + message --error "${MESSAGE_INSTALL_ALREADY_INSTALLED}"; + exit 1; + fi; + fi; + + # Create the target path if it does not exist + if [[ ! -d "$target" ]]; then + mkdir -p "$target"; + fi; + + # Create link to the stack binary + ln -sf "${STACK_DIR}/run" "$target/${PROJECT}" 2>/dev/null; + + # Set the permissions + chmod +x "$target/${PROJECT}"; + + # Check if the installation was successful + if [[ -f "$target/${PROJECT}" ]]; then + message --success "${MESSAGE_INSTALL_SUCCESS}"; + else + message --error "${MESSAGE_INSTALL_FAILED}"; + exit 1; + fi; +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + logo; + install; +fi \ No newline at end of file diff --git a/bin/commands/logs b/bin/commands/logs new file mode 100755 index 0000000..ff98e34 --- /dev/null +++ b/bin/commands/logs @@ -0,0 +1,72 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" + +# Save command name +args=("$@") +COMMAND_NAME="${args[0]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "logs" "[OPTIONS...]" + helpify_subtitle "Show all containers log"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "logs" "Show all containers log" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "$COMMAND_NAME" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +logs() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + isContainerRunning "${SERVICE_NAME}" + # + prompt -i "Getting all containers logs..." + dockerCompose logs -f -t +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + logs +fi \ No newline at end of file diff --git a/bin/commands/mailhog/attach b/bin/commands/mailhog/attach new file mode 100755 index 0000000..28e03b5 --- /dev/null +++ b/bin/commands/mailhog/attach @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} attach" "[OPTIONS...]" + helpify_subtitle "Access to ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container bash"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "attach" "Access to ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container bash" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +attach() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + isContainerRunning "${SERVICE_NAME}" + # + prompt -i "Attaching to ${SERVICE_NAME} container..." + dockerCompose exec -it "${SERVICE_NAME}" bash && return 0; +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + attach +fi \ No newline at end of file diff --git a/bin/commands/mailhog/build b/bin/commands/mailhog/build new file mode 100755 index 0000000..5fae5da --- /dev/null +++ b/bin/commands/mailhog/build @@ -0,0 +1,73 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} build" "[OPTIONS...]" + helpify_subtitle "Build ${SERVICE_NAME:-${CURRENT_DIR_NAME}} image and container"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "build" "Build ${SERVICE_NAME:-${CURRENT_DIR_NAME}} image and container" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +build() { + # + prompt -i "Building ${SERVICE_NAME} image and container..." + dockerCompose rm -s -f "${SERVICE_NAME}" && dockerCompose build --no-cache "${SERVICE_NAME}" && dockerCompose up -d "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + build +fi \ No newline at end of file diff --git a/bin/commands/mailhog/logs b/bin/commands/mailhog/logs new file mode 100755 index 0000000..83b6236 --- /dev/null +++ b/bin/commands/mailhog/logs @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} logs" "[OPTIONS...]" + helpify_subtitle "Show ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container's log"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "logs" "Show ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container's log" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +logs() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + isContainerRunning "${SERVICE_NAME}" + # + prompt -i "Getting ${SERVICE_NAME} container logs..." + dockerCompose logs -f -t "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + logs +fi \ No newline at end of file diff --git a/bin/commands/mailhog/restart b/bin/commands/mailhog/restart new file mode 100755 index 0000000..09c078c --- /dev/null +++ b/bin/commands/mailhog/restart @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} restart" "[OPTIONS...]" + helpify_subtitle "Restart ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" + +} + +# Function for showing usage of this command for help +usage4help() { + helpify "restart" "Restart ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +restart() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + # + prompt -i "Restarting ${SERVICE_NAME} container..." + dockerCompose restart "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + restart +fi \ No newline at end of file diff --git a/bin/commands/mailhog/start b/bin/commands/mailhog/start new file mode 100755 index 0000000..3dd0776 --- /dev/null +++ b/bin/commands/mailhog/start @@ -0,0 +1,75 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} start" "[OPTIONS...]" + helpify_subtitle "Start ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "start" "Start ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +start() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + # + prompt -i "Starting ${SERVICE_NAME} container..." + dockerCompose start "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + start +fi \ No newline at end of file diff --git a/bin/commands/mailhog/stop b/bin/commands/mailhog/stop new file mode 100755 index 0000000..a7bae55 --- /dev/null +++ b/bin/commands/mailhog/stop @@ -0,0 +1,75 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} stop" "[OPTIONS...]" + helpify_subtitle "Stop ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "stop" "Stop ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +stop() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + # + prompt -i "Stopping ${SERVICE_NAME} container..." + dockerCompose stop "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + stop +fi \ No newline at end of file diff --git a/bin/commands/phpmyadmin/attach b/bin/commands/phpmyadmin/attach new file mode 100755 index 0000000..28e03b5 --- /dev/null +++ b/bin/commands/phpmyadmin/attach @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} attach" "[OPTIONS...]" + helpify_subtitle "Access to ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container bash"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "attach" "Access to ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container bash" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +attach() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + isContainerRunning "${SERVICE_NAME}" + # + prompt -i "Attaching to ${SERVICE_NAME} container..." + dockerCompose exec -it "${SERVICE_NAME}" bash && return 0; +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + attach +fi \ No newline at end of file diff --git a/bin/commands/phpmyadmin/build b/bin/commands/phpmyadmin/build new file mode 100755 index 0000000..5fae5da --- /dev/null +++ b/bin/commands/phpmyadmin/build @@ -0,0 +1,73 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} build" "[OPTIONS...]" + helpify_subtitle "Build ${SERVICE_NAME:-${CURRENT_DIR_NAME}} image and container"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "build" "Build ${SERVICE_NAME:-${CURRENT_DIR_NAME}} image and container" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +build() { + # + prompt -i "Building ${SERVICE_NAME} image and container..." + dockerCompose rm -s -f "${SERVICE_NAME}" && dockerCompose build --no-cache "${SERVICE_NAME}" && dockerCompose up -d "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + build +fi \ No newline at end of file diff --git a/bin/commands/phpmyadmin/logs b/bin/commands/phpmyadmin/logs new file mode 100755 index 0000000..83b6236 --- /dev/null +++ b/bin/commands/phpmyadmin/logs @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} logs" "[OPTIONS...]" + helpify_subtitle "Show ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container's log"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "logs" "Show ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container's log" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +logs() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + isContainerRunning "${SERVICE_NAME}" + # + prompt -i "Getting ${SERVICE_NAME} container logs..." + dockerCompose logs -f -t "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + logs +fi \ No newline at end of file diff --git a/bin/commands/phpmyadmin/restart b/bin/commands/phpmyadmin/restart new file mode 100755 index 0000000..09c078c --- /dev/null +++ b/bin/commands/phpmyadmin/restart @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} restart" "[OPTIONS...]" + helpify_subtitle "Restart ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" + +} + +# Function for showing usage of this command for help +usage4help() { + helpify "restart" "Restart ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +restart() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + # + prompt -i "Restarting ${SERVICE_NAME} container..." + dockerCompose restart "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + restart +fi \ No newline at end of file diff --git a/bin/commands/phpmyadmin/start b/bin/commands/phpmyadmin/start new file mode 100755 index 0000000..3dd0776 --- /dev/null +++ b/bin/commands/phpmyadmin/start @@ -0,0 +1,75 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} start" "[OPTIONS...]" + helpify_subtitle "Start ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "start" "Start ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +start() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + # + prompt -i "Starting ${SERVICE_NAME} container..." + dockerCompose start "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + start +fi \ No newline at end of file diff --git a/bin/commands/phpmyadmin/stop b/bin/commands/phpmyadmin/stop new file mode 100755 index 0000000..a7bae55 --- /dev/null +++ b/bin/commands/phpmyadmin/stop @@ -0,0 +1,75 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} stop" "[OPTIONS...]" + helpify_subtitle "Stop ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "stop" "Stop ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +stop() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + # + prompt -i "Stopping ${SERVICE_NAME} container..." + dockerCompose stop "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + stop +fi \ No newline at end of file diff --git a/bin/commands/recreate b/bin/commands/recreate new file mode 100755 index 0000000..01d38b0 --- /dev/null +++ b/bin/commands/recreate @@ -0,0 +1,71 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" + +# Save command name +args=("$@") +COMMAND_NAME="${args[0]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "recreate" + helpify_subtitle "Recreate all containers and networks"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "recreate" "Recreate all containers and networks" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "$COMMAND_NAME" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +start() { + # Preliminary checks + isContainerExist + # + prompt -i "Recreating all stack stuff..." + dockerCompose up -d --force-recreate +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + start +fi \ No newline at end of file diff --git a/bin/commands/restart b/bin/commands/restart new file mode 100755 index 0000000..a8e0973 --- /dev/null +++ b/bin/commands/restart @@ -0,0 +1,71 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" + +# Save command name +args=("$@") +COMMAND_NAME="${args[0]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "restart" + helpify_subtitle "Restart all containers"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "restart" "Restart all containers" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "$COMMAND_NAME" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +restart() { + # Preliminary checks + isContainerExist + # + prompt -i "Restarting all containers..." + dockerCompose restart +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + restart +fi \ No newline at end of file diff --git a/bin/commands/service/disable b/bin/commands/service/disable new file mode 100755 index 0000000..0dd6d3a --- /dev/null +++ b/bin/commands/service/disable @@ -0,0 +1,82 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} disable" "" + helpify_subtitle "Disable optional services"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "disable" "Disable optional services" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +service_name="" + +# Parse arguments +while [[ $# -gt 0 ]]; do + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + case "$1" in + -h|--help) + need_help="true" + shift + ;; + *) + service_name="$1" + shift + ;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +# Validate required arguments +if [[ -z "$service_name" ]] && [[ "$need_help" != "true" ]]; then + message --error "Missing service name" + usage + exit 1 +fi + +#---------------------------RUN COMMAND-------------------------------# + +disable () { + disableService "$service_name" + message --success "Service $service_name disabled" + + # Restart containers to apply changes + dockerCompose up -d +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + disable +fi \ No newline at end of file diff --git a/bin/commands/service/enable b/bin/commands/service/enable new file mode 100755 index 0000000..55bade1 --- /dev/null +++ b/bin/commands/service/enable @@ -0,0 +1,81 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} enable" "" + helpify_subtitle "Enable optional services"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help"; +} + +# Function for showing usage of this command for help +usage4help() { + helpify "enable" "Enable optional services" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +service_name="" + +# Parse arguments +while [[ $# -gt 0 ]]; do + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + case "$1" in + -h|--help) + need_help="true" + shift + ;; + *) + service_name="$1" + shift + ;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +# Validate required arguments +if [[ -z "$service_name" ]] && [[ "$need_help" != "true" ]]; then + usage + exit 1 +fi + +#---------------------------RUN COMMAND-------------------------------# + +enable () { + enableService "$service_name" + message --success "Service $service_name enabled" + + # Restart containers to apply changes + dockerCompose up -d +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + enable +fi \ No newline at end of file diff --git a/bin/commands/service/list b/bin/commands/service/list new file mode 100755 index 0000000..ce9581d --- /dev/null +++ b/bin/commands/service/list @@ -0,0 +1,88 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} list" + helpify_subtitle "List all optional services"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help"; +} + +# Function for showing usage of this command for help +usage4help() { + helpify "list" "List all optional services" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +service_name="" + +# Parse arguments +while [[ $# -gt 0 ]]; do + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + case "$1" in + -h|--help) + need_help="true" + shift + ;; + *) + service_name="$1" + shift + ;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +list () { + # list all optional services + prompt -i "List all optional services..."; + + # Get all optional services + services=($(getAllOptionalServices)) + + # Print each service with its status and description + for service in "${services[@]}"; do + + status="disabled" + if isServiceEnabled "$service"; then + status="enabled" + fi + + description=$(getServiceDescription "$service") + + helpify "$service" "$description" "Status: $status" + done +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + list +fi \ No newline at end of file diff --git a/bin/commands/start b/bin/commands/start new file mode 100755 index 0000000..0498d9f --- /dev/null +++ b/bin/commands/start @@ -0,0 +1,71 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" + +# Save command name +args=("$@") +COMMAND_NAME="${args[0]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "start" + helpify_subtitle "Start all containers"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "start" "Start all containers" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "$COMMAND_NAME" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +start() { + # Preliminary checks + isContainerExist + # + prompt -i "Starting all containers..." + dockerCompose start +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + start +fi \ No newline at end of file diff --git a/bin/commands/stop b/bin/commands/stop new file mode 100755 index 0000000..1da6ef2 --- /dev/null +++ b/bin/commands/stop @@ -0,0 +1,71 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" + +# Save command name +args=("$@") +COMMAND_NAME="${args[0]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "stop" + helpify_subtitle "Stop all containers"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "stop" "Stop all containers" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "$COMMAND_NAME" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +stop() { + # Preliminary checks + isContainerExist + # + prompt -i "Stopping all containers..." + dockerCompose stop +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + stop +fi \ No newline at end of file diff --git a/bin/commands/webserver/attach b/bin/commands/webserver/attach new file mode 100755 index 0000000..28e03b5 --- /dev/null +++ b/bin/commands/webserver/attach @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} attach" "[OPTIONS...]" + helpify_subtitle "Access to ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container bash"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "attach" "Access to ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container bash" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +attach() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + isContainerRunning "${SERVICE_NAME}" + # + prompt -i "Attaching to ${SERVICE_NAME} container..." + dockerCompose exec -it "${SERVICE_NAME}" bash && return 0; +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + attach +fi \ No newline at end of file diff --git a/bin/commands/webserver/build b/bin/commands/webserver/build new file mode 100755 index 0000000..5fae5da --- /dev/null +++ b/bin/commands/webserver/build @@ -0,0 +1,73 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} build" "[OPTIONS...]" + helpify_subtitle "Build ${SERVICE_NAME:-${CURRENT_DIR_NAME}} image and container"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "build" "Build ${SERVICE_NAME:-${CURRENT_DIR_NAME}} image and container" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +build() { + # + prompt -i "Building ${SERVICE_NAME} image and container..." + dockerCompose rm -s -f "${SERVICE_NAME}" && dockerCompose build --no-cache "${SERVICE_NAME}" && dockerCompose up -d "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + build +fi \ No newline at end of file diff --git a/bin/commands/webserver/logs b/bin/commands/webserver/logs new file mode 100755 index 0000000..83b6236 --- /dev/null +++ b/bin/commands/webserver/logs @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} logs" "[OPTIONS...]" + helpify_subtitle "Show ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container's log"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "logs" "Show ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container's log" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +logs() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + isContainerRunning "${SERVICE_NAME}" + # + prompt -i "Getting ${SERVICE_NAME} container logs..." + dockerCompose logs -f -t "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + logs +fi \ No newline at end of file diff --git a/bin/commands/webserver/restart b/bin/commands/webserver/restart new file mode 100755 index 0000000..09c078c --- /dev/null +++ b/bin/commands/webserver/restart @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} restart" "[OPTIONS...]" + helpify_subtitle "Restart ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" + +} + +# Function for showing usage of this command for help +usage4help() { + helpify "restart" "Restart ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +restart() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + # + prompt -i "Restarting ${SERVICE_NAME} container..." + dockerCompose restart "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + restart +fi \ No newline at end of file diff --git a/bin/commands/webserver/start b/bin/commands/webserver/start new file mode 100755 index 0000000..3dd0776 --- /dev/null +++ b/bin/commands/webserver/start @@ -0,0 +1,75 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} start" "[OPTIONS...]" + helpify_subtitle "Start ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "start" "Start ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +start() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + # + prompt -i "Starting ${SERVICE_NAME} container..." + dockerCompose start "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + start +fi \ No newline at end of file diff --git a/bin/commands/webserver/stop b/bin/commands/webserver/stop new file mode 100755 index 0000000..a7bae55 --- /dev/null +++ b/bin/commands/webserver/stop @@ -0,0 +1,75 @@ +#!/usr/bin/env bash + +importLib "lib/lib-docker.sh" + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Get current file name +CURRENT_FILE_NAME="$(basename "${BASH_SOURCE[0]}")" +# Get directory name +CURRENT_DIR_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")" + +# Save command name +args=("$@") +# Set service name by arguments or by current directory name +SERVICE_NAME="${args[0]}" +COMMAND_NAME="${args[1]}" + +# --------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------- + +# Function for showing usage of this command +usage() { + helpify_title "${SERVICE_NAME:-${CURRENT_DIR_NAME}} stop" "[OPTIONS...]" + helpify_subtitle "Stop ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" +} + +# Function for showing usage of this command for help +usage4help() { + helpify "stop" "Stop ${SERVICE_NAME:-${CURRENT_DIR_NAME}} container" +} + +# --------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------- + +#---------------------------PARSE ARGUMENTS-------------------------------# + +# Parse arguments +while [[ $# -gt 0 ]]; do + # if the argument is the command name, skip it + if [[ "$1" == "${COMMAND_NAME}" || "$1" == "${SERVICE_NAME}" ]]; then + shift; continue + fi + + # + case "$1" in + -h|--help) + need_help="true"; shift ;; + *) + messageUnknownCommand "${SERVICE_NAME:-${CURRENT_DIR_NAME}} ${COMMAND_NAME}" "$1"; exit 1;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +#---------------------------RUN COMMAND-------------------------------# + +stop() { + # Preliminary checks + isContainerExist "${SERVICE_NAME}" + # + prompt -i "Stopping ${SERVICE_NAME} container..." + dockerCompose stop "${SERVICE_NAME}" +} + +# Run command if this file is the command file +if [[ "${COMMAND_NAME}" == "${CURRENT_FILE_NAME}" ]]; then + stop +fi \ No newline at end of file diff --git a/bin/lib/lib-commands.sh b/bin/lib/lib-commands.sh new file mode 100644 index 0000000..373f798 --- /dev/null +++ b/bin/lib/lib-commands.sh @@ -0,0 +1,198 @@ +#!/bin/bash + +# Set Library Name +LIB_NAME="lib-command.sh" + +# Check if STACK_DIR is set +if [[ ! "${STACK_DIR}" ]]; then + echo "Please define 'STACK_DIR' variable"; exit 1 +fi + +# Check if lib-core.sh is already imported +if [[ "${PROCESS_SOURCE[*]}" =~ $LIB_NAME ]]; then + echo "Warning! '${LIB_NAME}' is already imported"; exit 1 +fi + +# Add lib-core.sh to the list of imported files +PROCESS_SOURCE=("$LIB_NAME") + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +CORE_HANDLER=("service") + +# --------------------------------------------------------------------- +# Commands +# --------------------------------------------------------------------- + +# check if is a command +isCommand() { + case "${1}" in + "--abs-path") + ([[ -f "$2" ]] && command -v "$2") >/dev/null 2>&1;; + *) + ([[ -f "${COMMANDS_DIR}/$1" ]] && command -v "${COMMANDS_DIR}/$1") >/dev/null 2>&1;; + esac +} + +# check if is a service command +isSubCommand() { + # check if is a valid service or handler + # check if is a valid service or a handler + if [[ ! "${SERVICES[*]}" =~ $1 ]] && [[ ! "${CORE_HANDLER[*]}" =~ $1 ]]; then + return 1; + fi + + # check if exist directory + if [[ ! -d "${COMMANDS_DIR}/$1" ]]; then + return 1 + fi + + # check if directory contains at least one command + if ! hasCommands "${COMMANDS_DIR}/$1"; then + return 1 + fi + + return 0 +} + +# check if a directory contains at least one command +hasCommands() { + for file in "${1}/"*; do + if [[ -f "${file}" ]] && command -v "${file}" >/dev/null 2>&1; then + return 0 + fi + done + return 1 +} + +# --------------------------------------------------------------------- +# Usage +# --------------------------------------------------------------------- + +# Show commands +usageCommands () { + helpify_subtitle "COMMANDS:"; + + # scan bin directory for commands + for command in "${COMMANDS_DIR}"/*; do + # skip non-executable files + if [[ ! -x "${command}" ]]; then + continue + fi + + # skip directories + if [[ -d "${command}" ]]; then + continue + fi + + # check if is a command + if ! isCommand --abs-path "${command}"; then + continue + fi + + # execute command to get usage + # shellcheck disable=SC1090 + source "${command}" + done +} + +# Show commands for sub-directories +usageSubDirsCommands () { + # scan bin sub-directories for commands + for subCommandDir in "${COMMANDS_DIR}"/*; do + subCommand="$(basename "${subCommandDir}")" + usageSubDirCommands "${subCommand}" || continue + done +} + +# Show commands for a sub-directory +usageSubDirCommands () { + # get service name and directory + local subCommand="${1}" + local subDirCommand="${COMMANDS_DIR}/${subCommand}" + + # check if is a valid service or a handler + if [[ ! "${SERVICES[*]}" =~ $subCommand ]] && [[ ! "${CORE_HANDLER[*]}" =~ $subCommand ]]; then + return 1; + fi + + # skip non-directories + if [[ ! -d "${subDirCommand}" ]]; then + return 1; + fi + + # check if directory containains at least one command + if ! hasCommands "${subDirCommand}"; then + return 1; + fi + + # show title + local command_name="$(basename "${0}")" + echo; helpify_subcommand_title "${command_name} ${subCommand}" "" "[OPTIONS...] COMMAND [OPTIONS...]"; + + # scan bin sub-directories for commands + for command in "${subDirCommand}"/*; do + # skip non-executable files + if [[ ! -x "${command}" ]]; then + return 1; + fi + + # skip directories + if [[ -d "${command}" ]]; then + return 1; + fi + + # check if is a command + if ! isCommand --abs-path "${command}"; then + return 1; + fi + + # execute command to get usage + # shellcheck disable=SC1090 + source "${command}" + done +} + +messageUnknownCommand () { + # show error + prompt -e "Unknown argument: $2"; + prompt -i "Try './run $1 --help' for more information."; +} + +# --------------------------------------------------------------------- +# Misc +# --------------------------------------------------------------------- + +# TODO: rewrite this function to use a better approach +finalize_argument_parsing() { + # if has_any_error is true, exit with error code + if [[ "${has_any_error}" == "true" ]]; then + prompt -i "Try './run --help' for more information."; exit 1 + fi + + if [[ "${need_help}" == "true" ]]; then + # Force to stop the script + forceStop="true" + + # HELP > stack + if [[ "${need_usage4help}" == "true" && "${1}" != "run" ]]; then + usage4help; + # Continue the script execution + forceStop="false" + # HELP > stack > Command + else + usage; + fi + + # if has_any_error is true, exit with error code + if [[ "${has_any_error}" == "true" ]]; then + exit 1 + fi + # if forceStop is true, exit with success code + if [[ "${forceStop}" == "true" ]]; then + exit 0; + fi + fi +} \ No newline at end of file diff --git a/bin/lib/lib-core.sh b/bin/lib/lib-core.sh new file mode 100644 index 0000000..666c06d --- /dev/null +++ b/bin/lib/lib-core.sh @@ -0,0 +1,100 @@ +#!/bin/bash + +# Set Library Name +LIB_NAME="lib-core.sh" + +set -Eeo pipefail + +# Check if STACK_DIR is set +if [[ ! "${STACK_DIR}" ]]; then + echo "Please define 'STACK_DIR' variable" + exit 1 +fi + +# Check if lib-core.sh is already imported +if [[ "${PROCESS_SOURCE[*]}" =~ $LIB_NAME ]]; then + echo "Warning! '${LIB_NAME}' is already imported" + exit 1 +fi + +# Add lib-core.sh to the list of imported files +PROCESS_SOURCE=("$LIB_NAME") + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +#------------Directories--------------# +BIN_DIR="${STACK_DIR}/bin" +LIB_DIR="${STACK_DIR}/bin/lib" +COMMANDS_DIR="${STACK_DIR}/bin/commands" +DOCKER_DIR="${STACK_DIR}/.docker" + +#------------Decoration-----------# +export c_default="\033[0m" +export c_blue="\033[1;34m" +export c_magenta="\033[1;35m" +export c_cyan="\033[1;36m" +export c_green="\033[1;32m" +export c_red="\033[1;31m" +export c_yellow="\033[1;33m" + +#------------Trigger-----------# +need_help="false" +need_usage4help="false" +has_any_error="false" + +# --------------------------------------------------------------------- +# Utilities +# --------------------------------------------------------------------- + +getVersion() { + cat "${STACK_DIR}/version" | tr -d 'v' +} + +checkVersion() { + # Get the version from version file + local version + version=$(cat "${STACK_DIR}/version" | tr -d 'v') + + # Check if the version is the latest check last release on github + local latest_version + latest_version=$(curl -s "https://github.com/GDRCD/stack/releases" | grep -o "v[0-9]*\.[0-9]*\.[0-9]*" | head -n 1 | tr -d 'v') + + # Remove the point from the version and compare the two versions + if [[ "${version//./}" -lt "${latest_version//./}" ]]; then + prompt -w "Warning! A new version is available. Please update the stack." + prompt -w "Current version: ${version} - Latest version: ${latest_version}" + fi +} + +# --------------------------------------------------------------------- +# Imports Methods +# --------------------------------------------------------------------- + +importLib() { + # Check if library exists + if [[ ! -f "${BIN_DIR}/${1}" ]]; then + prompt -e "Error! '${1}' is not found." + exit 1 + fi + + # Check if library is already imported, if not import it + if [[ ! "${PROCESS_SOURCE[*]}" =~ ${1} ]]; then + # shellcheck source=bin/lib/lib-core.sh + source "${BIN_DIR}/${1}" + PROCESS_SOURCE+=("${1}") + fi +} + +importEnv() { + # Check if env file exists + if [[ ! -f "${STACK_DIR}/.env" ]]; then + prompt -e "Error! '.env' file is not found. Please create it first." + exit 1 + fi + + # shellcheck source=.env + source "${STACK_DIR}/.env" +} + diff --git a/bin/lib/lib-docker.sh b/bin/lib/lib-docker.sh new file mode 100644 index 0000000..ace1cf2 --- /dev/null +++ b/bin/lib/lib-docker.sh @@ -0,0 +1,125 @@ +#!/bin/bash + +# Set Library Name +LIB_NAME="lib-docker.sh" + +# Check if STACK_DIR is set +if [[ ! "${STACK_DIR}" ]]; then + echo "Please define 'STACK_DIR' variable"; exit 1 +fi + +# Check if lib-core.sh is already imported +if [[ "${PROCESS_SOURCE[*]}" =~ $LIB_NAME ]]; then + echo "Warning! '${LIB_NAME}' is already imported"; exit 1 +fi + +# Add lib-core.sh to the list of imported files +PROCESS_SOURCE=("$LIB_NAME") + +# --------------------------------------------------------------------- +# Utilities +# --------------------------------------------------------------------- + +# Check if docker is installed +isDockerInstalled() { + if ! command -v docker &> /dev/null; then + prompt -e "Error! Docker is not installed. Please install it first." + exit 1 + fi +} + +# Check if docker-compose is installed +isDockerComposeInstalled() { + if ! command -v docker compose &> /dev/null; then + prompt -e "Error! Docker Compose is not installed. Please install it first." + exit 1 + fi +} + +# Check if docker is running +isDockerRunning() { + if ! docker info &> /dev/null; then + prompt -e "Error! Docker is not running. Please start it first." + exit 1 + fi +} + +# Checks if a Docker container exists by name or for all services in SERVICES array. +isContainerExist() { + # if container name is passed as argument, check if it exists + if [[ "$1" ]]; then + if [[ ! "$(docker ps -aq -f name="${PROJECT}_$1")" ]] && [[ ! "$(docker ps -aq -f name="${PROJECT}_$1")" ]]; then + prompt -e "Error! Container '${PROJECT}_$1' is not found." + exit 1 + fi + return 0; + fi + # otherwise, check if all containers exist + for service in "${SERVICES[@]}"; do + # database + if [[ "$service" == "database" ]]; then + if [[ ! "$(docker ps -aq -f name="${PROJECT}_$service")" ]]; then + prompt -e "Error! Container '${PROJECT}_$service' is not found." + exit 1 + fi + continue + fi + + # other services + if [[ ! "$(docker ps -aq -f name="${PROJECT}_$service")" ]]; then + prompt -e "Error! Container '${PROJECT}_$service' is not found." + exit 1 + fi + done +} + +# Checks if a specific container or all containers are running and exits with an error if any are not. +isContainerRunning() { + # if container name is passed as argument, i check if it's running + if [[ "$1" ]]; then + if [[ ! "$(docker ps -q -f name="${PROJECT}_$1")" ]] && [[ ! "$(docker ps -q -f name="${PROJECT}_$1")" ]]; then + prompt -e "Error! Container '${PROJECT}_$1' is not running." + exit 1 + fi + return 0; + fi + + # otherwise, i check if all containers are running + for service in "${SERVICES[@]}"; do + # database + if [[ "$service" == "database" ]]; then + if [[ ! "$(docker ps -q -f name="${PROJECT}_$service")" ]]; then + prompt -e "Error! Container '${PROJECT}_$service' is not running." + exit 1 + fi + continue + fi + # other services + if [[ ! "$(docker ps -q -f name="${PROJECT}_$service")" ]]; then + prompt -e "Error! Container '${PROJECT}_$service' is not running." + exit 1 + fi + done +} + +# --------------------------------------------------------------------- +# Commands +# --------------------------------------------------------------------- + +dockerCompose() { + # Check if docker compose is installed + isDockerComposeInstalled + # Check if docker is running + isDockerRunning + + # Costruisci array dei profili attivi + profiles=() + for service in "phpmyadmin" "mailhog"; do + if isServiceEnabled "$service"; then + profiles+=("--profile" "$service") + fi + done + + export STACK_DIR + docker compose -p "${PROJECT}" -f "$DOCKER_DIR/compose.yml" --env-file "$STACK_DIR/.env" "${profiles[@]}" "$@" +} \ No newline at end of file diff --git a/bin/lib/lib-helpify.sh b/bin/lib/lib-helpify.sh new file mode 100644 index 0000000..babd976 --- /dev/null +++ b/bin/lib/lib-helpify.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +# Set Library Name +LIB_NAME="lib-helpify.sh" + +# Check if STACK_DIR is set +if [[ ! "${STACK_DIR}" ]]; then + echo "Please define 'STACK_DIR' variable"; exit 1 +fi + +# Check if lib-core.sh is already imported +if [[ "${PROCESS_SOURCE[*]}" =~ $LIB_NAME ]]; then + echo "Warning! '${LIB_NAME}' is already imported"; exit 1 +fi + +# Add lib-core.sh to the list of imported files +PROCESS_SOURCE=("$LIB_NAME") + +# --------------------------------------------------------------------- +# Helpify +# --------------------------------------------------------------------- + +helpify_title() { + local command_name="$(basename "${0}")" + printf " ${c_cyan}%s${c_blue}%s ${c_blue}%s ${c_green}%s\n\n" "Usage: " "$command_name" "$1" "$2" +} + +helpify_subtitle() { + printf " ${c_cyan}%s\n${c_default}" "$1" +} + +helpify_subcommand_title() { + printf " ${c_cyan}%s${c_red}%s ${c_red}%s ${c_green}%s\n\n${c_default}" "Usage: " "$1" "$2" "$3" + printf " ${c_cyan}%s\n${c_default}" "COMMANDS:" +} + +helpify_separator() { + printf "\n" +} + +helpify() { + printf " ${c_blue}%-20s ${c_green}%-60s ${c_magenta}%s\n${c_default}" "${1}" "${2}" "${3}" +} \ No newline at end of file diff --git a/bin/lib/lib-messages.sh b/bin/lib/lib-messages.sh new file mode 100644 index 0000000..399979b --- /dev/null +++ b/bin/lib/lib-messages.sh @@ -0,0 +1,96 @@ +#!/bin/bash + +# Set Library Name +LIB_NAME="lib-messages.sh" + +# Check if STACK_DIR is set +if [[ ! "${STACK_DIR}" ]]; then + echo "Please define 'STACK_DIR' variable"; exit 1 +fi + +# Check if lib-core.sh is already imported +if [[ "${PROCESS_SOURCE[*]}" =~ $LIB_NAME ]]; then + echo "Warning! '${LIB_NAME}' is already imported"; exit 1 +fi + +# Add lib-core.sh to the list of imported files +PROCESS_SOURCE=("$LIB_NAME") + +# --------------------------------------------------------------------- +# Messages +# --------------------------------------------------------------------- + +# Messages +MESSAGE_OPTIONS_ERROR="--error" +MESSAGE_OPTIONS_WARNING="--warning" +MESSAGE_OPTIONS_INFO="--info" +MESSAGE_OPTIONS_INFO_STATUS="--info-status" +MESSAGE_OPTIONS_SUCCESS="--success" +MESSAGE_OPTIONS_MAGENTA="--magenta" +MESSAGE_OPTIONS=("$MESSAGE_OPTIONS_ERROR" "$MESSAGE_OPTIONS_WARNING" "$MESSAGE_OPTIONS_INFO" "$MESSAGE_OPTIONS_SUCCESS" "$MESSAGE_OPTIONS_INFO_STATUS" "$MESSAGE_OPTIONS_MAGENTA") + +# Errors +MESSAGE_OPTIONS_NOT_SET="Message is not set" +MESSAGE_NO_ARGUMENTS="No arguments passed!" + +# --------------------------------------------------------------------- +# Utilities +# --------------------------------------------------------------------- + +# Echo a message in a specific color +prompt() { + case "${1}" in + "-s") + echo -e " ${c_green}${2}${c_default}" ;; # print success message + "-e") + echo -e " ${c_red}${2}${c_default}" ;; # print error message + "-w") + echo -e " ${c_yellow}${2}${c_default}" ;; # print warning message + "-i") + echo -e " ${c_cyan}${2}${c_default}" ;; # print info message + "-il") + echo -e " ${c_blue}${2}${c_default}" ;; # print info low message + "-m") + echo -e " ${c_magenta}${2}${c_default}" ;; # print magenta message + esac +} + +# Funzione per stampare un messaggio +message() { + # Check if message type is valid + if [[ ! "${MESSAGE_OPTIONS[*]}" =~ $1 ]]; then + message --error "Message type '${1}' is not valid"; exit 1 + fi + + # Check if message is set + if [[ ! "${2}" ]]; then + message --error "$MESSAGE_OPTIONS_NOT_SET"; exit 1 + fi + + # Print message + case "${1}" in + "${MESSAGE_OPTIONS_ERROR}") + prompt "-e" "Error: ${2}" ;; + "${MESSAGE_OPTIONS_WARNING}") + prompt "-w" "${2}" ;; + "${MESSAGE_OPTIONS_INFO}") + prompt "-i" "${2}" ;; + "${MESSAGE_OPTIONS_INFO_STATUS}") + prompt "-il" "${2}" ;; + "${MESSAGE_OPTIONS_SUCCESS}") + prompt "-s" "${2}" ;; + "${MESSAGE_OPTIONS_MAGENTA}") + prompt "-m" "${2}" ;; + esac +} + +logo() { + ascii_art='__ __ __ __ __ + / _` | \ |__) / ` | \ + \__> |__/ | \ \__, |__/' + echo -e " ${c_blue}${ascii_art}${c_default}"; +} + +versionInfo() { + echo -e " ${c_cyan}Version: ${c_magenta}$(getVersion)${c_default}"; +} diff --git a/bin/lib/lib-services.sh b/bin/lib/lib-services.sh new file mode 100644 index 0000000..26b5f96 --- /dev/null +++ b/bin/lib/lib-services.sh @@ -0,0 +1,124 @@ +#!/bin/bash + +# Set Library Name +LIB_NAME="lib-services.sh" + +# Check if STACK_DIR is set +if [[ ! "${STACK_DIR}" ]]; then + echo "Please define 'STACK_DIR' variable"; exit 1 +fi + +# Check if lib-core.sh is already imported +if [[ "${PROCESS_SOURCE[*]}" =~ $LIB_NAME ]]; then + echo "Warning! '${LIB_NAME}' is already imported"; exit 1 +fi + +# Add lib-core.sh to the list of imported files +PROCESS_SOURCE=("$LIB_NAME") + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# ------------Services--------------# +CORE_SERVICES=("webserver" "database") +OPTIONAL_SERVICES=("phpmyadmin" "mailhog") + +# Combine CORE_SERVICES and the services from services (only the enabled ones) +SERVICES=() +SERVICES=("${CORE_SERVICES[@]}") +if [[ -f "${STACK_DIR}/services" ]]; then + # Read comma-separated services from file + while IFS=',' read -ra ENABLED; do + for service in "${ENABLED[@]}"; do + # Trim whitespace + service=$(echo "$service" | xargs) + if [[ -n "$service" ]]; then + SERVICES+=("$service") + fi + done + done < "${STACK_DIR}/services" +fi + +# ------------Services Descriptions--------------# +SERVICE_DESCRIPTIONS=( + "phpmyadmin:Web interface for MySQL database management" + "mailhog:Email testing tool for local development" +) + +# --------------------------------------------------------------------- +# Services +# --------------------------------------------------------------------- + +# Get only enabled optional services +getOptionalServices() { + local services=() + for service in "${SERVICES[@]}"; do + if [[ ! "${CORE_SERVICES[*]}" =~ $service ]]; then + services+=("$service") + fi + done + echo "${services[@]}" +} + +# Get all available optional services +getAllOptionalServices() { + echo "${OPTIONAL_SERVICES[@]}" +} + +# Get service description +getServiceDescription() { + local service=$1 + + for desc in "${SERVICE_DESCRIPTIONS[@]}"; do + IFS=':' read -r svc description <<< "$desc" + if [[ "$svc" == "$service" ]]; then + echo "$description" + return + fi + done + + echo "No description available" +} + +# Check if service is enabled +isServiceEnabled() { + local service=$1 + [[ "${SERVICES[*]}" =~ $service ]] +} + +# Enable a service +enableService() { + local service=$1 + + # Validate service exists + if [[ ! "${OPTIONAL_SERVICES[*]}" =~ $service ]]; then + message --error "Service '$service' not found" + return 1 + fi + + # Add to services file if not already enabled + if ! isServiceEnabled "$service"; then + if [[ -f "${STACK_DIR}/services" ]]; then + echo -n ",$service" >> "${STACK_DIR}/services" + else + echo "$service" > "${STACK_DIR}/services" + fi + fi +} + +# Disable a service +disableService() { + local service=$1 + + # Validate service exists + if [[ ! "${OPTIONAL_SERVICES[*]}" =~ $service ]]; then + message --error "Service '$service' not found" + return 1 + fi + + # Remove from services file if enabled + if isServiceEnabled "$service"; then + sed -i "/$service/d" "${STACK_DIR}/services" + fi +} diff --git a/gdrcd-stack b/gdrcd-stack deleted file mode 100755 index 7b48181..0000000 --- a/gdrcd-stack +++ /dev/null @@ -1,124 +0,0 @@ -#!/usr/bin/env bash - -# The structure behind this script is based on the following snippet: -# @source: https://github.com/nickjj/docker-flask-example/blob/main/run - -set -eo pipefail - -# --------------------------------------------------------------------- -# Variables -# --------------------------------------------------------------------- - -_PATH_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)" -_PATH_DOCKER="$_PATH_DIR/.docker" - -# If we're running in CI we need to disable TTY allocation for docker-compose -# commands that enable it by default, such as exec and run. -TTY="" -if [[ ! -t 1 ]]; then - TTY="-T" -fi - -# --------------------------------------------------------------------- -# Environment -# --------------------------------------------------------------------- - -# import env file -if [[ -f "${_PATH_DIR}/.env" ]]; then - # shellcheck source=.env - source "${_PATH_DIR}/.env" - else - echo "Warning! No .env file found, please create one." - exit 1; -fi - -# ----------------------------------------------------------------------------- -# Helper functions start with _ and aren't listed in this script's help menu. -# ----------------------------------------------------------------------------- - -# Custom docker-compose command -_dc() { - docker compose -p "$PROJECT" -f "$_PATH_DOCKER/compose.yml" --env-file "$_PATH_DIR/.env" ${TTY} "$@" -} - -# Check non-existent arguments - _isCommand() { - if ! type "$1" >/dev/null 2>&1; then - echo "Warning! Command not found: $1" - exit 1; - fi; -} - -# Check if docker is installed -_checkDocker() { - if ! command -v docker &> /dev/null; then - echo "Error: Docker not found" - exit 1; - fi; -} - -# Check if docker compose is installed -_checkDockerCompose() { - # check if docker compose is installed - if ! command -v docker compose >/dev/null 2>&1; then - - # check if docker-compose is installed - if ! command -v docker-compose >/dev/null 2>&1; then - echo "Error: Docker Compose not found" - exit 1; - fi; - - # if docker-compose is installed, suggest to install docker compose v2 - echo "Error: this stack requires Docker Compose v2, please install it and try again" - exit 1; - fi; -} - -# ----------------------------------------------------------------------------- -# Actions -# ----------------------------------------------------------------------------- - -# Build the project -build() { - _dc build -} - -# Remove all containers, images, and volumes associated with the project -clean() { - _dc down --rmi all -v --remove-orphans -} - -# Get an help message -help() { - printf "%s [args]\n\nTasks:\n" "${0}" - compgen -A function | grep -v "^_" | cat -n - printf "\nExtended help:\n Each task has comments for general usage\n" -} - -# Run the project -start() { - _dc up -d -} - -# Stop the project -stop() { - _dc stop -} - -# Restart the project -restart() { - _dc restart -} - -# --------------------------------------------------------------------- -# Run the Project -# --------------------------------------------------------------------- - -# !IMPORTANT -# Execute the action passed as the first argument -_checkDocker; \ -_checkDockerCompose; \ -_isCommand "${1:-help}"; \ -# This idea is heavily inspired by: https://github.com/adriancooney/Taskfile -TIMEFORMAT=$'\nProcess completed in %3lR'; \ -time "${@:-help}"; diff --git a/run b/run new file mode 100755 index 0000000..b2a5504 --- /dev/null +++ b/run @@ -0,0 +1,143 @@ +#!/usr/bin/env bash + +################################################################################ + +# WARNING: Please make this shell not working-directory dependant, for example +# instead of using 'ls blabla', use 'ls "${STACK_DIR}/blabla"' +# +# WARNING: Don't use "cd" in this shell, use it in a subshell instead, +# for example ( cd blabla && do_blabla ) or $( cd .. && do_blabla ) + +################################################################################ + +# --------------------------------------------------------------------- +# Variables +# --------------------------------------------------------------------- + +# Check on OS type for stack directory +# Mac OS +if [[ "$(uname)" == "Darwin" ]]; then + # check if greadlink is installed + if ! command -v greadlink &> /dev/null; then + echo "greadlink could not be found, please install it with 'brew install coreutils'"; exit 1 + fi + readonly STACK_DIR="$(dirname "$(greadlink -m "${0}")")" +# Windows +elif [[ "$(expr substr "$(uname -s)" 1 10)" == "MINGW64_NT" ]]; then + readonly STACK_DIR="$(dirname "$(readlink -m "${0}")")" +# Linux +else + readonly STACK_DIR="$(dirname "$(readlink -m "${0}")")" +fi +source "${STACK_DIR}/bin/lib/lib-core.sh" + +# --------------------------------------------------------------------- +# Import Libraries +# --------------------------------------------------------------------- + +importLib "lib/lib-helpify.sh" +importLib "lib/lib-messages.sh" + +importLib "lib/lib-services.sh" +importLib "lib/lib-commands.sh" + + +# ----------------------------------------------------------------------------- +# Helpers +# ----------------------------------------------------------------------------- + +usage () { + # show usage for all commands + need_usage4help="true" + + # add logo + logo + # add version info (use function getVersion) + versionInfo + + echo; + + helpify_title "" "[OPTIONS...] COMMAND [OPTIONS...]" + helpify_subtitle "It allows you to easily orchestrate the development stack for GDRCD"; echo; + helpify_subtitle "OPTIONS:" + helpify "-h, --help" "Show this help" + helpify_separator + + # + usageCommands + + # + usageSubDirsCommands +} + +usageService () { + # show usage for service commands + need_help="true"; + need_usage4help="true" + + # + usageServiceCommands "${1}" + + # force exit + exit 0; +} + +# ----------------------------------------------------------------------------- +# Main +# ----------------------------------------------------------------------------- + +# Get environment variables +importEnv + +# Check if the version is up to date +checkVersion + +# If no arguments are passed, show usage +if [[ $# -eq 0 ]]; then + need_help="true"; +fi + +# Parse arguments +while [[ $# -gt 0 ]]; do + case "$1" in + -h|--help) + need_help="true"; shift ;; + -*|--*) + prompt -e "Error! Unrecognized stack option: ${1}"; + has_any_error="true"; shift ;; + *) + # if is a valid command, run it + if isCommand "$1" ; then + # shellcheck source=bin + source "${COMMANDS_DIR}/$1"; + exit 0; + # otherwise, check if is a valid sub-command + elif isSubCommand "$1"; then + # check if there is a second argument + if [[ $# -gt 1 ]]; then + # if is a valid command, run it + if isCommand --abs-path "${COMMANDS_DIR}/$1/$2"; then + # shellcheck source=bin + source "${COMMANDS_DIR}/$1/$2"; + exit 0; + # otherwise, if there is no other entries, show error + else + prompt -e "Error! Unrecognized $1 command: ${2}"; has_any_error="true"; + break; + fi + else + usageService "$1" || exit 1 + fi + # otherwise, if there is no other entries, show error + else + prompt -e "Error! Unrecognized stack command: ${*}"; has_any_error="true"; + break; + fi + shift ;; + esac +done + +# Execute post argument parsing checks +finalize_argument_parsing + +echo; \ No newline at end of file diff --git a/sample.env b/sample.env index 03c462c..b05e49e 100644 --- a/sample.env +++ b/sample.env @@ -2,13 +2,6 @@ # Set project name PROJECT= -## PHP -# Set PHP version (php56/php74/php8) -PHP_VERSION= -# UID Configuration for the PHP user -# by default, the user is "www-data" and the group is "www-data" -PHP_UID=1000 - ## SERVICES # Port configuration for nginx hosted services SERVICE_PORT= @@ -16,12 +9,17 @@ SERVICE_PORT= PMA_PORT= # Port configuration for mailhog MAILHOG_PORT= - -## DATABASE # Port configuration DB_PORT= -## DATABASE > MySQL SETTINGS +## PHP +# Set PHP version (php56/php74/php8) +PHP_VERSION= +# UID Configuration for the PHP user +# by default, the user is "www-data" and the group is "www-data" +PHP_UID=1000 + +## DATABASE SETTINGS # MySQL root password MYSQL_ROOT_PASSWORD= # MySQL user settings diff --git a/version b/version new file mode 100644 index 0000000..6eaf894 --- /dev/null +++ b/version @@ -0,0 +1 @@ +v2.0.0 \ No newline at end of file diff --git a/service/.gitkeep b/www/.gitkeep similarity index 100% rename from service/.gitkeep rename to www/.gitkeep