通过API操作阿里云ECS(开关机)

场景:定时开关机ECS,节省模式关机

完整代码示例

官方链接:https://next.api.aliyun.com/api-tools/sdk/Ecs?version=2014-05-26&language=go-tea

关机:https://next.api.aliyun.com/api/Ecs/2014-05-26/StopInstance?params={%22InstanceId%22:%22i-rj9i16z4dxexrvs60nvf%22,%22StoppedMode%22:%22StopCharging%22,%22ForceStop%22:false}

 

// This file is auto-generated, don't edit it. Thanks.
package main

import (
"os"
ecs20140526
"github.com/alibabacloud-go/ecs-20140526/v3/client"
openapi
"github.com/alibabacloud-go/darabonba-openapi/v2/client"
util
"github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)

/

  • 使用AK&SK初始化账号Client
  • @param accessKeyId
  • @param accessKeySecret
  • @return Client
  • @throws Exception
    /
    func CreateClient (accessKeyId
    string, accessKeySecret *string) (_result *ecs20140526.Client, _err error) {
    config :
    = &openapi.Config{
    // 必填,您的 AccessKey ID
    AccessKeyId: accessKeyId,
    // 必填,您的 AccessKey Secret
    AccessKeySecret: accessKeySecret,
    }
    // 访问的域名
    config.Endpoint = tea.String("ecs-cn-hangzhou.aliyuncs.com")
    _result
    = &ecs20140526.Client{}
    _result, _err
    = ecs20140526.NewClient(config)
    return _result, _err
    }

func _main (args []*string) (_err error) {
// 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html
client, _err := CreateClient(tea.String("accessKeyId"), tea.String("accessKeySecret"))
if _err != nil {
return _err
}

modifySecurityGroupRuleRequest := &ecs20140526.ModifySecurityGroupRuleRequest{
RegionId: tea.String(
"your_value"),
}
tryErr :
= func()(_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e
= r
}
}()
// 复制代码运行请自行打印 API 的返回值
_, _err = client.ModifySecurityGroupRuleWithOptions(modifySecurityGroupRuleRequest, &util.RuntimeOptions{})
if _err != nil {
return _err
}

</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> nil

}()

if tryErr != nil {
var error = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
error
= _t
}
else {
error.Message
= tea.String(tryErr.Error())
}
// 如有需要,请打印 error
_, _err = util.AssertAsString(error.Message)
if _err != nil {
return _err
}
}
return _err
}

func main() {
err := _main(tea.StringSlice(os.Args[1:]))
if err != nil {
panic(err)
}
}

 

 

 

 

 

编译docker镜像

拷贝项目 conf.yaml go.mod go.sum main.go

编译Dockerfile
FROM golang:alpine AS builder

MAINTAINER xueyf
WORKDIR /go/xueyfProject
COPY . /go/xueyfProject
ENV GOPROXY https://goproxy.cn,direct
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -s" main.go

FROM alpine AS runner
WORKDIR /go/kingProject
COPY --from=builder /go/xueyfProject/main .
COPY --from=builder /go/xueyfProject/conf.yaml ./conf.yaml

将时区设置为东八区

RUN echo "https://mirrors.aliyun.com/alpine/v3.8/main/" > /etc/apk/repositories
&& echo "https://mirrors.aliyun.com/alpine/v3.8/community/" >> /etc/apk/repositories
&& apk add --no-cache tzdata
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
&& echo Asia/Shanghai > /etc/timezone
&& apk del tzdata
ENTRYPOINT ["./main"]

docker build -t harbor.xueyf.com/infra/aliyun_stop_ecs:v1 .

发布k8s cronjob

创建cm
kubectl create configmap aliyunecs --from-file=conf.yaml -n infra

发布

posted @ 2022-12-13 14:28  xuefy  阅读(406)  评论(0编辑  收藏  举报