public IReportStatus Execute(ServerConfig server, IReportStatus status, ConDepOptions options) { try { Logger.LogSectionStart(_compositeName); foreach (var element in _sequence) { if (element is CompositeSequence) { ((CompositeSequence)element).Execute(server, status, options); } else if (element is IOperateRemote) { ((IOperateRemote)element).Execute(server, status, options); } if (status.HasErrors) return status; } } finally { Logger.LogSectionEnd(_compositeName); } return status; }
public IReportStatus Execute(ServerConfig server, IReportStatus status, ConDepOptions options) { try { Logger.LogSectionStart("Infrastructure"); foreach (var element in _sequence) { if (element is IOperateRemote) { ((IOperateRemote)element).Execute(server, status, options); if (status.HasErrors) return status; } else if (element is CompositeSequence) { ((CompositeSequence)element).Execute(server, status, options); } else { throw new NotSupportedException(); } if (status.HasErrors) return status; } } finally { Logger.LogSectionEnd("Infrastructure"); } return status; }
public PreRemoteOps(ServerConfig server, PreOpsSequence sequence, ConDepOptions options, IHandleWebDeploy webDeploy) { _server = server; _sequence = sequence; _options = options; _webDeploy = webDeploy; }
private static bool HaveAccessToServer(ServerConfig server) { Logger.Info(string.Format("Checking if WMI can be used to reach remote server [{0}]...", server.Name)); var success = false; try { var registry = new RemoteRegistry(server.Name, server.DeploymentUser.UserName, server.DeploymentUser.Password); string windowsName; success = registry.TryGetStringValue(RegistryHive.LocalMachine, @"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName", out windowsName); if(success) { Logger.Info(string.Format("Contact was made with server [{0}] using WMI. Server is {1}.", server.Name, windowsName)); } else { Logger.Error(string.Format("Unable to reach server [{0}] using WMI", server.Name)); } } catch(UnauthorizedAccessException accessException) { Logger.Error(string.Format("Unable to access remote server [{0}] using WMI. Unauthorized Access Exception reported. Please check your credentials.", server.Name), accessException); return false; } catch(Exception ex) { Logger.Error(string.Format("Unable to access remote server [{0}] using WMI.", server.Name), ex); return false; } return success; }
public override void BringOnline(ServerConfig server, IReportStatus status, ConDepSettings settings, CancellationToken token) { if (server.LoadBalancerState.KeepOffline) return; BringOnline(server, status, settings, _loadBalancer, token); }
public void Execute(ServerConfig server, IReportStatus status, ConDepSettings settings, CancellationToken token) { var assemblyLocalDir = Path.GetDirectoryName(GetType().Assembly.Location); var assemblyRemoteDir = Path.Combine(server.GetServerInfo().TempFolderDos, "Assemblies"); _filePublisher.PublishDirectory(assemblyLocalDir, assemblyRemoteDir, server, settings); var remoteAssemblyFileName = Path.Combine(Path.Combine(server.GetServerInfo().TempFolderPowerShell, "Assemblies"), Path.GetFileName(GetType().Assembly.Location)); var remoteJsonAssembly = Path.Combine(Path.Combine(server.GetServerInfo().TempFolderPowerShell, "Assemblies"), "Newtonsoft.Json.dll"); var typeName = GetType().FullName; var loggerTypeName = typeof (RemotePowerShellLogger).FullName; var parameters = GetPowerShellParameters(ConstructorArguments, GetType()).ToList(); var scriptParams = string.Join(",", parameters.Select(x => "$" + x.Name)); var argumentList = string.Join(",", GetType().GetConstructor(ConstructorArguments.Select(x => x.GetType()).ToArray()).GetParameters().Select(x => "$" + x.Name)); var deserializeScript = GetDeserializationScript(GetType().GetConstructor(ConstructorArguments.Select(x => x.GetType()).ToArray())); var script = string.Format(@" Param({3}) add-type -path {0} add-type -path {5} {4} $operation = new-object -typename {1} -ArgumentList {6} $logger = new-object -typename {2} -ArgumentList (Get-Host).UI $operation.Execute($logger) ", remoteAssemblyFileName, typeName, loggerTypeName, scriptParams, deserializeScript, remoteJsonAssembly, argumentList); _psExecutor.Execute(server, script, mod => mod.LoadConDepModule = false, parameters); }
public CredSSPHandler(WSManConnectionInfo connectionInfo, ServerConfig server) { _server = server; connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Credssp; EnableCredSSP(); }
public void StartNode(ServerConfig server) { _psExecutor.Execute(server, "Start-ConDepNode", mod => { mod.LoadConDepNodeModule = true; mod.LoadConDepModule = false; }, logOutput: false); }
public bool ValidateNode(ConDepNodeUrl url, string userName, string password, ServerConfig server) { var api = new Api(url, userName, password, server.Node.TimeoutInSeconds.Value * 1000); if (!api.Validate()) { Thread.Sleep(1000); return api.Validate(); } return true; }
public void Test() { var user = new DeploymentUserConfig(); var server = new ServerConfig() {Name = "jat-web03", DeploymentUserRemote = user }; using(new WebDeployDeployer(server)) { } }
protected void BringOnline(ServerConfig server, IReportStatus status, ConDepSettings settings, ILoadBalance loadBalancer, CancellationToken token) { if (settings.Config.LoadBalancer == null) return; if (server.LoadBalancerState.CurrentState == LoadBalanceState.Online) return; Logger.WithLogSection(string.Format("Taking server [{0}] online in load balancer.", server.Name), () => { loadBalancer.BringOnline(server.Name, server.LoadBalancerFarm, status); server.LoadBalancerState.CurrentState = LoadBalanceState.Online; }); }
public IEnumerable<dynamic> ExecuteLocal(ServerConfig localServer, string commandOrScript, Action<PowerShellModulesToLoad> modulesToLoad = null, IEnumerable<CommandParameter> parameters = null, bool logOutput = true) { var connectionInfo = new WSManConnectionInfo(); var modules = new PowerShellModulesToLoad(); if (modulesToLoad != null) { modulesToLoad(modules); } var folders = new RemoteScriptFolders(localServer); return ExecuteCommand(commandOrScript, connectionInfo, modules, folders, parameters, logOutput); }
public override void Execute(ServerConfig server, IReportStatus status, ConDepSettings settings, CancellationToken token) { var createFolderScript = string.Format(@" if(!(Test-Path ""{0}"")) {{ New-Item -ItemType directory -Path ""{0}"" }} ", _path); var psExecutor = new PowerShellExecutor(); psExecutor.Execute(server, createFolderScript); }
public void TestThatRoundRobinLoadBalancingGoesOnlineOfflineInCorrectOrder() { var config = new ConDepConfig { EnvironmentName = "bogusEnv" }; var server1 = new ServerConfig { Name = "jat-web01" }; var server2 = new ServerConfig { Name = "jat-web02" }; var server3 = new ServerConfig { Name = "jat-web03" }; var server4 = new ServerConfig { Name = "jat-web04" }; var server5 = new ServerConfig { Name = "jat-web05" }; config.Servers = new[] { server1, server2, server3, server4, server5 }; var infrastructureSequence = new InfrastructureSequence(); var preOpsSequence = new PreOpsSequence(new WebDeployHandlerMock()); var loadBalancer = new MockLoadBalancer { Mode = LbMode.RoundRobin }; var remoteSequence = new RemoteSequence(infrastructureSequence, preOpsSequence, config.Servers, loadBalancer); var status = new StatusReporter(); remoteSequence.Execute(status, new ConDepOptions(false, "", false, true, false, false, null)); Assert.That(loadBalancer.OnlineOfflineSequence.Count, Is.EqualTo(config.Servers.Count * 2)); Assert.That(loadBalancer.OnlineOfflineSequence[0].Item1, Is.EqualTo("jat-web01")); Assert.That(loadBalancer.OnlineOfflineSequence[0].Item2, Is.EqualTo("offline")); Assert.That(loadBalancer.OnlineOfflineSequence[1].Item1, Is.EqualTo("jat-web02")); Assert.That(loadBalancer.OnlineOfflineSequence[1].Item2, Is.EqualTo("offline")); Assert.That(loadBalancer.OnlineOfflineSequence[2].Item1, Is.EqualTo("jat-web03")); Assert.That(loadBalancer.OnlineOfflineSequence[2].Item2, Is.EqualTo("offline")); Assert.That(loadBalancer.OnlineOfflineSequence[3].Item1, Is.EqualTo("jat-web01")); Assert.That(loadBalancer.OnlineOfflineSequence[3].Item2, Is.EqualTo("online")); Assert.That(loadBalancer.OnlineOfflineSequence[4].Item1, Is.EqualTo("jat-web02")); Assert.That(loadBalancer.OnlineOfflineSequence[4].Item2, Is.EqualTo("online")); Assert.That(loadBalancer.OnlineOfflineSequence[5].Item1, Is.EqualTo("jat-web03")); Assert.That(loadBalancer.OnlineOfflineSequence[5].Item2, Is.EqualTo("online")); Assert.That(loadBalancer.OnlineOfflineSequence[6].Item1, Is.EqualTo("jat-web04")); Assert.That(loadBalancer.OnlineOfflineSequence[6].Item2, Is.EqualTo("offline")); Assert.That(loadBalancer.OnlineOfflineSequence[7].Item1, Is.EqualTo("jat-web05")); Assert.That(loadBalancer.OnlineOfflineSequence[7].Item2, Is.EqualTo("offline")); Assert.That(loadBalancer.OnlineOfflineSequence[8].Item1, Is.EqualTo("jat-web04")); Assert.That(loadBalancer.OnlineOfflineSequence[8].Item2, Is.EqualTo("online")); Assert.That(loadBalancer.OnlineOfflineSequence[9].Item1, Is.EqualTo("jat-web05")); Assert.That(loadBalancer.OnlineOfflineSequence[9].Item2, Is.EqualTo("online")); }
public virtual void Execute(ServerConfig server, IReportStatus status, ConDepSettings settings, CancellationToken token) { foreach (var element in _sequence) { token.ThrowIfCancellationRequested(); IExecuteRemotely elementToExecute = element; if (element is CompositeSequence) elementToExecute.Execute(server, status, settings, token); else Logger.WithLogSection(element.Name, () => elementToExecute.Execute(server, status, settings, token)); } }
private bool ConditionFulfilled(ServerConfig server) { if (string.IsNullOrEmpty(_conditionScript)) { return _condition(server.GetServerInfo()) == _expectedConditionResult; } else { var psExecutor = new PowerShellExecutor(); var result = psExecutor.Execute(server, _conditionScript); return result.First().ToString() == "True"; } }
//Web Deploy 3.0 //private const string MSDEPLOY_VERSION = "9.0.0.0"; public WebDeployDeployer(ServerConfig server) { _server = server; try { Init(); } catch { Dispose(); throw; } }
public override void Execute(ServerConfig server, IReportStatus status, ConDepSettings settings, CancellationToken token) { switch (_commandType) { case CommandType.CmdLine: ExecuteCommand(_cmd, server); break; case CommandType.ScriptFile: ExecuteScriptFile(_scriptFile, server); break; default: throw new ConDepInvalidEnumValueException(_commandType); } }
public override Result Execute(IOfferRemoteOperations remote, ServerConfig server, ConDepSettings settings, CancellationToken token) { token.ThrowIfCancellationRequested(); Logger.WithLogSection(string.Format("Stopping ConDepNode on server {0}", server.Name), () => { var executor = new PowerShellExecutor(); executor.Execute(server, "Stop-ConDepNode", mod => { mod.LoadConDepModule = false; mod.LoadConDepNodeModule = true; }, logOutput: false); }); return Result.SuccessUnChanged(); }
public IReportStatus Execute(ServerConfig server, IReportStatus status, ConDepOptions options) { bool sectionAdded = false; try { if (ConDepGlobals.ServersWithPreOps.ContainsKey(server.Name)) return status; ConDepGlobals.ServersWithPreOps.Add(server.Name, server); Logger.LogSectionStart("Pre-Operations"); sectionAdded = true; var remotePreOps = new PreRemoteOps(server, this, options, _webDeploy); remotePreOps.Configure(); remotePreOps.Execute(status); foreach (var element in _sequence) { if (element is IOperateRemote) { ((IOperateRemote)element).Execute(server, status, options); if (status.HasErrors) return status; } else if (element is CompositeSequence) { ((CompositeSequence)element).Execute(server, status, options); } else { throw new NotSupportedException(); } if (status.HasErrors) return status; } return status; } finally { if(sectionAdded) { Logger.LogSectionEnd("Pre-Operations"); } } }
public void Setup() { var app = new OnlyIfTestApp(); var config = new ConDepEnvConfig { EnvironmentName = "bogusEnv" }; var server = new ServerConfig { Name = "bogusHost" }; config.Servers = new[] { server }; _sequenceManager = new ExecutionSequenceManager(config.Servers, new DefaultLoadBalancer()); var settings = new ConDepSettings { Config = config }; var local = new LocalOperationsBuilder(_sequenceManager.NewLocalSequence("Test")); app.Configure(local, settings); _serverInfo = new ServerInfo {OperatingSystem = new OperatingSystemInfo {Name = "Windows Server 2012"}}; }
public override void Execute(ServerConfig server, IReportStatus status, ConDepSettings settings, CancellationToken token) { Logger.WithLogSection(Name, () => { if (ConditionFulfilled(server)) { foreach (var element in _sequence) { IExecuteRemotely elementToExecute = element; Logger.WithLogSection("Condition True, executing " + element.Name, () => elementToExecute.Execute(server, status, settings, token)); } } else { Logger.Info("Condition evaluated to false. Will not execute."); } }); }
public void TestThatExecutionSequenceIsValid() { var config = new ConDepEnvConfig {EnvironmentName = "bogusEnv"}; var server = new ServerConfig { Name = "jat-web03" }; config.Servers = new[] { server }; var sequenceManager = new ExecutionSequenceManager(config.Servers, new DefaultLoadBalancer()); var settings = new ConDepSettings(); settings.Config = config; var local = new LocalOperationsBuilder(sequenceManager.NewLocalSequence("Test")); //Configure.LocalOperations = local; _app.Configure(local, settings); var notification = new Notification(); Assert.That(sequenceManager.IsValid(notification)); }
private static bool HaveAccessToServer(ServerConfig server) { Logger.Info( string.Format("Checking if WinRM (Remote PowerShell) can be used to reach remote server [{0}]...", server.Name)); var cmd = server.DeploymentUser.IsDefined() ? string.Format("id -r:{0} -u:{1} -p:\"{2}\"", server.Name, server.DeploymentUser.UserName, server.DeploymentUser.Password) : string.Format("id -r:{0}", server.Name); if (server.PowerShell.SSL) { Logger.Info(string.Format("Using SSL via WinRM to reach remote server [{0}]...", server.Name)); cmd = string.Concat(cmd, " -usessl"); } var success = false; var path = Environment.ExpandEnvironmentVariables(@"%windir%\system32\WinRM.cmd"); var startInfo = new ProcessStartInfo(path) { Arguments = cmd, Verb = "RunAs", UseShellExecute = false, WindowStyle = ProcessWindowStyle.Hidden, RedirectStandardError = true, RedirectStandardOutput = true }; var process = Process.Start(startInfo); process.WaitForExit(); if (process.ExitCode == 0) { var message = process.StandardOutput.ReadToEnd(); Logger.Info(string.Format("Contact was made with server [{0}] using WinRM (Remote PowerShell). ", server.Name)); Logger.Verbose(string.Format("Details: {0} ", message)); success = true; } else { var errorMessage = process.StandardError.ReadToEnd(); Logger.Error(string.Format("Unable to reach server [{0}] using WinRM (Remote PowerShell)", server.Name)); Logger.Error(string.Format("Details: {0}", errorMessage)); } return success; }
public IReportStatus Execute(ServerConfig server, IReportStatus status, ConDepOptions options) { try { Logger.LogSectionStart("Infrastructure"); foreach (var element in _sequence) { element.Execute(server, status, options); if (status.HasErrors) return status; } } finally { Logger.LogSectionEnd("Infrastructure"); } return status; }
public override void Execute(ServerConfig server, IReportStatus status, ConDepSettings settings, CancellationToken token) { _api = new Api(new ConDepNodeUrl(server, settings), server.DeploymentUser.UserName, server.DeploymentUser.Password, server.Node.TimeoutInSeconds.Value * 1000); var result = _api.SyncDir(_srcDir, _dstDir); if (result == null) return; if(result.Log.Count > 0) { foreach (var entry in result.Log) { Logger.Info(entry); } } else { Logger.Info("Nothing to deploy. Everything is in sync."); } }
public void Execute(ServerConfig server) { DeployNodeModuleScript(server); var nodeState = GetNodeState(server); Logger.WithLogSection("Node State", () => { Logger.Info("Running : " + nodeState.IsNodeServiceRunning); Logger.Info("Need Update : " + nodeState.NeedNodeDeployment); }); if(nodeState.NeedNodeDeployment) { DeployNode(server); } else if (!nodeState.IsNodeServiceRunning) { StartNode(server); } }
public void PublishFile(string srcFile, string dstFile, ServerConfig server, ConDepSettings settings) { var api = new Api(new ConDepNodeUrl(server), server.DeploymentUser.UserName, server.DeploymentUser.Password, server.Node.TimeoutInSeconds.Value * 1000); var result = api.SyncFile(srcFile, dstFile); if (result == null) return; if (result.Log.Count > 0) { foreach (var entry in result.Log) { Logger.Info(entry); } } else { Logger.Info("Nothing to deploy. Everything is in sync."); } }
public IReportStatus Execute(ServerConfig server, IReportStatus status, ConDepOptions conDepOptions) { WebDeployOptions options = null; try { Logger.LogSectionStart(_provider.GetType().Name); _webDeploy.Sync(_provider, server, _provider.ContinueOnError, status, OnWebDeployTraceMessage); } catch (Exception ex) { HandleSyncException(status, ex); } finally { Logger.LogSectionEnd(_provider.GetType().Name); if (options != null && options.DestBaseOptions != null) options.DestBaseOptions.Trace -= OnWebDeployTraceMessage; if (options != null && options.SourceBaseOptions != null) options.SourceBaseOptions.Trace -= OnWebDeployTraceMessage; } return status; }
private void InstallChocolatey(ServerConfig server, ConDepSettings settings) { Logger.WithLogSection("Installing Chocolatey", () => { _psExecutor.Execute(server, @" try { if(Assert-ConDepChocoExist) { Invoke-ConDepChocoUpgrade } else { Invoke-ConDepChocoInstall } } catch { Write-Warning 'Failed to install Chocolatey! This could break operations depending on Chocolatey.' Write-Warning ""Error message: $($_.Exception.Message)"" } "); }); }