Esempio n. 1
0
func installService(name, desc string) error {
	exepath, err := exePath()
	if err != nil {
		return err
	}
	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()
	s, err := m.OpenService(name)
	if err == nil {
		s.Close()
		return fmt.Errorf("service %s already exists", name)
	}
	s, err = m.CreateService(name, exepath, mgr.Config{DisplayName: desc, StartType: mgr.StartAutomatic})
	if err != nil {
		return err
	}
	defer s.Close()
	err = eventlog.InstallAsEventCreate(name, eventlog.Error|eventlog.Warning|eventlog.Info)
	if err != nil {
		s.Delete()
		return fmt.Errorf("SetupEventLogSource() failed: %s", err)
	}
	return nil
}
Esempio n. 2
0
func (ws *windowsService) Install() error {
	exepath, err := osext.Executable()
	if err != nil {
		return err
	}
	// Used if path contains a space.
	exepath = `"` + exepath + `"`
	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()
	s, err := m.OpenService(ws.name)
	if err == nil {
		s.Close()
		return fmt.Errorf("service %s already exists", ws.name)
	}
	s, err = m.CreateService(ws.name, exepath, mgr.Config{
		DisplayName: ws.displayName,
		Description: ws.description,
		StartType:   mgr.StartAutomatic,
	})
	if err != nil {
		return err
	}
	defer s.Close()
	err = eventlog.InstallAsEventCreate(ws.name, eventlog.Error|eventlog.Warning|eventlog.Info)
	if err != nil {
		s.Delete()
		return fmt.Errorf("InstallAsEventCreate() failed: %s", err)
	}
	return nil
}
Esempio n. 3
0
func TestLog(t *testing.T) {
	const name = "mylog"
	const supports = eventlog.Error | eventlog.Warning | eventlog.Info
	err := eventlog.InstallAsEventCreate(name, supports)
	if err != nil {
		t.Fatalf("Install failed: %s", err)
	}
	l, err := eventlog.Open(name)
	if err != nil {
		t.Fatalf("Open failed: %s", err)
	}
	defer l.Close()
	err = l.Info(1, "info")
	if err != nil {
		t.Fatalf("Info failed: %s", err)
	}
	err = l.Warning(2, "warning")
	if err != nil {
		t.Fatalf("Warning failed: %s", err)
	}
	err = l.Error(3, "error")
	if err != nil {
		t.Fatalf("Error failed: %s", err)
	}
	err = eventlog.Remove(name)
	if err != nil {
		t.Fatalf("Remove failed: %s", err)
	}
}
func (ws *windowsService) Install() error {
	exepath, err := ws.execPath()
	if err != nil {
		return err
	}

	binPath := &bytes.Buffer{}
	// Quote exe path in case it contains a string.
	binPath.WriteRune('"')
	binPath.WriteString(exepath)
	binPath.WriteRune('"')

	// Arguments are encoded with the binary path to service.
	// Enclose arguments in quotes. Escape quotes with a backslash.
	for _, arg := range ws.Arguments {
		binPath.WriteRune(' ')
		binPath.WriteString(`"`)
		binPath.WriteString(strings.Replace(arg, `"`, `\"`, -1))
		binPath.WriteString(`"`)
	}
	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()
	s, err := m.OpenService(ws.Name)
	if err == nil {
		s.Close()
		return fmt.Errorf("service %s already exists", ws.Name)
	}
	s, err = m.CreateService(ws.Name, binPath.String(), mgr.Config{
		DisplayName:      ws.DisplayName,
		Description:      ws.Description,
		StartType:        mgr.StartAutomatic,
		ServiceStartName: ws.UserName,
		Password:         ws.Option.string("Password", ""),
		Dependencies:     ws.Dependencies,
	})
	if err != nil {
		return err
	}
	defer s.Close()
	err = eventlog.InstallAsEventCreate(ws.Name, eventlog.Error|eventlog.Warning|eventlog.Info)
	if err != nil {
		s.Delete()
		return fmt.Errorf("InstallAsEventCreate() failed: %s", err)
	}
	return nil
}
Esempio n. 5
0
func (this *Service) InstallService() error {
	// open logger
	elog, err := eventlog.Open(this.name)
	if err != nil {
		// install logger
		err = eventlog.InstallAsEventCreate(this.name, eventlog.Error|eventlog.Warning|eventlog.Info)
		if err != nil {
			return fmt.Errorf("SetupEventLogSource() failed: %s", err)
		}

		// open
		elog, err = eventlog.Open(this.name)
		if err != nil {
			return fmt.Errorf("Cannot open eventlog")
		}

	}
	defer elog.Close()

	m, err := mgr.Connect()
	if err != nil {
		elog.Error(3, fmt.Sprintf("install service: cannot connect to windows service manager.", err))
		return err
	}
	defer m.Disconnect()

	s, err := m.OpenService(this.name)
	if err == nil {
		s.Close()
		elog.Error(3, fmt.Sprintf("install service: service %s already exists", this.name))
		return fmt.Errorf("service %s already exists", this.name)
	}

	s, err = m.CreateService(this.name, this.path, mgr.Config{DisplayName: this.desc})
	if err != nil {
		elog.Error(3, fmt.Sprintf("install service: Failed to create service: %v", err))
		return err
	}
	defer s.Close()

	err = this.ConfigServiceAutoStart()
	if err != nil {
		elog.Error(3, fmt.Sprintf("install service: cannot set service to auto start: %v", err))
		return err
	}
	return nil
}
Esempio n. 6
0
// initEventlog creates a connection to event logs and stores the handler in ctx
func initEventlog(orig_logctx Logging, progname string) (logctx Logging, err error) {
	logctx = orig_logctx
	defer func() {
		if e := recover(); e != nil {
			err = fmt.Errorf("mig.initEventlog() -> %v", e)
		}
	}()
	const name = "mylog"
	const supports = eventlog.Error | eventlog.Warning | eventlog.Info
	err = eventlog.InstallAsEventCreate(name, supports)
	if err != nil {
		panic(err)
	}
	logctx.fd, err = eventlog.Open(progname)
	if err != nil {
		panic(err)
	}
	return
}