From 7f96e5c9669f30cd22914de7f092d009faddf304 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 18 Apr 2019 10:39:32 +0200 Subject: global-functions: add $WaitForFile, wait for file on fetch The fetch command is asynchronous, the file is not guaranteed to be available when command terminates. I opened an issue at Mikrotik support (Ticket#2019041722004999), their answer: > You should perform a check in a loop. > :delay until file exist > > That can happen also with any configuration not just files. So add a function to wait for a file with given name. I have not seen this with other configuration, though. --- check-certificates | 5 ++++- daily-psk.capsman | 4 +++- daily-psk.local | 4 +++- daily-psk.template | 4 +++- global-functions | 22 ++++++++++++++++++++++ 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/check-certificates b/check-certificates index bd1e0ed..9dd9acb 100644 --- a/check-certificates +++ b/check-certificates @@ -10,6 +10,7 @@ :global SendNotification; :global UrlEncode; +:global WaitForFile; :local GetIssuerCN do={ :foreach IssuerI in=$1 do={ @@ -39,7 +40,9 @@ :foreach Type in={ ".pem"; ".p12" } do={ :local CertFileName ([ $UrlEncode $CommonName ] . $Type); :do { - / tool fetch check-certificate=yes-without-crl ($CertRenewUrl . $CertFileName); + / tool fetch check-certificate=yes-without-crl \ + ($CertRenewUrl . $CertFileName) dst-path=$CertFileName; + $WaitForFile $CertFileName; :foreach PassPhrase in=$CertRenewPass do={ / certificate import file-name=$CertFileName passphrase=$PassPhrase; } diff --git a/daily-psk.capsman b/daily-psk.capsman index 06c69f8..8d562e6 100644 --- a/daily-psk.capsman +++ b/daily-psk.capsman @@ -9,9 +9,10 @@ :global Identity; :global DailyPskMatchComment; -:global UrlEncode; :global SendNotification; +:global UrlEncode; +:global WaitForFile; :local Seen [ :toarray "" ]; @@ -76,6 +77,7 @@ :do { / tool fetch check-certificate=yes-without-crl \ $Url dst-path=$Attach; + $WaitForFile $Attach; } on-error={ :set Attach ""; } diff --git a/daily-psk.local b/daily-psk.local index 8e22e54..78f7868 100644 --- a/daily-psk.local +++ b/daily-psk.local @@ -9,9 +9,10 @@ :global Identity; :global DailyPskMatchComment; -:global UrlEncode; :global SendNotification; +:global UrlEncode; +:global WaitForFile; :local Seen [ :toarray "" ]; @@ -76,6 +77,7 @@ :do { / tool fetch check-certificate=yes-without-crl \ $Url dst-path=$Attach; + $WaitForFile $Attach; } on-error={ :set Attach ""; } diff --git a/daily-psk.template b/daily-psk.template index 4cb7cd2..dfbac13 100644 --- a/daily-psk.template +++ b/daily-psk.template @@ -10,9 +10,10 @@ :global Identity; :global DailyPskMatchComment; -:global UrlEncode; :global SendNotification; +:global UrlEncode; +:global WaitForFile; :local Seen [ :toarray "" ]; @@ -82,6 +83,7 @@ :do { / tool fetch check-certificate=yes-without-crl \ $Url dst-path=$Attach; + $WaitForFile $Attach; } on-error={ :set Attach ""; } diff --git a/global-functions b/global-functions index 92d9d57..356a570 100644 --- a/global-functions +++ b/global-functions @@ -68,6 +68,8 @@ :global ScriptUpdatesBaseUrl; :global ScriptUpdatesUrlSuffix; + :global WaitForFile; + :if ([ / certificate print count-only where common-name=$CommonName ] = 0) do={ :log info ("Certificate with CommonName " . $CommonName . \ " not available, downloading and importing."); @@ -76,6 +78,7 @@ ($ScriptUpdatesBaseUrl . "certs/" . \ $FileName . $ScriptUpdatesUrlSuffix) \ dst-path=$FileName; + $WaitForFile $FileName; / certificate import file-name=$FileName passphrase=""; } on-error={ :log warning "Failed imprting certificate!"; @@ -166,6 +169,7 @@ :global CertificateAvailable; :global CleanFilePath; + :global WaitForFile; :if ([ :len $PkgName ] = 0) do={ return false; } :if ([ :len $PkgVer ] = 0) do={ :set PkgVer [ / system package update get installed-version ]; } @@ -179,6 +183,7 @@ / tool fetch check-certificate=yes-without-crl \ ("https://upgrade.mikrotik.com/routeros/" . $PkgVer . "/" . $PkgFile) \ dst-path=$PkgDest; + $WaitForFile $PkgDest; } on-error={ / file remove [ find where name=$PkgDest ]; :return false; @@ -196,3 +201,20 @@ :error "Locked." } } + +# wait for file to be available +:global WaitForFile do={ + :global CleanFilePath; + + :local FileName [ $CleanFilePath [ :tostr $1 ] ]; + :local I 0; + + :while ([ file print count-only where name=$FileName ] = 0) do={ + :if ($I > 20) do={ + :return false; + } + :delay 100ms; + :set I ($I + 1); + } + :return true; +} -- cgit v1.2.3-54-g00ecf