예제 #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
파일: 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")
}