Ejemplo n.º 1
0
// ServiceHandle handle service events
func ServiceHandle(svcName, svcDisp, svcDesc string, work func(), stopWork func()) {
	if len(os.Args) > 1 {
		cmd := os.Args[1]
		var s, err = service.NewService(svcName, svcDisp, svcDesc)
		if err != nil {
			fmt.Printf("%s unable to start: %s", svcDisp, err)
			return
		}
		switch cmd {
		case "install":
			err = s.Install()
			if err != nil {
				fmt.Printf("Failed to install:%s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" installed.\n", svcDisp)
		case "remove", "uninstall":
			err = s.Remove()
			if err != nil {
				fmt.Printf("Failed to remove: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" removed.\n", svcDisp)
		case "start":
			err = s.Start()
			if err != nil {
				fmt.Printf("Failed to start: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" started.\n", svcDisp)
		case "stop":
			err = s.Stop()
			if err != nil {
				fmt.Printf("Failed to stop: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" stoped.\n", svcDisp)
		case "run":
			work()
		}
		return
	}
	var s, err = service.NewService(svcName, svcDisp, svcDesc)
	if err != nil {
		fmt.Printf("%s unable to start: %s", svcDisp, err)
		return
	}
	err = s.Run(func() error {
		go work()
		return nil
	}, func() error {
		stopWork()
		return nil
	})
	//	if err != nil {
	//		s.Error(err.Error())
	//	}
}
Ejemplo n.º 2
0
func runService() {
	var name = "juju"
	var displayName = "juju service"
	var desc = "juju service"

	var s, err = service.NewService(name, displayName, desc)
	if err != nil {
		logger.Errorf("%s", err)
		os.Exit(1)
	}

	run := func() error {
		go Main(os.Args)
		return nil
	}
	stop := func() error {
		os.Exit(0)
		return nil
	}
	err = s.Run(run, stop)

	if err != nil {
		s.Error(err.Error())
	}
}
Ejemplo n.º 3
0
func main() {
	flag.Parse()

	var displayName = "StatsGod statistics server"
	var desc = "Aggregates and serves statistics"
	var ws, err = service.NewService("statsgod", displayName, desc)

	if err != nil {
		fmt.Fprintf(os.Stderr, "%s unable to start: %s", displayName, err)
		return
	}
	if len(flag.Args()) > 0 {
		var err error
		verb := flag.Args()[0]
		switch verb {
		case "install":
			err = ws.Install()
			if err != nil {
				fmt.Fprintf(os.Stderr, "%s: failed to install %q\n", err, displayName)
				return
			}
			ws.LogInfo("%q: service installed.", displayName)
		case "remove":
			err = ws.Remove()
			if err != nil {
				fmt.Printf("Failed to remove: %s\n", err)
				return
			}
			ws.LogInfo("%q: service removed.", displayName)
		default:
			fmt.Fprintf(os.Stderr, "%s: unknown command", verb)
		}
		return
	}
	err = ws.Run(func() error {
		// start
		err := doStart()
		if err != nil {
			return err
		}
		ws.LogInfo("%q: running", displayName)
		return nil
	}, func() error {
		// stop
		doStop()
		ws.LogInfo("%q: stopping", displayName)
		return nil
	})
	if err != nil {
		ws.LogError(err.Error())
	}
}
Ejemplo n.º 4
0
// serviceDeploy stops, removes, installs and start the mig-agent service in one go
func serviceDeploy(orig_ctx Context) (ctx Context, err error) {
	ctx = orig_ctx
	defer func() {
		if e := recover(); e != nil {
			err = fmt.Errorf("serviceDeploy() -> %v", e)
		}
		ctx.Channels.Log <- mig.Log{Desc: "leaving serviceDeploy()"}.Debug()
	}()
	svc, err := service.NewService("mig-agent", "MIG Agent", "Mozilla InvestiGator Agent")
	if err != nil {
		panic(err)
	}

	// TODO: FIX THIS. it appears that stopping a service on upstart will kill both the agent
	// running as a service, and the agent currently upgrading which isn't yet running as a service.

	// if already running, stop it. don't panic on error
	err = svc.Stop()
	if err != nil {
		ctx.Channels.Log <- mig.Log{Desc: fmt.Sprintf("Failed to stop service mig-agent: '%v'", err)}.Info()
	} else {
		ctx.Channels.Log <- mig.Log{Desc: "Stopped running mig-agent service"}.Info()
	}

	err = svc.Remove()
	if err != nil {
		// fail but continue, the service may not exist yet
		ctx.Channels.Log <- mig.Log{Desc: fmt.Sprintf("Failed to remove service mig-agent: '%v'", err)}.Info()
	} else {
		ctx.Channels.Log <- mig.Log{Desc: "Removed existing mig-agent service"}.Info()
	}
	err = svc.Install()
	if err != nil {
		panic(err)
	}
	ctx.Channels.Log <- mig.Log{Desc: "Installed mig-agent service"}.Info()
	err = svc.Start()
	if err != nil {
		panic(err)
	}
	ctx.Channels.Log <- mig.Log{Desc: "Started mig-agent service"}.Info()
	return
}
Ejemplo n.º 5
0
func main() {
	var name = "GoServiceTest"
	var displayName = "Go Service Test"
	var desc = "This is a test Go service.  It is designed to run well."

	var s, err = service.NewService(name, displayName, desc)
	log = s

	if err != nil {
		fmt.Printf("%s unable to start: %s", displayName, err)
		return
	}

	if len(os.Args) > 1 {
		var err error
		verb := os.Args[1]
		switch verb {
		case "install":
			err = s.Install()
			if err != nil {
				fmt.Printf("Failed to install: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" installed.\n", displayName)
		case "remove":
			err = s.Remove()
			if err != nil {
				fmt.Printf("Failed to remove: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" removed.\n", displayName)
		case "run":
			doWork()
		case "start":
			err = s.Start()
			if err != nil {
				fmt.Printf("Failed to start: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" started.\n", displayName)
		case "stop":
			err = s.Stop()
			if err != nil {
				fmt.Printf("Failed to stop: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" stopped.\n", displayName)
		}
		return
	}
	err = s.Run(func() error {
		// start
		go doWork()
		return nil
	}, func() error {
		// stop
		stopWork()
		return nil
	})
	if err != nil {
		s.Error(err.Error())
	}
}
Ejemplo n.º 6
0
func main() {
	var name = "SensuClient"
	var displayName = "Sensu client in Golang"
	var desc = "Sends keepalive and results messages to Sensu server."

	var s, err = service.NewService(name, displayName, desc)
	srvLog = s

	if err != nil {
		fmt.Printf("%s unable to start: %s", displayName, err)
		return
	}

	if len(os.Args) > 1 {
		var err error
		verb := os.Args[1]
		switch verb {
		case "install":
			err = s.Install()
			if err != nil {
				fmt.Printf("Failed to install: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" installed.\n", displayName)
		case "remove":
			err = s.Remove()
			if err != nil {
				fmt.Printf("Failed to remove: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" removed.\n", displayName)
		case "run":
			doWork()
		case "start":
			err = s.Start()
			if err != nil {
				fmt.Printf("Failed to start: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" started.\n", displayName)
		case "stop":
			err = s.Stop()
			if err != nil {
				fmt.Printf("Failed to stop: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" stopped.\n", displayName)
		}
		return
	}
	err = s.Run(func() error {
		// start
		go doWork()
		return nil
	}, func() error {
		// stop
		stopWork()
		return nil
	})
	if err != nil {
		s.Error(err.Error())
	}
}
Ejemplo n.º 7
0
Archivo: main.go Proyecto: jupj/hnrss
func main() {
	var name = "HnTopRss"
	var displayName = "HN Top Links RSS"
	var desc = "RSS feed for Hacker News Top Links"

	var s, err = service.NewService(name, displayName, desc)
	log = s

	if err != nil {
		fmt.Printf("%s unable to start: %s", displayName, err)
		return
	}

	if len(os.Args) > 1 {
		var err error
		verb := os.Args[1]
		switch verb {
		case "install":
			err = s.Install()
			if err != nil {
				fmt.Printf("Failed to install: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" installed.\n", displayName)
		case "remove":
			err = s.Remove()
			if err != nil {
				fmt.Printf("Failed to remove: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" removed.\n", displayName)
		case "run":
			startHttp()
		case "start":
			err = s.Start()
			if err != nil {
				fmt.Printf("Failed to start: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" started.\n", displayName)
		case "stop":
			err = s.Stop()
			if err != nil {
				fmt.Printf("Failed to stop: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" stopped.\n", displayName)
		}
		return
	}
	err = s.Run(func() error {
		// start
		go startHttp()
		return nil
	}, func() error {
		// stop
		stopHttp()
		return nil
	})
	if err != nil {
		s.Error(err.Error())
	}
}
Ejemplo n.º 8
0
func main() {

	// Config Defaults
	config.BindAddr = "[::]"
	config.Port = 8080
	config.SSLCrt = ""
	config.SSLKey = ""

	// The "go run" command creates the executable in the tmp dir of the system
	// the "service" wrapper runs a strange current dir

	curdir, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}

	//dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
	prgdir, err := osext.ExecutableFolder()
	if err != nil {
		log.Fatal(err)
	}

	// trying to find the config file
	if _, err := toml.DecodeFile(path.Join(prgdir, "config.toml"), &config); err != nil {
		if _, err := toml.DecodeFile(path.Join(curdir, "config.toml"), &config); err != nil {
			fmt.Println(err)
			return
		}
	}

	// Service Setup
	var name = "SlackToGo"
	var displayName = "Slack Team Gateway"
	var desc = "This Gateway connects two channels on different teams ins slack with each other."

	s, err := service.NewService(name, displayName, desc)
	logit = s

	if err != nil {
		fmt.Printf("%s unable to start: %s", displayName, err)
		return
	}

	if len(os.Args) > 1 {
		var err error
		verb := os.Args[1]
		switch verb {
		case "install":
			err = s.Install()
			if err != nil {
				fmt.Printf("Failed to install: %s\n", err)
				return
			}
			fmt.Printf("Service %q installed.\n", displayName)
		case "remove":
			err = s.Remove()
			if err != nil {
				fmt.Printf("Failed to remove: %s\n", err)
				return
			}
			fmt.Printf("Service %q removed.\n", displayName)
		case "start":
			err = s.Start()
			if err != nil {
				fmt.Printf("Failed to start: %s\n", err)
				return
			}
			fmt.Printf("Service %q started.\n", displayName)
		case "stop":
			err = s.Stop()
			if err != nil {
				fmt.Printf("Failed to stop: %s\n", err)
				return
			}
			fmt.Printf("Service %q stopped.\n", displayName)
		case "restart":
			err = s.Stop()
			if err != nil {
				fmt.Printf("Failed to stop: %s\n", err)
				return
			}
			fmt.Printf("Service %q stopped.\n", displayName)
			err = s.Start()
			if err != nil {
				fmt.Printf("Failed to start: %s\n", err)
				return
			}
			fmt.Printf("Service %q started.\n", displayName)
		case "check":
			checkConfig()
		case "run":
			doWork()
		}
		return
	}
	err = s.Run(func() error {
		// start
		go doWork()
		return nil
	}, func() error {
		// stop
		stopWork()
		return nil
	})
	if err != nil {
		logit.Error(err.Error())
	}
}
Ejemplo n.º 9
0
func run(c *Config) {
	var s, err = service.NewService(c.Name, c.DisplayName, c.LongDescription)
	c.s = s
	c.l = s

	if err != nil {
		fmt.Printf("%s unable to start: %s", c.DisplayName, err)
		return
	}

	if len(os.Args) > 1 {
		var err error
		verb := os.Args[1]
		switch verb {
		case "install":
			err = s.Install()
			if err != nil {
				fmt.Printf("Failed to install: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" installed.\n", c.DisplayName)
		case "remove":
			err = s.Remove()
			if err != nil {
				fmt.Printf("Failed to remove: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" removed.\n", c.DisplayName)
		case "run":
			c.l = ConsoleLogger{}
			defer func() {
				if c.Stop != nil {
					c.Stop(c)
				}
			}()
			if c.Init != nil {
				err := c.Init(c)
				if err != nil {
					c.l.Error(err.Error())
					return
				}
			}
			c.Start(c)
		case "start":
			err = s.Start()
			if err != nil {
				fmt.Printf("Failed to start: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" started.\n", c.DisplayName)
		case "stop":
			err = s.Stop()
			if err != nil {
				fmt.Printf("Failed to stop: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" stopped.\n", c.DisplayName)
		default:
			fmt.Printf("Options for \"%s\": (install | remove | run | start | stop)\n")
		}
		return
	}

	if c.Init != nil {
		err := c.Init(c)
		if err != nil {
			c.l.Error(err.Error())
			return
		}
	}

	err = s.Run(func() error {
		// start
		go c.Start(c)
		return nil
	}, func() error {
		// stop
		if c.Stop != nil {
			c.Stop(c)
		}
		return nil
	})
	if err != nil {
		c.l.Error(err.Error())
	}
}