| | | 1 | | // Copyright (c) Microsoft and contributors. All rights reserved. |
| | | 2 | | // |
| | | 3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
| | | 4 | | // you may not use this file except in compliance with the License. |
| | | 5 | | // You may obtain a copy of the License at |
| | | 6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
| | | 7 | | // |
| | | 8 | | // Unless required by applicable law or agreed to in writing, software |
| | | 9 | | // distributed under the License is distributed on an "AS IS" BASIS, |
| | | 10 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | | 11 | | // |
| | | 12 | | // See the License for the specific language governing permissions and |
| | | 13 | | // limitations under the License. |
| | | 14 | | |
| | | 15 | | using System; |
| | | 16 | | using System.Collections.Generic; |
| | | 17 | | using System.IO; |
| | | 18 | | using Microsoft.WindowsAzure.Storage; |
| | | 19 | | using Microsoft.WindowsAzure.Storage.Blob; |
| | | 20 | | |
| | | 21 | | |
| | | 22 | | namespace Microsoft.Azure.Batch.FileStaging |
| | | 23 | | { |
| | | 24 | | /// <summary> |
| | | 25 | | /// Provides for file staging of a local file to blob storage. |
| | | 26 | | /// </summary> |
| | | 27 | | public sealed class FileToStage : IFileStagingProvider |
| | | 28 | | { |
| | | 29 | | /// <summary> |
| | | 30 | | /// The name of the local file to stage to blob storage |
| | | 31 | | /// </summary> |
| | | 32 | | public string LocalFileToStage |
| | | 33 | | { |
| | 0 | 34 | | get; |
| | 0 | 35 | | internal set; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// The target filename, on the compute node, to which the blob contents will be downloaded. |
| | | 40 | | /// </summary> |
| | | 41 | | public string NodeFileName |
| | | 42 | | { |
| | 0 | 43 | | get; |
| | 0 | 44 | | internal set; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// The instances of ResourcesFile for the staged local file. |
| | | 49 | | /// For this implementation, successful file staging of this object will |
| | | 50 | | /// result in a collection with only one entry. |
| | | 51 | | /// </summary> |
| | | 52 | | public IEnumerable<ResourceFile> StagedFiles |
| | | 53 | | { |
| | 0 | 54 | | get; |
| | 0 | 55 | | internal set; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// The exception, if any, caught while attempting to stage this file. |
| | | 60 | | /// </summary> |
| | | 61 | | public Exception Exception |
| | | 62 | | { |
| | 0 | 63 | | get; |
| | 0 | 64 | | internal set; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | #region constructors |
| | | 68 | | |
| | 0 | 69 | | private FileToStage() |
| | | 70 | | { |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Specifies that a local file should be staged to blob storage. |
| | | 75 | | /// The specified account will be charged for storage costs. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="localFileToStage">The name of the local file.</param> |
| | | 78 | | /// <param name="storageCredentials">The storage credentials to be used when creating the default container.</pa |
| | | 79 | | /// <param name="nodeFileName">Optional name to be given to the file on the compute node. If this parameter is |
| | | 80 | | /// the name on the compute node will be set to the value of localFileToStage stripped of all path information.< |
| | 0 | 81 | | public FileToStage(string localFileToStage, StagingStorageAccount storageCredentials, string nodeFileName = null |
| | | 82 | | { |
| | 0 | 83 | | this.LocalFileToStage = localFileToStage; |
| | 0 | 84 | | this.StagingStorageAccount = storageCredentials; |
| | | 85 | | |
| | 0 | 86 | | if (string.IsNullOrWhiteSpace(this.LocalFileToStage)) |
| | | 87 | | { |
| | 0 | 88 | | throw new ArgumentOutOfRangeException("localFileToStage"); |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | // map null to base name of local file |
| | 0 | 92 | | if (string.IsNullOrWhiteSpace(nodeFileName)) |
| | | 93 | | { |
| | 0 | 94 | | this.NodeFileName = Path.GetFileName(this.LocalFileToStage); |
| | | 95 | | } |
| | | 96 | | else |
| | | 97 | | { |
| | 0 | 98 | | this.NodeFileName = nodeFileName; |
| | | 99 | | } |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | #endregion // constructors |
| | | 103 | | |
| | | 104 | | #region // IFileStagingProvider |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// See <see cref="IFileStagingProvider.StageFilesAsync"/>. |
| | | 108 | | /// </summary> |
| | | 109 | | /// <param name="filesToStage">The instances of IFileStagingProvider to stage.</param> |
| | | 110 | | /// <param name="fileStagingArtifact">IFileStagingProvider specific staging artifacts including error/progress.< |
| | | 111 | | /// <returns>A <see cref="System.Threading.Tasks.Task"/> object that represents the asynchronous operation.</ret |
| | | 112 | | public async System.Threading.Tasks.Task StageFilesAsync(List<IFileStagingProvider> filesToStage, IFileStagingAr |
| | | 113 | | { |
| | 0 | 114 | | System.Threading.Tasks.Task taskForStaticStaging = FileToStage.StageFilesInternalAsync(filesToStage, fileSta |
| | | 115 | | |
| | 0 | 116 | | await taskForStaticStaging.ConfigureAwait(continueOnCapturedContext: false); |
| | | 117 | | |
| | 0 | 118 | | return; |
| | 0 | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// See <see cref="IFileStagingProvider.CreateStagingArtifact"/>. |
| | | 123 | | /// </summary> |
| | | 124 | | /// <returns>An instance of IFileStagingArtifact with default values.</returns> |
| | | 125 | | public IFileStagingArtifact CreateStagingArtifact() |
| | | 126 | | { |
| | 0 | 127 | | return new SequentialFileStagingArtifact() as IFileStagingArtifact; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// See <see cref="IFileStagingProvider.Validate"/>. |
| | | 132 | | /// </summary> |
| | | 133 | | public void Validate() |
| | | 134 | | { |
| | 0 | 135 | | if (!File.Exists(this.LocalFileToStage)) |
| | | 136 | | { |
| | 0 | 137 | | throw new FileNotFoundException(string.Format(ErrorMessages.FileStagingLocalFileNotFound, this.LocalFile |
| | | 138 | | } |
| | 0 | 139 | | } |
| | | 140 | | |
| | | 141 | | #endregion // IFileStagingProvier |
| | | 142 | | |
| | | 143 | | #region internal/private |
| | | 144 | | |
| | | 145 | | // the staging code needs to get the secrets |
| | 0 | 146 | | internal StagingStorageAccount StagingStorageAccount { get; set; } |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// combine container and blob into an URL. |
| | | 150 | | /// </summary> |
| | | 151 | | /// <param name="container">container url</param> |
| | | 152 | | /// <param name="blob">blob url</param> |
| | | 153 | | /// <returns>full url</returns> |
| | | 154 | | private static string ConstructBlobSource(string container, string blob) |
| | | 155 | | { |
| | 0 | 156 | | int index = container.IndexOf("?"); |
| | | 157 | | |
| | 0 | 158 | | if (index != -1) |
| | | 159 | | { |
| | | 160 | | //SAS |
| | 0 | 161 | | string containerAbsoluteUrl = container.Substring(0, index); |
| | 0 | 162 | | return containerAbsoluteUrl + "/" + blob + container.Substring(index); |
| | | 163 | | } |
| | | 164 | | else |
| | | 165 | | { |
| | 0 | 166 | | return container + "/" + blob; |
| | | 167 | | } |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// create a container if doesn't exist, setting permission with policy, and return assosciated SAS signature |
| | | 172 | | /// </summary> |
| | | 173 | | /// <param name="account">storage account</param> |
| | | 174 | | /// <param name="key">storage key</param> |
| | | 175 | | /// <param name="container">container to be created</param> |
| | | 176 | | /// <param name="policy">name for the policy</param> |
| | | 177 | | /// <param name="start">start time of the policy</param> |
| | | 178 | | /// <param name="end">expire time of the policy</param> |
| | | 179 | | /// <param name="permissions">permission on the name</param> |
| | | 180 | | /// <param name="blobUri">blob URI</param> |
| | | 181 | | /// <returns>the SAS for the container, in full URI format.</returns> |
| | | 182 | | private static string CreateContainerWithPolicySASIfNotExist(string account, string key, Uri blobUri, string con |
| | | 183 | | { |
| | | 184 | | // 1. form the credentail and initial client |
| | 0 | 185 | | CloudStorageAccount storageaccount = new CloudStorageAccount(new WindowsAzure.Storage.Auth.StorageCredential |
| | 0 | 186 | | blobEndpoint: blobUri, |
| | 0 | 187 | | queueEndpoint: null, |
| | 0 | 188 | | tableEndpoint: null, |
| | 0 | 189 | | fileEndpoint: null); |
| | | 190 | | |
| | 0 | 191 | | CloudBlobClient client = storageaccount.CreateCloudBlobClient(); |
| | | 192 | | |
| | | 193 | | // 2. create container if it doesn't exist |
| | 0 | 194 | | CloudBlobContainer storagecontainer = client.GetContainerReference(container); |
| | 0 | 195 | | storagecontainer.CreateIfNotExistsAsync().GetAwaiter().GetResult(); |
| | | 196 | | |
| | | 197 | | // 3. validate policy, create/overwrite if doesn't match |
| | 0 | 198 | | bool policyFound = false; |
| | | 199 | | |
| | 0 | 200 | | SharedAccessBlobPolicy accesspolicy = new SharedAccessBlobPolicy() |
| | 0 | 201 | | { |
| | 0 | 202 | | SharedAccessExpiryTime = end, |
| | 0 | 203 | | SharedAccessStartTime = start, |
| | 0 | 204 | | Permissions = permissions |
| | 0 | 205 | | }; |
| | | 206 | | |
| | 0 | 207 | | BlobContainerPermissions blobPermissions = storagecontainer.GetPermissionsAsync().GetAwaiter().GetResult(); |
| | | 208 | | |
| | 0 | 209 | | if (blobPermissions.SharedAccessPolicies.ContainsKey(policy)) |
| | | 210 | | { |
| | 0 | 211 | | SharedAccessBlobPolicy containerpolicy = blobPermissions.SharedAccessPolicies[policy]; |
| | 0 | 212 | | if (!(permissions == (containerpolicy.Permissions & permissions) && start <= containerpolicy.SharedAcces |
| | | 213 | | { |
| | 0 | 214 | | blobPermissions.SharedAccessPolicies[policy] = accesspolicy; |
| | | 215 | | } |
| | | 216 | | else |
| | | 217 | | { |
| | 0 | 218 | | policyFound = true; |
| | | 219 | | } |
| | | 220 | | } |
| | | 221 | | else |
| | | 222 | | { |
| | 0 | 223 | | blobPermissions.SharedAccessPolicies.Add(policy, accesspolicy); |
| | | 224 | | } |
| | | 225 | | |
| | 0 | 226 | | if (!policyFound) |
| | | 227 | | { |
| | 0 | 228 | | storagecontainer.SetPermissionsAsync(blobPermissions).GetAwaiter().GetResult(); |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | // 4. genereate SAS and return |
| | 0 | 232 | | string container_sas = storagecontainer.GetSharedAccessSignature(new SharedAccessBlobPolicy(), policy); |
| | 0 | 233 | | string container_url = storagecontainer.Uri.AbsoluteUri + container_sas; |
| | | 234 | | |
| | 0 | 235 | | return container_url; |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | private static void CreateDefaultBlobContainerAndSASIfNeededReturn(List<IFileStagingProvider> filesToStage, Sequ |
| | | 239 | | { |
| | 0 | 240 | | if ((null != filesToStage) && (filesToStage.Count > 0)) |
| | | 241 | | { |
| | | 242 | | // construct the name of the new blob container. |
| | 0 | 243 | | seqArtifact.BlobContainerCreated = FileStagingNamingHelpers.ConstructDefaultName(seqArtifact.NamingFragm |
| | | 244 | | |
| | | 245 | | // get any instance for the storage credentials |
| | 0 | 246 | | FileToStage anyRealInstance = FindAtLeastOne(filesToStage); |
| | | 247 | | |
| | 0 | 248 | | if (null != anyRealInstance) |
| | | 249 | | { |
| | 0 | 250 | | StagingStorageAccount creds = anyRealInstance.StagingStorageAccount; |
| | 0 | 251 | | string policyName = Batch.Constants.DefaultConveniencePrefix + Constants.DefaultContainerPolicyFragm |
| | 0 | 252 | | DateTime startTime = DateTime.UtcNow; |
| | 0 | 253 | | DateTime expiredAtTime = startTime + new TimeSpan(24 /* hrs*/, 0, 0); |
| | | 254 | | |
| | 0 | 255 | | seqArtifact.DefaultContainerSAS = CreateContainerWithPolicySASIfNotExist( |
| | 0 | 256 | | creds.StorageAccount, |
| | 0 | 257 | | creds.StorageAccountKey, |
| | 0 | 258 | | creds.BlobUri, |
| | 0 | 259 | | seqArtifact.BlobContainerCreated, |
| | 0 | 260 | | policyName, |
| | 0 | 261 | | startTime, |
| | 0 | 262 | | expiredAtTime, |
| | 0 | 263 | | SharedAccessBlobPermissions.Read); |
| | | 264 | | |
| | 0 | 265 | | return; // done |
| | | 266 | | } |
| | | 267 | | } |
| | 0 | 268 | | } |
| | | 269 | | |
| | | 270 | | /// <summary> |
| | | 271 | | /// Since this is the SequentialFileStagingProvider, all files are supposed to be of this type. |
| | | 272 | | /// Find any one and return the implementation instance. |
| | | 273 | | /// </summary> |
| | | 274 | | /// <param name="filesToStage"></param> |
| | | 275 | | /// <returns>Null means there was not even one.</returns> |
| | | 276 | | private static FileToStage FindAtLeastOne(List<IFileStagingProvider> filesToStage) |
| | | 277 | | { |
| | 0 | 278 | | if ((null != filesToStage) && (filesToStage.Count > 0)) |
| | | 279 | | { |
| | 0 | 280 | | foreach(IFileStagingProvider curProvider in filesToStage) |
| | | 281 | | { |
| | 0 | 282 | | FileToStage thisIsReal = curProvider as FileToStage; |
| | | 283 | | |
| | 0 | 284 | | if (null != thisIsReal) |
| | | 285 | | { |
| | 0 | 286 | | return thisIsReal; |
| | | 287 | | } |
| | | 288 | | } |
| | | 289 | | } |
| | | 290 | | |
| | 0 | 291 | | return null; |
| | 0 | 292 | | } |
| | | 293 | | |
| | | 294 | | /// <summary> |
| | | 295 | | /// Starts an asynchronous call to stage the given files. |
| | | 296 | | /// </summary> |
| | | 297 | | private static async System.Threading.Tasks.Task StageFilesInternalAsync(List<IFileStagingProvider> filesToStage |
| | | 298 | | { |
| | 0 | 299 | | if (null == filesToStage) |
| | | 300 | | { |
| | 0 | 301 | | throw new ArgumentNullException("filesToStage"); |
| | | 302 | | } |
| | | 303 | | |
| | 0 | 304 | | if (null == fileStagingArtifact) |
| | | 305 | | { |
| | 0 | 306 | | throw new ArgumentNullException("filesStagingArtifact"); |
| | | 307 | | } |
| | | 308 | | |
| | 0 | 309 | | SequentialFileStagingArtifact seqArtifact = fileStagingArtifact as SequentialFileStagingArtifact; |
| | | 310 | | |
| | 0 | 311 | | if (null == seqArtifact) |
| | | 312 | | { |
| | 0 | 313 | | throw new ArgumentOutOfRangeException(ErrorMessages.FileStagingIncorrectArtifact); |
| | | 314 | | } |
| | | 315 | | |
| | | 316 | | // is there any work to do? |
| | 0 | 317 | | if (null == FindAtLeastOne(filesToStage)) |
| | | 318 | | { |
| | 0 | 319 | | return; // now work to do. none of the files belong to this provider |
| | | 320 | | } |
| | | 321 | | |
| | | 322 | | // is there any work to do |
| | 0 | 323 | | if ((null == filesToStage) || (filesToStage.Count <= 0)) |
| | | 324 | | { |
| | 0 | 325 | | return; // we are done |
| | | 326 | | } |
| | | 327 | | |
| | | 328 | | // create a Run task to create the blob containers if needed |
| | 0 | 329 | | System.Threading.Tasks.Task createContainerTask = System.Threading.Tasks.Task.Run(() => { CreateDefaultBlobC |
| | | 330 | | |
| | | 331 | | // wait for container to be created |
| | 0 | 332 | | await createContainerTask.ConfigureAwait(continueOnCapturedContext: false); |
| | | 333 | | |
| | | 334 | | // begin staging the files |
| | 0 | 335 | | System.Threading.Tasks.Task stageTask = StageFilesAsync(filesToStage, seqArtifact); |
| | | 336 | | |
| | | 337 | | // wait for files to be staged |
| | 0 | 338 | | await stageTask.ConfigureAwait(continueOnCapturedContext: false); |
| | 0 | 339 | | } |
| | | 340 | | |
| | | 341 | | /// <summary> |
| | | 342 | | /// Stages all files in the queue |
| | | 343 | | /// </summary> |
| | | 344 | | private async static System.Threading.Tasks.Task StageFilesAsync(List<IFileStagingProvider> filesToStage, Sequen |
| | | 345 | | { |
| | 0 | 346 | | foreach (IFileStagingProvider currentFile in filesToStage) |
| | | 347 | | { |
| | | 348 | | // for "retry" and/or "double calls" we ignore files that have already been staged |
| | 0 | 349 | | if (null == currentFile.StagedFiles) |
| | | 350 | | { |
| | 0 | 351 | | FileToStage fts = currentFile as FileToStage; |
| | | 352 | | |
| | 0 | 353 | | if (null != fts) |
| | | 354 | | { |
| | 0 | 355 | | System.Threading.Tasks.Task stageTask = StageOneFileAsync(fts, seqArtifacts); |
| | | 356 | | |
| | 0 | 357 | | await stageTask.ConfigureAwait(continueOnCapturedContext: false); |
| | | 358 | | } |
| | | 359 | | } |
| | | 360 | | } |
| | 0 | 361 | | } |
| | | 362 | | |
| | | 363 | | /// <summary> |
| | | 364 | | /// Stage a single file. |
| | | 365 | | /// </summary> |
| | | 366 | | private async static System.Threading.Tasks.Task StageOneFileAsync(FileToStage stageThisFile, SequentialFileStag |
| | | 367 | | { |
| | 0 | 368 | | StagingStorageAccount storecreds = stageThisFile.StagingStorageAccount; |
| | 0 | 369 | | string containerName = seqArtifacts.BlobContainerCreated; |
| | | 370 | | |
| | | 371 | | // TODO: this flattens all files to the top of the compute node/task relative file directory. solve the hiea |
| | 0 | 372 | | string blobName = Path.GetFileName(stageThisFile.LocalFileToStage); |
| | | 373 | | |
| | | 374 | | // Create the storage account with the connection string. |
| | 0 | 375 | | CloudStorageAccount storageAccount = new CloudStorageAccount( |
| | 0 | 376 | | new WindowsAzure.Storage.Auth.StorageCredentials(storecreds.Stor |
| | 0 | 377 | | blobEndpoint: storecreds.BlobUri, |
| | 0 | 378 | | queueEndpoint: null, |
| | 0 | 379 | | tableEndpoint: null, |
| | 0 | 380 | | fileEndpoint: null); |
| | | 381 | | |
| | 0 | 382 | | CloudBlobClient client = storageAccount.CreateCloudBlobClient(); |
| | 0 | 383 | | CloudBlobContainer container = client.GetContainerReference(containerName); |
| | 0 | 384 | | ICloudBlob blob = container.GetBlockBlobReference(blobName); |
| | | 385 | | bool doesBlobExist; |
| | | 386 | | |
| | | 387 | | try |
| | | 388 | | { |
| | | 389 | | // fetch attributes so we can compare file lengths |
| | 0 | 390 | | System.Threading.Tasks.Task fetchTask = blob.FetchAttributesAsync(); |
| | | 391 | | |
| | 0 | 392 | | await fetchTask.ConfigureAwait(continueOnCapturedContext: false); |
| | | 393 | | |
| | 0 | 394 | | doesBlobExist = true; |
| | 0 | 395 | | } |
| | 0 | 396 | | catch (StorageException scex) |
| | | 397 | | { |
| | | 398 | | // check to see if blob does not exist |
| | 0 | 399 | | if ((int)System.Net.HttpStatusCode.NotFound == scex.RequestInformation.HttpStatusCode) |
| | | 400 | | { |
| | 0 | 401 | | doesBlobExist = false; |
| | | 402 | | } |
| | | 403 | | else |
| | | 404 | | { |
| | 0 | 405 | | throw; // unknown exception, throw to caller |
| | | 406 | | } |
| | 0 | 407 | | } |
| | | 408 | | |
| | 0 | 409 | | bool mustUploadBlob = true; // we do not re-upload blobs if they have already been uploaded |
| | | 410 | | |
| | 0 | 411 | | if (doesBlobExist) // if the blob exists, compare |
| | | 412 | | { |
| | 0 | 413 | | FileInfo fi = new FileInfo(stageThisFile.LocalFileToStage); |
| | | 414 | | |
| | | 415 | | // since we don't have a hash of the contents... we check length |
| | 0 | 416 | | if (blob.Properties.Length == fi.Length) |
| | | 417 | | { |
| | 0 | 418 | | mustUploadBlob = false; |
| | | 419 | | } |
| | | 420 | | } |
| | | 421 | | |
| | 0 | 422 | | if (mustUploadBlob) |
| | | 423 | | { |
| | | 424 | | // upload the file |
| | 0 | 425 | | System.Threading.Tasks.Task uploadTask = blob.UploadFromFileAsync(stageThisFile.LocalFileToStage); |
| | | 426 | | |
| | 0 | 427 | | await uploadTask.ConfigureAwait(continueOnCapturedContext: false); |
| | | 428 | | } |
| | | 429 | | |
| | | 430 | | // get the SAS for the blob |
| | 0 | 431 | | string blobSAS = ConstructBlobSource(seqArtifacts.DefaultContainerSAS, blobName); |
| | 0 | 432 | | string nodeFileName = stageThisFile.NodeFileName; |
| | | 433 | | |
| | | 434 | | // create a new ResourceFile and populate it. This file is now staged! |
| | 0 | 435 | | stageThisFile.StagedFiles = new ResourceFile[] { ResourceFile.FromUrl(blobSAS, nodeFileName) }; |
| | 0 | 436 | | } |
| | | 437 | | |
| | | 438 | | #endregion internal/private |
| | | 439 | | } |
| | | 440 | | } |