调用阿里云OpenAPI创建抢占式实例
简单封装了一下阿里云的openapi,作用就是:
- 创建指定数量的抢占式实例
- 销毁实例
- 执行命令
package aliyun
import (
"encoding/base64"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
ecs20140526 "github.com/alibabacloud-go/ecs-20140526/v3/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)
type AyunClient struct {
AK string
SK string
Client *ecs20140526.Client
}
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
func (ayun *AyunClient) Init() (err error) {
err = ayun.CreateClient()
if err != nil {
return err
}
return nil
}
func (ayun *AyunClient) CreateClient() (_err error) {
ayunConfig := &openapi.Config{
// 必填,您的 AccessKey ID
AccessKeyId: &ayun.AK,
// 必填,您的 AccessKey Secret
AccessKeySecret: &ayun.SK,
}
// 访问的域名
ayunConfig.Endpoint = tea.String("ecs-cn-hangzhou.aliyuncs.com")
ayun.Client, _err = ecs20140526.NewClient(ayunConfig)
return _err
}
func (ayun *AyunClient) CreateECS(Amount int, password string) (InstanceID []*string, err error) {
// 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html
systemDisk := &ecs20140526.RunInstancesRequestSystemDisk{
DiskName: tea.String(""),
Category: tea.String("cloud_essd"),
PerformanceLevel: tea.String("PL0"),
Size: tea.String("40"),
}
runInstancesRequest := &ecs20140526.RunInstancesRequest{
RegionId: tea.String("cn-hangzhou"),
SystemDisk: systemDisk,
ImageId: tea.String("m-xx"),
InstanceType: tea.String("xx"),
SecurityGroupId: tea.String("sgxx"),
InstanceName: tea.String("xx-node"),
VSwitchId: tea.String("vsw-xx"),
InternetMaxBandwidthOut: tea.Int32(10),
UniqueSuffix: tea.Bool(true),
Password: tea.String(password),
InternetChargeType: tea.String("PayByTraffic"),
Amount: tea.Int32(int32(Amount)),
SpotStrategy: tea.String("SpotAsPriceGo"),
SpotDuration: tea.Int32(0),
SecurityEnhancementStrategy: tea.String("Active"),
InstanceChargeType: tea.String("PostPaid"),
}
runtime := &util.RuntimeOptions{}
resp, err := ayun.Client.RunInstancesWithOptions(runInstancesRequest, runtime)
if err != nil {
return nil, err
}
return resp.Body.InstanceIdSets.InstanceIdSet, err
}
func (ayun *AyunClient) CreateInvokeId(instanceId, command string) (InvokeId string, err error) {
createCommandRequest := &ecs20140526.CreateCommandRequest{
Type: tea.String("RunShellScript"),
RegionId: tea.String("cn-hangzhou"),
Name: tea.String("cmd"),
CommandContent: tea.String(base64.StdEncoding.EncodeToString([]byte(command))),
Timeout: tea.Int64(600),
}
result, err := ayun.Client.CreateCommandWithOptions(createCommandRequest, &util.RuntimeOptions{})
if err != nil {
return "", err
}
invokeCommandRequest := &ecs20140526.InvokeCommandRequest{
RegionId: tea.String("cn-hangzhou"),
CommandId: tea.String(*result.Body.CommandId),
InstanceId: []*string{tea.String(instanceId)},
}
resp, err := ayun.Client.InvokeCommandWithOptions(invokeCommandRequest, &util.RuntimeOptions{})
if err != nil {
return "", err
}
return *resp.Body.InvokeId, err
}
func (ayun *AyunClient) GetCmdResult(InvokeId string) (result string, err error) {
describeInvocationResultsRequest := &ecs20140526.DescribeInvocationResultsRequest{
RegionId: tea.String("cn-hangzhou"),
InvokeId: tea.String(InvokeId),
// CommandId: tea.String(CommandId),
}
runtime := &util.RuntimeOptions{}
resp, err := ayun.Client.DescribeInvocationResultsWithOptions(describeInvocationResultsRequest, runtime)
if err != nil {
return "", err
}
return *resp.Body.Invocation.InvocationResults.InvocationResult[0].Output, nil
}
func (ayun *AyunClient) DestroyInstances(InstanceID string) (result int32, err error) {
deleteInstanceRequest := &ecs20140526.DeleteInstanceRequest{
InstanceId: tea.String(InstanceID),
Force: tea.Bool(true),
}
runtime := &util.RuntimeOptions{}
resp, err := ayun.Client.DeleteInstanceWithOptions(deleteInstanceRequest, runtime)
if err != nil {
return result, err
}
return *resp.StatusCode, nil
}
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
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
调用demo:
client := aliyun.AyunClient{
AK: "xxx",
SK: "xxx",
}
client.Init()
resp, err := client.DestroyInstances("i-xxxx")
......
1
2
3
4
5
6
7
2
3
4
5
6
7