public static string GetLogUrlWithId(this DeploymentResponse deploymentResponse) { if (deploymentResponse is null || string.IsNullOrEmpty(deploymentResponse.LogUrl) || string.IsNullOrEmpty(deploymentResponse.Id)) { return(deploymentResponse?.LogUrl); } try { Uri logUrl = new(deploymentResponse.LogUrl); string pathAndQuery = logUrl.PathAndQuery; // try to replace '../latest/log' with '../{deploymentResponse.Id}/log' if (!string.IsNullOrEmpty(pathAndQuery)) { string[] pathAndQueryParts = pathAndQuery.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); string[] pathWithIdParts = new string[pathAndQueryParts.Length]; for (int i = pathAndQueryParts.Length - 1; i >= 0; i--) { if (string.Equals("latest", pathAndQueryParts[i], StringComparison.Ordinal)) { pathWithIdParts[i] = deploymentResponse.Id; continue; } pathWithIdParts[i] = pathAndQueryParts[i].Trim(); } return(new UriBuilder() { Scheme = logUrl.Scheme, Host = logUrl.Host, Path = string.Join("/", pathWithIdParts) }.ToString()); } } catch { // do nothing } return(deploymentResponse.LogUrl); }
public async Task <DeploymentResponse> PollDeploymentStatusAsync(string deploymentUrl, string userName, string password) { var tokenSource = new CancellationTokenSource(TimeSpan.FromMinutes(MaxMinutesToWait)); if (_logMessages) { _log.LogMessage(Resources.ZIPDEPLOY_DeploymentStatusPolling); } DeploymentResponse deploymentResponse = null; DeployStatus? deployStatus = DeployStatus.Pending; while (!tokenSource.IsCancellationRequested && deployStatus != DeployStatus.Success && deployStatus != DeployStatus.Failed && deployStatus != DeployStatus.Unknown) { try { deploymentResponse = await InvokeGetRequestWithRetryAsync <DeploymentResponse>( deploymentUrl, userName, password, RetryCount, TimeSpan.FromSeconds(RetryDelaySeconds), tokenSource); deployStatus = deploymentResponse?.Status is not null ? deploymentResponse.Status : DeployStatus.Unknown; if (_logMessages) { _log.LogMessage(string.Format(Resources.ZIPDEPLOY_DeploymentStatus, Enum.GetName(typeof(DeployStatus), deployStatus))); } } catch (HttpRequestException) { return(deploymentResponse); } await Task.Delay(TimeSpan.FromSeconds(StatusRefreshDelaySeconds)); } return(deploymentResponse ?? new() { Status = DeployStatus.Unknown });
public async System.Threading.Tasks.Task <bool> ZipDeployAsync(string zipToPublishPath, string userName, string password, string publishUrl, string siteName, string userAgentVersion, IHttpClient client, bool logMessages) { if (!File.Exists(zipToPublishPath) || client == null) { return(false); } string zipDeployPublishUrl = null; if (!string.IsNullOrEmpty(publishUrl)) { if (!publishUrl.EndsWith("/")) { publishUrl += "/"; } zipDeployPublishUrl = publishUrl + "api/zipdeploy"; } else if (!string.IsNullOrEmpty(siteName)) { zipDeployPublishUrl = $"https://{siteName}.scm.azurewebsites.net/api/zipdeploy"; } else { if (logMessages) { Log.LogError(Resources.ZIPDEPLOY_InvalidSiteNamePublishUrl); } return(false); } if (logMessages) { Log.LogMessage(MessageImportance.High, string.Format(Resources.ZIPDEPLOY_PublishingZip, zipToPublishPath, zipDeployPublishUrl)); } // use the async version of the api Uri uri = new Uri($"{zipDeployPublishUrl}?isAsync=true", UriKind.Absolute); string userAgent = $"{UserAgentName}/{userAgentVersion}"; FileStream stream = File.OpenRead(zipToPublishPath); IHttpResponse response = await client.PostWithBasicAuthAsync(uri, userName, password, "application/zip", userAgent, Encoding.UTF8, stream); if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Accepted) { if (logMessages) { Log.LogError(string.Format(Resources.ZIPDEPLOY_FailedDeploy, zipDeployPublishUrl, response.StatusCode)); } return(false); } else { if (logMessages) { Log.LogMessage(Resources.ZIPDEPLOY_Uploaded); } string deploymentUrl = response.GetHeader("Location").FirstOrDefault(); if (!string.IsNullOrEmpty(deploymentUrl)) { ZipDeploymentStatus deploymentStatus = new(client, userAgent, Log, logMessages); DeploymentResponse deploymentResponse = await deploymentStatus.PollDeploymentStatusAsync(deploymentUrl, userName, password); if (deploymentResponse?.Status == DeployStatus.Success) { Log.LogMessage(MessageImportance.High, Resources.ZIPDEPLOY_Succeeded); return(true); } else if (deploymentResponse is null || deploymentResponse?.Status == DeployStatus.Failed || deploymentResponse?.Status == DeployStatus.Unknown) { Log.LogError(string.Format(Resources.ZIPDEPLOY_Failed, zipDeployPublishUrl, deploymentResponse?.Status ?? DeployStatus.Unknown, deploymentResponse?.GetLogUrlWithId())); return(false); } } } return(true); }