forked from nullstalgia/PrivateVoiceBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws.go
More file actions
166 lines (151 loc) · 4.21 KB
/
aws.go
File metadata and controls
166 lines (151 loc) · 4.21 KB
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
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"strings"
)
type Instance struct {
Id string
Name string
Description string
State string
}
func (o *Instance) ToString() string {
return fmt.Sprintf("%s\t%-50s\t%s\n", o.Name, o.Description, o.State)
}
type AwsClient struct {
Session *session.Session
Ec2Client *ec2.EC2
Instances []Instance
}
func (o *AwsClient) State(user string) string {
err := o.describeInstances()
if err != nil {
return formatErr(err)
}
userInstance := o.userInstance(user)
if userInstance == nil {
return fmt.Sprintf("User `%s` has no access to AWS instances", user)
}
text := "Current state of the AWS instances:\n"
block := ""
for _, instance := range awsClient.Instances {
block += instance.ToString()
}
text += wrapCodeBlock(block)
return text
}
func (o *AwsClient) StartInstance(user string) string {
userInstance := o.userInstance(user)
if userInstance == nil {
return fmt.Sprintf("User `%s` has no access to AWS instances", user)
}
startInstancesOutput, err := o.Ec2Client.StartInstances(&ec2.StartInstancesInput{
DryRun: aws.Bool(false),
InstanceIds: []*string{&userInstance.Id},
})
if err != nil {
return formatErr(err)
}
userInstance.State = *startInstancesOutput.StartingInstances[0].CurrentState.Name
text := "Starting instance:\n"
text += wrapCodeBlock(userInstance.ToString())
return text
}
func (o *AwsClient) HibernateInstance(user string) string {
userInstance := o.userInstance(user)
if userInstance == nil {
return fmt.Sprintf("User `%s` has no access to AWS instances", user)
}
stopInstancesOutput, err := o.Ec2Client.StopInstances(&ec2.StopInstancesInput{
DryRun: aws.Bool(false),
Force: nil,
Hibernate: aws.Bool(true),
InstanceIds: []*string{&userInstance.Id},
})
if err != nil {
return formatErr(err)
}
userInstance.State = *stopInstancesOutput.StoppingInstances[0].CurrentState.Name
text := "Hibernating instance:\n"
text += wrapCodeBlock(userInstance.ToString())
return text
}
func (o *AwsClient) StopInstance(user string) string {
userInstance := o.userInstance(user)
if userInstance == nil {
return fmt.Sprintf("User `%s` has no access to AWS instances", user)
}
stopInstancesOutput, err := o.Ec2Client.StopInstances(&ec2.StopInstancesInput{
DryRun: aws.Bool(false),
InstanceIds: []*string{&userInstance.Id},
})
if err != nil {
return formatErr(err)
}
userInstance.State = *stopInstancesOutput.StoppingInstances[0].CurrentState.Name
text := "Stopping instance:\n"
text += wrapCodeBlock(userInstance.ToString())
return text
}
func (o *AwsClient) userInstance(user string) *Instance {
for _, instance := range o.Instances {
if strings.Contains(instance.Description, user) {
return &instance
}
}
return nil
}
func (o *AwsClient) describeInstances() error {
describeInstancesOutput, err := o.Ec2Client.DescribeInstances(&ec2.DescribeInstancesInput{})
if err != nil {
return err
}
o.Instances = make([]Instance, 0)
for _, reservation := range describeInstancesOutput.Reservations {
for _, instance := range reservation.Instances {
name := ""
description := ""
for _, tag := range instance.Tags {
switch *tag.Key {
case "Name":
name = *tag.Value
break
case "Description":
description = *tag.Value
break
}
}
o.Instances = append(o.Instances, Instance{
Id: *instance.InstanceId,
Name: name,
Description: description,
State: *instance.State.Name,
})
}
}
return nil
}
func NewAwsClient(accessKeyId, secretAccessKey, region string) *AwsClient {
thisSession := session.Must(session.NewSession(&aws.Config{
Region: aws.String(region),
Credentials: credentials.NewStaticCredentials(accessKeyId, secretAccessKey, ""),
}))
client := &AwsClient{
Session: thisSession,
Ec2Client: ec2.New(thisSession),
}
err := client.describeInstances()
checkError(err)
return client
}
const blockMarker = "```\n"
func wrapCodeBlock(text string) string {
return blockMarker + text + blockMarker
}
func formatErr(err error) string {
return wrapCodeBlock(err.Error())
}