internal ApplicationManager(ISiteManager siteManager, Site site, string appName, ISettingsResolver settingsResolver) { _siteManager = siteManager; _site = site; _appName = appName; _settingsResolver = settingsResolver; SiteUrl = site.SiteUrl; ServiceUrl = site.ServiceUrl; DeploymentManager = new RemoteDeploymentManager(site.ServiceUrl + "deployments"); SettingsManager = new RemoteDeploymentSettingsManager(site.ServiceUrl + "settings"); LogStreamManager = new RemoteLogStreamManager(site.ServiceUrl + "logstream"); SSHKeyManager = new RemoteSSHKeyManager(site.ServiceUrl + "sshkey"); VfsManager = new RemoteVfsManager(site.ServiceUrl + "vfs"); VfsWebRootManager = new RemoteVfsManager(site.ServiceUrl + "vfs/site/wwwroot"); LiveScmVfsManager = new RemoteVfsManager(site.ServiceUrl + "scmvfs"); ZipManager = new RemoteZipManager(site.ServiceUrl + "zip"); CommandExecutor = new RemoteCommandExecutor(site.ServiceUrl + "command"); ProcessManager = new RemoteProcessManager(site.ServiceUrl + "diagnostics/processes"); WebHooksManager = new RemoteWebHooksManager(site.ServiceUrl + "hooks"); RepositoryManager = new RemoteRepositoryManager(site.ServiceUrl + "scm"); var repositoryInfo = RepositoryManager.GetRepositoryInfo().Result; GitUrl = repositoryInfo.GitUrl.OriginalString; }
private async Task <ResourceGroup> LogActiveUsageStatistics(ResourceGroup resourceGroup) { try { var site = resourceGroup.Sites.First(s => s.IsSimpleWAWSOriginalSite); var credentials = new NetworkCredential(site.PublishingUserName, site.PublishingPassword); var zipManager = new RemoteZipManager(site.ScmUrl + "zip/", credentials); using (var httpContentStream = await zipManager.GetZipFileStreamAsync("LogFiles/http/RawLogs")) { await StorageHelper.UploadBlob(resourceGroup.ResourceUniqueId, httpContentStream); } await StorageHelper.AddQueueMessage(new { BlobName = resourceGroup.ResourceUniqueId }); SimpleTrace.TraceInformation("{0}; {1}", AnalyticsEvents.SiteIISLogsName, resourceGroup.ResourceUniqueId); } catch (Exception e) { if (!(e is HttpRequestException)) { SimpleTrace.Diagnostics.Error(e, "Error logging active usage numbers"); } } return(resourceGroup); }
internal ApplicationManager(ISiteManager siteManager, Site site, string appName, ISettingsResolver settingsResolver) { _siteManager = siteManager; _site = site; _appName = appName; _settingsResolver = settingsResolver; // Always null in public Kudu, but makes the code more similar to private Kudu NetworkCredential credentials = null; SiteUrl = site.SiteUrl.ToString(); ServiceUrl = site.ServiceUrl.ToString(); DeploymentManager = new RemoteDeploymentManager(site.ServiceUrl + "api", credentials); SettingsManager = new RemoteDeploymentSettingsManager(site.ServiceUrl + "api/settings", credentials); LegacySettingsManager = new RemoteDeploymentLegacySettingsManager(site.ServiceUrl + "settings", credentials); LogStreamManager = new RemoteLogStreamManager(site.ServiceUrl + "api/logstream", credentials); SSHKeyManager = new RemoteSSHKeyManager(site.ServiceUrl + "api/sshkey", credentials); VfsManager = new RemoteVfsManager(site.ServiceUrl + "api/vfs", credentials); VfsWebRootManager = new RemoteVfsManager(site.ServiceUrl + "api/vfs/site/wwwroot", credentials); LiveScmVfsManager = new RemoteVfsManager(site.ServiceUrl + "api/scmvfs", credentials); ZipManager = new RemoteZipManager(site.ServiceUrl + "api/zip", credentials); RuntimeManager = new RemoteRuntimeManager(site.ServiceUrl + "api/diagnostics/runtime", credentials); CommandExecutor = new RemoteCommandExecutor(site.ServiceUrl + "api/command", credentials); ProcessManager = new RemoteProcessManager(site.ServiceUrl + "api/processes", credentials); WebHooksManager = new RemoteWebHooksManager(site.ServiceUrl + "api/hooks", credentials); RepositoryManager = new RemoteRepositoryManager(site.ServiceUrl + "api/scm", credentials); JobsManager = new RemoteJobsManager(site.ServiceUrl + "api", credentials); LogFilesManager = new RemoteLogFilesManager(site.ServiceUrl + "api/logs", credentials); SiteExtensionManager = new RemoteSiteExtensionManager(site.ServiceUrl + "api", credentials); var repositoryInfo = RepositoryManager.GetRepositoryInfo().Result; GitUrl = repositoryInfo.GitUrl.OriginalString; }
public static async Task <Stream> GetSiteContent(this Site site) { Validate.ValidateCsmSite(site); var credentials = new NetworkCredential(site.PublishingUserName, site.PublishingPassword); var zipManager = new RemoteZipManager(site.ScmUrl + "zip/", credentials, retryCount: 3); return(await zipManager.GetZipFileStreamAsync("site/wwwroot")); }
private static async Task PublishCustomSiteExtensions(Site site) { var credentials = new NetworkCredential(site.PublishingUserName, site.PublishingPassword); var zipManager = new RemoteZipManager($"{site.ScmUrl}zip/", credentials, retryCount: 3); var appDataFolder = HostingEnvironment.MapPath(@"~/App_Data/SiteExtensions"); await zipManager.PutZipFileAsync(string.Empty, Path.Combine(appDataFolder, "Kudu.zip")); await zipManager.PutZipFileAsync(string.Empty, Path.Combine(appDataFolder, "AzureFunctions.zip")); site.AppSettings[Constants.SiteExtensionsVersion] = Constants.CurrentSiteExtensionsVersion; await site.UpdateAppSettings(); }
public static async Task DeployLinuxTemplateToSite(BaseTemplate template, Site site) { if (template?.MSDeployPackageUrl != null) { try { var credentials = new NetworkCredential(site.PublishingUserName, site.PublishingPassword); var zipManager = new RemoteZipManager(site.ScmUrl + "zip/", credentials, retryCount: 3); Task zipUpload = zipManager.PutZipFileAsync("site/wwwroot", template.MSDeployPackageUrl); var vfsManager = new RemoteVfsManager(site.ScmUrl + "vfs/", credentials, retryCount: 3); Task deleteHostingStart = vfsManager.Delete("site/wwwroot/hostingstart.html"); await Task.WhenAll(zipUpload); if (template.Name.Equals(Constants.PHPWebAppLinuxTemplateName, StringComparison.OrdinalIgnoreCase)) { await site.UpdateConfig( new { properties = new { linuxFxVersion = "PHP|7.2", appCommandLine = "process.json", alwaysOn = true, httpLoggingEnabled = true } }); } else { await site.UpdateConfig( new { properties = new { linuxFxVersion = "NODE|9.4", appCommandLine = "process.json", alwaysOn = true, httpLoggingEnabled = true } }); } } catch (Exception ex) { SimpleTrace.TraceError(ex.Message + ex.StackTrace); } } }
public async Task <ResourceGroup> ActivateLinuxResource(LinuxTemplate template, TryWebsitesIdentity userIdentity, string anonymousUserName) { return(await ActivateResourceGroup(userIdentity, AppService.Web, true, DeploymentType.CsmDeploy, async (resourceGroup, inProgressOperation) => { SimpleTrace.TraceInformation("{0}; {1}; {2}; {3}; {4}", AnalyticsEvents.OldUserCreatedSiteWithLanguageAndTemplateName, "Linux", template.Name, resourceGroup.ResourceUniqueId, AppService.Web.ToString()); var site = resourceGroup.Sites.First(s => s.IsSimpleWAWSOriginalSite); resourceGroup.Tags[Constants.TemplateName] = template.Name; resourceGroup = await resourceGroup.Update(); if (template?.MSDeployPackageUrl != null) { try { var credentials = new NetworkCredential(site.PublishingUserName, site.PublishingPassword); var zipManager = new RemoteZipManager(site.ScmUrl + "zip/", credentials, retryCount: 3); Task zipUpload = zipManager.PutZipFileAsync("site/wwwroot", template.MSDeployPackageUrl); var vfsManager = new RemoteVfsManager(site.ScmUrl + "vfs/", credentials, retryCount: 3); Task deleteHostingStart = vfsManager.Delete("site/wwwroot/hostingstart.html"); await Task.WhenAll(zipUpload, deleteHostingStart); } catch (Exception ex) { SimpleTrace.TraceError(ex.Message + ex.StackTrace); } } if (template.Name.Equals(Constants.NodeJSWebAppLinuxTemplateName, StringComparison.OrdinalIgnoreCase)) { await site.UpdateConfig(new { properties = new { linuxFxVersion = "NODE|6.10", appCommandLine = "process.json" } }); } Util.FireAndForget($"{resourceGroup.Sites.FirstOrDefault().HostName}"); Util.FireAndForget($"{resourceGroup.Sites.FirstOrDefault().ScmHostName}"); var rbacTask = resourceGroup.AddResourceGroupRbac(userIdentity.Puid, userIdentity.Email); resourceGroup.IsRbacEnabled = await rbacTask; return resourceGroup; })); }
// ARM public async Task <ResourceGroup> ActivateWebApp(WebsiteTemplate template, TryWebsitesIdentity userIdentity, string anonymousUserName, AppService temp = AppService.Web) { // Start site specific stuff var deploymentType = template != null && template.GithubRepo != null ? DeploymentType.GitWithCsmDeploy : DeploymentType.ZipDeploy; return(await ActivateResourceGroup(userIdentity, temp, deploymentType, async (resourceGroup, inProgressOperation) => { SimpleTrace.Analytics.Information(AnalyticsEvents.UserCreatedSiteWithLanguageAndTemplateName, userIdentity, template, resourceGroup.CsmId); SimpleTrace.TraceInformation("{0}; {1}; {2}; {3}; {4}; {5}; {6}", AnalyticsEvents.OldUserCreatedSiteWithLanguageAndTemplateName, userIdentity.Name, template.Language, template.Name, resourceGroup.ResourceUniqueId, temp.ToString(), anonymousUserName); SimpleTrace.UserCreatedApp(userIdentity, template, resourceGroup, temp); var site = resourceGroup.Sites.First(s => s.IsSimpleWAWSOriginalSite); var rbacTask = resourceGroup.AddResourceGroupRbac(userIdentity.Puid, userIdentity.Email); if (template != null && template.FileName != null) { var credentials = new NetworkCredential(site.PublishingUserName, site.PublishingPassword); var zipManager = new RemoteZipManager(site.ScmUrl + "zip/", credentials, retryCount: 3); Task zipUpload = zipManager.PutZipFileAsync("site/wwwroot", template.GetFullPath()); var vfsManager = new RemoteVfsManager(site.ScmUrl + "vfs/", credentials, retryCount: 3); Task deleteHostingStart = vfsManager.Delete("site/wwwroot/hostingstart.html"); await Task.WhenAll(zipUpload, deleteHostingStart); } else if (template != null && template.GithubRepo != null) { Uri githubRepo; var validUri = Uri.TryCreate(template.GithubRepo, UriKind.Absolute, out githubRepo); if (validUri && (githubRepo.AbsoluteUri.StartsWith("https://github.com/davidebbo-test/") || githubRepo.AbsoluteUri.StartsWith("https://github.com/ahmelsayed-test"))) { //Do CSM template deployment var csmTemplate = new CsmTemplateWrapper { properties = new CsmTemplateProperties { mode = "Incremental", parameters = new { siteName = new CsmTemplateParameter(site.SiteName), hostingPlanName = new CsmTemplateParameter(resourceGroup.ServerFarms.Select(sf => sf.ServerFarmName).FirstOrDefault()), repoUrl = new CsmTemplateParameter(githubRepo.AbsoluteUri) }, templateLink = new CsmTemplateLink { contentVersion = "1.0.0.0", uri = new Uri("https://raw.githubusercontent.com/" + githubRepo.AbsolutePath.Trim('/') + "/master/azuredeploy.json") } } }; await inProgressOperation.CreateDeployment(csmTemplate, block: true); await site.GetKuduDeploymentStatus(block: true); await resourceGroup.Load(); } else if (validUri && githubRepo.AbsoluteUri.StartsWith("https://github.com/")) { //Do Kudu deployment throw new InvalidGithubRepoException(); } else { throw new InvalidGithubRepoException(); } } resourceGroup.Tags[Constants.TemplateName] = template.Name; site.AppSettings["LAST_MODIFIED_TIME_UTC"] = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture); site.AppSettings["SITE_LIFE_TIME_IN_MINUTES"] = SimpleSettings.SiteExpiryMinutes; site.AppSettings["MONACO_EXTENSION_VERSION"] = SimpleSettings.MonacoExtensionVersion; site.AppSettings["WEBSITE_TRY_MODE"] = "1"; if (site.AppSettings.ContainsKey("FUNCTIONS_EXTENSION_VERSION")) { site.AppSettings.Remove("FUNCTIONS_EXTENSION_VERSION"); } if (template.Name.Equals("ASP.NET with Azure Search Site", StringComparison.OrdinalIgnoreCase)) { site.AppSettings["SearchServiceName"] = SimpleSettings.SearchServiceName; site.AppSettings["SearchServiceApiKey"] = AzureSearchHelper.GetApiKey(); } else if (template.Name.Equals("PHP Starter Site", StringComparison.OrdinalIgnoreCase)) { //Enable ZRay await site.EnableZRay(resourceGroup.GeoRegion); } await Task.WhenAll(site.UpdateAppSettings(), resourceGroup.Update()); if (template.GithubRepo == null) { if (site.IsFunctionsContainer) { await site.UpdateConfig(new { properties = new { scmType = "None", httpLoggingEnabled = true } }); } else { await site.UpdateConfig(new { properties = new { scmType = "LocalGit", httpLoggingEnabled = true } }); } } resourceGroup.IsRbacEnabled = await rbacTask; site.FireAndForget(); return resourceGroup; })); }
// ARM private async Task LogActiveUsageStatistics(ResourceGroup resourceGroup) { try { var site = resourceGroup.Sites.First(); var credentials = new NetworkCredential(site.PublishingUserName, site.PublishingPassword); var zipManager = new RemoteZipManager(site.ScmUrl + "zip/", credentials); using (var httpContentStream = await zipManager.GetZipFileStreamAsync("LogFiles/http/RawLogs")) { await StorageHelper.UploadBlob(resourceGroup.ResourceUniqueId, httpContentStream); } await StorageHelper.AddQueueMessage(new { BlobName = resourceGroup.ResourceUniqueId }); } catch { } }
// ARM public async Task<ResourceGroup> ActivateWebApp(WebsiteTemplate template, TryWebsitesIdentity userIdentity, string anonymousUserName, AppService temp = AppService.Web) { // Start site specific stuff var deploymentType = template != null && template.GithubRepo != null ? DeploymentType.GitWithCsmDeploy : DeploymentType.ZipDeploy; return await ActivateResourceGroup(userIdentity, temp, deploymentType, async (resourceGroup, inProgressOperation) => { SimpleTrace.Analytics.Information(AnalyticsEvents.UserCreatedSiteWithLanguageAndTemplateName, userIdentity, template, resourceGroup.CsmId); SimpleTrace.TraceInformation("{0}; {1}; {2}; {3}; {4}; {5}; {6}", AnalyticsEvents.OldUserCreatedSiteWithLanguageAndTemplateName, userIdentity.Name, template.Language, template.Name, resourceGroup.ResourceUniqueId, temp.ToString(), anonymousUserName); var site = resourceGroup.Sites.First(s => s.IsSimpleWAWSOriginalSite); var rbacTask = resourceGroup.AddResourceGroupRbac(userIdentity.Puid, userIdentity.Email); if (template != null && template.FileName != null) { var credentials = new NetworkCredential(site.PublishingUserName, site.PublishingPassword); var zipManager = new RemoteZipManager(site.ScmUrl + "zip/", credentials, retryCount: 3); Task zipUpload = zipManager.PutZipFileAsync("site/wwwroot", template.GetFullPath()); var vfsManager = new RemoteVfsManager(site.ScmUrl + "vfs/", credentials, retryCount: 3); Task deleteHostingStart = vfsManager.Delete("site/wwwroot/hostingstart.html"); await Task.WhenAll(zipUpload, deleteHostingStart); } else if (template != null && template.GithubRepo != null) { Uri githubRepo; var validUri = Uri.TryCreate(template.GithubRepo, UriKind.Absolute, out githubRepo); if (validUri && (githubRepo.AbsoluteUri.StartsWith("https://github.com/davidebbo-test/") || githubRepo.AbsoluteUri.StartsWith("https://github.com/ahmelsayed-test"))) { //Do CSM template deployment var csmTemplate = new CsmTemplateWrapper { properties = new CsmTemplateProperties { mode = "Incremental", parameters = new { siteName = new CsmTemplateParameter(site.SiteName), hostingPlanName = new CsmTemplateParameter(resourceGroup.ServerFarms.Select(sf => sf.ServerFarmName).FirstOrDefault()), repoUrl = new CsmTemplateParameter(githubRepo.AbsoluteUri) }, templateLink = new CsmTemplateLink { contentVersion = "1.0.0.0", uri = new Uri("https://raw.githubusercontent.com/" + githubRepo.AbsolutePath.Trim('/') + "/master/azuredeploy.json") } } }; await inProgressOperation.CreateDeployment(csmTemplate, block: true); await site.GetKuduDeploymentStatus(block: true); await resourceGroup.Load(); } else if (validUri && githubRepo.AbsoluteUri.StartsWith("https://github.com/")) { //Do Kudu deployment throw new InvalidGithubRepoException(); } else { throw new InvalidGithubRepoException(); } } site.AppSettings["LAST_MODIFIED_TIME_UTC"] = DateTime.UtcNow.ToString("u"); site.AppSettings["SITE_LIFE_TIME_IN_MINUTES"] = SimpleSettings.SiteExpiryMinutes; site.AppSettings["MONACO_EXTENSION_VERSION"] = "beta"; site.AppSettings["WEBSITE_TRY_MODE"] = "1"; if (template.Name.Equals("ASP.NET + Azure Search Site", StringComparison.OrdinalIgnoreCase)) { site.AppSettings["SearchServiceName"] = SimpleSettings.SearchServiceName; site.AppSettings["SearchServiceApiKey"] = AzureSearchHelper.GetApiKey(); } else if (template.Name.Equals("PHP Starter Site", StringComparison.OrdinalIgnoreCase)) { //Enable ZRay await site.EnableZRay(resourceGroup.GeoRegion); } await site.UpdateAppSettings(); if (template.GithubRepo == null) { await site.UpdateConfig(new { properties = new { scmType = "LocalGit", httpLoggingEnabled = true } }); } resourceGroup.IsRbacEnabled = await rbacTask; site.FireAndForget(); return resourceGroup; }); }
public static async Task DeployVSCodeLinuxTemplateToSite(BaseTemplate template, Site site) { SimpleTrace.TraceInformation($"Site ZipDeploy started: for {template?.MSDeployPackageUrl} on {site.SiteName}->{site.ResourceGroupName}->{site.SubscriptionId}"); if (template?.MSDeployPackageUrl != null) { try { var credentials = new NetworkCredential(site.PublishingUserName, site.PublishingPassword); var zipManager = new RemoteZipManager(site.ScmUrl + "api/zipdeploy?isAsync=true", credentials, retryCount: 3); Task <Uri> zipUpload = zipManager.PostZipFileAsync("", template.MSDeployPackageUrl); var deploystatusurl = await zipUpload; SimpleTrace.TraceInformation($"Site ZipDeployed: StatusUrl: {deploystatusurl} for {template?.MSDeployPackageUrl} on {site.SiteName}->{site.ResourceGroupName}->{site.SubscriptionId}"); var deploycheck = 0; var deploycheckTimes = 150; while (deploycheck++ < deploycheckTimes) { try { await Task.Delay(10 * 1000); var url = site.MonacoUrl.Replace(@"/basicauth", deploystatusurl.PathAndQuery); var httpClient = (HttpWebRequest)WebRequest.Create(url); { var creds = $"{site.PublishingUserName }:{ site.PublishingPassword}"; var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(creds); var credsbase64 = System.Convert.ToBase64String(plainTextBytes); httpClient.Headers.Add($"Authorization: Basic {credsbase64}"); using (var response = await httpClient.GetResponseAsync()) { using (var content = new StreamReader(response.GetResponseStream())) { var message = Newtonsoft.Json.Linq.JObject.Parse(content.ReadToEnd()); if ((bool)message["complete"] == false) { var failed = $"Zip Deployment going on: StatusUrl: {deploystatusurl} -{JsonConvert.SerializeObject(message)} for {template?.MSDeployPackageUrl} on {site.SiteName}->{site.ResourceGroupName}->{site.SubscriptionId}"; SimpleTrace.TraceInformation(failed); File.AppendAllLines(HostingEnvironment.MapPath(string.Format(CultureInfo.InvariantCulture, "~/App_data/filestatus.txt")), new string [] { failed }); } else { var success = ($"Zip Deployment completed: StatusUrl: {deploystatusurl} -{JsonConvert.SerializeObject(message)} for {template?.MSDeployPackageUrl} on {site.SiteName}->{site.ResourceGroupName}->{site.SubscriptionId}"); SimpleTrace.TraceInformation(success); File.AppendAllLines(HostingEnvironment.MapPath(string.Format(CultureInfo.InvariantCulture, "~/App_data/filestatus.txt")), new string[] { success }); break; } } } } } catch (Exception ex) { var exception = ($"Ping post ZipDeployed: StatusUrl: {deploystatusurl} for {template?.MSDeployPackageUrl} on {site.SiteName}->{site.ResourceGroupName}->{site.SubscriptionId} -> {ex.Message}->{ex.StackTrace}s"); SimpleTrace.TraceError(exception); File.AppendAllLines(HostingEnvironment.MapPath(string.Format(CultureInfo.InvariantCulture, "~/App_data/filestatus.txt")), new string[] { exception }); } } File.AppendAllLines(HostingEnvironment.MapPath(string.Format(CultureInfo.InvariantCulture, "~/App_data/filestatus.txt")), new string[] { "Starting hostingstart file delete" }); var vfsManager = new RemoteVfsManager(site.ScmUrl + "vfs/", credentials, retryCount: 3); Task deleteHostingStart = vfsManager.Delete("site/wwwroot/hostingstart.html"); List <Task> taskList = new List <Task>(); //taskList.Add(zipUpload); taskList.Add(deleteHostingStart); //if (template.Name == Constants.ReactVSCodeWebAppLinuxTemplateName) //{ // Task uploaddeploymentfile = vfsManager.Put("site/wwwroot/deploy.sh", (template?.MSDeployPackageUrl.Replace("ReactVSCodeWebApp.zip", "deploy.sh"))); // Task uploaddeploysh = vfsManager.Put("site/wwwroot/.deployment", (template?.MSDeployPackageUrl.Replace("ReactVSCodeWebApp.zip", ".deployment"))); // taskList.Add(uploaddeploymentfile); // taskList.Add(uploaddeploysh); //} //if (addTimeStampFile) //{ // SimpleTrace.TraceInformation($"Adding TimeStamp File started: for {site.SiteName}->{site.ResourceGroupName}->{site.SubscriptionId}"); // Task timeStampAdd = AddTimeStampFile(site, ""); // taskList.Add(timeStampAdd); //} await Task.WhenAll(taskList.ToArray()); SimpleTrace.TraceInformation($"Site ZipDeploy and Delete HostingStart completed: for {site.SiteName}->{site.ResourceGroupName}->{site.SubscriptionId}"); File.AppendAllLines(HostingEnvironment.MapPath(string.Format(CultureInfo.InvariantCulture, "~/App_data/filestatus.txt")), new string[] { "Starting hostingstart file delete" }); await Task.Delay(10 * 1000); } catch (Exception ex) { var message = "New Site wasnt deployed: " + ex.Message + ex.StackTrace; SimpleTrace.TraceError(message); throw new ZipDeploymentFailedException(message); } } else { SimpleTrace.TraceError("New Site wasnt deployed: MsDeployPackageUrl wasnt set"); } }