/
reboot_vm.go
executable file
·49 lines (40 loc) · 1.09 KB
/
reboot_vm.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
/*
* Copyright (C) 2017-2019 Alibaba Group Holding Limited
*/
package action
import (
"bosh-alicloud-cpi/alicloud"
"fmt"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
"github.com/cppforlife/bosh-cpi-go/apiv1"
)
type RebootVMMethod struct {
CallContext
instances alicloud.InstanceManager
}
func NewRebootVMMethod(cc CallContext, instances alicloud.InstanceManager) RebootVMMethod {
return RebootVMMethod{cc, instances}
}
func (a RebootVMMethod) RebootVM(cid apiv1.VMCID) error {
instCid := cid.AsString()
err := a.instances.RebootInstance(instCid)
if err != nil {
return bosherr.WrapErrorf(err, "RebootInstance failed cid=%s", instCid)
}
err = a.instances.ChangeInstanceStatus(instCid, alicloud.Running, func(status alicloud.InstanceStatus) (bool, error) {
switch status {
case alicloud.Running:
return true, nil
case alicloud.Stopping:
return false, nil
case alicloud.Starting:
return false, nil
default:
return false, fmt.Errorf("unexpect status %s", status)
}
})
if err != nil {
return bosherr.WrapErrorf(err, "reboot %s failed", instCid)
}
return nil
}