Debian Codegraph @

provider/aliyun/aliyun_discover.go

raw
1 // Package aliyun provides node discovery for Aliyun.
2 package aliyun
3
4 import (
5 "fmt"
6 "io/ioutil"
7 "log"
8
9 "github.com/denverdino/aliyungo/common"
10 "github.com/denverdino/aliyungo/ecs"
11 )
12
13 type Provider struct {
14 userAgent string
15 }
16
17 func (p *Provider) SetUserAgent(s string) {
18 p.userAgent = s
19 }
20
21 func (p *Provider) Help() string {
22 return `Aliyun(Alibaba Cloud):
23
24 provider: "aliyun"
25 region: The Aliyun region.
26 tag_key: The tag key to filter on
27 tag_value: The tag value to filter on
28 access_key_id: The Aliyun access key to use
29 access_key_secret: The Aliyun access key secret to use
30
31 The required RAM permission is 'ecs:DescribeInstances'.
32 It is recommended you make a dedicated key used only for auto-joining.
33 `
34 }
35
36 func (p *Provider) Addrs(args map[string]string, l *log.Logger) ([]string, error) {
37 if args["provider"] != "aliyun" {
38 return nil, fmt.Errorf("discover-aliyun: invalid provider " + args["provider"])
39 }
40
41 if l == nil {
42 l = log.New(ioutil.Discard, "", 0)
43 }
44
45 region := args["region"]
46 tagKey := args["tag_key"]
47 tagValue := args["tag_value"]
48 accessKeyID := args["access_key_id"]
49 accessKeySecret := args["access_key_secret"]
50
51 log.Printf("[DEBUG] discover-aliyun: Using region=%s tag_key=%s tag_value=%s", region, tagKey, tagValue)
52 if accessKeyID == "" && accessKeySecret == "" {
53 log.Printf("[DEBUG] discover-aliyun: No static credentials")
54 } else {
55 log.Printf("[DEBUG] discover-aliyun: Static credentials provided")
56 }
57
58 if region == "" {
59 l.Printf("[DEBUG] discover-aliyun: Region not provided")
60 return nil, fmt.Errorf("discover-aliyun: invalid region")
61 }
62 l.Printf("[INFO] discover-aliyun: Region is %s", region)
63
64 svc := ecs.NewClient(accessKeyID, accessKeySecret)
65
66 if p.userAgent != "" {
67 svc.SetUserAgent(p.userAgent)
68 }
69
70 l.Printf("[INFO] discover-aliyun: Filter instances with %s=%s", tagKey, tagValue)
71 resp, err := svc.DescribeInstancesWithRaw(&ecs.DescribeInstancesArgs{
72 RegionId: common.Region(region),
73 Status: ecs.Running,
74 Tag: map[string]string{
75 tagKey: tagValue,
76 }},
77 )
78
79 if err != nil {
80 return nil, fmt.Errorf("discover-aliyun: DescribeInstancesWithRaw failed: %s", err)
81 }
82
83 l.Printf("[DEBUG] discover-aliyun: Found total %d instances", resp.TotalCount)
84
85 var addrs []string
86 for _, instanceAttributesType := range resp.Instances.Instance {
87 switch instanceAttributesType.InstanceNetworkType {
88 case "classic":
89 for _, ipAddress := range instanceAttributesType.InnerIpAddress.IpAddress {
90 l.Printf("[DEBUG] discover-aliyun: Instance %s has innner ip %s ", instanceAttributesType.InstanceId, ipAddress)
91 addrs = append(addrs, ipAddress)
92 }
93 case "vpc":
94 for _, ipAddress := range instanceAttributesType.VpcAttributes.PrivateIpAddress.IpAddress {
95 l.Printf("[DEBUG] discover-aliyun: Instance %s has private ip %s ", instanceAttributesType.InstanceId, ipAddress)
96 addrs = append(addrs, ipAddress)
97 }
98 }
99 }
100
101 l.Printf("[DEBUG] discover-aliyun: Found ip addresses: %v", addrs)
102 return addrs, nil
103 }