-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphx-docker-compose-new.sh
executable file
·183 lines (137 loc) · 3.72 KB
/
phx-docker-compose-new.sh
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/bin/sh
#
# Creates a new Phoenix project and development environment with Docker Compose.
#
# It expects the path of the project as an argument.
#
# $ mix-docker-compose-new PATH [--module MODULE] [--app APP]
#
# For possible options, see Phoenix documentation at https://hexdocs.pm/phoenix/Mix.Tasks.Phx.New.html
#
set -eu
if echo "$*" | grep -q "verbose"; then
set -x
fi
## validation
# ensure that arguments are provided
if [ $# -eq 0 ]; then
echo '
Creates a new Phoenix project.
It expects the path of the project as an argument.
$ mix-docker-compose-new PATH [--module MODULE] [--app APP]
A project at the given PATH will be created. The application name and module
name will be retrieved from the path, unless --module or --app is given.
'
exit 1
fi
# docker and docker compose are required
docker --version
docker compose version
## calculate necessary values
APP_NAME="$(basename "$1")"
# underscores aren't valid characters in DNS hostnames
APP_NODE_NAME="$(echo "$APP_NAME" | tr _ -)"
## check paths
CALLER_DIR="$(pwd)"
SCRIPT_DIR="$(
cd -- "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
# ensure that app directory is an absolute path
case "$1" in
/*)
# already absolute path
APP_DIR="$1"
;;
*)
APP_DIR="$CALLER_DIR/$APP_NAME"
;;
esac
if [ -d "$APP_DIR" ]; then
echo "$APP_DIR already exists."
printf "Overwrite (y/N)? "
read -r option
case "$option" in
y | Y)
rm -rf "$APP_DIR"
;;
*)
echo "Please select another directory for installation."
exit 1
;;
esac
fi
echo "script location: $SCRIPT_DIR"
echo "app directory: $APP_DIR"
echo "app project name: $APP_NAME"
echo "app node name: $APP_NODE_NAME"
echo "docker context: $(docker context show)"
if docker context show | grep -iq rootless; then
echo "warning: rootless mode might cause file access issues"
fi
## build image
IMAGE_NAME="phx-docker-compose-new"
docker build -t "$IMAGE_NAME" \
--build-arg GID="$(id -g)" \
--build-arg UID="$(id -u)" \
--build-arg UNAME="$(uname)" \
--build-arg USER="$USER" \
"$SCRIPT_DIR/templates"
## generate phoenix app
docker run \
--rm \
--mount type=bind,source="$CALLER_DIR",target=/app \
--workdir /app \
"$IMAGE_NAME" \
bash -c "mix archive.install --force hex phx_new && mix phx.new $*"
## set up phoenix app
sed_i() {
if [ "$(uname)" = "Darwin" ]; then
sed -i '' "$1" "$2"
else
sed -i "$1" "$2"
fi
}
(
cd "$APP_DIR"
# copy files from templates
cp "$SCRIPT_DIR/templates/Dockerfile" .
cp "$SCRIPT_DIR/templates/docker-compose.yml" .
cp -f "$SCRIPT_DIR/templates/README.md" .
cp -r "$SCRIPT_DIR/templates/bin" ./bin
# app name and node name are determined based on the user input
sed_i "s/__DECLARE_APP_NAME__/$APP_NAME/" ./bin/bootstrap
sed_i "s/__DECLARE_APP_NODE_NAME__/$APP_NODE_NAME/" ./bin/bootstrap
# prepare .gitignore
echo ".env*" >>.gitignore
# make a git commit with default phoenix app
if command -v git >/dev/null; then
git init
git add .
git commit -m "initial commit"
fi
# adjust phoenix config files so we can connect to app and db containers
sed_i 's/hostname: "localhost"/hostname: "db"/' ./config/dev.exs
sed_i 's/hostname: "localhost"/hostname: "db"/' ./config/test.exs
sed_i 's/ip: {127, 0, 0, 1}/ip: {0, 0, 0, 0}/' ./config/dev.exs
# run commands
bin/bootstrap
bin/setup
bin/logs
bin/test
bin/stop
)
echo "
$APP_NAME has been successsfully generated at $APP_DIR 🎉🎉🎉
$ cd $APP_NAME
Load environment variables with:
$ . ./.env
Start your Phoenix app with:
$ bin/start
Open your Phoenix app from a browser at:
http://localhost:4000/
Open Phoenix LiveDashboard at:
http://localhost:4000/dev/dashboard/
Open Livebook at:
http://localhost:8080/
"