-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.go
91 lines (82 loc) · 2.13 KB
/
aws.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
package main
import (
"regexp"
"strings"
"sort"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/cfstras/go-utils/color"
"github.com/cfstras/go-utils/fileutil"
"github.com/cfstras/pcm/types"
)
const (
awsRootName = "--AWS--"
)
var (
iniRegex = regexp.MustCompile(`^\s*([^=\s]+)\s*=\s*([^=\s]+)\s*$`)
)
type Inst ec2.Instance
func importAWS(conf *types.Configuration) error {
cfg := &aws.Config{}
path := replaceHome("~/.aws/config")
if e, _ := fileutil.Exists(path); e {
lines, err := fileutil.ReadLines(path)
if err != nil {
color.Yellowln("Could not parse", path, ":", err)
} else {
for _, l := range lines {
if match := iniRegex.FindStringSubmatch(l); match != nil {
if strings.ToLower(match[1]) == "region" {
cfg.Region = &match[2]
}
}
}
}
}
sess, err := session.NewSession(cfg)
if err != nil {
return err
}
ec2 := ec2.New(sess)
inst, err := ec2.DescribeInstances(nil)
if err != nil {
return err
}
container := types.Container{}
container.Name = awsRootName
for _, r := range inst.Reservations {
for _, in := range r.Instances {
inst := Inst(*in)
conn := types.Connection{}
conn.Info = types.Info{Name: inst.getTag("Name"),
Host: *inst.PublicDnsName,
Port: 22}
conn.Name = conn.Info.Name
for i, t := range inst.Tags {
if i != 0 {
conn.Info.Description += "; "
}
conn.Info.Description += *t.Key + ": " + *t.Value
}
conn.Login.User = "centos"
container.Connections = append(container.Connections, conn)
}
}
sort.Sort(SortConn(container.Connections))
//TODO order by tags
conf.Root.Containers = append([]types.Container{container}, conf.Root.Containers...)
return nil
}
func (inst Inst) getTag(n string) string {
for _, t := range inst.Tags {
if *t.Key == n {
return *t.Value
}
}
return "not found: " + n
}
type SortConn []types.Connection
func (a SortConn) Len() int { return len(a) }
func (a SortConn) Less(i, j int) bool { return strings.Compare(a[i].Name, a[j].Name) < 0 }
func (a SortConn) Swap(i, j int) { a[i], a[j] = a[j], a[i] }