-
Notifications
You must be signed in to change notification settings - Fork 19
/
standalone
executable file
·150 lines (111 loc) · 4.23 KB
/
standalone
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
read -r -d '' usage <<EOF
Convenience script to run code on the standalone container's sub-instances.
Automatically syncs the codebase to the standalone container before each
command.
Usage:
$0 <name_for_standalone_group> <compose_command_to_run>
$0 <name_for_standalone_group> sync # Copies the codebase to the standalone container
$0 <name_for_standalone_group> setup # Brings up the container and runs commands to create a superuser, seed the database, etc.
Examples:
$0 lint up
$0 lint sync # Copies the codebase to the standalone container
$0 lint run web npm run lint:check
$0 lint run web manage.py /code/crt_portal/manage.py check
Environment:
You can set optional environment variables to change ports and profiles:
WEB_PORT=8002 JUPYTER_PORT=8004 ./standalone cake up
Recognized variables include:
COMPOSE_PROFILES
WEB_PORT
DATABASE_PORT
JUPYTER_PORT
EOF
# Echo usage and exit if less than two args are present:
if [ "$#" -lt 2 ]; then
echo "$usage"
exit 1
fi
project_suffix="$1"
command_to_run="$2"
shift 2
compose_files="-f docker-compose.yml -f docker-compose.standalone.yml"
if command -v netstat >/dev/null && \
[[ -z "$WEB_PORT" ]] || [[ -z "$JUPYTER_PORT" ]] || [[ -z "$DATABASE_PORT" ]]; then
echo "(Ports currently in use:)"
netstat -natp tcp | grep "\*\." | sort 2>/dev/null
fi
if [[ -z "$WEB_PORT" ]]; then
read -p "Enter a port for the web server [8010]: "
WEB_PORT="${REPLY:-8010}"
fi
if [[ -z "$JUPYTER_PORT" ]]; then
read -p "Enter a port for Jupyter [8011]: "
JUPYTER_PORT="${REPLY:-8011}"
fi
if [[ -z "$DATABASE_PORT" ]]; then
read -p "Enter a port for the database (leave blank to not bind one): "
DATABASE_PORT="$REPLY"
fi
function standalone_compose() {
# Run in a subshell so as to not pollute environment variables.
(
export COMPOSE_PROJECT_NAME="crt-portal-standalone-$project_suffix";
export COMPOSE_PROFILES="${COMPOSE_PROFILES:-tests,not-tests}";
export WEB_PORT="$WEB_PORT";
export DATABASE_PORT="$DATABASE_PORT";
export JUPYTER_PORT="$JUPYTER_PORT";
docker compose $compose_files $@
)
return $?
}
function wait_for_migrations {
until standalone_compose run --rm web python /code/crt_portal/manage.py migrate --check > /dev/null 2>&1
do
sleep 10
echo 'Waiting for migrations to be applied, please hold for a few minutes...'
done
echo "All migrations have been applied, continuing!"
}
# Create the env file if it doesn't exist.
touch ".crt-portal-standalone-$project_suffix.env"
if [[ "$command_to_run" == "sync" ]]; then
echo "Copying codebase to $project_suffix:web and $project_suffix:jupyter"
standalone_compose cp . web:/code/ $@
standalone_compose cp ./jupyterhub jupyter:/srv/jupyterhub $@
standalone_compose down
standalone_compose up -d
exit $?
fi
if [[ "$command_to_run" == "setup" ]]; then
echo "Bringing up and configuring $project_suffix, bear with me..."
standalone_compose up -d
wait_for_migrations
read -p "Create superuser [y/N]? "
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
standalone_compose run --rm web python /code/crt_portal/manage.py createsuperuser
fi
read -p "Seed the database (100 records) [y/N]? "
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
standalone_compose run --rm web python /code/crt_portal/manage.py create_mock_reports 100
fi
read -p "Configure Jupyter oauth (and restart jupyter) [y/N]? "
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
standalone_compose run --rm web python /code/crt_portal/manage.py create_jupyter_oauth --port "$JUPYTER_PORT" >> ".crt-portal-standalone-$project_suffix.env"
standalone_compose stop jupyter && standalone_compose up -d jupyter
fi
read -r -d '' setup_complete <<EOF
Group $project_suffix has been set up!
Postgres URI, if a database port was specified:
- DB: postgres://postgres:passwordfortests@localhost:$DATABASE_PORT/postgres
Quick URLs:
- Landing: http://localhost:$WEB_PORT
- Intake: http://localhost:$WEB_PORT/form/view
- Admin: http://localhost:$WEB_PORT/admin
- Jupyter: http://localhost:$JUPYTER_PORT
EOF
echo "$setup_complete"
exit 0
fi
printf "Note: Standalone instances do not stay in sync with the local codebase. Run $0 $project_suffix sync to update it, if needed.\n\n"
standalone_compose $command_to_run $@