Skip to content

Commit 87196f7

Browse files
authored
Merge pull request #1926 from larsewi/transfer-to-windowsmachine
ENT-12600: transfer-to-windowsmachine: documented & refactored script
2 parents 5ed5b9b + 783b8b3 commit 87196f7

File tree

1 file changed

+56
-9
lines changed

1 file changed

+56
-9
lines changed
Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,82 @@
1-
#!/bin/sh -x
1+
#!/bin/sh
2+
#
3+
# Transfer CFEngine binaries and tests to a Windows test machine
4+
# Packages binaries and test files as ZIP archives and uploads them via SFTP
5+
# for testing CFEngine on Windows environments
26

3-
# assume `. `dirname "$0"`/functions` was executed by caller
7+
# assume `. "$(dirname "$0")"/functions` was executed by caller
48
test -z "$PREFIX" && exit 1
59

10+
# Configure SSH/SFTP with batch mode (no password prompts) and skip host key checking
611
SSH="ssh -o BatchMode=yes -o StrictHostKeyChecking=no"
712
SFTP="sftp -o BatchMode=yes -o StrictHostKeyChecking=no"
813

14+
# Windows paths for 7-Zip extraction tool and Jenkins user home
915
SEVENZIP="\"c:/program files/7-zip/7z\""
1016
HOMEPATH="\"c:\\Users\\jenkins\""
1117

18+
# Determine directory names based on whether this is a CI job or manual build
1219
if [ -z "$JOB_NAME" ]; then
20+
# Manual build: use version and architecture in directory name
1321
DIRNAME=build-$VERSION-$ARCH
1422
TESTDIR="\"c:\\Users\\jenkins\""
1523
else
24+
# CI build: extract job name prefix (before first slash)
1625
DIRNAME=$(echo "${JOB_NAME}" | sed 's/\(.*\)\/.*/\1/g')
1726
TESTDIR="\"c:\\Users\\jenkins\\$DIRNAME\\tests\""
1827
fi
1928

20-
MAX_TRY=50
29+
# Retry wrapper function - attempts command up to 10 times
30+
# Used for network operations that might fail temporarily
31+
MAX_TRY=10
2132
try_run() {
22-
for _ in $(seq $MAX_TRY); do
33+
attempt=1
34+
log_debug "Starting retry wrapper for: $*"
35+
for i in $(seq $MAX_TRY); do
2336
if "$@"; then
37+
log_debug "Command succeeded on attempt $attempt: $*"
2438
return
2539
fi
40+
log_debug "Attempt [$i/$MAX_TRY] failed for: $*"
41+
attempt=$((attempt + 1))
42+
sleep 1
2643
done
44+
log_error "Command failed after $MAX_TRY attempts: $*"
2745
return 1
2846
}
2947

48+
# Package CFEngine binaries into a ZIP file for Windows deployment
3049
prepare_bin() {
3150
PKGD=$BASEDIR/packaging/cfengine-nova/pkg
3251
P=$PKGD/$DIRNAME
3352
rm -rf "$PKGD"
3453
mkdir -p "$P"/bin
3554

55+
# Copy core binaries and distribution binaries
3656
cp -a "$PREFIX/bin"/* "$P"/bin
3757
cp -a "$BASEDIR/cfengine/dist$PREFIX/bin"/* "$P"/bin
3858

59+
# Copy architecture-specific event DLL
3960
case "$ARCH" in
4061
x86) cp -a "$BASEDIR"/enterprise/libcfenterprise/cf.events.i686.dll "$P"/bin/cf.events.dll ;;
4162
x64) cp -a "$BASEDIR"/enterprise/libcfenterprise/cf.events.x86_64.dll "$P"/bin/cf.events.dll ;;
4263
*)
43-
echo "Unknown architecture: $ARCH"
64+
log_error "Unknown architecture: $ARCH"
4465
exit 1
4566
;;
4667
esac
4768

69+
# Copy Windows installer configuration file
4870
cp "$BASEDIR"/buildscripts/packaging/cfengine-nova/cfengine-nova.wxs "$P"
4971

72+
# Create ZIP archive of the prepared directory
5073
(
5174
cd "$PKGD"
5275
zip -r "$DIRNAME".zip "$DIRNAME"
5376
) || false
5477
}
5578

79+
# Package test suite files into a ZIP archive
5680
prepare_tests() {
5781
TESTD=$BASEDIR/core/tests
5882
(
@@ -61,21 +85,44 @@ prepare_tests() {
6185
) || false
6286
}
6387

88+
# Clean up previous deployment directories on Windows machine
89+
# Note: '|| true' ensures this doesn't fail if directories don't exist
6490
pre_put() {
65-
$SSH "$1" cmd /c "cd $HOMEPATH && rmdir /s /q $DIRNAME tests" || :
91+
$SSH "$1" cmd /c "cd $HOMEPATH && rmdir /s /q $DIRNAME tests" || true
6692
}
6793

94+
# Upload ZIP files to Windows machine via SFTP
6895
put_zip() {
6996
echo "put $P.zip" | $SFTP "$1"
7097
echo "put $BASEDIR/core/tests/tests.zip" | $SFTP "$1"
7198
}
7299

100+
# Extract uploaded ZIP files on Windows machine using 7-Zip
101+
# -y: assume Yes on all queries, -o: output directory, -r: recurse subdirectories
73102
post_put() {
74103
$SSH "$1" cmd /c "cd $HOMEPATH && $SEVENZIP x -y $DIRNAME.zip && $SEVENZIP x -y tests.zip -o$TESTDIR -r"
75104
}
76105

106+
# Main upload function - coordinates cleanup, upload, and extraction
107+
# Uses try_run for each step to handle network failures gracefully
77108
put() {
78-
try_run pre_put "$1"
79-
try_run put_zip "$1"
80-
try_run post_put "$1"
109+
target="$1"
110+
log_debug "Starting transfer to Windows machine: $target"
111+
112+
if ! try_run pre_put "$target"; then
113+
log_error "Failed to clean up previous deployment on $target"
114+
return 1
115+
fi
116+
117+
if ! try_run put_zip "$target"; then
118+
log_error "Failed to upload files to $target"
119+
return 1
120+
fi
121+
122+
if ! try_run post_put "$target"; then
123+
log_error "Failed to extract files on $target"
124+
return 1
125+
fi
126+
127+
log_debug "Successfully completed transfer to $target"
81128
}

0 commit comments

Comments
 (0)