|
1 | | -#!/usr/bin/env bash |
| 1 | +#!/bin/bash |
2 | 2 |
|
3 | | -set -euo pipefail |
| 3 | +set -e # Stop script on error |
| 4 | +set -a # Automatically export all variables |
4 | 5 |
|
5 | | -# Colors for output |
6 | | -RED='\033[0;31m' |
7 | | -GREEN='\033[0;32m' |
8 | | -YELLOW='\033[1;33m' |
9 | | -NC='\033[0m' # No Color |
10 | | - |
11 | | -# Function to print colored output |
12 | | -print_status() { |
13 | | - echo -e "${GREEN}[INFO]${NC} $1" |
14 | | -} |
15 | | - |
16 | | -print_warning() { |
17 | | - echo -e "${YELLOW}[WARNING]${NC} $1" |
18 | | -} |
19 | | - |
20 | | -print_error() { |
21 | | - echo -e "${RED}[ERROR]${NC} $1" |
22 | | -} |
23 | 6 |
|
24 | 7 | # Determine script's directory |
25 | 8 | SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
|
32 | 15 |
|
33 | 16 | # --- Main SocialPredict Functionality --- |
34 | 17 |
|
35 | | -# Function to check if a command exists |
36 | | -command_exists() { |
37 | | - command -v "$1" &> /dev/null 2>&1 |
38 | | -} |
| 18 | +# Ensure .env file exists before resolving path |
| 19 | +if [ ! -f "$SCRIPT_DIR/.env" ]; then |
| 20 | + echo ".env file not found." |
39 | 21 |
|
40 | | -# Initial Checks |
41 | | -# Check if docker compose is installed. Fails if not found. |
42 | | -# Check if .env file exists. Fails if not found. |
43 | | -check_docker() { |
44 | | - print_status "Checking that docker compose is installed..." |
45 | | - if command_exists docker && docker compose version &> /dev/null; then |
46 | | - print_status "Found docker compose." |
47 | | - elif command_exists docker-compose; then |
48 | | - print_warning "Found docker-compose V1. Please update to V2." |
49 | | - print_warning "https://docs.docker.com/compose/migrate/" |
50 | | - exit 1 |
| 22 | + if [ -f "$SCRIPT_DIR/scripts/dev/env_writer_dev.sh" ]; then |
| 23 | + echo "Initializing .env file using env_writer_dev.sh (first-run only)..." |
| 24 | + export CALLED_FROM_SOCIALPREDICT=yes |
| 25 | + SCRIPT_INTERACTIVE=true source "$SCRIPT_DIR/scripts/dev/env_writer_dev.sh" |
| 26 | + unset CALLED_FROM_SOCIALPREDICT |
51 | 27 | else |
52 | | - print_error "Docker Compose is not installed." |
| 28 | + echo "Cannot continue. .env file is missing and env_writer_dev.sh was not found." |
53 | 29 | exit 1 |
54 | 30 | fi |
| 31 | +fi |
| 32 | + |
| 33 | +# Calculate the absolute path to the .env file |
| 34 | +ENV_PATH="$( readlink -f "$SCRIPT_DIR/.env" )" |
| 35 | +ENV_FILE="--env-file $ENV_PATH" |
| 36 | + |
| 37 | +# Function to check if a command exists |
| 38 | +command_exists() { |
| 39 | + command -v "$1" &> /dev/null 2>&1 |
55 | 40 | } |
56 | 41 |
|
57 | | -init() { |
58 | | - check_docker |
59 | | - if [ ! -f "$SCRIPT_DIR/.env" ]; then |
60 | | - print_status "Looks like this is the first time running SocialPredict." |
61 | | - print_warning "Please run './SocialPredict install' to initialize the application." |
62 | | - exit 1 |
| 42 | +# Function to check if Docker && Docker Compose are installed |
| 43 | +docker_check() { |
| 44 | + if command_exists docker && docker compose version &> /dev/null; then |
| 45 | + echo "Found docker compose." |
| 46 | + COMPOSE='docker compose' |
| 47 | + elif command_exists docker-compose; then |
| 48 | + echo "Error: Found docker-compose V1. Please update to V2." |
| 49 | + echo "https://docs.docker.com/compose/migrate/" |
| 50 | + exit 1 |
| 51 | + |
| 52 | + else |
| 53 | + echo "Error: Docker Compose is not installed." |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | +} |
| 57 | + |
| 58 | +# Function to Source the .env file |
| 59 | +source_env() { |
| 60 | + if [ -f "${SCRIPT_DIR}/.env" ]; then |
| 61 | + source "${SCRIPT_DIR}/.env" |
63 | 62 | else |
64 | | - print_status "Loading configuration from .env file" |
65 | | - source "$SCRIPT_DIR/.env" |
| 63 | + echo "Error: .env file not found." |
| 64 | + exit 1 |
66 | 65 | fi |
67 | 66 | } |
68 | 67 |
|
69 | | -print_help() { |
70 | | - cat <<EOF |
71 | | -Usage: ./SocialPredict COMMAND |
72 | | -
|
73 | | -Commands: |
74 | | - install Initialize SocialPredict |
75 | | - up Start SocialPredict containers |
76 | | - down Stop SocialPredict containers |
77 | | - exec Execute command on SocialPredict containers |
78 | | - backup Backup operations on SocialPredict |
| 68 | +if [ "$1" = "install" ]; then |
| 69 | + # Echo initial message |
| 70 | + echo "### Building and Deploying SocialPredict ..." |
| 71 | + echo |
| 72 | + sleep 1; |
| 73 | + |
| 74 | + # Check that docker is installed |
| 75 | + echo "### Checking that docker compose is installed ..." |
| 76 | + docker_check |
| 77 | + echo |
| 78 | + sleep 1; |
| 79 | + |
| 80 | + # Ask user input for Application Environment |
| 81 | + echo "### Select Application Environment: " |
| 82 | + PS3='Please enter your choice: ' |
| 83 | + options=("Development" "Localhost" "Production" "Quit") |
| 84 | + select opt in "${options[@]}" |
| 85 | + do |
| 86 | + case $opt in |
| 87 | + "Development") |
| 88 | + echo |
| 89 | + echo "Building for Development" |
| 90 | + echo |
| 91 | + export CALLED_FROM_SOCIALPREDICT=yes |
| 92 | + source "$SCRIPT_DIR/scripts/dev.sh" |
| 93 | + unset CALLED_FROM_SOCIALPREDICT |
| 94 | + break |
| 95 | + ;; |
| 96 | + "Localhost") |
| 97 | + echo |
| 98 | + echo "Building for Localhost" |
| 99 | + echo |
| 100 | + export CALLED_FROM_SOCIALPREDICT=yes |
| 101 | + source "$SCRIPT_DIR/scripts/localhost.sh" |
| 102 | + unset CALLED_FROM_SOCIALPREDICT |
| 103 | + break |
| 104 | + ;; |
| 105 | + "Production") |
| 106 | + echo |
| 107 | + echo "Building for Production" |
| 108 | + echo |
| 109 | + export CALLED_FROM_SOCIALPREDICT=yes |
| 110 | + source "$SCRIPT_DIR/scripts/prod.sh" |
| 111 | + unset CALLED_FROM_SOCIALPREDICT |
| 112 | + break |
| 113 | + ;; |
| 114 | + "Quit") |
| 115 | + break |
| 116 | + ;; |
| 117 | + *) echo "Invalid option $REPLY";; |
| 118 | + esac |
| 119 | + done |
| 120 | +fi |
79 | 121 |
|
80 | | -Run './SocialPredict COMMAND --help' for more information on a command. |
| 122 | +# Run Docker Exec |
| 123 | +if [ "$1" = "exec" ]; then |
| 124 | + source_env |
| 125 | + export CALLED_FROM_SOCIALPREDICT=yes |
| 126 | + if [ -z "$3" ]; then |
| 127 | + source "$SCRIPT_DIR/scripts/docker-commands.sh" exec "$2" |
| 128 | + else |
| 129 | + source "$SCRIPT_DIR/scripts/docker-commands.sh" exec "$2" "$3" |
| 130 | + fi |
| 131 | + unset CALLED_FROM_SOCIALPREDICT |
| 132 | +fi |
81 | 133 |
|
82 | | -For more help on how to use SocialPredict, head to https://github.com/openpredictionmarkets/socialpredict/ |
83 | | -EOF |
84 | | -} |
| 134 | +# Run docker compose up |
| 135 | +if [ "$1" = "up" ]; then |
| 136 | + source_env |
| 137 | + export CALLED_FROM_SOCIALPREDICT=yes |
| 138 | + source "$SCRIPT_DIR/scripts/docker-commands.sh" up |
| 139 | + unset CALLED_FROM_SOCIALPREDICT |
| 140 | +fi |
85 | 141 |
|
86 | | -COMMAND="${1:-"--help"}" |
87 | | -case "$COMMAND" in |
88 | | - install) |
89 | | - check_docker |
90 | | - shift |
91 | | - export CALLED_FROM_SOCIALPREDICT=yes |
92 | | - source "${SCRIPT_DIR}/scripts/install.sh" |
93 | | - unset CALLED_FROM_SOCIALPREDICT |
94 | | - ;; |
95 | | - up) |
96 | | - init |
97 | | - export CALLED_FROM_SOCIALPREDICT=yes |
98 | | - source "${SCRIPT_DIR}/scripts/docker-commands.sh" up |
| 142 | +# Run docker compose down |
| 143 | +if [ "$1" = "down" ]; then |
| 144 | + source_env |
| 145 | + export CALLED_FROM_SOCIALPREDICT=yes |
| 146 | + source "$SCRIPT_DIR/scripts/docker-commands.sh" down |
99 | 147 | unset CALLED_FROM_SOCIALPREDICT |
100 | | - ;; |
101 | | - down) |
102 | | - init |
103 | | - export CALLED_FROM_SOCIALPREDICT=yes |
104 | | - source "${SCRIPT_DIR}/scripts/docker-commands.sh" down |
105 | | - unset CALLED_FROM_SOCIALPREDICT |
106 | | - ;; |
107 | | - exec) |
108 | | - init |
109 | | - export CALLED_FROM_SOCIALPREDICT=yes |
110 | | - shift |
111 | | - source "${SCRIPT_DIR}/scripts/docker-commands.sh" exec "$@" |
112 | | - unset CALLED_FROM_SOCIALPREDICT |
113 | | - ;; |
114 | | - backup) |
115 | | - init |
116 | | - export CALLED_FROM_SOCIALPREDICT=yes |
117 | | - shift |
118 | | - # Pass remaining args to the backup script, e.g. --save / --list / --restore <file> |
119 | | - source "${SCRIPT_DIR}/scripts/backup/db_backup.sh" "$@" |
120 | | - unset CALLED_FROM_SOCIALPREDICT |
121 | | - ;; |
122 | | - --help|-h|help) |
123 | | - print_help |
124 | | - ;; |
125 | | - *) |
126 | | - echo "Unknown command: $COMMAND" |
127 | | - echo |
128 | | - print_help |
129 | | - exit 1 |
130 | | - ;; |
131 | | - esac |
| 148 | +fi |
| 149 | + |
| 150 | +# Database backup/restore |
| 151 | +if [ "$1" = "backup" ]; then |
| 152 | + source_env |
| 153 | + export CALLED_FROM_SOCIALPREDICT=yes |
| 154 | + shift |
| 155 | + # Pass remaining args to the backup script, e.g. --save / --list / --restore <file> / --restore-latest |
| 156 | + source "$SCRIPT_DIR/scripts/backup/db_backup.sh" "$@" |
| 157 | + unset CALLED_FROM_SOCIALPREDICT |
| 158 | +fi |
0 commit comments