public override void Install(IDictionary mySavedState) { // <Snippet6> StringDictionary myStringDictionary = myInstallContext.Parameters; if (myStringDictionary.Count == 0) { Console.WriteLine("No parameters have been entered in the command line " + "hence, the install will take place in the silent mode"); } else { // <Snippet4> // <Snippet5> // Check whether the "LogtoConsole" parameter has been set. if (myInstallContext.IsParameterTrue("LogtoConsole") == true) { // Display the message to the console and add it to the logfile. myInstallContext.LogMessage("The 'Install' method has been called"); } // </Snippet5> // </Snippet4> } // </Snippet6> // The 'Install procedure should be added here. }
public static void UpdateCommandLine(InstallContext context) { if (context == null) { throw new ArgumentNullException("context"); } var path = context.Parameters[AssemblyPathContextKey]; var command = context.Parameters[CommandParameter]; var builder = new StringBuilder(); builder.Append(DoubleQuote).Append(path).Append(DoubleQuote); builder.Append(Space).Append(CommandLine.ServiceWithPrefix); if (!string.IsNullOrEmpty(command)) { builder.Append(Space).Append(command); } context.Parameters[AssemblyPathContextKey] = builder.ToString(); const string LogMessageFormat1 = "Assemply path updated to: {0}"; context.LogMessage(string.Format(LogMessageFormat1, builder)); }
public void LogMessage(string str) { if (null != context) { context.LogMessage(str); } }
public static void Prepare(string pathToXmlAddInFile, string pathToAddInAssembly, InstallContext installContext) { installContext.LogMessage("Preparing AddIn - File " + pathToXmlAddInFile + " with value " + pathToAddInAssembly); string fileContent = ReadFile(pathToXmlAddInFile); fileContent = fileContent.Replace(@"%AssemblyPath%", pathToAddInAssembly); WriteFile(pathToXmlAddInFile, fileContent); }
private static void LogMessage(string message, InstallContext context) { if (context == null) { Console.WriteLine(message); } else { context.LogMessage(message); } }
private static void WriteExceptions(Exception e, InstallContext context) { if(context == null) { return; } AggregateException aggregateException = e as AggregateException; if(aggregateException != null) { foreach(var child in aggregateException.InnerExceptions) { context.LogMessage(child.Message); } } else { context.LogMessage(e.Message); } }
static void EnsureClassExists(InstallContext context, string classPath, ClassMaker classMakerFunction) { try { context.LogMessage("Ensuring that class exists: " + classPath); ManagementClass theClass = new ManagementClass(classPath); theClass.Get(); } catch (ManagementException e) { if (e.ErrorCode == ManagementStatus.NotFound) { // The class does not exist. Create it. context.LogMessage("Ensuring that class exists: CREATING " + classPath); ManagementClass theClass = classMakerFunction(); theClass.Put(); } else { throw e; } } }
public void CreateAddInLinks(string pathOfAddInFile) { installContext.LogMessage("Creating AddIn - Links"); if (Directory.Exists(GetVS2005Folder())) { CreateDirectoryIfNotExisting(GetVS2005Folder() + @"\Addins"); File.Copy(pathOfAddInFile, GetVS2005Folder() + @"\Addins\csql.AddIn", true); } if (Directory.Exists(GetVS2008Folder())) { CreateDirectoryIfNotExisting(GetVS2008Folder() + @"\Addins"); File.Copy(pathOfAddInFile, GetVS2008Folder() + @"\Addins\csql.AddIn", true); } if (Directory.Exists(GetVS2010Folder())) { CreateDirectoryIfNotExisting(GetVS2010Folder() + @"\Addins"); File.Copy(pathOfAddInFile, GetVS2010Folder() + @"\Addins\csql.AddIn", true); } }
static void EnsureNamespace(InstallContext context, string namespaceName) { context.LogMessage("Ensuring that namespace exists: " + namespaceName); string fullNamespace = null; foreach (string name in namespaceName.Split(new char[] { '\\' })) { if (fullNamespace == null) { fullNamespace = name; continue; } EnsureNamespace(fullNamespace, name); fullNamespace += "\\" + name; } }
private void DoUninstall(string serviceName) { _logger.LogInformation("Uninstall: Preparing"); var isMissingParam = false; if (string.IsNullOrWhiteSpace(serviceName)) { _logger.LogWarning("Missing parameter : [/serviceName <value>]"); isMissingParam = true; } if (isMissingParam) { _logger.LogError("Uninstall : Aborting due to missing parameters"); return; } var exeName = Process.GetCurrentProcess().MainModule.FileName; _logger.LogInformation($"Uninstall: Parameters [serviceName:{serviceName}] [path:{exeName}]"); var installer = new TransactedInstaller(); var pi = new ServiceInstaller(string.Empty, serviceName, string.Empty); installer.Installers.Add(pi); var path = $"/assemblypath={exeName}"; var context = new InstallContext("install.log", new[] { path }); context.LogMessage("Using path " + path); installer.Context = context; try { installer.Uninstall(null); } catch (Exception e) { _logger.LogExceptionEx(e, e.Message, ("serviceName", serviceName)); } _logger.LogInformation("Uninstall : Done"); }
private static void ApplyParameter(InstallContext context, string parameter, Action <string> apply) { if (context == null) { throw new ArgumentNullException("context"); } if (apply == null) { throw new ArgumentNullException("apply"); } var value = context.Parameters[parameter]; if (!string.IsNullOrEmpty(value)) { apply(value); const string AppliedParameterFormat2 = "Applied parameter: {0} = \"{1}\""; context.LogMessage(string.Format(AppliedParameterFormat2, parameter, value)); } }
public static bool InstallOrUpdateService(XElement xElement, string ServiceProcess, string AgentAddress, string logfile) { try { var ServiceLabel = xElement.xLabel(xElement.Name.LocalName); var ServiceDescription = xElement.xDescription(); var ServiceName = AgentInstancePrefix + xElement.xKey(); var ServiceArgs = new string[0]; var ServicesDependedOn = new string[0]; var ServiceLogon = ServiceAccount.NetworkService; var ServiceStartMode = xElement.xAdminState() == "disabled" ? System.ServiceProcess.ServiceStartMode.Disabled : System.ServiceProcess.ServiceStartMode.Manual; switch (xElement.Name.LocalName) { case "agent": ServiceLabel = "Urbetrack Agent (" + ServiceLabel + ")"; ServiceName = AgentServiceName; ServiceLogon = ServiceAccount.LocalSystem; ServiceArgs = new[] { "-A" }; ServiceStartMode = System.ServiceProcess.ServiceStartMode.Automatic; break; case "instance": ServiceLabel = "Urbetrack Instance (" + ServiceLabel + ")"; ServiceArgs = new[] { "-I", xElement.xKey(), AgentAddress }; ServicesDependedOn = new[] { AgentServiceName }; break; } var serviceController = GetWindowsService(ServiceName); /////// UPDATE if (serviceController != null) { if (serviceController.DisplayName != ServiceLabel) { serviceController.DisplayName = ServiceLabel; } try { if (ServiceStartMode == ServiceStartMode.Automatic && GetServiceStart(serviceController.ServiceName) != ServiceStartType.Automatic) { SetServiceStart(serviceController.ServiceName, ServiceStartType.Automatic); } if (ServiceStartMode == ServiceStartMode.Disabled && GetServiceStart(serviceController.ServiceName) != ServiceStartType.Disabled) { SetServiceStart(serviceController.ServiceName, ServiceStartType.Disabled); } } catch (ApplicationException e) { T.EXCEPTION(e); } return(true); } //////// INSTALL var path = String.Format("/assemblypath={0}", ServiceProcess); String[] cmdline = { path }; var Context = new InstallContext(logfile, cmdline); var Runner = new Installer { Context = Context }; var ProcesServiceInstaller = new ServiceProcessInstaller { Context = Context, Account = ServiceLogon, Username = null, Password = null, CmdLineArgs = ServiceArgs }; var ServiceInstallerObj = new ServiceInstaller { Context = Context, DisplayName = ServiceLabel, Description = ServiceDescription, ServiceName = ServiceName, ServicesDependedOn = ServicesDependedOn, StartType = ServiceStartMode, StartOnInstall = false }; ServiceInstallerObj.FailureActions.Add(new FailureAction(RecoverAction.Restart, 60000)); ServiceInstallerObj.FailureActions.Add(new FailureAction(RecoverAction.Restart, 60000)); ServiceInstallerObj.FailureActions.Add(new FailureAction(RecoverAction.Restart, 60000)); ServiceInstallerObj.FailCountResetTime = 60 * 60 * 24; // 1 dia. Runner.Installers.Add(ProcesServiceInstaller); Runner.Installers.Add(ServiceInstallerObj); var state = new System.Collections.Specialized.ListDictionary(); try { Runner.Install(state); Runner.Commit(state); } catch (Exception e) { Context.LogMessage(Format.Join(Format.Exception(e, "Ejecutando el instaldor"))); Runner.Rollback(state); } return(true); } catch (Exception e) { T.EXCEPTION(e); return(false); } }