-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmutagen
executable file
·205 lines (168 loc) · 4.91 KB
/
mutagen
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env bash
## Create and manage a Docksal project with Mutagen (for help: fin mutagen)
VERSION=0.2.2
if [ "$TERM" != "dumb" ]; then
red='\033[0;91m'
green='\033[0;32m'
yellow='\033[0;33m'
NC='\033[0m'
fi
mutagen > /dev/null 2>&1 || { echo -e "${red}Error${NC}${yellow}: Mutagen not installed${NC}"; exit 1; }
fin docker ps > /dev/null 2>&1 || { echo -e "${red}Error${NC}${yellow}: Docker not running${NC}"; exit 1; }
start() {
if fin config env > /dev/null 2>&1 ; then
if [[ -z $(get_fin_variable "MUTAGEN") ]]; then
new_project=false
echo -e "${red}WARNING${NC}${yellow}: A Docksal project already exists.${NC}"
read -rp "You can start Mutagen project after project containers reset
which may produce data loss (ie. database tables).
Do you want to proceed: [y/n] " proceed
if [ "$proceed" == y ] || [ "$proceed" == Y ] ; then
init $new_project
else
echo 'Aborting'
return
fi
fi
else
init true
fi
project_root=$(get_fin_variable "PROJECT_ROOT")
cd "${project_root//\"/}" || { echo "Cannot start"; exit 1; }
if [ -z $new_project ]; then
fin "$1"
fi
mutagen project start
}
stop() {
project_root=$(get_fin_variable "PROJECT_ROOT")
cd "${project_root//\"/}" || { echo "Cannot stop"; exit 1; }
mutagen project terminate
if [ -z "$1" ]; then
fin rm cli
fi
fin stop
}
restart() {
stop "$1"
start start
}
init() {
if [ "$1" == true ]; then
fin config generate
fi
set_fin_variable "MUTAGEN" 1
set_fin_variable "PROJECT_ROOT" "$(fin config | grep PROJECT_ROOT | sed 's/PROJECT_ROOT: //')"
set_fin_variable "PROJECT_NAME" "$(fin config | grep COMPOSE_PROJECT_NAME_SAFE | sed 's/COMPOSE_PROJECT_NAME_SAFE: //')"
set_fin_variable "DOCKSAL_VOLUMES" "none"
create_mutagen_config
if [ "$1" == false ]; then
fin rm
fin up
fi
}
create_mutagen_config() {
project_root=$(get_fin_variable "PROJECT_ROOT")
project_root="${project_root//\"/}"
if [ ! -f "$project_root/mutagen.yml" ]; then
project_name=$(get_fin_variable "PROJECT_NAME")
project_name="${project_name//\"/}"
cd "$project_root" || { echo "Cannot create a mutagen.yml configuration in $project_root"; exit 1; }
beta="docker://docker@$project_name""_cli_1/var/www"
echo "sync:
defaults:
flushOnCreate: true
ignore:
vcs: true
permissions:
defaultFileMode: 644
defaultDirectoryMode: 755
${project_name}-cli:
alpha: './'
beta: '$beta'
mode: 'two-way-resolved'" > mutagen.yml
echo 'mutagen.yml configuration created' in "$project_root"
fi
}
update() {
latest_release=$(curl --silent "https://api.github.com/repos/nicoschi/mutagen-for-docksal/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [ "$latest_release" != "$VERSION" ]; then
echo -e "${yellow}New release found ($latest_release):
Do you want to proceed with the update:${NC} [y/n] "
read -rp "" proceed
if [ "$proceed" == y ] || [ "$proceed" == Y ] ; then
echo -e "${yellow}Overriding mutagen command in ~/.docksal/commands/${NC}"
curl "https://raw.githubusercontent.com/nicoschi/mutagen-for-docksal/$latest_release/mutagen" > ~/.docksal/commands/mutagen
echo -e "
${green}Update done. 'fin mutagen' is now at version $latest_release ${NC}"
else
echo 'Aborting'
return
fi
else
echo -e "${green}Latest release ($VERSION) is already installed${NC}"
fi
}
version() {
echo -e "$VERSION"
}
usage() {
echo '
Usage: fin mutagen [command]
Commands:
start | up Start / initialize and start / convert and start a Docksal project alongside with a Mutagen project
stop [options] Stop Docksal project and terminate Mutagen project (if already existing)
-nr Do not remove cli container (automatically recreated on start)
restart [options] Restart Docksal project and Mutagen project (if already existing)
-nr Do not remove cli container (automatically recreated on start)
version Print "fin mutagen" version
update Check for updates and update to a new release if exists and you want'
}
get_fin_variable() {
variable=$(fin config get --env=local "$1" 2> /dev/null)
if [ -z "$variable" ]; then
fin config get "$1" 2> /dev/null
else
echo "$variable"
fi
}
set_fin_variable() {
fin config set --env=local "$1"="$2"
}
check_options() {
if [[ -z $(get_fin_variable "MUTAGEN") ]] && { [ "$1" == 'stop' ] || [ "$1" == 'restart' ]; }; then
usage
exit 1
fi
if [[ "$1" == 'start' || "$1" == 'up' ]] && [ "$2" ]; then
usage
exit 1
fi
if [ "$2" ] && [ "$2" != '-nr' ]; then
usage
exit 1
fi
}
case $1 in
"start"|"up")
check_options "$@"
start "$1"
;;
"stop")
check_options "$@"
stop "$2"
;;
"restart")
check_options "$@"
restart "$2"
;;
"update")
update
;;
"version")
version
;;
*)
usage
;;
esac