-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2.go
54 lines (45 loc) · 1.16 KB
/
ec2.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
package tachi
import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/pkg/errors"
)
func (c *Client) restartServer(server Server, conf Config) error {
conf.Logger.Infof("Instance %s will restart from now on", server.id)
// Stop instance
if _, err := c.ec2Svc.StopInstances(&ec2.StopInstancesInput{
InstanceIds: []*string{
aws.String(server.id),
},
Force: aws.Bool(true),
}); err != nil {
return errors.WithStack(err)
}
if err := c.ec2Svc.WaitUntilInstanceStopped(&ec2.DescribeInstancesInput{
InstanceIds: []*string{
aws.String(server.id),
},
}); err != nil {
return errors.WithStack(err)
}
// Start instance
if _, err := c.ec2Svc.StartInstances(&ec2.StartInstancesInput{
InstanceIds: []*string{
aws.String(server.id),
},
}); err != nil {
return errors.WithStack(err)
}
if err := c.ec2Svc.WaitUntilInstanceStatusOk(&ec2.DescribeInstanceStatusInput{
InstanceIds: []*string{
aws.String(server.id),
},
}); err != nil {
return errors.WithStack(err)
}
// Wait until cool down time
time.Sleep(conf.CoolDown)
conf.Logger.Infof("Instance %s has been restartd", server.id)
return nil
}