Skip to content

Instantly share code, notes, and snippets.

@inokappa
Created July 2, 2017 10:44
Show Gist options
  • Save inokappa/12b0f484bdae8876f6917ff5b43dcfae to your computer and use it in GitHub Desktop.
Save inokappa/12b0f484bdae8876f6917ff5b43dcfae to your computer and use it in GitHub Desktop.
EC2 の一覧を取得する Golang Sample スクリプト
package main
import (
"os"
"os/exec"
"fmt"
"flag"
"strings"
"io/ioutil"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/olekukonko/tablewriter"
)
var (
argProfile = flag.String("profile", "", "AWS Shared Credential の Profile 名を指定する")
argRegion = flag.String("region", "ap-northeast-1", "AWS Region 名を指定する")
argDnsMasq = flag.Bool("dnsmasq", false, "dnsmasq 用の hosts リストを生成する")
argComma = flag.Bool("comma", false, "カンマ区切りの hosts リストを生成する")
argMarkDown = flag.Bool("markdown", false, "Markdown のテーブルフォーマットで出力する")
argRead = flag.Bool("read", false, "-path オプションで指定したファイルを読み取る")
argFilePath = flag.String("path", "hosts_list", "ファイルのパスを指定する")
argCommand = flag.String("command", "", "指定したコマンドを実行する")
)
func output_tbl(data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"tag:Name", "InstanceId", "InstanceType", "AZ", "PrivateIp", "PublicIp", "Status"})
for _, value := range data {
table.Append(value)
}
table.Render()
}
func output_markdown(data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"tag:Name", "InstanceId", "InstanceType", "AZ", "PrivateIp", "PublicIp", "Status"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
for _, value := range data {
table.Append(value)
}
table.Render()
}
func output_dnsmasq(data [][]string, file_path string) {
var tag_name string
hosts_list := []string{}
for _, value := range data {
if value[0] == "" {
tag_name = "TagName_Not_Assigned_" + value[4]
} else {
tag_name = value[0]
}
line := []string{
"address=",
tag_name,
value[4],
}
host_list := strings.Join(line, "/")
hosts_list = append(hosts_list, host_list)
}
write_hosts_list_file(hosts_list, file_path)
}
func output_comma(data [][]string, file_path string) {
hosts_list := []string{}
for _, value := range data {
host_list := strings.Join(value, ",")
hosts_list = append(hosts_list, host_list)
}
write_hosts_list_file(hosts_list, file_path)
}
func read_hosts_list_file(file_path string) {
contents, _ := ioutil.ReadFile(file_path)
fmt.Println(string(contents))
}
func write_hosts_list_file(hosts_list []string, file_path string) {
var contents string
contents = strings.Join(hosts_list, "\n")
err := ioutil.WriteFile(file_path, []byte(contents), 0644)
if err != nil {
panic(err)
os.Exit(1)
}
}
func exec_command(command string) {
out, err := exec.Command("sh", "-c", command).Output()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(out))
}
func aws_ec2_client(profile string, region string) *ec2.EC2 {
var config aws.Config
if profile != "" {
creds := credentials.NewSharedCredentials("", profile)
config = aws.Config{Region: aws.String(region), Credentials: creds}
} else {
config = aws.Config{Region: aws.String(region)}
}
sess := session.New(&config)
ec2_client := ec2.New(sess)
return ec2_client
}
func main() {
flag.Parse()
if *argRead {
if *argFilePath != "" {
read_hosts_list_file(*argFilePath)
os.Exit(0)
} else {
os.Exit(1)
}
}
ec2_client := aws_ec2_client(*argProfile, *argRegion)
res, err := ec2_client.DescribeInstances(nil)
if err != nil {
panic(err)
os.Exit(1)
}
instances := [][]string{}
for _, r := range res.Reservations {
for _, i := range r.Instances {
var tag_name string
for _, t := range i.Tags {
if *t.Key == "Name" {
tag_name = *t.Value
}
}
if i.PublicIpAddress == nil {
i.PublicIpAddress = aws.String("Not assignment")
}
instance := []string{
tag_name,
*i.InstanceId,
*i.InstanceType,
*i.Placement.AvailabilityZone,
*i.PrivateIpAddress,
*i.PublicIpAddress,
*i.State.Name,
}
instances = append(instances, instance)
}
}
if *argDnsMasq {
output_dnsmasq(instances, *argFilePath)
if *argCommand != "" {
exec_command(*argCommand)
}
} else if *argComma {
output_comma(instances, *argFilePath)
} else if *argMarkDown {
output_markdown(instances)
} else {
output_tbl(instances)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment