-
Notifications
You must be signed in to change notification settings - Fork 10
/
runbook.go
108 lines (93 loc) · 3.61 KB
/
runbook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package executor
import (
"errors"
"fmt"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/deployments"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/runbooks"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/spaces"
"strconv"
"strings"
)
// ----- Create Release --------------------------------------
type TaskResultRunbookRun struct {
Version string
}
// the command processor is responsible for accepting related entity names from the end user
// and looking them up for their ID's; we should only deal with strong references at this level
type TaskOptionsRunbookRun struct {
ProjectName string // required
RunbookName string // the name of the runbook to run
Environments []string
Tenants []string
TenantTags []string
ScheduledStartTime string
ScheduledExpiryTime string
ExcludedSteps []string
GuidedFailureMode string // ["", "true", "false", "default"]. Note default and "" are the same, the only difference is whether interactive mode prompts you
ForcePackageDownload bool
RunTargets []string
ExcludeTargets []string
Variables map[string]string
Snapshot string
// extra behaviour commands
// true if the value was specified on the command line (because ForcePackageDownload is bool, we can't distinguish 'false' from 'missing')
ForcePackageDownloadWasSpecified bool
// so the automation command can mask sensitive variable output
SensitiveVariableNames []string
// if the task succeeds, the resulting output will be stored here
Response *runbooks.RunbookRunResponseV1
}
func runbookRun(octopus *client.Client, space *spaces.Space, input any) error {
params, ok := input.(*TaskOptionsRunbookRun)
if !ok {
return errors.New("invalid input type; expecting TaskOptionsRunbookRun")
}
if space == nil {
return errors.New("space must be specified")
}
// we have the provided project name; go look it up
if params.ProjectName == "" {
return errors.New("project must be specified")
}
if params.RunbookName == "" {
return errors.New("runbook name must be specified")
}
if len(params.Environments) == 0 {
return errors.New("environment(s) must be specified")
}
// common properties
abstractCmd := deployments.CreateExecutionAbstractCommandV1{
SpaceID: space.ID,
ProjectIDOrName: params.ProjectName,
ForcePackageDownload: params.ForcePackageDownload,
SpecificMachineNames: params.RunTargets,
ExcludedMachineNames: params.ExcludeTargets,
SkipStepNames: params.ExcludedSteps,
RunAt: params.ScheduledStartTime,
NoRunAfter: params.ScheduledExpiryTime,
Variables: params.Variables,
}
b, err := strconv.ParseBool(params.GuidedFailureMode)
if err == nil {
abstractCmd.UseGuidedFailure = &b
} else {
// else they must have specified nothing, or perhaps "default". Sanity check it's not garbage
if params.GuidedFailureMode != "" && !strings.EqualFold("default", params.GuidedFailureMode) {
return fmt.Errorf("'%s' is not a valid value for guided failure mode", params.GuidedFailureMode)
}
}
runCommand := runbooks.NewRunbookRunCommandV1(space.ID, params.ProjectName)
runCommand.RunbookName = params.RunbookName
runCommand.EnvironmentNames = params.Environments
runCommand.Tenants = params.Tenants
runCommand.TenantTags = params.TenantTags
runCommand.Snapshot = params.Snapshot
runCommand.CreateExecutionAbstractCommandV1 = abstractCmd
runResponse, err := runbooks.RunbookRunV1(octopus, runCommand)
if err != nil {
return err
}
params.Response = runResponse
return nil
}