Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated wait behavior for open file handles to custom-script-extension binary #155

Merged
merged 6 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions misc/custom-script-shim
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,42 @@ check_binary_write_lock() {
set +e # disable exit on non-zero return code
local retry_attempts=0
while (( retry_attempts < 10 )); do
lsof_output="$(lsof ${bin})"
if [ "$?" -eq 0 ]; then
echo "${HANDLER_BIN} is open by the following processes: "
echo "${lsof_output}"
lsof_result="$(lsof -F ac ${bin})"
lsof_return_code=$?
if [ "$lsof_return_code" -eq 0 ]; then
#"lsof -F" outputs results in more parse-able format, "-F ac" option prints access mode and command name for process
#access mode and command names are prepended with a and c
file_mode="$(echo "$lsof_result" | awk 'match($0, /^a(.*)$/) {print $0}')"
process_name="$(echo "$lsof_result" | awk 'match($0, /^c(.*)$/) {print substr($0, RSTART+1, RLENGTH-1)}')"

found_write_lock=0
file_mode_array=($file_mode)
i=0
for name in $process_name
do
file_handle_mode=${file_mode_array[$i]}
echo "$name has access mode '$file_handle_mode' file handle on ${HANDLER_BIN}"
## w and u are file descriptor modes for write and read/write access
if [[ $file_handle_mode == "aw" ]] || [[ $file_handle_mode == "au" ]]; then
found_write_lock=1
fi
((++i))
done
if [ "$found_write_lock" -eq 0 ]; then
# did not find write lock on any file no need to wait or retry
break
fi
((++retry_attempts))
echo "waiting for process(es) with write handle on ${HANDLER_BIN}"
echo "sleeping for 3 seconds before retry, attempt ${retry_attempts} of 10"
sleep 3
else
set -e
return 0 #Success path
break
fi
done
echo "Timed out waiting for lock on ${HANDLER_BIN}"
echo "File handle is still open by the following processes: "
echo "${lsof_output}"
exit 1
# do not return error if file descriptor is open after retries expire, make a best effort attempt to start custom-script-extension
set -e
return 0
}

if [ "$#" -ne 1 ]; then
Expand Down
2 changes: 1 addition & 1 deletion misc/manifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<ExtensionImage xmlns="http://schemas.microsoft.com/windowsazure">
<ProviderNameSpace>Microsoft.Azure.Extensions</ProviderNameSpace>
<Type>CustomScript</Type>
<Version>2.1.1</Version>
<Version>2.1.2</Version>
<Label>Microsoft Azure Custom Script Extension for Linux Virtual Machines</Label>
<HostingResources>VmRole</HostingResources>
<MediaLink></MediaLink>
Expand Down
8 changes: 4 additions & 4 deletions pkg/download/blobwithmsitoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func GetMsiProviderForStorageAccountsImplicitly(blobUri string) MsiProvider {
return func() (msi.Msi, error) {
msi, err := msiProvider.GetMsiForResource(GetResourceNameFromBlobUri(blobUri))
if err != nil {
return msi, errors.Wrapf(err, "Unable to get managed identity. "+
"Please make sure that system assigned managed identity is enabled on the VM "+
return msi, fmt.Errorf("Unable to get managed identity. " +
"Please make sure that system assigned managed identity is enabled on the VM " +
"or user assigned identity is added to the system.")
}
return msi, nil
Expand All @@ -73,7 +73,7 @@ func GetMsiProviderForStorageAccountsWithClientId(blobUri, clientId string) MsiP
return func() (msi.Msi, error) {
msi, err := msiProvider.GetMsiUsingClientId(clientId, GetResourceNameFromBlobUri(blobUri))
if err != nil {
return msi, errors.Wrapf(err, "Unable to get managed identity with client id %s. "+
return msi, fmt.Errorf("Unable to get managed identity with client id %s. "+
"Please make sure that the user assigned managed identity is added to the VM ", clientId)
}
return msi, nil
Expand All @@ -85,7 +85,7 @@ func GetMsiProviderForStorageAccountsWithObjectId(blobUri, objectId string) MsiP
return func() (msi.Msi, error) {
msi, err := msiProvider.GetMsiUsingObjectId(objectId, GetResourceNameFromBlobUri(blobUri))
if err != nil {
return msi, errors.Wrapf(err, "Unable to get managed identity with object id %s. "+
return msi, fmt.Errorf("Unable to get managed identity with object id %s. "+
"Please make sure that the user assigned managed identity is added to the VM ", objectId)
}
return msi, nil
Expand Down