예제 #1
0
파일: worker.go 프로젝트: Collinux/snap
func (w worker) createTask(taskID string, startOnCreate bool) {
	logger := w.logger.WithFields(log.Fields{
		"task-id": taskID,
		"_block":  "create-task",
	})
	done := false
	_, err := w.taskManager.GetTask(taskID)
	if err == nil {
		return
	}
	for {
		members, err := w.memberManager.GetTaskAgreementMembers()
		if err != nil {
			logger.Error(err)
			continue
		}
		for _, member := range shuffle(members) {
			uri := fmt.Sprintf("%s://%s:%s", member.GetRestProto(), member.GetAddr(), member.GetRestPort())
			logger.Debugf("getting task %v from %v", taskID, uri)

			c, err := client.New(uri, "v1", member.GetRestInsecureSkipVerify(), client.Password(w.memberManager.GetRequestPassword()))
			if err != nil {
				logger.Error(err)
				continue
			}

			taskResult := c.GetTask(taskID)
			if taskResult.Err != nil {
				logger.WithField("err", taskResult.Err.Error()).Debug("error getting task")
				continue
			}
			logger.Debug("creating task")
			opt := core.SetTaskID(taskID)
			_, errs := w.taskManager.CreateTaskTribe(
				getSchedule(taskResult.ScheduledTaskReturned.Schedule),
				taskResult.Workflow,
				startOnCreate,
				opt)
			if errs != nil && len(errs.Errors()) > 0 {
				fields := log.Fields{}
				for idx, e := range errs.Errors() {
					fields[fmt.Sprintf("err-%d", idx)] = e
				}
				logger.WithFields(fields).Debug("error creating task")
				continue
			}
			logger.Debugf("task created")
			done = true
			break
		}
		if done {
			break
		}
		time.Sleep(500 * time.Millisecond)
	}
}
예제 #2
0
파일: main.go 프로젝트: IRCody/snap
// Run before every command
func beforeAction(ctx *cli.Context) error {
	username, password := checkForAuth(ctx)
	pClient, err = client.New(ctx.String("url"), ctx.String("api-version"), ctx.Bool("insecure"))
	if err != nil {
		return fmt.Errorf("%v", err)
	}
	pClient.Password = password
	pClient.Username = username
	if err = checkTribeCommand(ctx); err != nil {
		return fmt.Errorf("%v", err)
	}
	return nil
}
예제 #3
0
파일: main.go 프로젝트: jeffweiss/snap
func init() {
	f1 := flag.NewFlagSet("f1", flag.ContinueOnError)
	prtURL := f1.String("url", flURL.Value, flURL.Usage)
	prtU := f1.String("u", flURL.Value, flURL.Usage)
	prtAv := f1.String("api-version", flAPIVer.Value, flAPIVer.Usage)
	prtA := f1.String("a", flAPIVer.Value, flAPIVer.Usage)
	prti := f1.Bool("insecure", false, flSecure.Usage)

	url := flURL.Value
	ver := flAPIVer.Value
	secure := false

	for idx, a := range os.Args {
		switch a {
		case "--url":
			if err := f1.Parse(os.Args[idx : idx+2]); err == nil {
				url = *prtURL
			}
		case "--u":
			if err := f1.Parse(os.Args[idx : idx+2]); err == nil {
				url = *prtU
			}
		case "--api-version":
			if err := f1.Parse(os.Args[idx : idx+2]); err == nil {
				ver = *prtAv
			}
		case "--a":
			if err := f1.Parse(os.Args[idx : idx+2]); err == nil {
				ver = *prtA
			}
		case "--insecure":
			if err := f1.Parse(os.Args[idx:]); err == nil {
				secure = *prti
			}
		}
	}
	pClient = client.New(url, ver, secure)
	resp := pClient.ListAgreements()
	if resp.Err == nil {
		commands = append(commands, tribeCommands...)
	}

}
예제 #4
0
func TestSnapClientTribe(t *testing.T) {
	numOfTribes := 4
	ports := startTribes(numOfTribes)
	c, err := client.New(fmt.Sprintf("http://localhost:%d", ports[0]), "v1", true)

	Convey("REST API functional V1 - TRIBE", t, func() {
		So(err, ShouldBeNil)

		Convey("Get global membership", func() {
			resp := c.ListMembers()
			So(resp.Err, ShouldBeNil)
			So(resp.Members, ShouldNotBeNil)
			So(resp, ShouldHaveSameTypeAs, &client.ListMembersResult{})
			So(len(resp.Members), ShouldEqual, numOfTribes)
		})

		Convey("An agreement is added", func() {
			agreement := "agreement1"
			resp := c.AddAgreement(agreement)
			So(resp.Err, ShouldBeNil)
			So(resp, ShouldHaveSameTypeAs, &client.AddAgreementResult{})
			So(len(resp.Agreements), ShouldEqual, 1)
			resp2 := c.ListAgreements()
			So(resp2.Err, ShouldBeNil)
			So(resp2, ShouldHaveSameTypeAs, &client.ListAgreementResult{})
			So(len(resp2.Agreements), ShouldEqual, 1)
			Convey("A node joins the agreement", func() {
				resp := c.JoinAgreement(agreement, fmt.Sprintf("member-%d", ports[0]))
				So(resp.Err, ShouldBeNil)
				So(resp, ShouldHaveSameTypeAs, &client.JoinAgreementResult{})
				So(resp.Agreement, ShouldNotBeNil)
				So(len(resp.Agreement.Members), ShouldEqual, 1)
				Convey("The rest of the members join the agreement", func() {
					for i := 1; i < numOfTribes; i++ {
						resp := c.JoinAgreement(agreement, fmt.Sprintf("member-%d", ports[i]))
						So(resp.Err, ShouldBeNil)
						So(resp, ShouldHaveSameTypeAs, &client.JoinAgreementResult{})
						So(resp.Agreement, ShouldNotBeNil)
						So(len(resp.Agreement.Members), ShouldEqual, i+1)
					}
					Convey("A member is removed from the agreement", func() {
						resp := c.LeaveAgreement(agreement, fmt.Sprintf("member-%d", ports[0]))
						So(resp.Err, ShouldBeNil)
						So(resp, ShouldHaveSameTypeAs, &client.LeaveAgreementResult{})
						So(resp.Agreement, ShouldNotBeNil)
						So(len(resp.Agreement.Members), ShouldEqual, numOfTribes-1)
						Convey("A member is retrieved", func() {
							resp := c.GetMember(fmt.Sprintf("member-%d", ports[1]))
							So(resp.Err, ShouldBeNil)
							So(resp, ShouldHaveSameTypeAs, &client.GetMemberResult{})
							So(resp.Name, ShouldNotBeNil)
							So(resp.Name, ShouldResemble, fmt.Sprintf("member-%d", ports[1]))
							Convey("An Agreement is retrieved", func() {
								resp := c.GetAgreement(agreement)
								So(resp.Err, ShouldBeNil)
								So(resp, ShouldHaveSameTypeAs, &client.GetAgreementResult{})
								So(resp.Agreement.Name, ShouldNotBeNil)
								So(resp.Agreement.Name, ShouldResemble, agreement)
								So(len(resp.Agreement.Members), ShouldEqual, 3)
								Convey("An agreement is deleted", func() {
									resp := c.DeleteAgreement(agreement)
									So(resp.Err, ShouldBeNil)
									So(resp, ShouldHaveSameTypeAs, &client.DeleteAgreementResult{})
									So(len(resp.Agreements), ShouldEqual, 0)
								})
							})
						})
					})
				})
			})
		})
	})
}
예제 #5
0
파일: worker.go 프로젝트: Collinux/snap
func (w worker) loadPlugin(plugin core.Plugin) error {
	logger := w.logger.WithFields(log.Fields{
		"plugin-name":    plugin.Name(),
		"plugin-version": plugin.Version(),
		"plugin-type":    plugin.TypeName(),
		"_block":         "load-plugin",
	})
	if w.isPluginLoaded(plugin.Name(), plugin.TypeName(), plugin.Version()) {
		return nil
	}
	members, err := w.memberManager.GetPluginAgreementMembers()
	if err != nil {
		logger.Error(err)
		return err
	}
	for _, member := range shuffle(members) {
		url := fmt.Sprintf("%s://%s:%s/v1/plugins/%s/%s/%d?download=true", member.GetRestProto(), member.GetAddr(), member.GetRestPort(), plugin.TypeName(), plugin.Name(), plugin.Version())
		c, err := client.New(url, "v1", member.GetRestInsecureSkipVerify(), client.Password(w.memberManager.GetRequestPassword()))
		if err != nil {
			logger.WithFields(log.Fields{
				"err": err,
				"url": url,
			}).Info("unable to create client")
			continue
		}
		resp, err := c.TribeRequest()
		if err != nil {
			logger.WithFields(log.Fields{
				"err": err,
				"url": url,
			}).Info("plugin not found")
			continue
		}
		if resp.StatusCode == 200 {
			if resp.Header.Get("Content-Type") != "application/x-gzip" {
				logger.WithField("content-type", resp.Header.Get("Content-Type")).Error("Expected application/x-gzip")
			}
			dir, err := ioutil.TempDir("", "")
			if err != nil {
				logger.Error(err)
				return err
			}
			f, err := os.Create(path.Join(dir, fmt.Sprintf("%s-%s-%d", plugin.TypeName(), plugin.Name(), plugin.Version())))
			if err != nil {
				logger.Error(err)
				f.Close()
				return err
			}
			io.Copy(f, resp.Body)
			f.Close()
			err = os.Chmod(f.Name(), 0700)
			if err != nil {
				logger.Error(err)
				return err
			}
			rp, err := core.NewRequestedPlugin(f.Name())
			if err != nil {
				logger.Error(err)
				return err
			}
			_, err = w.pluginManager.Load(rp)
			if err != nil {
				logger.Error(err)
				return err
			}
			if w.isPluginLoaded(plugin.Name(), plugin.TypeName(), plugin.Version()) {
				return nil
			}
			return errors.New("failed to load plugin")
		}
	}
	return errors.New("failed to find a member with the plugin")
}