Created
December 12, 2014 10:43
-
-
Save s-kiriki/7b6c3b6c8f2b41c4fdfc to your computer and use it in GitHub Desktop.
ChatOps( Slack / Hubot / Docker )で検証環境をポンポン作って、ポンポン捨てる ref: http://qiita.com/s-kiriki/items/501c08d22827def4ec9f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ $# -ne 1 ]; then | |
echo "You must specify branch name" | |
exit 1 | |
fi | |
BRANCH_NAME=$1 | |
cd /var/www/html && git clone -b $BRANCH_NAME --depth 1 [email protected]:s-kiriki/docker-test-app.git |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ $# -ne 1 ]; then | |
echo "You must specify container name or ID" | |
exit 1 | |
fi | |
CONTAINER_ID=$1 | |
REMOVED_CONTAINER=`sudo docker rm -f $CONTAINER_ID` | |
if [ $? -ne 0 ] | |
then | |
echo "Error occured on removing container $CONTAINER_ID." | |
exit 1 | |
fi | |
echo ":docker: Removed container: $REMOVED_CONTAINER" # :docker: というのはSlackのカスタム絵文字 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ $# -ne 1 ]; then | |
echo "You must specify branch name" | |
exit 1 | |
fi | |
BRANCH_NAME=$1 | |
CONTAINER_ID=`sudo docker run -d -P docker_app` # コンテナの起動 | |
if [ $? -ne 0 ] | |
then | |
echo "Error occured on running container." | |
exit 1 | |
fi | |
echo "Created Container:$CONTAINER_ID" | |
PORT_FORWARDING=`sudo docker port $CONTAINER_ID` | |
if [ $? -ne 0 ] | |
then | |
echo "Error occured on checking port forwarding." | |
exit 1 | |
fi | |
sudo docker exec $CONTAINER_ID sh app_init.sh $BRANCH_NAME # アプリの設置 | |
if [ $? -ne 0 ] | |
then | |
echo "Error occured on setting app." | |
exit 1 | |
fi | |
echo ":docker: Your app is ready!! $PORT_FORWARDING" # :docker: というのはSlackのカスタム絵文字 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM centos:centos6 | |
RUN yum update -y | |
RUN yum install -y httpd | |
RUN yum install -y git | |
ADD ./app_init.sh / | |
RUN mkdir /root/.ssh/ | |
ADD id_rsa /root/.ssh/id_rsa | |
RUN chmod 700 /root/.ssh/id_rsa | |
RUN echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config | |
EXPOSE 80 | |
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ssh -i .ssh/docker-key.pem [email protected] | |
> ubuntu@ip-172-31-6-189:~ $ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ubuntu:~$ sudo adduser wanko | |
Adding user `wanko' ... | |
Adding new group `wanko' (1001) ... | |
Adding new user `wanko' (1001) with group `wanko' ... | |
Creating home directory `/home/wanko' ... | |
Copying files from `/etc/skel' ... | |
Enter new UNIX password: | |
Retype new UNIX password: | |
passwd: password updated successfully | |
Changing the user information for wanko | |
Enter the new value, or press ENTER for the default | |
Full Name []: | |
Room Number []: | |
Work Phone []: | |
Home Phone []: | |
Other []: | |
Is the information correct? [Y/n] y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -sSL https://get.docker.com/ubuntu/ | sudo sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo docker --version | |
Docker version 1.4.0, build 4595d4f |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wanko@ip-172-31-6-189:~$ ls | |
app_init.sh Dockerfile id_rsa |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo docker build -t docker_app . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo docker images | |
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE | |
docker_app latest 6900f52b1ac5 5 seconds ago 466.5 MB | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo docker run -d -P docker_app | |
afbf3bd7c6be6e36ca436d34880b49970bacb25a72a9a351c9d3c28d5a8cd0a6 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo docker ps | |
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | |
afbf3bd7c6be docker_app:latest "/usr/sbin/httpd -D 2 minutes ago Up 2 minutes 0.0.0.0:49157->80/tcp kickass_brown |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo docker exec kickass_brown sh app_init.sh master |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ubuntu:~$ sudo usermod -G sudo wanko |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install simple-ssh --save |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ubuntu:~$ sudo visudo | |
#以下を、追記 | |
wanko ALL=(ALL) NOPASSWD: ALL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ubuntu:~$ sudo vi /etc/ssh/sshd_config |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo service ssh restart |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ssh [email protected] | |
[email protected]'s password: | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo apt-get update | |
sudo apt-get install docker.io |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo docker --version | |
Docker version 1.0.1, build 990021a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Description: | |
# Handling Docker containers. | |
# | |
# Dependencies: | |
# "simple-ssh": "^0.8.1" | |
# "hubot-slack": "^2.2.0" | |
# | |
# Configuration: | |
# [Required] | |
# DOCKER_HOST | |
# DOCKER_USER | |
# DOCKER_PASSWD | |
# | |
# Author: | |
# s-kiriki | |
module.exports = (robot) -> | |
_getSSH = (msg) -> | |
if !process.env.DOCKER_HOST | |
return msg.send 'You must set ENV: DOCKER_HOST' | |
if !process.env.DOCKER_USER | |
return msg.send 'You must set ENV: DOCKER_USER' | |
if !process.env.DOCKER_PASSWD | |
return msg.send 'You must set ENV: DOCKER_PASSWD' | |
SSH = require('simple-ssh'); | |
ssh = new SSH({ | |
host: process.env.DOCKER_HOST, | |
user: process.env.DOCKER_USER, | |
pass: process.env.DOCKER_PASSWD | |
}); | |
return ssh | |
robot.respond /container up( (.+))?/i, (msg) -> | |
branch_name = msg.match[2] || "master" | |
ssh = _getSSH(msg) | |
ssh.exec("sh container-up.sh #{branch_name}", { | |
out: (stdout) -> | |
msg.send stdout | |
}).start(); | |
robot.respond /container remove (\w+)/i, (msg) -> | |
container_id = msg.match[1] | |
ssh = _getSSH(msg) | |
ssh.exec("sh container-remove.sh #{container_id}", { | |
out: (stdout) -> | |
msg.send stdout | |
}).start(); | |
robot.respond /container list/i, (msg) -> | |
ssh = _getSSH(msg) | |
ssh.exec("sudo docker ps | awk '{print $1, $(NF-1), $(NF)}'", { | |
out: (stdout) -> | |
msg.send stdout | |
}).start(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#PasswordAuthentication no | |
PasswordAuthentication yes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment