Пример #1
0
func getAgent(c *cli.Context) *rolly.Agent {
	config, err := rolly.LoadConfig(rolly.ConfigPath())
	if err != nil {
		log.Fatal(err)
	}
	a := rolly.NewAgent(BackendAddr(), config.HostId, config.Token, Version)
	return a
}
Пример #2
0
func registerHost(c *cli.Context) error {
	authData := c.Args().First()

	var hostId string
	var token string

	if strings.HasPrefix(authData, "u") {
		conn, err := rolly.NewBackend(BackendAddr())
		if err != nil {
			return err
		}

		if hostAuth, err := rolly.InitHost(conn, authData, Version); err == nil {
			hostId = hostAuth.HostId
			token = hostAuth.Token
		} else {
			return err
		}

	} else {
		hostIdAndToken := strings.SplitN(authData, "@", 2)
		if len(hostIdAndToken) != 2 {
			return errors.New("Invalid auth token")
		}

		hostId = hostIdAndToken[0]
		token = hostIdAndToken[1]
	}

	// generate ssh key, if not exists
	if _, err := os.Stat(rolly.KeyPath); os.IsNotExist(err) {
		if err := rolly.GenerateClientKey(rolly.KeyPath); err != nil {
			return err
		}
	}

	publicKey, err := ioutil.ReadFile(rolly.PublicKeyPath())
	if err != nil {
		return err
	}

	a := rolly.NewAgent(BackendAddr(), hostId, token, Version)
	if err := a.Register(string(publicKey)); err != nil {
		return err
	}

	conf := &rolly.Config{HostId: hostId, Token: token, Version: Version}
	if err := rolly.WriteConfig(conf, rolly.ConfigPath()); err != nil {
		return err
	}

	if err := a.TrackMetrics(); err != nil {
		fmt.Println(err)
	}

	return nil
}