static Application CreateApp(Site createdSite, IisSite site, IisSiteApplication app) { _logger.Info("Creating application {0}...", app.Name); if (app.AppPoolName == null) { app.AppPoolName = site.Name; } // If no service account was specifically assigned to this app use the default if (app.ServiceAccount == null) { app.ServiceAccount = site.ServiceAccount; } var appPool = _serverManager.ApplicationPools.SingleOrDefault(x => x.Name == app.AppPoolName); if (site.Options.DeleteExistingAppPools && appPool != null) { // If the option is set to delete existing app pools and this one exists, delete it _serverManager.ApplicationPools.Remove(appPool); appPool = null; } if (appPool == null) { _logger.Info("App pool {0} not found, creating...", app.AppPoolName); CreateAppPool(new IisApplicationPool { Name = app.AppPoolName, ServiceAccount = app.ServiceAccount, ManagedRuntimeVersion = site.ManagedRuntimeVersion }); } Application createdApp = createdSite.Applications.Add(app.Path, app.PhysicalPath); createdApp.VirtualDirectories[0].UserName = app.ServiceAccount.Username; createdApp.VirtualDirectories[0].Password = app.ServiceAccount.Password; createdApp.ApplicationPoolName = app.AppPoolName; if (app.VirtualDirectories != null) { _logger.Info(string.Format("Creating child application virtual directories...")); foreach (IisSiteVirtualDirectory vd in app.VirtualDirectories) { if (vd.ServiceAccount == null) { vd.ServiceAccount = app.ServiceAccount; } CreateVirtualDirectory(createdSite, createdApp, site, vd); } } _logger.Info("Application {0} created successfully!", app.Name); return(createdApp); }
static Site CreateSite(IisSite site) { Site createdSite; try { var existingSite = _serverManager.Sites.SingleOrDefault(x => x.Name == site.Name); _logger.Info(string.Format("Creating site {0}...", site.Name)); if (existingSite != null) { _logger.Info(string.Format("Site already exists")); if (site.Options.DeleteExistingSite) { _logger.Info("Deleting existing site..."); _serverManager.Sites.Remove(existingSite); } else { throw new Exception("IisSite.Options.DeleteExistingSite is FALSE, exiting."); } } if (site.Options.CreatePhysicalDirectory) { _logger.Info("Creating physical directory {0}...", site.PhysicalPath); Directory.CreateDirectory(site.PhysicalPath); } else { if (!Directory.Exists(site.PhysicalPath)) { _logger.Info("Warning, physical directory {0} does not exist.", site.PhysicalPath); } } _logger.Info(string.Format("Adding site to IIS with initial binding {0}:{1}...", site.DefaultBinding.Protocol, site.DefaultBinding.BindingInformation)); if (site.AppPoolName == null) { site.AppPoolName = site.Name; _logger.Info("No application pool name specificed, using site name {0}", site.AppPoolName); } var appPool = _serverManager.ApplicationPools.SingleOrDefault(x => x.Name == site.AppPoolName); if (site.Options.DeleteExistingAppPools && appPool != null) { // If the option is set to delete existing app pools and this one exists, delete it _serverManager.ApplicationPools.Remove(appPool); appPool = null; } if (appPool == null) { _logger.Info("App pool {0} not found, creating...", site.AppPoolName); CreateAppPool(new IisApplicationPool { Name = site.AppPoolName, ServiceAccount = site.ServiceAccount, ManagedRuntimeVersion = site.ManagedRuntimeVersion }); } // Creating a site requires an initial binding createdSite = _serverManager.Sites.Add(site.Name, site.DefaultBinding.Protocol, site.DefaultBinding.BindingInformation, site.PhysicalPath); var rootApp = createdSite.Applications.Single(x => x.Path == "/"); rootApp.VirtualDirectories[0].UserName = site.ServiceAccount.Username; rootApp.VirtualDirectories[0].Password = site.ServiceAccount.Password; rootApp.ApplicationPoolName = site.AppPoolName; // Add the rest of the bindings if (site.Bindings != null) { foreach (IisSiteBinding binding in site.Bindings) { _logger.Info(string.Format("Adding additional binding {0}:{1}...", binding.Protocol, binding.BindingInformation)); createdSite.Bindings.Add(binding.BindingInformation, binding.Protocol); } } _logger.Info(string.Format("Configuring additional site options...")); // Set auto start property createdSite.ServerAutoStart = site.ServerAutoStart.HasValue ? site.ServerAutoStart.Value : true; _logger.Info(string.Format("Creating root Virtual Directories...")); foreach (IisSiteVirtualDirectory vd in site.VirtualDirectories) { CreateVirtualDirectory(createdSite, rootApp, site, vd); } _logger.Info(string.Format("Creating child applications...")); foreach (IisSiteApplication app in site.Applications) { CreateApp(createdSite, site, app); } _logger.Info(string.Format("Site created successfully!")); return(createdSite); } catch (Exception ex) { _logger.Fatal(string.Format("Site creation failed! {0}", ex.Message)); throw; } }
static VirtualDirectory CreateVirtualDirectory(Site createdSite, Application createdApp, IisSite site, IisSiteVirtualDirectory virtualDir) { _logger.Info("Creating virtual directory {0}...", virtualDir.Name); // If certain properties were not specified, use the parent's if (virtualDir.ServiceAccount == null) { virtualDir.ServiceAccount = site.ServiceAccount; } VirtualDirectory newVirtualDir = createdApp.VirtualDirectories.Add(virtualDir.Path, virtualDir.PhysicalPath); newVirtualDir.UserName = virtualDir.ServiceAccount.Username; newVirtualDir.Password = virtualDir.ServiceAccount.Password; _logger.Info(string.Format("Creating child applications...")); if (virtualDir.Applications != null) { foreach (IisSiteApplication childApp in virtualDir.Applications) { if (childApp.ServiceAccount == null) { childApp.ServiceAccount = virtualDir.ServiceAccount; } CreateApp(createdSite, site, childApp); } } _logger.Info("Virtual directory {0} created successfully!", virtualDir.Name); return(newVirtualDir); }