public static bool DownloadConfig(string downloadedZipPath, string AzureAccountName, string AzureAccountKey, string orgID, string studyID, string homeID, string configFilename, NLog.Logger logger=null)
        {

            Microsoft.WindowsAzure.CloudStorageAccount storageAccount = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobClient blobClient = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobContainer container = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlockBlob blockBlob = null;
            string leaseId = null;

            try
            {
                storageAccount = new Microsoft.WindowsAzure.CloudStorageAccount(new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(AzureAccountName, AzureAccountKey), true);
                blobClient = storageAccount.CreateCloudBlobClient();
                container = blobClient.GetContainerReference(AzureConfigContainerName);

                if (configFilename == PackagerHelper.ConfigPackagerHelper.actualConfigFileName)
                {
                    blockBlob = container.GetBlockBlobReference(ActualConfigBlobName(orgID, studyID, homeID, configFilename));
                }
                else if (configFilename == PackagerHelper.ConfigPackagerHelper.desiredConfigFileName)
                {
                    blockBlob = container.GetBlockBlobReference(DesiredConfigBlobName(orgID, studyID, homeID, configFilename));
                }

                bool blobExists = BlockBlobExists(blockBlob);

                if (blobExists)
                    leaseId = AcquireLease(blockBlob, logger); // Acquire Lease on Blob
                else
                    return false;

                if (blobExists && leaseId == null)
                {
                    if (null != logger)
                    {
                        logger.Error("AcquireLease on Blob: " + ActualConfigBlobName(orgID, studyID, homeID, configFilename) + " Failed");
                    }
                    return false;
                }

                string url = blockBlob.Uri.ToString();
                if (blockBlob.ServiceClient.Credentials.NeedsTransformUri)
                {
                    url = blockBlob.ServiceClient.Credentials.TransformUri(url);
                }

                var req = BlobRequest.Get(new Uri(url), AzureBlobLeaseTimeout, null, leaseId);
                blockBlob.ServiceClient.Credentials.SignRequest(req);

                using (var reader = new BinaryReader(req.GetResponse().GetResponseStream()))
                {
                    FileStream zipFile = new FileStream(downloadedZipPath, FileMode.OpenOrCreate);
                    reader.BaseStream.CopyTo(zipFile);
                    zipFile.Close();
                }
                req.GetResponse().GetResponseStream().Close();

                ReleaseLease(blockBlob, leaseId); // Release Lease on Blob
                return true;
            }
            catch (Exception e)
            {
                if (null != logger)
                {
                    logger.ErrorException("DownloadConfig_Azure, downloadZipPath: " + downloadedZipPath, e);
                }
                ReleaseLease(blockBlob, leaseId);
                return false;
            }
        }
 private static void DoLeaseOperation(CloudBlob blob, string leaseId, Microsoft.WindowsAzure.StorageClient.Protocol.LeaseAction action, NLog.Logger logger)
 {
     try
     {
         if (blob == null || leaseId == null)
             return;
         var creds = blob.ServiceClient.Credentials;
         var transformedUri = new Uri(creds.TransformUri(blob.Uri.ToString()));
         var req = BlobRequest.Lease(transformedUri, AzureBlobLeaseTimeout, action, leaseId);
         creds.SignRequest(req);
         req.GetResponse().Close();
     }
     catch (WebException e)
     {
         if (null != logger)
         {
             logger.ErrorException("DoLeaseOperation, blob: " + blob.Name + ", leaseId: " + leaseId + ", action " + action, e);
         }
     }
 }
        private static string AcquireLease(Microsoft.WindowsAzure.StorageClient.CloudBlockBlob blob, NLog.Logger logger)
        {
            try
            {
                var creds = blob.ServiceClient.Credentials;
                var transformedUri = new Uri(creds.TransformUri(blob.Uri.ToString()));
                var req = BlobRequest.Lease(transformedUri, AzureBlobLeaseTimeout, // timeout (in seconds)
                    Microsoft.WindowsAzure.StorageClient.Protocol.LeaseAction.Acquire, // as opposed to "break" "release" or "renew"
                    null); // name of the existing lease, if any
                blob.ServiceClient.Credentials.SignRequest(req);
                using (var response = req.GetResponse())
                {
                    return response.Headers["x-ms-lease-id"];
                }
            }

            catch (WebException e)
            {
                if (null != logger)
                {
                    logger.ErrorException("AcquireLease, blob: " + blob, e);
                }
                return null;
            }
        }
        public static bool UploadConfig(string configZipPath, string AzureAccountName, string AzureAccountKey, string orgID, string studyID, string homeID, string desiredConfigFilename, NLog.Logger logger = null)
        {
            Microsoft.WindowsAzure.CloudStorageAccount storageAccount = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobClient blobClient = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobContainer container = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlockBlob blockBlob = null;
            string leaseId = null;

            try
            {
                storageAccount = new Microsoft.WindowsAzure.CloudStorageAccount(new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(AzureAccountName, AzureAccountKey), true);
                blobClient = storageAccount.CreateCloudBlobClient();
                container = blobClient.GetContainerReference(AzureConfigContainerName);
                container.CreateIfNotExist();
                blockBlob = container.GetBlockBlobReference(DesiredConfigBlobName(orgID, studyID, homeID, desiredConfigFilename));

                bool blobExists = BlockBlobExists(blockBlob);

                if (blobExists)
                    leaseId = AcquireLease(blockBlob, logger); // Acquire Lease on Blob
                else
                    blockBlob.Container.CreateIfNotExist();

                if (blobExists && leaseId == null)
                {
                    if (null != logger)
                    {
                        logger.Error("AcquireLease on Blob: " + DesiredConfigBlobName(orgID, studyID, homeID, desiredConfigFilename) + " Failed");
                    }
                    return false;
                }

                string url = blockBlob.Uri.ToString();
                if (blockBlob.ServiceClient.Credentials.NeedsTransformUri)
                {
                    url = blockBlob.ServiceClient.Credentials.TransformUri(url);
                }

                var req = BlobRequest.Put(new Uri(url), AzureBlobLeaseTimeout, new Microsoft.WindowsAzure.StorageClient.BlobProperties(), Microsoft.WindowsAzure.StorageClient.BlobType.BlockBlob, leaseId, 0);

                using (var writer = new BinaryWriter(req.GetRequestStream()))
                {
                    writer.Write(File.ReadAllBytes(configZipPath));
                    writer.Close();
                }

                blockBlob.ServiceClient.Credentials.SignRequest(req);
                req.GetResponse().Close();
                ReleaseLease(blockBlob, leaseId); // Release Lease on Blob
                return true;
            }
            catch (Exception e)
            {
                if (null != logger)
                {
                    logger.ErrorException("UploadConfig_Azure, configZipPath: " + configZipPath, e);
                }
                ReleaseLease(blockBlob, leaseId);
                return false;
            }
        }
示例#5
0
        internal static bool IsFtpBinaryVersionPresent(Uri uriRemoteDirPath, string version, string ftpUser, string ftpPassword, NLog.Logger logger)
        {
            bool present = false;
            Version versionCheck = new Version(version);
            try
            {
                string[] dirContents = SecureFtpRepoUpdate.ListDirectory(uriRemoteDirPath, ftpUser, ftpPassword, false /*details*/, true /*enableSSL*/);
                for (int i = 0; i < dirContents.Length; ++i)
                {
                    if (dirContents[i] == version)
                        continue;

                    Version ver = new Version(dirContents[i]);

                    if (ver == versionCheck)
                    {
                        present = true;
                        break;
                    }                        
                }
            }
            catch (Exception e)
            {
                logger.ErrorException("Failed to retrieve the latest version from remote ftp server", e);
            }

            return present;
        }
示例#6
0
        internal static Version GetFtpHighestVersionFromDir(Uri uriRemoteDirPath, string ftpUser, string ftpPassword, NLog.Logger logger)
        {
            string version = "0.0.0.0";
            Version highest = new Version(version);
            try
            {
                string[] dirContents = SecureFtpRepoUpdate.ListDirectory(uriRemoteDirPath, ftpUser, ftpPassword, false /*details*/, true /*enableSSL*/);
                for (int i = 0; i < dirContents.Length; ++i)
                {
                    if (dirContents[i] == "Latest")
                        continue;

                    Version ver = new Version(dirContents[i]);

                    if (ver > highest)
                        highest = ver;
                }
            }
            catch (Exception e)
            {
                logger.ErrorException("Failed to retrieve the latest version from remote ftp server", e);
             
            }

            return highest;
        }