forked from vmware-archive/fly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.go
155 lines (128 loc) · 3.92 KB
/
execute.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package commands
import (
"fmt"
"os"
"os/signal"
"strconv"
"syscall"
"github.com/concourse/atc"
"github.com/concourse/fly/commands/internal/executehelpers"
"github.com/concourse/fly/commands/internal/flaghelpers"
"github.com/concourse/fly/config"
"github.com/concourse/fly/eventstream"
"github.com/concourse/fly/rc"
"github.com/concourse/fly/ui"
"github.com/concourse/go-concourse/concourse"
)
type ExecuteCommand struct {
TaskConfig atc.PathFlag `short:"c" long:"config" required:"true" description:"The task config to execute"`
Privileged bool `short:"p" long:"privileged" description:"Run the task with full privileges"`
ExcludeIgnored bool `short:"x" long:"exclude-ignored" description:"Skip uploading .gitignored paths. This uses the file paths that are in your Git index. Make sure it's up to date!"`
Inputs []flaghelpers.InputPairFlag `short:"i" long:"input" value-name:"NAME=PATH" description:"An input to provide to the task (can be specified multiple times)"`
InputsFrom flaghelpers.JobFlag `short:"j" long:"inputs-from" value-name:"PIPELINE/JOB" description:"A job to base the inputs on"`
Outputs []flaghelpers.OutputPairFlag `short:"o" long:"output" value-name:"NAME=PATH" description:"An output to fetch from the task (can be specified multiple times)"`
Tags []string ` long:"tag" value-name:"TAG" description:"A tag for a specific environment (can be specified multiple times)"`
}
func (command *ExecuteCommand) Execute(args []string) error {
target, err := rc.LoadTarget(Fly.Target)
if err != nil {
return err
}
err = target.Validate()
if err != nil {
return err
}
taskConfigFile := command.TaskConfig
excludeIgnored := command.ExcludeIgnored
taskConfig, err := config.LoadTaskConfig(string(taskConfigFile), args)
if err != nil {
return err
}
client := target.Client()
inputs, err := executehelpers.DetermineInputs(
client,
target.Team(),
taskConfig.Inputs,
command.Inputs,
command.InputsFrom,
)
if err != nil {
return err
}
outputs, err := executehelpers.DetermineOutputs(
client,
taskConfig.Outputs,
command.Outputs,
)
if err != nil {
return err
}
build, err := executehelpers.CreateBuild(
client,
command.Privileged,
inputs,
outputs,
taskConfig,
command.Tags,
Fly.Target,
)
if err != nil {
return err
}
fmt.Println("executing build", build.ID)
terminate := make(chan os.Signal, 1)
go abortOnSignal(client, terminate, build)
signal.Notify(terminate, syscall.SIGINT, syscall.SIGTERM)
inputChan := make(chan interface{})
go func() {
for _, i := range inputs {
if i.Path != "" {
executehelpers.Upload(client, i, excludeIgnored)
}
}
close(inputChan)
}()
var outputChans []chan (interface{})
if len(outputs) > 0 {
for i, output := range outputs {
outputChans = append(outputChans, make(chan interface{}, 1))
go func(o executehelpers.Output, outputChan chan<- interface{}) {
if o.Path != "" {
executehelpers.Download(client, o)
}
close(outputChan)
}(output, outputChans[i])
}
}
eventSource, err := client.BuildEvents(fmt.Sprintf("%d", build.ID))
if err != nil {
return err
}
exitCode := eventstream.Render(os.Stdout, eventSource)
eventSource.Close()
<-inputChan
if len(outputs) > 0 {
for _, outputChan := range outputChans {
<-outputChan
}
}
os.Exit(exitCode)
return nil
}
func abortOnSignal(
client concourse.Client,
terminate <-chan os.Signal,
build atc.Build,
) {
<-terminate
fmt.Fprintf(ui.Stderr, "\naborting...\n")
err := client.AbortBuild(strconv.Itoa(build.ID))
if err != nil {
fmt.Fprintln(ui.Stderr, "failed to abort:", err)
return
}
// if told to terminate again, exit immediately
<-terminate
fmt.Fprintln(ui.Stderr, "exiting immediately")
os.Exit(2)
}