diff --git a/hastebin-server/centos7/Dockerfile b/hastebin-server/centos7/Dockerfile new file mode 100644 index 00000000..6e12a200 --- /dev/null +++ b/hastebin-server/centos7/Dockerfile @@ -0,0 +1,23 @@ +FROM registry.centos.org/centos/centos:7 + +MAINTAINER Mohammed Zeeshan Ahmed + +# RUN yum -y update && yum clean all + +ENV PATH=/opt/nodejs/bin:$PATH + +RUN mkdir -p /opt/scripts /opt/hastebin-server /opt/data /opt/nodejs + +ADD install.sh run.sh config.js.template fix-permissions.sh /opt/scripts/ + +RUN chmod -R 777 /opt/scripts && . /opt/scripts/install.sh + +WORKDIR /opt/hastebin-server + +EXPOSE 7777 + +ENTRYPOINT ["/opt/scripts/run.sh"] + +CMD ["hastebin"] + +USER 1001 diff --git a/hastebin-server/centos7/README.md b/hastebin-server/centos7/README.md new file mode 100644 index 00000000..315cdde0 --- /dev/null +++ b/hastebin-server/centos7/README.md @@ -0,0 +1,50 @@ + + +# Hastebin Container + +## Customizations + +### Environment Variables + + 1. KEY_LENGTH: The number which indicates the length of the keys to generate for identifying entries. Default is 10 + 2. MAX_LENGTH: The maximum length of the key. Default is 400000. + 3. STATIC_MAX_AGE: The maximum time of inactivity before a paste is cleaned up. Default is 86400. + 4. KEY_TYPE: The type of the keys to generate. Default is 'phonetic' which acts like pwgen or it can be 'random'. + 5. STORAGE_TYPE: The type of storage to be used for storing the pastes. The default type is 'file' which will be stored + in directory within the container itself. It can also be 'redis', 'postgres' or memcached. + 6. STORAGE_HOST: The reachable ip or host name of where the storage is running. This can be the redis server or the + memcached server. + 7. STORAGE_PORT: The port at which the storage service is exposed. Again, applicable to redis and memcached. Default + value is 6379. + 8. STORAGE_DB: The storage DB. Required for redis with the default value is 2. + 9. STORAGE_FILE_PATH: Required if STORAGE_TYPE is 'file'. This is the path of directory where pastes will be stored. + Default value is /opt/data. This is where you will mount storage into the container as well. + 10. STORAGE_EXPIRE: The maximum time of inactivity before a paste is removed from the server in seconds. Default value + is 2592000. + 11. DATABASE_URL: Required in case of postgresql database. The format of the same will be `postgres://user:password@hos + t:5432/database`. + +### Storage Mount + +In case you are using the default 'file' storage type, you will need to mount a volume, with appropriate permissions +into the container. Unless you are specifying your own mount point this can be done at /opt/data. + +## Usage + +### Building the container + +You can build the container with + + $ docker build -t YOUR_NAME -f Dockerfile . + +### Running the container + +You can run the container with + + $ docker run -d YOUR_NAME + +**NOTE**: If your are using postgres as the storage, then make sure you manually create a necessary table in the same in the database by running the following: + + create table entries (id serial primary key, key varchar(255) not null, value text not null, expiration int, unique(key)); + +You can find out more about hastebin server [here](https://github.com/seejohnrun/haste-server/blob/master/README.md "Hastebin readme"). \ No newline at end of file diff --git a/hastebin-server/centos7/cccp.yml b/hastebin-server/centos7/cccp.yml new file mode 100644 index 00000000..1d2bf9f1 --- /dev/null +++ b/hastebin-server/centos7/cccp.yml @@ -0,0 +1 @@ +job-id: hastebin \ No newline at end of file diff --git a/hastebin-server/centos7/config.js.template b/hastebin-server/centos7/config.js.template new file mode 100644 index 00000000..43f0add1 --- /dev/null +++ b/hastebin-server/centos7/config.js.template @@ -0,0 +1,49 @@ +{ + + "host": "0.0.0.0", + "port": 7777, + + "keyLength": ${KEY_LENGTH}, + + "maxLength": ${MAX_LENGTH}, + + "staticMaxAge": ${STATIC_MAX_AGE}, + + "recompressStaticAssets": true, + + "logging": [ + { + "level": "verbose", + "type": "Console", + "colorize": true + } + ], + + "keyGenerator": { + "type": "${KEY_TYPE}" + }, + + "rateLimits": { + "categories": { + "normal": { + "totalRequests": 500, + "every": 60000 + } + } + }, + + "storage": { + "type": "${STORAGE_TYPE}", + "host": "${STORAGE_HOST}", + "port": ${STORAGE_PORT}, + "db": ${STORAGE_DB}, + "path": "${STORAGE_FILE_PATH}", + "expire": ${STORAGE_EXPIRE}, + "connectionUrl": "${DATABASE_URL}" + }, + + "documents": { + "about": "./about.md" + } + +} diff --git a/hastebin-server/centos7/fix-permissions.sh b/hastebin-server/centos7/fix-permissions.sh new file mode 100755 index 00000000..ea55d486 --- /dev/null +++ b/hastebin-server/centos7/fix-permissions.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +# Fix permissions on the given directory to allow group read/write of +# regular files and execute of directories. +set -eux +find "$1" -exec chown ${2} {} \; +find "$1" -exec chgrp 0 {} \; +find "$1" -exec chmod g+rw {} \; +find "$1" -type d -exec chmod g+x {} + diff --git a/hastebin-server/centos7/install.sh b/hastebin-server/centos7/install.sh new file mode 100755 index 00000000..336e1dd5 --- /dev/null +++ b/hastebin-server/centos7/install.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +set -eux + +# Setup Envs + +BASE_PACKAGES="epel-release centos-release-scl" +CORE_PACKAGES="gettext" +TMP_PACKAGES="git wget" + +NODE_VERSION=${NODE_VERSION:="v6.9.4"} +ARCH=${ARCH:="x64"} +NODE_NAME="node-${NODE_VERSION}-linux-${ARCH}" +NODE_TAR="${NODE_NAME}.tar.xz" +NODE_DIR="/opt/nodejs" + +NODE_DOWNLOAD="https://nodejs.org/dist/${NODE_VERSION}/${NODE_TAR}" + +HASTE_REPO="https://github.com/seejohnrun/haste-server.git" +HASTE_DIR="/opt/hastebin-server" +HASTE_DATA_DIR="/opt/data" + +# Install begins +# * Setup basics +yum -y install ${BASE_PACKAGES} && yum -y install --skip-broken ${CORE_PACKAGES} ${TMP_PACKAGES}; + +# ** Install full nodejs + +wget ${NODE_DOWNLOAD} && tar -xf ${NODE_TAR} && mv ${NODE_NAME}/* ${NODE_DIR} \ + && rm -rf ${NODE_NAME}*; +useradd -u 1001 -g 0 -d ${HASTE_DIR} nodejsuser; + +# * Setup hastebin server +git clone https://github.com/seejohnrun/haste-server.git _tmp && cp -avrf _tmp/* ${HASTE_DIR} && rm -rf _tmp; + +pushd ${HASTE_DIR} && npm install && popd; + +for item in ${HASTE_DIR} ${HASTE_DATA_DIR} ${NODE_DIR}; do + . /opt/scripts/fix-permissions.sh ${item} nodejsuser; +done + +# Cleanup +yum -y remove ${TMP_PACKAGES} && yum clean all diff --git a/hastebin-server/centos7/run.sh b/hastebin-server/centos7/run.sh new file mode 100755 index 00000000..3f6059a4 --- /dev/null +++ b/hastebin-server/centos7/run.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +HASTE_CONFIG_TEMPLATE="/opt/scripts/config.js.template" +HASTE_CONFIG="/opt/hastebin-server/config.js" + +CONFIG_HASTEBIN(){ + + export KEY_LENGTH=${KEY_LENGTH:-10}; + export MAX_LENGTH=${MAX_LENGTH:-400000}; + export STATIC_MAX_AGE=${STATIC_MAX_AGE:-86400} + export KEY_TYPE=${KEY_TYPE:-phonetic}; + export DATABASE_URL=${DATABASE_URL:-""} + export STORAGE_TYPE=${STORAGE_TYPE:-"file"} + export STORAGE_FILE_PATH=${STORAGE_FILE_PATH:-"/opt/data"} + export STORAGE_HOST=${STORAGE_HOST:="0.0.0.0"} + export STORAGE_PORT=${STORAGE_PORT:-6379} + export STORAGE_DB=${STORAGE_DB:-2} + export STORAGE_EXPIRE=${STORAGE_EXPIRE:-2592000} + + envsubst < ${HASTE_CONFIG_TEMPLATE} > ${HASTE_CONFIG} + +} + + +if [ $1 == "hastebin" ]; then + CONFIG_HASTEBIN; + exec npm start; +else + exec $@ +fi \ No newline at end of file