private static async Task LoadDefinitionsFromWebAsync(AmazonEC2Config ec2Config)
{
lock (LOCK_OBJECT)
{
if (ImageDefinitionsLoaded)
return;
}
IWebProxy webProxy = null;
if (ec2Config != null)
webProxy = ec2Config.GetWebProxy();
int retries = 0;
while (retries < MAX_DOWNLOAD_RETRIES)
{
try
{
HttpWebResponse response = null;
foreach (var location in DownloadLocations)
{
try
{
response = await DownloadControlFileAsync(location, webProxy).ConfigureAwait(false);
if (response != null)
break;
}
catch (Exception e)
{
Logger.InfoFormat("Failed to download stockamis.json from {0}, exception {1}", location, e);
}
}
if (response == null)
throw new AmazonClientException("Failed to download ImageUtilities metadata file stockamis.json from known locations.");
using (response)
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
lock (LOCK_OBJECT)
{
ParseAMIDefinitions(reader);
ImageDefinitionsLoaded = true;
return;
}
}
}
}
catch (AmazonClientException e)
{
retries++;
if (retries == MAX_DOWNLOAD_RETRIES)
{
Logger.Error(e, "Error downloading AMI definition file, ImageDescriptors were not initialized.");
break;
}
}
int delay = (int)(Math.Pow(4, retries) * 100);
delay = Math.Min(delay, 30 * 1000);
await Task.Delay(delay).ConfigureAwait(false);
}
}
private static async Task<HttpWebResponse> DownloadControlFileAsync(string location, IWebProxy proxy)