protected override void OnStop()
 {
     CtxTrace.TraceInformation();
     if (_Service != null)
     {
         _Service.Stop();
     }
 }
 public void Stop()
 {
     CtxTrace.TraceInformation();
     try {
         userDataService.Stop();
     } catch (Exception e) {
         CtxTrace.TraceError(e);
     }
 }
 static void OnUnhandledException(object sender, UnhandledExceptionEventArgs args)
 {
     CtxTrace.TraceInformation();
     try {
         Exception e = args.ExceptionObject as Exception;
         CtxTrace.TraceError(e.Message + "\n" + e.StackTrace);
         AgentService.StopService();
     } catch (Exception ex) {
         CtxTrace.TraceError(ex);
     }
 }
        private void ProcessUserDataTask(object ignored)
        {
            CtxTrace.TraceInformation();

            // Check sanity of configuration
            if (userDataUrl.Contains(DhcpServerPlaceholder))
            {
                List <IPAddress> dhcpServers = NetworkUtilities.GetDhcpServers();
                if (dhcpServers.Count == 0)
                {
                    CtxTrace.TraceError("Configured to get UserData from DHCP server, but no DHCP server found");
                    stateService.InitialisationComplete = true;
                }
            }

            // UserData may be a little slow to be delivered
            while (!stateService.InitialisationComplete && !stopSignalled)
            {
                string userData = GetUserData();
                if (!string.IsNullOrEmpty(userData))
                {
                    // May be multiple root elements, so wrap for Xml parser
                    string wrappedUserData = string.Format("<dummyRoot>{0}</dummyRoot>", userData);
                    try {
                        XDocument doc    = XDocument.Parse(wrappedUserData);
                        XElement  script = doc.XPathSelectElement("//script");
                        if (script != null)
                        {
                            // Script may initiate a reboot, so mark the processing done once userData read.
                            stateService.InitialisationComplete = true;
                            ExecuteScript(script.Value);
                        }
                        script = doc.XPathSelectElement("//powershell");
                        if (script != null)
                        {
                            // Script may initiate a reboot, so mark the processing done once userData read.
                            stateService.InitialisationComplete = true;
                            ExecutePowerShellScript(script.Value);
                        }
                    } catch (Exception ex) {
                        CtxTrace.TraceError(ex);
                    }
                    break;
                }
                // Sleep a while but ensure timely response to task cancel
                DateTime waitUntil = DateTime.Now + TimeSpan.FromSeconds(5);
                while (!stopSignalled && (DateTime.Now < waitUntil))
                {
                    Thread.Sleep(TimeSpan.FromMilliseconds(1000));
                }
            }
            CtxTrace.TraceInformation("Task exit");
        }
 private void ExecuteScript(string contents)
 {
     CtxTrace.TraceInformation();
     try {
         string dir      = CreateWorkingDirectory(cfnFolder);
         string fileName = Path.Combine(dir, "user-data.cmd");
         File.WriteAllText(fileName, contents);
         ScriptUtilities.ExecuteProcess("cmd.exe", "/c " + fileName, dir);
     } catch (Exception e) {
         CtxTrace.TraceError(e);
     }
 }
        private void ExecutePowerShellScript(string contents)
        {
            CtxTrace.TraceInformation();
            string oldPolicy = null;

            try {
                oldPolicy = ScriptUtilities.SetPowerShellExectionPolicy("Unrestricted");
                string dir      = CreateWorkingDirectory(cfnFolder);
                string fileName = Path.Combine(dir, "user-data.ps1");
                File.WriteAllText(fileName, contents);
                ScriptUtilities.ExecuteProcess("powershell.exe", String.Format("-F {0}", fileName), dir);
            } catch (Exception e) {
                CtxTrace.TraceError(e);
            } finally {
                if (oldPolicy != null)
                {
                    ScriptUtilities.SetPowerShellExectionPolicy(oldPolicy);
                }
            }
        }
Exemple #7
0
        public void Execute()
        {
            CtxTrace.TraceInformation("Executable = {0}", startInfo.FileName);
            try {
                using (var process = new Process()) {
                    process.StartInfo           = this.startInfo;
                    process.OutputDataReceived += process_OutputDataReceived;
                    process.ErrorDataReceived  += process_ErrorDataReceived;
                    process.Start();
                    process.BeginOutputReadLine();
                    process.BeginErrorReadLine();

                    process.WaitForExit();
                    CtxTrace.TraceInformation("Child process has exited");
                }
            } catch (Exception e) {
                CtxTrace.TraceError(e);
            } finally {
                Out.Close();
                Err.Close();
            }
        }
 protected override void OnStart(string[] args)
 {
     CtxTrace.TraceInformation();
     _Service = new CloudworksServices();
     _Service.Start();
 }
 public void Stop()
 {
     CtxTrace.TraceInformation();
     stopSignalled = true;
 }
 public void Start()
 {
     CtxTrace.TraceInformation();
     stopSignalled = false;
     ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessUserDataTask));
 }