Exemplo n.º 1
0
func (a *Agent) Register(ctx context.Context, agnt *agent.Agent) error {
	logrus.Info("Register")
	if created, err := a.api.Agents.Create(ctx, agnt); err != nil {
		if !client.IsConflicted(err) {
			logrus.Error(err)
			return err
		}
		logrus.Info("Already existed")
		// agent is already existed
		// retrieve existed
		logrus.Info("Get by name and type")
		agentList, err := a.api.Agents.List(ctx, &client.AgentsListOpts{Name: agnt.Name, Type: agnt.Type})
		if err != nil {
			return err
		}
		if agentList.Count != 1 {
			err := fmt.Errorf("expected 1 agent, but actual is %d", agentList.Count)
			return err
		}
		*agnt = *agentList.Results[0]
	} else {
		*agnt = *created
	}
	return nil
}
Exemplo n.º 2
0
func plansLoadAction(ctx *cli.Context, api *client.Client, timeout Timeout) {
	if len(ctx.Args()) == 0 {
		fmt.Printf("You should set filename argument: f.e plans load ./extra/data/plans.json\n")
		os.Exit(1)
	}
	filename := ctx.Args()[0]
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		if !os.IsExist(err) {
			fmt.Printf("File %s is not existed\n", filename)
			os.Exit(1)
		}
		panic(err)
	}
	plans := []*plan.Plan{}
	if err := json.Unmarshal(data, &plans); err != nil {
		panic(err)
	}
	update := ctx.Bool("update")
	if update {
		fmt.Println("Autoupdate is enabled")
	}
	fmt.Printf("Found %d plans\n", len(plans))
	for i, p := range plans {
		fmt.Printf("%d) %s\n", i, p)
		fmt.Printf("Creating..\n")

		_, err := api.Plans.Create(timeout(), p)
		if err != nil {
			if client.IsConflicted(err) {
				fmt.Println("Plan with this name is already existed")
				if update {
					fmt.Println("Updating..")
					// retreive existed plan
					planList, err := api.Plans.List(timeout(), &client.PlansListOpts{Name: p.Name})
					if err != nil {
						panic(err)
					}
					if planList.Count != 1 {
						err := fmt.Errorf("Expected 1 plan, but actual is %d", planList.Count)
						panic(err)
					}

					// update it
					p.Id = planList.Results[0].Id
					_, err = api.Plans.Update(timeout(), p)
					if err != nil {
						fmt.Printf("Plugin updating failed, because: %v", err)
						continue
					}
				} else {
					continue
				}
			} else {
				fmt.Printf("Plan wasn't created because: %v", err)
				continue
			}
		}
		fmt.Println("Successful")
		//		fmt.Printf("%s\n", created)
	}

}