| 1 |
// Package aws provides node discovery for Joyent Triton. |
| 2 |
package triton |
| 3 |
|
| 4 |
import ( |
| 5 |
"context" |
| 6 |
"fmt" |
| 7 |
"io/ioutil" |
| 8 |
"log" |
| 9 |
|
| 10 |
"github.com/joyent/triton-go" |
| 11 |
"github.com/joyent/triton-go/authentication" |
| 12 |
"github.com/joyent/triton-go/compute" |
| 13 |
) |
| 14 |
|
| 15 |
type Provider struct{} |
| 16 |
|
| 17 |
func (p *Provider) Help() string { |
| 18 |
return `triton: |
| 19 |
|
| 20 |
provider: "triton" |
| 21 |
account: The Triton account name |
| 22 |
key_id: The Triton KeyID |
| 23 |
url: The Triton URL |
| 24 |
tag_key: The tag key to filter on |
| 25 |
tag_value: The tag value to filter on |
| 26 |
` |
| 27 |
} |
| 28 |
|
| 29 |
func (p *Provider) Addrs(args map[string]string, l *log.Logger) ([]string, error) { |
| 30 |
if args["provider"] != "triton" { |
| 31 |
return nil, fmt.Errorf("discover-triton: invalid provider " + args["provider"]) |
| 32 |
} |
| 33 |
|
| 34 |
if l == nil { |
| 35 |
l = log.New(ioutil.Discard, "", 0) |
| 36 |
} |
| 37 |
|
| 38 |
account := args["account"] |
| 39 |
keyID := args["key_id"] |
| 40 |
url := args["url"] |
| 41 |
tagKey := args["tag_key"] |
| 42 |
tagValue := args["tag_value"] |
| 43 |
|
| 44 |
l.Printf("[INFO] discover-triton: Account is %q", account) |
| 45 |
l.Printf("[INFO] discover-triton: URL is %q", url) |
| 46 |
|
| 47 |
input := authentication.SSHAgentSignerInput{ |
| 48 |
KeyID: keyID, |
| 49 |
AccountName: account, |
| 50 |
} |
| 51 |
signer, err := authentication.NewSSHAgentSigner(input) |
| 52 |
if err != nil { |
| 53 |
return nil, fmt.Errorf("error Creating SSH Agent Signer: %v", err) |
| 54 |
} |
| 55 |
|
| 56 |
config := &triton.ClientConfig{ |
| 57 |
TritonURL: url, |
| 58 |
AccountName: account, |
| 59 |
Signers: []authentication.Signer{signer}, |
| 60 |
} |
| 61 |
|
| 62 |
c, err := compute.NewClient(config) |
| 63 |
if err != nil { |
| 64 |
return nil, fmt.Errorf("error constructing Compute Client: %v", err) |
| 65 |
} |
| 66 |
|
| 67 |
t := make(map[string]interface{}, 0) |
| 68 |
t[tagKey] = tagValue |
| 69 |
|
| 70 |
listInput := &compute.ListInstancesInput{ |
| 71 |
Tags: t, |
| 72 |
} |
| 73 |
instances, err := c.Instances().List(context.Background(), listInput) |
| 74 |
if err != nil { |
| 75 |
return nil, fmt.Errorf("error getting instance list: %v", err) |
| 76 |
} |
| 77 |
var addrs []string |
| 78 |
for _, instance := range instances { |
| 79 |
l.Printf("[DEBUG] Instance ID: %q", instance.ID) |
| 80 |
l.Printf("[DEBUG] Instance PrimaryIP: %q", instance.PrimaryIP) |
| 81 |
if instance.PrimaryIP == "" { |
| 82 |
l.Printf("[DEBUG] discover-triton: Instance %s has no marked PrimaryIP", instance.ID) |
| 83 |
continue |
| 84 |
} else { |
| 85 |
addrs = append(addrs, instance.PrimaryIP) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return addrs, nil |
| 90 |
} |