一 chatops的概念在國內雖然有一部分的文章談過,但不是很廣泛,這裏牛刀小試,作一個有關chatops的功能。node
二 什麼是chatops?git
chatops表面上就是在一個聊天工具中,發送一條命令給運維機器人bot,而後bot根據咱們預約義的操做進行執行,並返回執行結果。至於更深層次的做用,就是將重複性的手工的運維工做自動化了,開發人員、運維人員能夠按需執行一些運維操做。說完這些,咱們看看今天實驗的一些工具和介紹。github
2.1 RocketChat:能夠把它想象成微信,它依賴於MongoDB。web
2.2 hubot:hubot是github出品的一個運維機器人。本質就是一個接收命令消息,執行預約義操做的一個程序。而接收命令消息的這個組件在hubot中被稱爲adapter,eg:rocketChat的adapter:hubot。市面上已經有不少了。hubot接收命令後如何執行就須要咱們來實現了。docker
2.3 jenkins:很少講,應該都知道,CICD的神器。shell
三 本文主題npm
在RocketChat的聊天窗口命令hubot執行一次jenkins構建任務。json
四 安裝bash
個人環境微信
10.10.0.171 MongoDB、RocketChat Server、Hubot
10.10.0.144 jenkins
4.1 MongoDB:
docker run --name db -d mongo:3.0 --smallfiles
4.2 RocketChat Server:
docker run --name rocketchat -p 80:3000 --env ROOT_URL=http://10.10.0.171 --link db -d rocket.chat
(參考連接:https://hub.docker.com/_/rocket-chat)
4.3 RocketChat web端訪問:http://10.10.0.171
(同時能夠藉助客戶端鏈接工具ROCKET.CHAT工具,該工具能夠直接下載。)
首次登錄,我這裏建立了一個超級用戶fzh42353:
用fzh42353這個用戶登錄,從新打開終端註冊一個新用戶:bot,密碼:bot123(此用戶便於hubot鏈接到rocketchat使用),角色權限以下:
五 jenkins安裝
docker run --name chat-jenkins -p 8080:8080 -p 50000:50000 -v /data/jenkins_workspaces/:/var/jenkins_home jenkins
六 hubot-rocketchat adapter安裝
docker run -it -e ROCKETCHAT_URL=http://10.10.0.171:80 \ -e ROCKETCHAT_ROOM='general' \ -e LISTEN_ON_ALL_PUBLIC=true \ -e ROCKETCHAT_USER=bot \ -e ROCKETCHAT_PASSWORD=bot123 \ -e ROCKETCHAT_AUTH=password \ -e BOT_NAME=bot \ -e HUBOT_JENKINS_URL=http://10.10.0.144:8080 \ -e HUBOT_JENKINS_AUTH=jfsor:c1ee2b0b \ -e EXTERNAL_SCRIPTS=hubot-pugme,hubot-help,hubot-script-shellcmd,hubot-jenkins \ -v /opt/scripts:/home/hubot/scripts \ -d rocketchat/hubot-rocketchat
我這裏添加了hubot-script-shellcmd,hubot-jenkins兩個插件,也能夠不加上,跑起來後進入容器npm進行安裝。
參數解釋:
ROCKETCHAT_URL: server地址
ROCKETCHAT_ROOM: 默認加入的channel(房間),能夠不填
ROCKETCHAT_USER: robot名字,我上面的bot
ROCKETCHAT_PASSWORD: bot的密碼:bot123
ROCKETCHAT_AUTH:認證的方式:password
HUBOT_JENKINS_URL:jenkins地址,hubot-jenkins插件會用到
HUBOT_JENKINS_AUTH:登陸jenkins的用戶名和密碼,user:password,hubot-jenkins插件會用到
/opt/scripts:/home/hubot/scripts: 本地scripts腳本映射到容器內,hubot-script-shellcmd插件會用到
六 以上過程後,整個搭建工做就算是完成了,接下來看下怎麼實踐。
[root@k8s-master-02 ~]# grep -v "^#\| #\|^$" /opt/scripts/example.coffee module.exports = (robot) -> robot.hear /hi/i, (res) -> res.reply "hello" robot.hear /吃飯了嗎/, (res) -> res.send '只有大家這些愚蠢的人類才吃飯'
hubot@759f2a19692f:~$ cat external-scripts.json ["hubot-pugme","hubot-help","hubot-script-shellcmd","hubot-jenkins"] hubot@759f2a19692f:~$ cd - /home/hubot/node_modules/hubot-script-shellcmd/bash/handlers hubot@759f2a19692f:~/node_modules/hubot-script-shellcmd/bash/handlers$ ls helloworld update hubot@759f2a19692f:~/node_modules/hubot-script-shellcmd/bash/handlers$ cat helloworld #!/bin/bash echo "Hello.." sleep 1s; echo "Sleepy World!"
(上面的example.coffee爲自帶的測試腳本,此處我進行了適當修改。~/node_modules/hubot-script-shellcmd/bash/handlers下面咱們能夠自定義可執行的shell腳本。而後shellcmd command便可。)
hubot-jenkins的用法見下面連接:
https://www.npmjs.com/package/hubot-jenkins-enhanced
七 上訴咱們能實現經過rocketchat server發送命令給hubot adapter進而觸發jenkins構建工做,但若是想獲得jenkins的返回結果,還少了一部。
這樣咱們就拿到結果了,能夠看出構建結果是成功的,同時會返回構建時間和構建版本號。
八 咱們可使用七的方式,經過jenkins插件來完成這件事,同時也能夠經過rocketchat的webhook來實現,看下列截圖:
最後保存修改,你能夠獲得一個webhook url,一個token,一個發送通知的url,eg:
curl -X POST -H 'Content-Type: application/json' --data '{"text":"Example message","attachments":[{"title":"Rocket.Chat","title_link":"https://rocket.chat","text":"Rocket.Chat, the best open source chat","image_url":"/images/integration-attachment-example.png","color":"#764FA5"}]}' http://10.10.0.171/hooks/wDLZ2y4jsR8rP7gMP/TmirXF8tEbAGo9gNxbvitf5tuBEmWKHGAinxHzB8zMjwXNmp
修改傳輸接口便可達到發送通知的目的。url能夠在jenkins中設定。
備註:
參考連接:https://www.jianshu.com/p/76d547fa23ac