Example #1
0
func (a *App) getItems() ([]*rest.ServiceInfo, error) {
	names, e := a.client.Services()
	if e != nil {
		return nil, e
	}
	items := make([]*rest.ServiceInfo, 0, len(names))
	for _, n := range names {
		item, e := a.client.GetService(n)
		if e == nil {
			items = append(items, item)
		}
	}
	util.SortServices(items)
	return items, nil
}
Example #2
0
func main() {
	user := ""
	pass := ""
	logfile := ""
	cafile := ""
	capath := ""
	insecure := false

	flag.StringVar(&addr, "addr", addr, "govisor address")
	flag.StringVar(&user, "user", user, "user name for authentication")
	flag.StringVar(&pass, "pass", pass, "password for authentication")
	flag.StringVar(&cafile, "cacert", cafile, "CA certificate file")
	flag.StringVar(&capath, "capath", capath, "CA certificates directory")
	flag.StringVar(&logfile, "debuglog", logfile, "debug log file")
	flag.BoolVar(&insecure, "insecure", insecure, "allow insecure TLS connections")
	flag.Parse()

	var dlog *log.Logger
	if logfile != "" {
		f, e := os.Create(logfile)
		if e == nil {
			dlog = log.New(f, "DEBUG:", log.LstdFlags)
			log.SetOutput(f)
		}
	}

	roots := x509.NewCertPool()
	if cafile == "" && capath == "" {
		roots = nil
	} else {
		if cafile != "" {
			if e := loadCertFile(roots, cafile); e != nil {
				fatal("Unable to load cert file", e)
			}
		}
		if capath != "" {
			if e := loadCertPath(roots, capath); e != nil {
				fatal("Unable to load cert path", e)
			}
		}
	}

	u, e := url.Parse(addr)
	if e != nil {
		fatal("Bad address", e)
	}
	tcfg := &tls.Config{
		RootCAs:            roots,
		ServerName:         u.Host,
		InsecureSkipVerify: insecure,
	}
	tran := &http.Transport{
		TLSClientConfig: tcfg,
	}

	client := rest.NewClient(tran, addr)
	if user != "" || pass != "" {
		client.SetAuth(user, pass)
	}

	args := flag.Args()
	if len(args) == 0 {
		if e := doUI(client, addr, dlog); e != nil {
			// status by default
			args = []string{"status"}
		} else {
			return
		}
	}

	switch args[0] {
	case "services":
		if len(args) != 1 {
			usage()
		}
		s, e := client.Services()
		if e != nil {
			fatal("Error", e)
		}
		sort.Strings(s)
		for _, name := range s {
			fmt.Println(name)
		}
	case "enable":
		if len(args) != 2 {
			usage()
		}
		e := client.EnableService(args[1])
		if e != nil {
			fatal("Error", e)
		}
	case "disable":
		if len(args) != 2 {
			usage()
		}
		e := client.DisableService(args[1])
		if e != nil {
			fatal("Error", e)
		}

	case "restart":
		if len(args) != 2 {
			usage()
		}
		e := client.RestartService(args[1])
		if e != nil {
			fatal("Error", e)
		}

	case "clear":
		if len(args) != 2 {
			usage()
		}
		e := client.ClearService(args[1])
		if e != nil {
			fatal("Error", e)
		}

	case "log":
		loginfo := &rest.LogInfo{}
		switch len(args) {
		case 0:
			usage()
		case 1:
			loginfo, e = client.GetLog("")
		case 2:
			loginfo, e = client.GetLog(args[1])
		}
		if e != nil {
			fatal("Error", e)
		}
		for _, line := range loginfo.Records {
			fmt.Printf("%s %s\n",
				line.Time.Format(time.StampMilli), line.Text)
		}
	case "info":
		if len(args) != 2 {
			usage()
		}
		s, e := client.GetService(args[1])
		if e != nil {
			fatal("Failed", e)
		}
		fmt.Printf("Name:      %s\n", s.Name)
		fmt.Printf("Desc:      %s\n", s.Description)
		fmt.Printf("Status:    %s\n", util.Status(s))
		fmt.Printf("Since:     %v\n", time.Now().Sub(s.TimeStamp))
		fmt.Printf("Detail:    %s\n", s.Status)
		fmt.Printf("Provides: ")
		for _, p := range s.Provides {
			fmt.Printf(" %s", p)
		}
		fmt.Printf("\n")
		fmt.Printf("Depends:   ")
		for _, p := range s.Depends {
			fmt.Printf(" %s", p)
		}
		fmt.Printf("\n")
		fmt.Printf("Conflicts: ")
		for _, p := range s.Conflicts {
			fmt.Printf(" %s", p)
		}
		fmt.Printf("\n")
	case "status":
		names := args[1:]
		var e error
		if len(names) == 0 {
			names, e = client.Services()
			if e != nil {
				fatal("Error", e)
			}
		}
		if len(names) == 0 {
			// No services?
			return
		}
		infos := []*rest.ServiceInfo{}
		for _, n := range names {
			info, e := client.GetService(n)
			if e == nil {
				infos = append(infos, info)
			} else {
				fatal("Error", e)
			}
		}
		util.SortServices(infos)
		for _, info := range infos {
			showStatus(info)
		}
	case "ui":
		if e := doUI(client, addr, dlog); e != nil {
			fatal("Error", e)
		}
	}
}