/// <summary> /// Notifies the provider of a CloudInit log file /// </summary> /// <param name="session">The current CloudInit session</param> public override void Notify(CloudInitSession session) { // Get the settings from the registry String toEmail = this.GetSetting <String>("ToEmail"); String fromEmail = this.GetSetting <String>("FromEmail"); String mailServer = this.GetSetting <String>("MailServer"); String username = this.GetSetting <String>("Username"); String password = this.GetSetting <String>("Password"); Boolean integratedAuthentication = this.GetSetting <Boolean>("IntegratedAuthentication"); Boolean enableSsl = this.GetSetting <Boolean>("EnableSSL"); // Create a message we want to send MailMessage message = new MailMessage(fromEmail, toEmail); message.Subject = String.Format("LOG Notification - instance:{0} {1}", session.Machine, session.Address); message.Body = session.Data; // Send the message SmtpClient smtp = new SmtpClient(mailServer); // If the user has integrated authentication turned on assign the default network credentials, // if they provided a username and password then use that instead. if (integratedAuthentication) { smtp.Credentials = CredentialCache.DefaultNetworkCredentials; } else if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password)) { smtp.Credentials = new NetworkCredential(username, password); } // Enable SSL if the user requests this if (enableSsl) { smtp.EnableSsl = true; } smtp.Send(message); }
/// <summary> /// Executes the main code for this service /// </summary> /// <param name="state">The service object</param> public static void Main(Object state) { // Set the event log from the servicebase EventLog EventLog = state == null ? new EventLog("Application") : ((ServiceBase)state).EventLog; // Load registry settings CIRegistry registry = new CIRegistry(EventLog); var address = registry.ReadUserDataFileKey(); var logFile = registry.ReadLogFileKey(); LogFile = logFile; WriteOutput("Starting Service"); // Load all the assemblies and scan the types CIAssemblyLoader.Configure(); ObjectFactory.Configure(c => { foreach (var type in CIAssemblyLoader.TypesToScan) { // Make sure the type is assignable to IInputModule if (typeof(IInputModule).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract) { // Run the init method to configure this database endpoint IInputModule obj = Activator.CreateInstance(type) as IInputModule; CIEngine.Headers.Add(obj.Header); // Set the properties for this object obj.EventLog = EventLog; obj.LogFile = logFile; // Add the object to the ObjectFactory c.For <IInputModule>().Singleton().Use(obj).Named(obj.Header); WriteOutput(String.Format("Adding module: {0}", type.ToString())); } } }); try { // Execute the script CIEngine engine = new CIEngine(EventLog, logFile); engine.Execute(address); } catch (Exception ex) { WriteOutput(ex.Message); WriteOutput(ex.StackTrace); } // Get the log file FileInfo info = new FileInfo(logFile); foreach (var type in CIAssemblyLoader.TypesToScan) { // Make sure the type is assignable to INotificationProvider if (typeof(INotificationProvider).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract) { INotificationProvider obj = Activator.CreateInstance(type) as INotificationProvider; // Make sure the notification is active before sending the notify command if (obj.IsActive) { using (FileStream stream = info.OpenRead()) using (StreamReader reader = new StreamReader(stream)) { // Send the log file via this notification provider CloudInitSession session = new CloudInitSession(reader.ReadToEnd()); obj.Notify(session); } } } } if (state != null) { ServiceBase service = (ServiceBase)state; // Disable this service if run once is enabled if (registry.ReadRunOnceKey()) { ServiceController sc = new ServiceController(service.ServiceName); ServiceHelper.ChangeStartMode(sc, ServiceStartMode.Disabled); } // Stop the service service.Stop(); } }