Skip to content

Commit 7e302d2

Browse files
committed
Copying CNI binaries should be an atomic operation.
It was previously copying directly to where the binaries are executed, which can cause for half-written binaries to be executed. Adds: * a log function so we can differentiate the cnibincopy logs * uses a subdirectory named `upgrade_$(uuidgen)` so that each instance uses a unique directory.
1 parent bc1d913 commit 7e302d2

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

bindata/network/multus/multus.yaml

+29-10
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,23 @@ data:
1313
#!/bin/bash
1414
set -e
1515
16+
function log()
17+
{
18+
echo "$(date --iso-8601=seconds) [cnibincopy] ${1}"
19+
}
20+
1621
DESTINATION_DIRECTORY=/host/opt/cni/bin/
1722
1823
# Perform validation of usage
1924
if [ -z "$RHEL7_SOURCE_DIRECTORY" ] ||
2025
[ -z "$RHEL8_SOURCE_DIRECTORY" ] ||
2126
[ -z "$DEFAULT_SOURCE_DIRECTORY" ]; then
22-
echo "FATAL ERROR: You must set env variables: RHEL7_SOURCE_DIRECTORY, RHEL8_SOURCE_DIRECTORY, DEFAULT_SOURCE_DIRECTORY"
27+
log "FATAL ERROR: You must set env variables: RHEL7_SOURCE_DIRECTORY, RHEL8_SOURCE_DIRECTORY, DEFAULT_SOURCE_DIRECTORY"
2328
exit 1
2429
fi
2530
2631
if [ ! -d "$DESTINATION_DIRECTORY" ]; then
27-
echo "FATAL ERROR: Destination directory ($DESTINATION_DIRECTORY) does not exist"
32+
log "FATAL ERROR: Destination directory ($DESTINATION_DIRECTORY) does not exist"
2833
exit 1
2934
fi
3035
@@ -41,11 +46,11 @@ data:
4146
if [ "${VARIANT_ID}" == "coreos" ]; then
4247
rhelmajor=8
4348
else
44-
echo "FATAL ERROR: Unsupported Fedora variant=${VARIANT_ID}"
49+
log "FATAL ERROR: Unsupported Fedora variant=${VARIANT_ID}"
4550
exit 1
4651
fi
4752
;;
48-
*) echo "FATAL ERROR: Unsupported OS ID=${ID}"; exit 1
53+
*) log "FATAL ERROR: Unsupported OS ID=${ID}"; exit 1
4954
;;
5055
esac
5156
@@ -66,24 +71,38 @@ data:
6671
fi
6772
;;
6873
*)
69-
echo "ERROR: RHEL Major Version Unsupported, rhelmajor=${rhelmajor}"
74+
log "ERROR: RHEL Major Version Unsupported, rhelmajor=${rhelmajor}"
7075
;;
7176
esac
7277
7378
# When it doesn't exist, fall back to the original directory.
7479
if [ "$founddir" == false ]; then
75-
echo "Source directory unavailable for OS version: ${rhelmajor}"
80+
log "Source directory unavailable for OS version: ${rhelmajor}"
7681
sourcedir=$DEFAULT_SOURCE_DIRECTORY
7782
fi
7883
79-
cp -rf ${sourcedir}* $DESTINATION_DIRECTORY
80-
84+
# Use a subdirectory called "upgrade" so we can atomically move fully copied files.
85+
UPGRADE_DIRECTORY=${DESTINATION_DIRECTORY}upgrade_$(uuidgen)
86+
rm -Rf $UPGRADE_DIRECTORY
87+
mkdir -p $UPGRADE_DIRECTORY
88+
cp -rf ${sourcedir}* $UPGRADE_DIRECTORY
8189
if [ $? -eq 0 ]; then
82-
echo "Successfully copied files in ${sourcedir} to $DESTINATION_DIRECTORY"
90+
log "Successfully copied files in ${sourcedir} to $UPGRADE_DIRECTORY"
8391
else
84-
echo "Failed to copy files in ${sourcedir} to $DESTINATION_DIRECTORY"
92+
log "Failed to copy files in ${sourcedir} to $UPGRADE_DIRECTORY"
93+
rm -Rf $UPGRADE_DIRECTORY
8594
exit 1
8695
fi
96+
mv -f $UPGRADE_DIRECTORY/* ${DESTINATION_DIRECTORY}/
97+
if [ $? -eq 0 ]; then
98+
log "Successfully moved files in $UPGRADE_DIRECTORY to ${DESTINATION_DIRECTORY}"
99+
else
100+
log "Failed to move files in $UPGRADE_DIRECTORY to ${DESTINATION_DIRECTORY}"
101+
rm -Rf $UPGRADE_DIRECTORY
102+
exit 1
103+
fi
104+
rm -Rf $UPGRADE_DIRECTORY
105+
87106
#
88107
# Safe sysctls
89108
# -------------

0 commit comments

Comments
 (0)