Skip to content

Commit ce497d4

Browse files
committed
Only restore settings when -c given explicitly
The previous behavior would detect if the appliance had ever had a manual config run and automatically restore settings if not. This has caused a number of problems in manual testing, some due to bugs importing settings from an 11.10.34x appliance into a v2.0 appliance and others due to settings like hostname and SSL being imported into appliances that are being set up as secondary / staging servers. It's also just been surprising that settings aren't restored on subsequent runs. Making the settings restore an option that must be given explicitly removes much of the confusion around the auto detection at the expense of a slightly smoother experience in the perfect case.
1 parent e624b6b commit ce497d4

File tree

2 files changed

+53
-81
lines changed

2 files changed

+53
-81
lines changed

bin/ghe-restore

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
#/ argument is provided, it always overrides the configured restore host.
77
#/
88
#/ Options:
9-
#/ -c Clean restore. Restores appliance settings and license
10-
#/ in addition to datastores. This is the default for
11-
#/ appliances that have not yet been configured but can be
12-
#/ forced by passing the argument explicitly.
9+
#/ -c Restore appliance settings and license in addition to
10+
#/ datastores. Settings are not restored by default to
11+
#/ prevent overwriting different configuration on the
12+
#/ restore host.
1313
#/ -s <snapshot-id> Restore from the snapshot with the given id. Available
1414
#/ snapshots may be listed under the data directory.
1515
#/ -v Enable verbose output.
@@ -31,15 +31,15 @@ GHE_RESTORE_SNAPSHOT="current"
3131
export GHE_RESTORE_SNAPSHOT
3232

3333
# Parse arguments
34-
clean_restore=false
34+
restore_settings=false
3535
while true; do
3636
case "$1" in
3737
-s)
3838
GHE_RESTORE_SNAPSHOT="$(basename "$2")"
3939
shift 2
4040
;;
4141
-c)
42-
clean_restore=true
42+
restore_settings=true
4343
shift
4444
;;
4545
-*)
@@ -55,6 +55,9 @@ done
5555
# Grab the host arg
5656
host="${1:-$GHE_RESTORE_HOST}"
5757

58+
# Hostname without any port suffix
59+
hostname=$(echo "$host" | cut -f 1 -d :)
60+
5861
# Show usage with no <host>
5962
[ -z "$host" ] && print_usage
6063

@@ -72,25 +75,29 @@ echo "Starting $GHE_BACKUP_STRATEGY restore of $host from snapshot $GHE_RESTORE_
7275
# Perform a host-check and establish the remote version in GHE_REMOTE_VERSION.
7376
ghe_remote_version_required "$host"
7477

75-
# Figure out if host has ever been configured based on the presence of the
76-
# dna.json file. If not, enable restoring settings and license in addition
77-
# to primary datastores.
78-
if ! $clean_restore &&
78+
# Verify the host has been fully configured at least once unless the -c
79+
# argument was provided.
80+
if ! $restore_settings &&
7981
! ghe-ssh "$host" -- "test -f '$GHE_REMOTE_DATA_DIR/enterprise/dna.json' -o \
8082
-f '$GHE_REMOTE_DATA_USER_DIR/common/dna.json'"; then
81-
clean_restore=true
83+
echo "Error: $hostname not configured." 1>&2
84+
echo "Please visit https://$hostname/setup/settings to configure base appliance settings before continuing." 1>&2
85+
exit 1
8286
fi
8387

8488
# Make sure the GitHub appliance is in maintenance mode and all writing
8589
# processes have bled out.
8690
ghe-maintenance-mode-enable "$host"
8791

88-
# Restore settings and license if performing a clean restore.
89-
if $clean_restore; then
92+
# Restore settings and license if requested.
93+
if $restore_settings; then
9094
ghe-restore-settings "$host"
95+
fi
9196

92-
# Make sure mysql and elasticsearch are setup and running since this is a
93-
# fresh install which hasn't had a configure run yet.
97+
# Make sure mysql and elasticsearch are prep'd and running before restoring into
98+
# appliances v2.x or greater. These services will not have been started on appliances
99+
# that have not been configured yet.
100+
if [ "$GHE_VERSION_MAJOR" -ge 2 ]; then
94101
echo "sudo ghe-service-ensure-mysql && sudo ghe-service-ensure-elasticsearch" |
95102
ghe-ssh "$host" -- /bin/sh 1>&3
96103
fi
@@ -132,4 +139,4 @@ echo "Restoring SSH host keys ..."
132139
ghe-ssh "$host" -- 'ghe-import-ssh-host-keys' < "$GHE_RESTORE_SNAPSHOT_PATH/ssh-host-keys.tar" 1>&3
133140

134141
echo "Completed restore of $host from snapshot $GHE_RESTORE_SNAPSHOT"
135-
echo "Visit https://$(echo "$host" | cut -f 1 -d :)/setup/settings in a browser to configure the recovered appliance."
142+
echo "Visit https://$hostname/setup/settings to configure the recovered appliance."

test/test-ghe-restore.sh

Lines changed: 30 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,22 @@ echo "fake license data" > "$GHE_DATA_DIR/current/enterprise.ghl"
6363
echo "fake manage password hash data" > "$GHE_DATA_DIR/current/manage-password"
6464
echo "rsync" > "$GHE_DATA_DIR/current/strategy"
6565

66-
begin_test "ghe-restore into unconfigured vm"
66+
begin_test "ghe-restore into configured vm"
6767
(
6868
set -e
6969
rm -rf "$GHE_REMOTE_DATA_DIR"
7070
setup_remote_metadata
7171

72+
# create settings file -- used to determine if instance has been configured.
73+
touch "$GHE_REMOTE_DATA_DIR/enterprise/dna.json"
74+
7275
# set restore host environ var
7376
GHE_RESTORE_HOST=127.0.0.1
7477
export GHE_RESTORE_HOST
7578

7679
# run ghe-restore and write output to file for asserting against
77-
if ! ghe-restore -v > "$TRASHDIR/restore-out" 2>&1; then
78-
cat "$TRASHDIR/restore-out"
79-
false
80-
fi
80+
ghe-restore -v > "$TRASHDIR/restore-out" 2>&1
81+
cat "$TRASHDIR/restore-out"
8182

8283
# verify connect to right host
8384
grep -q "Connect 127.0.0.1 OK" "$TRASHDIR/restore-out"
@@ -88,27 +89,10 @@ begin_test "ghe-restore into unconfigured vm"
8889
grep -q "fake ghe-export-redis data" "$TRASHDIR/restore-out"
8990
grep -q "fake ghe-export-authorized-keys data" "$TRASHDIR/restore-out"
9091
grep -q "fake ghe-export-ssh-host-keys data" "$TRASHDIR/restore-out"
91-
grep -q "fake ghe-export-settings data" "$TRASHDIR/restore-out"
9292

93-
# verify ghe-import-es-indices is run under 1.x VMs and that the
94-
# elasticsearch-legacy directory was created under 2.x VMs.
95-
if [ "$GHE_VERSION_MAJOR" -eq 1 ]; then
96-
grep -q "ghe-import-es-indices" "$TRASHDIR/restore-out"
97-
elif [ "$GHE_VERSION_MAJOR" -ge 2 ]; then
98-
test -d "$GHE_REMOTE_DATA_USER_DIR/elasticsearch-legacy"
99-
fi
100-
101-
# verify manage password was restored under v2.x or greater VMs
102-
if [ "$GHE_VERSION_MAJOR" -ge 2 ]; then
103-
test -f "$GHE_REMOTE_DATA_USER_DIR/common/manage-password"
104-
[ "$(cat "$GHE_REMOTE_DATA_USER_DIR/common/manage-password")" = "fake manage password hash data" ]
105-
fi
106-
107-
# verify service-ensure scripts were run under versions >= v2.x
108-
if [ "$GHE_VERSION_MAJOR" -ge 2 ]; then
109-
grep -q "ghe-service-ensure-mysql OK" "$TRASHDIR/restore-out"
110-
grep -q "ghe-service-ensure-elasticsearch OK" "$TRASHDIR/restore-out"
111-
fi
93+
# verify settings import was *not* run due to instance already being
94+
# configured.
95+
! grep -q "fake ghe-export-settings data" "$TRASHDIR/restore-out"
11296

11397
# verify all repository data was transferred to the restore location
11498
diff -ru "$GHE_DATA_DIR/current/repositories" "$GHE_REMOTE_DATA_USER_DIR/repositories"
@@ -126,21 +110,18 @@ begin_test "ghe-restore into unconfigured vm"
126110
)
127111
end_test
128112

129-
begin_test "ghe-restore into configured vm"
113+
begin_test "ghe-restore -c into unconfigured vm"
130114
(
131115
set -e
132116
rm -rf "$GHE_REMOTE_DATA_DIR"
133117
setup_remote_metadata
134118

135-
# create settings file -- used to determine if instance has been configured.
136-
touch "$GHE_REMOTE_DATA_DIR/enterprise/dna.json"
137-
138119
# set restore host environ var
139120
GHE_RESTORE_HOST=127.0.0.1
140121
export GHE_RESTORE_HOST
141122

142123
# run ghe-restore and write output to file for asserting against
143-
ghe-restore -v > "$TRASHDIR/restore-out" 2>&1
124+
ghe-restore -v -c > "$TRASHDIR/restore-out" 2>&1
144125
cat "$TRASHDIR/restore-out"
145126

146127
# verify connect to right host
@@ -153,9 +134,8 @@ begin_test "ghe-restore into configured vm"
153134
grep -q "fake ghe-export-authorized-keys data" "$TRASHDIR/restore-out"
154135
grep -q "fake ghe-export-ssh-host-keys data" "$TRASHDIR/restore-out"
155136

156-
# verify settings import was *not* run due to instance already being
157-
# configured.
158-
! grep -q "fake ghe-export-settings data" "$TRASHDIR/restore-out"
137+
# verify settings were imported
138+
grep -q "fake ghe-export-settings data" "$TRASHDIR/restore-out"
159139

160140
# verify all repository data was transferred to the restore location
161141
diff -ru "$GHE_DATA_DIR/current/repositories" "$GHE_REMOTE_DATA_USER_DIR/repositories"
@@ -173,49 +153,22 @@ begin_test "ghe-restore into configured vm"
173153
)
174154
end_test
175155

176-
begin_test "ghe-restore -c into configured vm"
156+
begin_test "ghe-restore into unconfigured vm"
177157
(
178158
set -e
179159
rm -rf "$GHE_REMOTE_DATA_DIR"
180160
setup_remote_metadata
181161

182-
# create settings file -- used to determine if instance has been configured.
183-
touch "$GHE_REMOTE_DATA_DIR/enterprise/dna.json"
184-
185162
# set restore host environ var
186163
GHE_RESTORE_HOST=127.0.0.1
187164
export GHE_RESTORE_HOST
188165

189166
# run ghe-restore and write output to file for asserting against
190-
ghe-restore -v -c > "$TRASHDIR/restore-out" 2>&1
191-
cat "$TRASHDIR/restore-out"
192-
193-
# verify connect to right host
194-
grep -q "Connect 127.0.0.1 OK" "$TRASHDIR/restore-out"
195-
196-
# verify all import scripts were run
197-
grep -q "alice/index.html" "$TRASHDIR/restore-out"
198-
grep -q "fake ghe-export-mysql data" "$TRASHDIR/restore-out"
199-
grep -q "fake ghe-export-redis data" "$TRASHDIR/restore-out"
200-
grep -q "fake ghe-export-authorized-keys data" "$TRASHDIR/restore-out"
201-
grep -q "fake ghe-export-ssh-host-keys data" "$TRASHDIR/restore-out"
167+
# this should fail due to the appliance being in an unconfigured state
168+
! ghe-restore -v > "$TRASHDIR/restore-out" 2>&1
202169

203-
# verify settings were imported
204-
grep -q "fake ghe-export-settings data" "$TRASHDIR/restore-out"
205-
206-
# verify all repository data was transferred to the restore location
207-
diff -ru "$GHE_DATA_DIR/current/repositories" "$GHE_REMOTE_DATA_USER_DIR/repositories"
208-
209-
# verify all pages data was transferred to the restore location
210-
diff -ru "$GHE_DATA_DIR/current/pages" "$GHE_REMOTE_DATA_USER_DIR/pages"
211-
212-
if [ "$GHE_VERSION_MAJOR" -ge 2 ]; then
213-
# verify all hookshot user data was transferred
214-
diff -ru "$GHE_DATA_DIR/current/hookshot" "$GHE_REMOTE_DATA_USER_DIR/hookshot"
215-
216-
# verify all alambic assets user data was transferred
217-
diff -ru "$GHE_DATA_DIR/current/alambic_assets" "$GHE_REMOTE_DATA_USER_DIR/alambic_assets"
218-
fi
170+
# verify that ghe-restore failed due to the appliance not being configured
171+
grep -q -e "Error: $GHE_RESTORE_HOST not configured" "$TRASHDIR/restore-out"
219172
)
220173
end_test
221174

@@ -225,6 +178,9 @@ begin_test "ghe-restore with host arg"
225178
rm -rf "$GHE_REMOTE_DATA_DIR"
226179
setup_remote_metadata
227180

181+
# create settings file -- used to determine if instance has been configured.
182+
touch "$GHE_REMOTE_DATA_DIR/enterprise/dna.json"
183+
228184
# set restore host environ var
229185
GHE_RESTORE_HOST=127.0.0.1
230186
export GHE_RESTORE_HOST
@@ -257,6 +213,9 @@ begin_test "ghe-restore no host arg or configured restore host"
257213
rm -rf "$GHE_REMOTE_DATA_DIR"
258214
setup_remote_metadata
259215

216+
# create settings file -- used to determine if instance has been configured.
217+
touch "$GHE_REMOTE_DATA_DIR/enterprise/dna.json"
218+
260219
# unset configured restore host
261220
unset GHE_RESTORE_HOST
262221

@@ -271,6 +230,9 @@ begin_test "ghe-restore with no pages backup"
271230
rm -rf "$GHE_REMOTE_DATA_DIR"
272231
setup_remote_metadata
273232

233+
# create settings file -- used to determine if instance has been configured.
234+
touch "$GHE_REMOTE_DATA_DIR/enterprise/dna.json"
235+
274236
# remove pages data
275237
rm -rf "$GHE_DATA_DIR/1/pages"
276238

@@ -285,6 +247,9 @@ begin_test "ghe-restore with tarball strategy"
285247
rm -rf "$GHE_REMOTE_DATA_DIR"
286248
setup_remote_metadata
287249

250+
# create settings file -- used to determine if instance has been configured.
251+
touch "$GHE_REMOTE_DATA_DIR/enterprise/dna.json"
252+
288253
# run it
289254
echo "tarball" > "$GHE_DATA_DIR/current/strategy"
290255
output=$(ghe-restore -v localhost)

0 commit comments

Comments
 (0)