├── setup_database.sh ├── validate_functions.sh ├── README.md ├── LICENSE ├── create_docker_environment.sh └── setup.sh /setup_database.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | APP_NAME=$1 4 | PROJECT_DIR=$(pwd)/../$APP_NAME 5 | 6 | cat > ${PROJECT_DIR}/config/database.yml < 11 | username: <%= ENV['POSTGRES_USER'] || 'postgres' %> 12 | password: <%= ENV['POSTGRES_PASSWORD'] || 'password' %> 13 | host: <%= ENV['DB_HOST'] || 'db' %> 14 | port: <%= ENV['DB_PORT'] || 5432 %> 15 | 16 | development: 17 | <<: *default 18 | database: ${APP_NAME}_development 19 | 20 | test: 21 | <<: *default 22 | database: ${APP_NAME}_test 23 | 24 | production: 25 | <<: *default 26 | database: ${APP_NAME}_production 27 | 28 | EOF -------------------------------------------------------------------------------- /validate_functions.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | validate_version() { 4 | local version="$1" 5 | if ! [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then 6 | echo "Error: Format of version number is invalid. Please use the format x.y.z" 7 | exit 1 8 | fi 9 | } 10 | 11 | validate_app_name() { 12 | local app_name="$1" 13 | clean_name=$(echo "$app_name" | tr -cd '[:alnum:]_') 14 | if [[ "$clean_name" != "$app_name" ]]; then 15 | echo "Error: The application name can only contain letters, numbers, and underscores." 16 | exit 1 17 | fi 18 | if ! [[ "$app_name" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then 19 | echo "Error: The application name must start with a letter and can only contain letters, numbers, and underscores." 20 | exit 1 21 | fi 22 | } 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rails Docker Setup Script 2 | 3 | ## Description 4 | This script automates the setup of a new Rails API project using Docker. It configures Ruby, Rails, and all necessary Docker settings to get a new Rails application up and running quickly. 5 | 6 | ## Features 7 | - Validates user input for Ruby and Rails versions. 8 | - Generates a new Rails application with Docker configuration. 9 | - Automatically creates and configures PostgreSQL as the database. 10 | 11 | ## Usage 12 | To use this script, follow these steps: 13 | 14 | 1. Ensure Docker and Docker Compose are installed on your system. 15 | 2. Download or clone this repository to your local machine. 16 | 3. Run the script: 17 | ```bash 18 | bash setup.sh 19 | 20 | ## Demo 21 | https://github.com/igpires/rails_docker_wizard/assets/54286685/fe4a0e35-b131-44cd-9e3d-1792af912c87 22 | 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Igor Pires 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /create_docker_environment.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | APP_NAME=$1 4 | RUBY_VERSION=$2 5 | PROJECT_DIR=$(pwd)/../$APP_NAME 6 | 7 | # Create dockerfile 8 | cat > ${PROJECT_DIR}/Dockerfile < ${PROJECT_DIR}/docker-compose.yml < /dev/null; then 5 | echo "Error: Docker is not installed. Please install Docker and try again." 6 | exit 1 7 | fi 8 | 9 | if ! command -v docker-compose &> /dev/null; then 10 | echo "Error: Docker Compose is not installed. Please install Docker Compose and try again." 11 | exit 1 12 | fi 13 | 14 | 15 | echo "Rails Docker Setup Wizard v1.0.2" 16 | 17 | read -p "Enter your Ruby version or press enter to use the default (3.3.0): " RUBY_VERSION 18 | RUBY_VERSION=${RUBY_VERSION:-"3.3.0"} 19 | validate_version "$RUBY_VERSION" 20 | 21 | clear 22 | 23 | read -p "Enter your Rails version or press enter to use the default (7.1.0): " RAILS_VERSION 24 | RAILS_VERSION=${RAILS_VERSION:-"7.1.0"} 25 | validate_version "$RAILS_VERSION" 26 | 27 | clear 28 | 29 | read -p "Enter your application name: " APP_NAME 30 | validate_app_name "$APP_NAME" 31 | 32 | clear 33 | 34 | echo "Ruby Version: $RUBY_VERSION" 35 | echo "Rails Version: $RAILS_VERSION" 36 | echo "Application Name: $APP_NAME" 37 | echo "This script will create a new Rails API project with the following configurations. Do you want to continue? (y/n)" 38 | read confirmation 39 | if [[ "$confirmation" != "y" ]]; then 40 | echo "Script aborted." 41 | exit 1 42 | fi 43 | 44 | PROJECT_DIR=$(pwd)/../$APP_NAME 45 | 46 | docker run --rm -v "$PROJECT_DIR:/app" ruby:$RUBY_VERSION bash -c " 47 | gem install rails -v '$RAILS_VERSION' && 48 | rails new /app --database=postgresql --skip-bundle --skip-git --api --skip-test --skip-system-test --skip-action-mailbox --skip-action-text --skip-active-storage --skip-action-cable --skip-sprockets --skip-spring --skip-javascript --skip-turbolinks --skip-webpack-install --skip-bootsnap" 49 | 50 | sudo chown -R $(id -u):$(id -g) "$PROJECT_DIR" 51 | 52 | echo 'creating docker environment...' 53 | bash $(dirname "$0")/create_docker_environment.sh $APP_NAME $RUBY_VERSION 54 | 55 | echo 'setting up database...' 56 | bash $(dirname "$0")/setup_database.sh $APP_NAME 57 | 58 | echo 'creating and iniliazing docker container...' 59 | docker-compose -f "$PROJECT_DIR/docker-compose.yml" up -d --build 60 | 61 | echo ' installing gems and creating database...' 62 | docker-compose -f "$PROJECT_DIR/docker-compose.yml" exec $APP_NAME bash -c "bundle install && rails db:prepare" 63 | 64 | echo "Project $APP_NAME created successfully in $PROJECT_DIR" --------------------------------------------------------------------------------