Ejemplo n.º 1
0
func createTaskUsingWFManifest(ctx *cli.Context) error {
	// Get the workflow
	path := ctx.String("workflow-manifest")
	ext := filepath.Ext(path)
	file, e := ioutil.ReadFile(path)

	if !ctx.IsSet("interval") && !ctx.IsSet("i") {
		fmt.Println("Workflow manifest requires interval to be set via flag.")
		return errCritical
	}
	if e != nil {
		fmt.Printf("File error [%s]- %v\n", ext, e)
		return errCritical
	}

	var wf *wmap.WorkflowMap
	switch ext {
	case ".yaml", ".yml":
		// e = yaml.Unmarshal(file, &t)
		wf, e = wmap.FromYaml(file)
		if e != nil {
			fmt.Printf("Error parsing YAML file input - %v\n", e)
			return errCritical
		}
	case ".json":
		wf, e = wmap.FromJson(file)
		// e = json.Unmarshal(file, &t)
		if e != nil {
			fmt.Printf("Error parsing JSON file input - %v\n", e)
			return errCritical
		}
	}
	// Get the task name
	name := ctx.String("name")
	// Get the interval
	isCron := false
	i := ctx.String("interval")
	_, err := time.ParseDuration(i)
	if err != nil {
		// try interpreting interval as cron entry
		_, e := cron.Parse(i)
		if e != nil {
			fmt.Printf("Bad interval format:\nfor simple schedule: %v\nfor cron schedule: %v\n", err, e)
			return errCritical
		}
		isCron = true
	}

	// Deadline for a task
	dl := ctx.String("deadline")
	maxFailures := ctx.Int("max-failures")

	var sch *client.Schedule
	// None of these mean it is a simple schedule
	if !ctx.IsSet("start-date") && !ctx.IsSet("start-time") && !ctx.IsSet("stop-date") && !ctx.IsSet("stop-time") {
		// Check if duration was set
		if ctx.IsSet("duration") && !isCron {
			d, err := time.ParseDuration(ctx.String("duration"))
			if err != nil {
				fmt.Printf("Bad duration format:\n%v\n", err)
				return errCritical
			}
			start := time.Now().Add(createTaskNowPad)
			stop := start.Add(d)
			sch = &client.Schedule{
				Type:      "windowed",
				Interval:  i,
				StartTime: &start,
				StopTime:  &stop,
			}
		} else {
			// No start or stop and no duration == simple schedule
			t := "simple"
			if isCron {
				// It's a cron schedule, ignore "duration" if set
				t = "cron"
			}
			sch = &client.Schedule{
				Type:     t,
				Interval: i,
			}
		}
	} else {
		// We have some form of windowed schedule
		start := mergeDateTime(
			strings.ToUpper(ctx.String("start-time")),
			strings.ToUpper(ctx.String("start-date")),
		)
		stop := mergeDateTime(
			strings.ToUpper(ctx.String("stop-time")),
			strings.ToUpper(ctx.String("stop-date")),
		)

		// Use duration to create missing start or stop
		if ctx.IsSet("duration") {
			d, err := time.ParseDuration(ctx.String("duration"))
			if err != nil {
				fmt.Printf("Bad duration format:\n%v\n", err)
				return errCritical
			}
			// if start is set and stop is not then use duration to create stop
			if start != nil && stop == nil {
				t := start.Add(d)
				stop = &t
			}
			// if stop is set and start is not then use duration to create start
			if stop != nil && start == nil {
				t := stop.Add(d * -1)
				start = &t
			}
		}
		sch = &client.Schedule{
			Type:      "windowed",
			Interval:  i,
			StartTime: start,
			StopTime:  stop,
		}
	}
	// Create task
	r := pClient.CreateTask(sch, wf, name, dl, !ctx.IsSet("no-start"), maxFailures)
	if r.Err != nil {
		errors := strings.Split(r.Err.Error(), " -- ")
		fmt.Println("Error creating task:")
		for _, err := range errors {
			fmt.Printf("%v\n", err)
		}
		return errCritical
	}
	fmt.Println("Task created")
	fmt.Printf("ID: %s\n", r.ID)
	fmt.Printf("Name: %s\n", r.Name)
	fmt.Printf("State: %s\n", r.State)

	return nil
}
Ejemplo n.º 2
0
func createTaskUsingWFManifest(ctx *cli.Context) {
	// Get the workflow
	path := ctx.String("workflow-manifest")
	ext := filepath.Ext(path)
	file, e := ioutil.ReadFile(path)
	if e != nil {
		fmt.Printf("File error [%s]- %v\n", ext, e)
		os.Exit(1)
	}

	var wf *wmap.WorkflowMap
	switch ext {
	case ".yaml", ".yml":
		// e = yaml.Unmarshal(file, &t)
		wf, e = wmap.FromYaml(file)
		if e != nil {
			fmt.Printf("Error parsing YAML file input - %v\n", e)
			os.Exit(1)
		}
	case ".json":
		wf, e = wmap.FromJson(file)
		// e = json.Unmarshal(file, &t)
		if e != nil {
			fmt.Printf("Error parsing JSON file input - %v\n", e)
			os.Exit(1)
		}
	}
	// Get the task name
	name := ctx.String("name")

	// Get the interval
	i := ctx.String("interval")
	_, err := time.ParseDuration(i)
	if err != nil {
		fmt.Printf("Bad interval format:\n%v\n", err)
		os.Exit(1)
	}

	var sch *client.Schedule
	// None of these mean it is a simple schedule
	if !ctx.IsSet("start-date") && !ctx.IsSet("start-time") && !ctx.IsSet("stop-date") && !ctx.IsSet("stop-time") {
		// Check if duration was set
		if ctx.IsSet("duration") {
			d, err := time.ParseDuration(ctx.String("duration"))
			if err != nil {
				fmt.Printf("Bad duration format:\n%v\n", err)
				os.Exit(1)
			}
			start := time.Now().Add(createTaskNowPad)
			stop := start.Add(d)
			sch = &client.Schedule{
				Type:      "windowed",
				Interval:  i,
				StartTime: &start,
				StopTime:  &stop,
			}
		} else {
			// No start or stop and no duration == simple schedule
			sch = &client.Schedule{
				Type:     "simple",
				Interval: i,
			}
		}
	} else {
		// We have some form of windowed schedule
		start := mergeDateTime(
			strings.ToUpper(ctx.String("start-time")),
			strings.ToUpper(ctx.String("start-date")),
		)
		stop := mergeDateTime(
			strings.ToUpper(ctx.String("stop-time")),
			strings.ToUpper(ctx.String("stop-date")),
		)

		// Use duration to create missing start or stop
		if ctx.IsSet("duration") {
			d, err := time.ParseDuration(ctx.String("duration"))
			if err != nil {
				fmt.Printf("Bad duration format:\n%v\n", err)
				os.Exit(1)
			}
			// if start is set and stop is not then use duration to create stop
			if start != nil && stop == nil {
				t := start.Add(d)
				stop = &t
			}
			// if stop is set and start is not then use duration to create start
			if stop != nil && start == nil {
				t := stop.Add(d * -1)
				start = &t
			}
		}
		sch = &client.Schedule{
			Type:      "windowed",
			Interval:  i,
			StartTime: start,
			StopTime:  stop,
		}
	}
	// Create task
	r := pClient.CreateTask(sch, wf, name, !ctx.IsSet("no-start"))
	if r.Err != nil {
		errors := strings.Split(r.Err.Error(), " -- ")
		fmt.Println("Error creating task:")
		for _, err := range errors {
			fmt.Printf("%v\n", err)
		}
		os.Exit(1)
	}
	fmt.Println("Task created")
	fmt.Printf("ID: %s\n", r.ID)
	fmt.Printf("Name: %s\n", r.Name)
	fmt.Printf("State: %s\n", r.State)
}
Ejemplo n.º 3
0
func createTaskUsingWFManifest(ctx *cli.Context) error {
	// Get the workflow manifest filename from the command-line
	path := ctx.String("workflow-manifest")
	ext := filepath.Ext(path)
	file, e := ioutil.ReadFile(path)
	if e != nil {
		return fmt.Errorf("File error [%s] - %v\n", ext, e)
	}

	// check to make sure that an interval was specified using the appropriate command-line flag
	interval := ctx.String("interval")
	if !ctx.IsSet("interval") && interval != "" {
		return fmt.Errorf("Workflow manifest requires that an interval be set via a command-line flag.")
	}

	// and unmarshal the contents of the workflow manifest file into a local workflow map
	var wf *wmap.WorkflowMap
	switch ext {
	case ".yaml", ".yml":
		// e = yaml.Unmarshal(file, &t)
		wf, e = wmap.FromYaml(file)
		if e != nil {
			return fmt.Errorf("Error parsing YAML file input - %v\n", e)
		}
	case ".json":
		wf, e = wmap.FromJson(file)
		// e = json.Unmarshal(file, &t)
		if e != nil {
			return fmt.Errorf("Error parsing JSON file input - %v\n", e)
		}
	}

	// create a dummy task with an empty schedule
	t := task{
		Version:  1,
		Schedule: &client.Schedule{},
	}

	// fill in the details for that task from the command-line arguments passed in by the user;
	// if an error is encountered, return it
	if err := t.mergeCliOptions(ctx); err != nil {
		return err
	}

	// and use the resulting struct (along with the workflow map we constructed, above) to create a new task
	r := pClient.CreateTask(t.Schedule, wf, t.Name, t.Deadline, !ctx.IsSet("no-start"), t.MaxFailures)
	if r.Err != nil {
		errors := strings.Split(r.Err.Error(), " -- ")
		errString := "Error creating task:"
		for _, err := range errors {
			errString += fmt.Sprintf("%v\n", err)
		}
		return fmt.Errorf(errString)
	}
	fmt.Println("Task created")
	fmt.Printf("ID: %s\n", r.ID)
	fmt.Printf("Name: %s\n", r.Name)
	fmt.Printf("State: %s\n", r.State)

	return nil
}