-
Notifications
You must be signed in to change notification settings - Fork 0
/
rds.go
166 lines (114 loc) · 3.87 KB
/
rds.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
package aliyun
import (
"encoding/json"
"fmt"
"sync"
"github.com/gogap/config"
"github.com/gogap/context"
"github.com/gogap/flow"
"github.com/sirupsen/logrus"
)
func init() {
flow.RegisterHandler("devops.aliyun.rds.instance.create", CreateRDSInstance)
flow.RegisterHandler("devops.aliyun.rds.instance.attribute.describe", DescribeRDSInstanceAttr)
flow.RegisterHandler("devops.aliyun.rds.instance.netinfo.describe", DescribeRDSInstanceNetInfo)
flow.RegisterHandler("devops.aliyun.rds.instance.delete", DeleteRDSInstance)
flow.RegisterHandler("devops.aliyun.rds.instance.running.wait", WaitForAllRDSRunning)
flow.RegisterHandler("devops.aliyun.rds.instance.account.create", CreateRDSDbAccounts)
flow.RegisterHandler("devops.aliyun.rds.instance.conn.public.alloc", AllocateInstancePublicConnection)
flow.RegisterHandler("devops.aliyun.rds.instance.conn.public.release", ReleaseInstancePublicConnection)
}
func CreateRDSInstance(ctx context.Context, conf config.Configuration) (err error) {
aliyun := NewAliyun(ctx, conf)
_, err = aliyun.CreateRDSInstances()
if err != nil {
return
}
return
}
func CreateRDSDbAccounts(ctx context.Context, conf config.Configuration) (err error) {
aliyun := NewAliyun(ctx, conf)
err = aliyun.CreateRDSDbAccount()
return
}
func DeleteRDSInstance(ctx context.Context, conf config.Configuration) (err error) {
aliyun := NewAliyun(ctx, conf)
err = aliyun.DeleteRDSInstances()
return
}
func AllocateInstancePublicConnection(ctx context.Context, conf config.Configuration) (err error) {
aliyun := NewAliyun(ctx, conf)
err = aliyun.AllocateInstancePublicConnection()
return
}
func ReleaseInstancePublicConnection(ctx context.Context, conf config.Configuration) (err error) {
aliyun := NewAliyun(ctx, conf)
err = aliyun.ReleaseInstancePublicConnection()
return
}
func DescribeRDSInstanceAttr(ctx context.Context, conf config.Configuration) (err error) {
aliyun := NewAliyun(ctx, conf)
insts, err := aliyun.DescribeRDSInstancesAttr()
if err != nil {
return
}
if len(insts) == 0 {
return
}
ouputData, err := json.Marshal(insts)
if err != nil {
return
}
var tags []string
for _, inst := range insts {
tags = append(tags, inst.Name)
setENV(fmt.Sprintf("rds_db_%s_host", inst.Name), inst.ConnectionString)
setENV(fmt.Sprintf("rds_db_%s_port", inst.Name), inst.Port)
}
tags = append(tags, "aliyun", "rds", aliyun.Code)
flow.AppendOutput(ctx, flow.NameValue{Name: "ALIYUN_RDS_INSTANCES", Value: ouputData, Tags: tags})
return
}
func DescribeRDSInstanceNetInfo(ctx context.Context, conf config.Configuration) (err error) {
aliyun := NewAliyun(ctx, conf)
insts, err := aliyun.DescribeDBInstanceNetInfo()
if err != nil {
return
}
if len(insts) == 0 {
return
}
ouputData, err := json.Marshal(insts)
if err != nil {
return
}
var tags []string
for _, inst := range insts {
tags = append(tags, inst.InstanceName)
for _, netinfo := range inst.NetInfo {
setENV(fmt.Sprintf("rds_db_%s_%s_host", inst.InstanceName, netinfo.IPType), netinfo.ConnectionString)
setENV(fmt.Sprintf("rds_db_%s_%s_port", inst.InstanceName, netinfo.IPType), netinfo.Port)
}
}
tags = append(tags, "aliyun", "rds", aliyun.Code)
flow.AppendOutput(ctx, flow.NameValue{Name: "ALIYUN_RDS_INSTANCES_NET_INFO", Value: ouputData, Tags: tags})
return
}
func WaitForAllRDSRunning(ctx context.Context, conf config.Configuration) (err error) {
aliyun := NewAliyun(ctx, conf)
inst, err := aliyun.listRDSInstance(nil)
if err != nil {
return
}
wg := &sync.WaitGroup{}
for _, v := range inst.Items.DBInstance {
wg.Add(1)
go func(instId, name string) {
defer wg.Done()
logrus.WithField("CODE", aliyun.Code).WithField("RDS-DBINSTANCE-ID", instId).WithField("RDS-DBINSTANCE-NAME", name).Infoln("Waiting db instance")
aliyun.WaitForDBInstance(instId, "Running", 60*20)
}(v.DBInstanceId, v.DBInstanceDescription)
}
wg.Wait()
return
}