Skip to content

[WIP] Rebuild and Improve Docker Container #289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
49 changes: 24 additions & 25 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
FROM node:4.8.3
MAINTAINER Rocket.Chat Team <[email protected]>
FROM node:8.11.2-alpine
LABEL maintainer="Rocket.Chat Team <[email protected]>"

RUN npm install -g coffee-script yo generator-hubot && \
useradd hubot -m
ENV npm_config_loglevel=error

USER hubot
USER root

WORKDIR /home/hubot
COPY bin/hubot /home/hubot/bin/
COPY package.json /home/hubot/

ENV BOT_NAME "rocketbot"
ENV BOT_OWNER "No owner specified"
ENV BOT_DESC "Hubot with rocketbot adapter"
RUN apk add --update --no-cache \
git && \
adduser -S hubot && \
addgroup -S hubot && \
touch ~/.bashrc && \
npm install --global npm@latest && \
npm install -g coffee-script && \
mkdir /home/hubot/scripts/ && \
chown -R hubot:hubot /home/hubot/

ENV EXTERNAL_SCRIPTS=hubot-diagnostics,hubot-help,hubot-google-images,hubot-google-translate,hubot-pugme,hubot-maps,hubot-rules,hubot-shipit
WORKDIR /home/hubot/

RUN yo hubot --owner="$BOT_OWNER" --name="$BOT_NAME" --description="$BOT_DESC" --defaults && \
sed -i /heroku/d ./external-scripts.json && \
sed -i /redis-brain/d ./external-scripts.json && \
npm install hubot-scripts
ENV BOT_OWNER "No owner specified"
ENV BOT_DESC "Hubot with the Rocket.Chat adapter"

ADD . /home/hubot/node_modules/hubot-rocketchat
#ENV EXTERNAL_SCRIPTS=hubot-diagnostics,hubot-google-images,hubot-google-translate,hubot-pugme,hubot-maps,hubot-rules,hubot-shipit

# hack added to get around owner issue: https://github.com/docker/docker/issues/6119
USER root
RUN chown hubot:hubot -R /home/hubot/node_modules/hubot-rocketchat
USER hubot

RUN cd /home/hubot/node_modules/hubot-rocketchat && \
npm install && \
#coffee -c /home/hubot/node_modules/hubot-rocketchat/src/*.coffee && \
cd /home/hubot

CMD node -e "console.log(JSON.stringify('$EXTERNAL_SCRIPTS'.split(',')))" > external-scripts.json && \
npm install $(node -e "console.log('$EXTERNAL_SCRIPTS'.split(',').join(' '))") && \
bin/hubot -n $BOT_NAME -a rocketchat
RUN npm install --no-audit

VOLUME ["/home/hubot/scripts"]

CMD ["/bin/ash", "/home/hubot/bin/hubot"]
24 changes: 9 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
![Rocket.Chat logo](https://rocket.chat/images/logo/logo-dark.svg?v3)
![Rocket.Chat logo](https://rocket.chat/images/default/logo--dark.svg)

[![Rocket.Chat](https://open.rocket.chat/api/v1/shield.svg?type=channel&name=Rocket.Chat&channel=hubot)](https://open.rocket.chat/channel/hubot)
[![Test Coverage](https://codeclimate.com/github/RocketChat/hubot-rocketchat/badges/coverage.svg)](https://codeclimate.com/github/RocketChat/hubot-rocketchat/coverage)
[![Code Climate](https://codeclimate.com/github/RocketChat/hubot-rocketchat/badges/gpa.svg)](https://codeclimate.com/github/RocketChat/hubot-rocketchat)
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/RocketChat/Rocket.Chat/raw/master/LICENSE)
[![MIT License](https://camo.githubusercontent.com/3ccf4c50a1576b0dd30b286717451fa56b783512/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://github.com/RocketChat/Rocket.Chat/raw/master/LICENSE)

# hubot-rocketchat

Expand Down Expand Up @@ -135,33 +135,27 @@ relevant to Hubot. It has some additional configs, [documented here][rcsdk-env].
| Env variable | Description |
| ---------------------- | ----------------------------------------------------- |
| **Hubot** | A subset of relevant [Hubot env vars][hubot-env] |
| `HUBOT_ADAPTER` | Set to `rocketchat` (or pass as launch argument) |
| `HUBOT_NAME` | The programmatic name for listeners |
| `HUBOT_ALIAS` | An alternate name for the bot to respond to |
| `HUBOT_LOG_LEVEL` | The minimum level of logs to output |
| `BOT_NAME` / `HUBOT_NAME` | The programmatic name for listeners |
| `BOT_ALIAS` / `HUBOT_ALIAS` | An alternate name for the bot to respond to |
| `HUBOT_LOG_LEVEL` | Verbosity of the bots console logs. Can be **info, warn or error** |
| `HUBOT_HTTPD` | If the bot needs to listen to or make HTTP requests |
| **Rocket.Chat SDK** | A subset of relevant [SDK env vars][rcsdk-env] |
| `ROCKETCHAT_URL`* | Local Rocketchat address (start before the bot) |
| `ROCKETCHAT_USER`* | Name in the platform (bot user must be created first) |
| `ROCKETCHAT_PASSWORD`* | Matching the credentials setup in Rocket.Chat |
| `ROCKETCHAT_URL`* | URL of your Rocket.Chat instance, eg, https://open.rocket.chat |
| `ROCKETCHAT_USER`* | Username of a bot user that has been pre-created in your Rocket.Chat instance |
| `ROCKETCHAT_PASSWORD`* | Password for the user specified in ROCKETCHAT_USER |
| `ROCKETCHAT_ROOM` | The default room/s for the bot to listen in to (csv) |
| `LISTEN_ON_ALL_PUBLIC` | Whether the bot should be listening everywhere |
| `RESPOND_TO_DM` | If the bot can respond privately or only in the open |
| `RESPOND_TO_EDITED` | If the bot should reply / re-reply to edited messages |
| `RESPOND_TO_LIVECHAT` | If the bot should respond in livechat rooms |
| `INTEGRATION_ID` | Name to ID source of messages in code (e.g Hubot) |

`*` Required settings, unless running locally with testing defaults:
- url: `localhost:3000`
- username: `bot`
- password: `pass`

If you wish that your bot listen to all public rooms and all private rooms it
is joined to set the env `LISTEN_ON_ALL_PUBLIC` to true. `ROCKETCHAT_ROOM` will
be ignored.

Be aware you *must* add the bot's user as a member of the new private group(s)
before it will respond.
before it will respond. If you attempt to start the bot with a room listed in the ROCKETCHAT_ROOM variable that it's not already a member of, the bot will error on startup.

## Connecting to Rocket.Chat

Expand Down
178 changes: 178 additions & 0 deletions bin/hubot
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
#!/bin/bash

#Stop NPM from complaining about useless stuff.
export npm_config_loglevel=error

#This function is used to listen for SIGINT/SIGTERM so the container can be killed with CTRL+C
#Also prevents the docker reboot bug where the container freezes for 30-60 seconds on reboot.
asyncRun() {
"$@" &
pid="$!"
trap "echo 'Stopping PID $pid'; kill -SIGTERM $pid" SIGINT SIGTERM

# A signal emitted while waiting will make the wait command return code > 128
# Let's wrap it in a loop that doesn't end before the process is indeed stopped
while kill -0 $pid >/dev/null 2>&1; do
wait
done
}

cat <<EOF
██████╗ ██████╗ ██████╗██╗ ██╗███████╗████████╗ ██████╗██╗ ██╗ █████╗ ████████╗
██╔══██╗██╔═══██╗██╔════╝██║ ██╔╝██╔════╝╚══██╔══╝ ██╔════╝██║ ██║██╔══██╗╚══██╔══╝
██████╔╝██║ ██║██║ █████╔╝ █████╗ ██║ ██║ ███████║███████║ ██║
██╔══██╗██║ ██║██║ ██╔═██╗ ██╔══╝ ██║ ██║ ██╔══██║██╔══██║ ██║
██║ ██║╚██████╔╝╚██████╗██║ ██╗███████╗ ██║ ██╗╚██████╗██║ ██║██║ ██║ ██║
╚═╝ ╚═╝ ╚═════╝ ╚═════╝╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝
██╗ ██╗██╗ ██╗██████╗ ██████╗ ████████╗
██║ ██║██║ ██║██╔══██╗██╔═══██╗╚══██╔══╝
███████║██║ ██║██████╔╝██║ ██║ ██║
██╔══██║██║ ██║██╔══██╗██║ ██║ ██║
██║ ██║╚██████╔╝██████╔╝╚██████╔╝ ██║
╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝
EOF

echo "Your Rocket.Chat Hubot Docker Container is now starting. Please wait...."

#Set log level to info by default if it has not been set
if [[ -z "${HUBOT_LOG_LEVEL}" ]]; then
echo "INFO: HUBOT_LOG_LEVEL not set. Using HUBOT_LOG_LEVEL=info as the default"
export HUBOT_LOG_LEVEL=info
fi
#Check if the Rocket.Chat URL has been set.
if [[ -z "${ROCKETCHAT_URL}" ]]; then
echo "-------------"
echo "The ROCKETCHAT_URL Environment Variable has not been set. Set this to your Rocket.Chat Server URL"
echo "Example: ROCKETCHAT_URL=https://open.rocket.chat"
echo "Exiting...."
echo "-------------"
exit 1
fi

#Check if the Rocket.Chat User has been set.
if [[ -z "${ROCKETCHAT_USER}" ]]; then
echo "-------------"
echo "ROCKETCHAT_USER Environment Variable has not been set. Set this to the username on your Rocket.Chat server that the bot will log in with."
echo "Example: ROCKETCHAT_USER=rocketbot"
echo "Exiting...."
echo "-------------"
exit 1
fi

#Check if the Rocket.Chat Password has been set for the user above
if [[ -z "${ROCKETCHAT_PASSWORD}" ]]; then
echo "-------------"
echo "----ERROR----"
echo "-------------"
echo "The ROCKETCHAT_PASSWORD Environment Variable has not been set. Set this to the password for the user specified in the ROCKETCHAT_USER environment variable."
echo "Example: ROCKETCHAT_PASSWORD=supersecret"
echo "Exiting...."
echo "-------------"
exit 1

fi

#Check for BOT_NAME or HUBOT_NAME.
if [[ -z "${BOT_NAME}" ]]; then
if [[ -z "${HUBOT_NAME}" ]]; then
echo "-------------"
echo "ERROR: The BOT_NAME Environment Variable has not been set. Set this to the name your bot will respond too."
echo "Exiting...."
echo "-------------"
exit 1
fi
else
export HUBOT_NAME=${BOT_NAME}
fi

#Install any required deps.
cd /home/hubot/

if [[ -z "${NPM_REGISTRY}" ]]; then
echo "INFO: The NPM_REGISTRY environment variable has not been set. Using npmjs as the default."
else
echo "INFO: The NPM_REGISTRY environment variable is $NPM_REGISTRY. NPM will use this registry to pull packages from."
npm set registry $NPM_REGISTRY
fi

#This happens here as well as during the container build process. There seems to be a bug where sometimes hubot misses 1 or 2 deps. This is insurance for that. Some people also mount node_modules externally and this will ensure that the base deps are there in those cases.
echo "INFO: Attempting to install this containers dependancies"
npm install --no-audit

#Check for BOT_ALIAS or HUBOT_ALIAS.
if [[ -z "${BOT_ALIAS}" ]]; then
if [[ -z "${HUBOT_ALIAS}" ]]; then
echo "-------------"
echo "WARN: BOT_ALIAS has not been set. This is used to call your bot by a 'short name'."
echo "WARN: For example if the following are set: BOT_NAME=rocketbot and BOT_ALIAS='!' - Your bot will respond to both '@rocketbot help' and '!help'"
echo "-------------"
fi
else
export HUBOT_ALIAS=${BOT_ALIAS}
fi

if [[ "${ROCKETCHAT_BOT_DIAGNOSTICS}" == 'true' ]]; then
echo "INFO: ROCKETCHAT_BOT_DIAGNOSTICS is enabled. A set of diagnostics/test scripts will be loaded."
if [[ -z "${EXTERNAL_SCRIPTS}" ]]; then
#If no EXTERNAL_SCRIPTS are set, but this is set to true, only load the diagnostics package
EXTERNAL_SCRIPTS=hubot-rocketchat-diagnostics
else
#If we have specified some EXTERNAL_SCRIPTS then append the diagnostics package to the end of them
EXTERNAL_SCRIPTS+=,hubot-rocketchat-diagnostics
fi
fi

if [[ -z "${EXTERNAL_SCRIPTS}" ]]; then
echo "-------------"
echo "WARN: The EXTERNAL_SCRIPTS environment variable has not been set."
echo "WARN: This means no additional hubot scripts will be loaded except for whatever is bundled by default with this container"
echo "WARN: This should be set with a array of hubot NPM script repos like the example below."
echo "Example: EXTERNAL_SCRIPTS=hubot-help,hubot-security,hubot-auth"
echo "-------------"
else
echo "INFO: Installing hubot scripts we passed in the EXTERNAL_SCRIPTS environment variable. These will be installed from NPM."
npm i -S $(node -e "console.log('$EXTERNAL_SCRIPTS'.split(',').join(' '))") --no-audit
node -e "console.log(JSON.stringify('$EXTERNAL_SCRIPTS'.split(',')))" >/home/hubot/external-scripts.json
fi

if [[ -z "${ROCKETCHAT_AUTH}" ]]; then
echo "-------------"
echo "WARN: The ROCKETCHAT_AUTH environment variable has not been set."
echo "WARN: If the bot is using a LDAP account to log into your Rocket.Chat instance and are seeing issues, try setting this to ROCKETCHAT_AUTH=ldap"
echo "Default: ROCKETCHAT_AUTH=password"
echo "-------------"
export ROCKETCHAT_AUTH=password
fi

if [[ -z "${RESPOND_TO_DM}" ]]; then
echo "-------------"
echo "INFO: The RESPOND_TO_DM environment variable has not been set. This bot will not respond to Direct/Private messages"
echo "INFO: Set RESPOND_TO_DM=true if you want your bot to respond to direct/private messages."
echo "Default: RESPOND_TO_DM=false"
echo "-------------"
fi

if [[ -z "${RESPOND_TO_EDITED}" ]]; then
echo "-------------"
echo "INFO: The RESPOND_TO_EDITED environment varialbe is not set."
echo "INFO: Set RESPOND_TO_EDITED=true if you want this bot to respond to messages after they have been edited"
echo "Default: RESPOND_TO_EDITED=false"
echo "-------------"
fi

if [[ -z "${RESPOND_TO_LIVECHAT}" ]]; then
echo "-------------"
echo "INFO: The RESPOND_TO_LIVECHAT environment varialbe is not set. The default is RESPOND_TO_LIVECHAT=false"
echo "INFO: This means the bot will not respond in a LiveChat conversation"
echo "Default: RESPOND_TO_LIVECHAT=false"
echo "-------------"
fi
set -e

export PATH="node_modules/.bin:node_modules/hubot/node_modules/.bin:$PATH"

#Hack to add coffeescript requirement as the fix was never merged in the official Hubot repo
sed -i '2irequire("coffee-script")' /home/hubot/node_modules/hubot/src/robot.js

#Start Hubot using the asyncRun function
asyncRun node node_modules/hubot/bin/hubot.js -a rocketchat "$@"
20 changes: 20 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: "2"
services:
rocketbot:
image: rocketchat-hubot-2.0
environment:
- ROCKETCHAT_URL=https://open.rocket.chat
- ROCKETCHAT_USER=rocketbot
- ROCKETCHAT_PASSWORD=supersecret
- BOT_NAME=rocketbot
- ROCKETCHAT_ROOM=
- BOT_ALIAS=!
- LISTEN_ON_ALL_PUBLIC=false
- HUBOT_LOG_LEVEL=debug
- RESPOND_TO_DM=true
- RESPOND_TO_EDITED=false
- RESPOND_TO_LIVECHAT=false
- INTEGRATION_ID=rocketbot
- EXTERNAL_SCRIPTS=hubot-help,hubot-rocketchat-diagnostics
volumes:
- ./scripts:/home/hubot/scripts
Loading