5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Hubot】チャットからHubotとJenkins経由でEC2インスタンスのステートを操作する

Last updated at Posted at 2015-04-02

Flowchart.png

はじめに

AWS CLIの勉強がてらにやってみた内容です。

1 チャットツールでJenkinsのジョブの実行を唱える

2 HubotがJenkinsにジョブを実行しろと伝達

3 Jenkinsがジョブに登録されたインスタンスを破壊(Terminate)するコマンドを実行

遠隔で手軽にサーバの操作ができるのは便利です。
チャットツールは「slack」でも「Hipchat」でも「chatwork」でもどれでもいいです。
(chatworkでやりました)

Hubotの設定

Jenkinsと連携させなければいけません。
とは言ってもスクリプトを書く必要はありません。

『hubot-scripts.json』を修正します。

hubot-scripts.json
["jenkins.coffee"]



次にJenkins連携用の環境変数を設定します。


export HUBOT_JENKINS_URL=YourJenkinsUrl
export HUBOT_JENKINS_AUTH=xxxxxx:xxxxxx

「HUBOT_JENKINS_AUTH」にはコロン区切りでJenkinsのユーザー名とパスワードを入力してください。

これでHubotの設定は以上です。

Jenkinsのジョブ設定

本筋から外れるので、詳しく説明しませんが、Jenkinsが動くサーバでAWS CLIをインストールしてコマンド操作ができる状態にしておきます。

さらに、

『AWSマネジメントコンソール』

右上メニュー『Security Credentials』

『Access Keys (Access Key ID and Secret Access Key)』

から「Access Key ID」と「Secret Access Key」を控えておく。

で、ジョブ作ってシェルスクリプトを書きます。

スクリーンショット 2015-04-02 15.36.29.png



export WORKSPACE
sh -x ${WORKSPACE}/instart.sh

保存する。

実行ファイルでさきほどの「Access Key ID」と「Secret Access Key」を記入ます。

XXXX.sh

# !/bin/bash -xe

aws configure set aws_access_key_id AWSアクセスキーID

aws configure set aws_secret_access_key AWSシークレットアクセスキーID

aws ec2 terminate-instances --region=ap-northeast-1 --instance-ids=インスタンスID

Jenkins側もこれで以上です。

chatworkで実行

あとは禁断の台詞を唱えるだけです。

スクリーンショット 2015-04-02 16.47.40.png

うまくいけば、破壊に成功しているでしょう。

最後に

アメリカの核戦略ミサイルの発射プロセスに似てます。
めちゃくちゃ単純に解釈すると

大統領(ユーザー)が命令

補佐官・取り巻き(Hubot)がシークレットコードを送信

SSBNの士官連中(Jenkins)が受け取ったシークレットコードを基に協議・合意

発射

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?