/
describe-volume-status.go
303 lines (252 loc) · 8.87 KB
/
describe-volume-status.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Obdi - a REST interface and GUI for deploying software
// Copyright (C) 2014 Mark Clarkson
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"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"
"io/ioutil"
"net"
"net/rpc"
"os"
"strconv"
)
var region = "us-east-1"
// ***************************************************************************
// GO RPC PLUGIN
// ***************************************************************************
/*
* For running scripts, the script and arguments etc. are sent to the target
* Obdi worker as an Obdi job. The job ID is returned to the client straight
* away and then the job is run. The client should poll the jobs table to
* see when the job finishes. When the job is finished, the output will be
* available to the client in the outputlines table.
*/
// Create tables and indexes in InitDB
func (gormInst *GormDB) InitDB(dbname string) error {
// We just read the database so empty
return nil
}
func (t *Plugin) GetRequest(args *Args, response *[]byte) error {
// GET requests don't change state, so, don't change state
// env_id is required, '?env_id=xxx'
if len(args.QueryString["env_id"]) == 0 {
ReturnError("'env_id' must be set", response)
return nil
}
env_id_str := args.QueryString["env_id"][0]
// region is required, '?region=xxx'
if len(args.QueryString["region"]) == 0 {
ReturnError("'region' must be set", response)
return nil
}
region := args.QueryString["region"][0]
// Get aws_access_key_id and aws_secret_access_key from
// the AWS_ACCESS_KEY_ID_1 capability using sdtoken
envcaps := []EnvCap{}
{
resp, _ := GET("https://127.0.0.1/api/"+args.PathParams["login"]+
"/"+args.PathParams["GUID"], "/envcaps?code=AWS_ACCESS_KEY_ID_1")
if b, err := ioutil.ReadAll(resp.Body); err != nil {
txt := fmt.Sprintf("Error reading Body ('%s').", err.Error())
ReturnError(txt, response)
return nil
} else {
json.Unmarshal(b, &envcaps)
}
}
json_objects := []JsonObject{}
{
env_cap_id_str := strconv.FormatInt(envcaps[0].Id, 10)
resp, _ := GET("https://127.0.0.1/api/sduser/"+args.SDToken,
"/jsonobjects?env_id="+env_id_str+"&env_cap_id="+env_cap_id_str)
if b, err := ioutil.ReadAll(resp.Body); err != nil {
txt := fmt.Sprintf("Error reading Body ('%s').", err.Error())
ReturnError(txt, response)
return nil
} else {
json.Unmarshal(b, &json_objects)
}
}
type AWSData struct {
Aws_access_key_id string // E.g. OKLAJX2KN6OXZEFV4B1Q
Aws_secret_access_key string // E.g. oiwjeg^laGDIUsg@jfa
Aws_obdi_worker_instance_id string // E.g. i-e19ec362
Aws_obdi_worker_region string // E.g. us-east-1
Aws_obdi_worker_url string // E.g. https://1.2.3.4:4443/
Aws_obdi_worker_key string // E.g. secretkey
Aws_filter string // E.g. key-name=itsupkey
}
awsdata := AWSData{}
if err := json.Unmarshal([]byte(json_objects[0].Json), &awsdata); err != nil {
txt := fmt.Sprintf("Error decoding JsonObject ('%s').", err.Error())
ReturnError(txt, response)
return nil
}
// Describe the volume
creds := credentials.NewStaticCredentials(
awsdata.Aws_access_key_id,
awsdata.Aws_secret_access_key,
"")
config := aws.Config{
Region: aws.String(region),
Credentials: creds,
}
svc := ec2.New(session.New(), &config)
params := &ec2.DescribeVolumeStatusInput{
// Checks whether you have the required permissions for the action, without
// actually making the request, and provides an error response. If you have
// the required permissions, the error response is DryRunOperation. Otherwise,
// it is UnauthorizedOperation.
//DryRun: aws.Bool(DryRun),
// One or more filters.
//
// action.code - The action code for the event (for example,
// enable-volume-io).
//
// action.description - A description of the action.
//
// action.event-id - The event ID associated with the action.
//
// availability-zone - The Availability Zone of the instance.
//
// event.description - A description of the event.
//
// event.event-id - The event ID.
//
// event.event-type - The event type (for io-enabled: passed |
// failed; for io-performance: io-performance:degraded |
// io-performance:severely-degraded | io-performance:stalled).
//
// event.not-after - The latest end time for the event.
//
// event.not-before - The earliest start time for the event.
//
// volume-status.details-name - The cause for volume-status.status
// (io-enabled | io-performance).
//
// volume-status.details-status - The status of
// volume-status.details-name (for io-enabled: passed | failed; for
// io-performance: normal | degraded | severely-degraded | stalled).
//
// volume-status.status - The status of the volume (ok | impaired |
// warning | insufficient-data).
//Filters []*Filter `locationName:"Filter" locationNameList:"Filter" type:"list"`
// The maximum number of volume results returned by
// DescribeVolumeStatus in paginated output. When this parameter is
// used, the request only returns MaxResults results in a single page
// along with a NextToken response element. The remaining results of
// the initial request can be seen by sending another request with the
// returned NextToken value. This value can be between 5 and 1000; if
// MaxResults is given a value larger than 1000, only 1000 results are
// returned. If this parameter is not used, then DescribeVolumeStatus
// returns all results. You cannot specify this parameter and the
// volume IDs parameter in the same request.
//MaxResults *int64 `type:"integer"`
// The NextToken value to include in a future DescribeVolumeStatus
// request. When the results of the request exceed MaxResults, this
// value can be used to retrieve the next page of results. This value
// is null when there are no more results to return.
//NextToken *string `type:"string"`
// One or more volume IDs.
//
// Default: Describes all your volumes.
//VolumeIds: postedData.VolumeIds,
}
var ptrliststring []*string
if len(args.QueryString["volume_id"]) > 0 {
for i := range args.QueryString["volume_id"] {
ptrliststring = append(ptrliststring, &args.QueryString["volume_id"][i])
}
}
params.VolumeIds = ptrliststring
if len(args.QueryString["dry_run"]) > 0 {
if args.QueryString["dry_run"][0] == "true" {
params.DryRun = aws.Bool(true)
} else {
params.DryRun = aws.Bool(false)
}
}
resp, err := svc.DescribeVolumeStatus(params)
if err != nil {
t := "Error running AttachVolume: " + err.Error()
ReturnError(t, response)
return nil
}
jsondata, err := json.Marshal(resp)
if err != nil {
ReturnError("Marshal error: "+err.Error(), response)
return nil
}
reply := Reply{0, string(jsondata), SUCCESS, ""}
jsondata, err = json.Marshal(reply)
if err != nil {
ReturnError("Marshal error: "+err.Error(), response)
return nil
}
*response = jsondata
return nil
}
func (t *Plugin) PostRequest(args *Args, response *[]byte) error {
// POST requests can change state
ReturnError("Internal error: Unimplemented HTTP POST", response)
return nil
}
func (t *Plugin) HandleRequest(args *Args, response *[]byte) error {
// All plugins must have this.
if len(args.QueryType) > 0 {
switch args.QueryType {
case "GET":
t.GetRequest(args, response)
return nil
case "POST":
t.PostRequest(args, response)
return nil
}
ReturnError("Internal error: Invalid HTTP request type for this plugin "+
args.QueryType, response)
return nil
} else {
ReturnError("Internal error: HTTP request type was not set", response)
return nil
}
}
func main() {
//logit("Plugin starting")
// Sets the global config var, needed for PluginDatabasePath
NewConfig()
// Create a lock file to use for synchronisation
//config.Port = 49995
//config.Portlock = NewPortLock(config.Port)
plugin := new(Plugin)
rpc.Register(plugin)
listener, err := net.Listen("tcp", ":"+os.Args[1])
if err != nil {
txt := fmt.Sprintf("Listen error. %s", err)
logit(txt)
}
//logit("Plugin listening on port " + os.Args[1])
if conn, err := listener.Accept(); err != nil {
txt := fmt.Sprintf("Accept error. %s", err)
logit(txt)
} else {
//logit("New connection established")
rpc.ServeConn(conn)
}
}