東京は
涼しい朝だった。
よしじん
でよっちゃんマスターと朝ごはん。天ぷら定食を食べる。
天ぷらのひらおには敵わないけど、揚げたてでとても美味しかった。
博多で
最後の歌のレッスンとなった奥さんと待ち合わせて炭寅で焼き鳥。
みつせ鶏のもも身、肝が美味しゅうございました。肝はレア焼きなら塩が良い。
涼しい朝だった。
でよっちゃんマスターと朝ごはん。天ぷら定食を食べる。
天ぷらのひらおには敵わないけど、揚げたてでとても美味しかった。
最後の歌のレッスンとなった奥さんと待ち合わせて炭寅で焼き鳥。
みつせ鶏のもも身、肝が美味しゅうございました。肝はレア焼きなら塩が良い。
当然、インターネットは利用出来ませんので、moto を使って擬似的な API エンドポイントを実行して検証します。
moto に同梱されている moto_server を利用します。
moto_server ec2
これだけで EC2 の API エンドポイントが起動するので、AWS CLI から以下のようにアクセスします。
# # インスタンス起動 # aws \ --profile mock_profile \ --region us-west-2 \ --endpoint-url http://127.0.0.1:5000 \ ec2 run-instances \ --image-id ami-1a2b3c4d \ --count 1 \ --instance-type c3.large \ --key-name MyKeyPair \ --security-groups MySecurityGroup
エンドポイントはデフォルトでローカルホストのポート 5000 番で起動します。
EC2 インスタンスにタグを付けたり、外したりしてみたいと思います。
$ sw_vers ProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G1421 $ go version go version go1.8.3 darwin/amd64
$ ./tagCtrl -h Usage of ./tagCtrl: -add タグをインスタンスに付与. -del タグをインスタンスから削除. -endpoint string AWS API のエンドポイントを指定. -instances string Instance ID 又は Instance Tag 名を指定. -profile string Profile 名を指定. -region string Region 名を指定. (default "ap-northeast-1") -tags string Tag Key(Key=) 及び Tag Value(Value=) を指定.
実行。
$ ./tagCtrl -endpoint=http://127.0.0.1:5000 -instances=i-e7f4f175 -tags='Key=hogehoge,Value=fugafuga' -add
確認。
$ aws \ --profile mock_profile \ --region us-west-2 \ --endpoint-url http://127.0.0.1:5000 \ ec2 describe-instances \ --query 'Reservations[].Instances[].Tags[]' \ --output table -------------------------- | DescribeInstances | +-----------+------------+ | Key | Value | +-----------+------------+ | hogehoge | fugafuga | +-----------+------------+
複数のタグを付与。
$ ./tagCtrl -endpoint=http://127.0.0.1:5000 -instances=i-e7f4f175 -tags='Key=hogehoge,Value=fugafuga Key=hage,Value=atama' -add
確認。
$ aws \ --profile mock_profile \ --region us-west-2 \ --endpoint-url http://127.0.0.1:5000 \ ec2 describe-instances \ --query 'Reservations[].Instances[].Tags[]' \ --output table -------------------------- | DescribeInstances | +-----------+------------+ | Key | Value | +-----------+------------+ | hogehoge | fugafuga | | hage | atama | +-----------+------------+
複数のインスタンスに複数のタグを付与。
$ ./tagCtrl -endpoint=http://127.0.0.1:5000 -instances=i-e7f4f175,i-e8b89e8b -tags='Key=hogehoge,Value=fugafuga Key=hage,Value=atama' -add
確認。
$ aws \ --profile mock_profile \ --region us-west-2 \ --endpoint-url http://127.0.0.1:5000 \ ec2 describe-instances \ --query 'Reservations[].Instances[].{InstanceID:InstanceId,Tags:Tags}' \ --output table ---------------------------- | DescribeInstances | +--------------------------+ | InstanceID | +--------------------------+ | i-e7f4f175 | +--------------------------+ || Tags || |+-----------+------------+| || Key | Value || |+-----------+------------+| || hogehoge | fugafuga || || hage | atama || |+-----------+------------+| | DescribeInstances | +--------------------------+ | InstanceID | +--------------------------+ | i-e8b89e8b | +--------------------------+ || Tags || |+-----------+------------+| || Key | Value || |+-----------+------------+| || hogehoge | fugafuga || || hage | atama || |+-----------+------------+|
実行。
$ ./tagCtrl -endpoint=http://127.0.0.1:5000 -instances=i-e7f4f175 -tags='Key=hogehoge,Value=fugafuga Key=hage,Value=atama' -del
確認。
$ aws \ --profile mock_profile \ --region us-west-2 \ --endpoint-url http://127.0.0.1:5000 \ ec2 describe-instances \ --query 'Reservations[].Instances[].Tags[]' \ --output table ------------------- |DescribeInstances|
複数のインスタンスの任意のタグだけを削除。
$ ./tagCtrl -endpoint=http://127.0.0.1:5000 -instances=i-e7f4f175,i-e8b89e8b -tags='Key=hogehoge,Value=fugafuga' -del
確認。
$ aws \ --profile mock_profile \ --region us-west-2 \ --endpoint-url http://127.0.0.1:5000 \ ec2 describe-instances \ --query 'Reservations[].Instances[].{InstanceID:InstanceId,Tags:Tags}' \ --output table --------------------- | DescribeInstances | +-------------------+ | InstanceID | +-------------------+ | i-e7f4f175 | +-------------------+ || Tags || |+-------+---------+| || Key | Value || |+-------+---------+| || hage | atama || |+-------+---------+| | DescribeInstances | +-------------------+ | InstanceID | +-------------------+ | i-e8b89e8b | +-------------------+ || Tags || |+-------+---------+| || Key | Value || |+-------+---------+| || hage | atama || |+-------+---------+|
EC2 インスタンスの時と同様に以下の通り CreateTagsInput
関数を利用する。
func createTag(ec2Client *ec2.EC2, instances string, tags []*ec2.Tag) { splitedInstances := strings.Split(instances, ",") var instanceIds []*string for _, i := range splitedInstances { instanceIds = append(instanceIds, aws.String(i)) } input := &ec2.CreateTagsInput{ Resources: instanceIds, Tags: tags, } result, err := ec2Client.CreateTags(input) if err != nil { fmt.Println(err.Error()) os.Exit(1) } fmt.Println(result) }
クライアントの初期化時に以下のように指定したらイケた。
func awsEc2Client(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, Endpoint: aws.String(*argEndpoint)} } else { config = aws.Config{Region: aws.String(region), Endpoint: aws.String(*argEndpoint)} } sess := session.New(&config) ec2Client := ec2.New(sess) return ec2Client }
メモでした。
鹿児島でとてもお世話になっていた福田治療室が神楽坂に開院されたとのことで挨拶に伺った。
福田治療室とは自分が高校時代からの付き合いになるので、かれこれ 20 年以上お世話になっているが、先生も奥様もとても元気で色々な昔話に会話が弾んだ。
久しぶりに施術もして頂いて体が軽くなった気がする。
福田治療室から、一旦、ホテルに帰って少し仕事をしてから金町へ。
第二の故郷みたいなもので、よっちゃんで三時間くらい、マスターの愚痴と焼き鳥、レモンハイでダラダラと過ごす。よっちゃんマスター、久しぶりだったけど相変わらず元気そうでなにより。
その後、金町の兄貴こときょうじさんのお店「よしじん」で少しきょうじさんと話をしつつ、金町のうたひろ「やすらぎ」へ。きょうじさんとの Let’s Try Again のコラボ、最高だった。
いつ行っても、何も変わらず受け入れてくれる金町の皆さんに本当に感謝。
新橋に戻ってから、家系ラーメンというものを食べたことが無かったので食べてみた。
自称博多の男としては麺は硬めを貫きたかったので、硬めでオーダーしたら残念な感じだったので、次からは「普通」でオーダーしようと思う。
$ sw_vers ProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G1421 $ go version go version go1.8.3 darwin/amd64
$ ./ec2Ctrl -h Usage of ./ec2Ctrl: -instances string Instance ID 又は Instance Tag 名を指定. -profile string Profile 名を指定. -region string Region 名を指定. (default "ap-northeast-1") -start Instance を起動. -stop Instance を停止.
$ ./ec2Ctrl -profile=your-profile +------------------+---------------------+--------------+-----------------+---------------+----------------+---------+ | TAG:NAME | INSTANCEID | INSTANCETYPE | AZ | PRIVATEIP | PUBLICIP | STATUS | +------------------+---------------------+--------------+-----------------+---------------+----------------+---------+ | dev-aaa2 | i-xxxxxxxxxxxxxxxxx | t2.micro | ap-northeast-1a | 172.xx.y.187 | Not assignment | stopped | | aaaaaa | i-xxxxxxxx | t2.micro | ap-northeast-1a | 172.xx.z.39 | Not assignment | stopped | | HOGEHOGE | i-x1x1x1x1x1x1x1x1x | t2.micro | ap-northeast-1a | 172.xx.y.89 | Not assignment | stopped | | DEV-bbb | i-yyyyyyyy | t2.medium | ap-northeast-1a | 172.xx.xx.139 | Not assignment | stopped | | dev-hogehoge | i-pppppppp | t2.micro | ap-northeast-1a | 172.xx.y.117 | 52.xxx.xxx.123 | stopped | | kawahara-sandbox | i-x2x2x2x2x2x2x2x2x | t2.micro | ap-northeast-1a | 172.xx.p.28 | Not assignment | stopped | +------------------+---------------------+--------------+-----------------+---------------+----------------+---------+
$ ./ec2Ctrl -start -instances=HOGEHOGE,kawahara-sandbox +------------------+---------------------+--------------+-----------------+-------------+----------------+---------+ | TAG:NAME | INSTANCEID | INSTANCETYPE | AZ | PRIVATEIP | PUBLICIP | STATUS | +------------------+---------------------+--------------+-----------------+-------------+----------------+---------+ | HOGEHOGE | i-x1x1x1x1x1x1x1x1x | t2.micro | ap-northeast-1a | 172.xx.y.89 | Not assignment | stopped | | kawahara-sandbox | i-x2x2x2x2x2x2x2x2x | t2.micro | ap-northeast-1a | 172.xx.p.28 | Not assignment | stopped | +------------------+---------------------+--------------+-----------------+-------------+----------------+---------+ 上記のインスタンスを操作しますか?(y/n): y EC2 を起動します. i-x1x1x1x1x1x1x1x1x を起動しました. i-x2x2x2x2x2x2x2x2x を起動しました.
$ ./ec2Ctrl -stop -instances=HOGEHOGE,kawahara-sandbox +------------------+---------------------+--------------+-----------------+-------------+----------------+---------+ | TAG:NAME | INSTANCEID | INSTANCETYPE | AZ | PRIVATEIP | PUBLICIP | STATUS | +------------------+---------------------+--------------+-----------------+-------------+----------------+---------+ | HOGEHOGE | i-x1x1x1x1x1x1x1x1x | t2.micro | ap-northeast-1a | 172.xx.y.89 | 52.xxx.xxx.123 | running | | kawahara-sandbox | i-x2x2x2x2x2x2x2x2x | t2.micro | ap-northeast-1a | 172.xx.p.28 | 13.xxx.z.123 | running | +------------------+---------------------+--------------+-----------------+-------------+----------------+---------+ 上記のインスタンスを操作しますか?(y/n): y EC2 を停止します. i-x1x1x1x1x1x1x1x1x を停止しました. i-x2x2x2x2x2x2x2x2x を停止しました.
以下のような xxxxxxxxInput
関数を使って定義する。
params := &ec2.DescribeInstancesInput { InstanceIds: instances, } res, err := ec2Client.DescribeInstances(params) if err != nil { fmt.Println(err.Error()) os.Exit(1) }
色々とググった結果、結局以下の公式サンプルにたどり着いた。
やっぱり、最初は公式ドキュメントを見るべき。反省。
メモでした。
$ sw_vers ProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G1421 $ go version go version go1.8.3 darwin/amd64
$ ./cmdRunner -help Usage of ./cmdRunner: -command string Specify a Command. -interval int Specify a Wait Interval Times. (default 3) -times int Specify a Loop Counts Number. (default 1)
$ ./cmdRunner -command="date +%Y%m%d-%H:%M:%S" -times=5 -interval=1 | jq . { "outputs": [ "20170718-07:56:55", "20170718-07:56:56", "20170718-07:56:57", "20170718-07:56:58", "20170718-07:56:59" ] }
$ ./cmdRunner -command="date +%Y%m%d-%H:%M:%S" -times=3 -interval=3 | jq . { "outputs": [ "20170718-07:57:54", "20170718-07:57:57", "20170718-07:58:00" ] }
CombinedOutput()
を利用すると良いらしい。
func runCommand(command string) (output string){ cmd := exec.Command("sh", "-c", command) stdout, err := cmd.CombinedOutput() output = strings.TrimRight(string(stdout), "\n") if err != nil { errMessage := err.Error() + ": " + output log.Printf(string(errMessage)) return output } return output }
// 配列を含んだ JSON の構造体を定義 type OutputMessages struct { Outputs []string `json:"outputs"` } ... // JSON オブジェクトを生成 outputMessage := OutputMessages{Outputs: []string(stdouts)} // Marshal 関数を利用して JSON を生成 jsonBytes, err := json.Marshal(outputMessage)
メモでした。