forked from travis-ci/worker
/
step_start_instance.go
66 lines (51 loc) · 1.83 KB
/
step_start_instance.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
package worker
import (
"time"
"github.com/Sirupsen/logrus"
"github.com/mitchellh/multistep"
"github.com/travis-ci/worker/backend"
"github.com/travis-ci/worker/context"
gocontext "golang.org/x/net/context"
)
type stepStartInstance struct {
provider backend.Provider
startTimeout time.Duration
}
func (s *stepStartInstance) Run(state multistep.StateBag) multistep.StepAction {
buildJob := state.Get("buildJob").(Job)
ctx := state.Get("ctx").(gocontext.Context)
context.LoggerFromContext(ctx).Info("starting instance")
ctx, cancel := gocontext.WithTimeout(ctx, s.startTimeout)
defer cancel()
startTime := time.Now()
instance, err := s.provider.Start(ctx, buildJob.StartAttributes())
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).Error("couldn't start instance")
err := buildJob.Requeue()
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).Error("couldn't requeue job")
}
return multistep.ActionHalt
}
context.LoggerFromContext(ctx).WithField("boot_time", time.Now().Sub(startTime)).Info("started instance")
state.Put("instance", instance)
return multistep.ActionContinue
}
func (s *stepStartInstance) Cleanup(state multistep.StateBag) {
ctx := state.Get("ctx").(gocontext.Context)
instance, ok := state.Get("instance").(backend.Instance)
if !ok {
context.LoggerFromContext(ctx).Info("no instance to stop")
return
}
skipShutdown, ok := state.Get("skipShutdown").(bool)
if ok && skipShutdown {
context.LoggerFromContext(ctx).WithFields(logrus.Fields{"instance": instance}).Error("skipping shutdown, VM will be left running")
return
}
if err := instance.Stop(ctx); err != nil {
context.LoggerFromContext(ctx).WithFields(logrus.Fields{"err": err, "instance": instance}).Warn("couldn't stop instance")
} else {
context.LoggerFromContext(ctx).Info("stopped instance")
}
}