[
  {
    "path": "README.md",
    "content": "![Alt text](/img/logo.png?raw=true \"Beluga Docker Deployment Tool\")\n\n\n# **Beluga**\n### **Intro**\nWe've decided to create Beluga in order to fix a very common problem that is the complexity of multi-tenants Docker installations. Indeed, Beluga enables you to quickly draft deployment scripts that will provide you with all the flexibility needed to quickly launch new projects using Docker.\n\nWith Beluga, pre-setups are a thing of the past, the kickoff phase becomes extremely short which is an enormous gain if you need to work with multiple clients/projects.\n\nBeluga has no run-time requirements for Linux operating machines and has pretty much no specific requirements outside of actually having Docker installed. Beluga also support private repositories without the need to use Docker Hub.\n\n### **Why Don't You Use Kubernetes or Mesos?**\n\n- Currently Docker doesn't support Multi-Tenant environments\n- Therefore, neither Kubernetes and Mesos do from the docker/container layer...\n- Many actual issues :\n- ```#2918 (PR: #4572) (container root is identical to host root -- volumes can be written and read from as host root inside container).```\n- (WIP) Docker doesn't have any ACL. Writing to docker.sock == root.\n-    ```#5619 (PR: #6000) (absolute symlinks and symlink path components copy host target).```\n\n### **What Beluga Doesn't Do?**\n- Down-to-distro cluster managements\n- Docker registry or app management itself\n- Magic service discovery\n- Infrastructure management\n\n### **How Beluga Works?**\n  - Run dockerfiles to build them\n  - Push them to repository\n  - Pull them when connected to server by ssh\n  - That's it!\n\n### **Requirements**\n  - A Unix compatible system with RSync\n  - SSH\n  - Docker Compose\n  - Obviously, Docker...\n  - Some love\n\n# **Install for OS X Systems using Homebrew**\n\n    brew tap cortexmedia/beluga\n\n### Stable version\n\n     brew install beluga\n### Latest version\n\n     brew install --head beluga\n\n#### **How is Beluga Structured**\n  - *scripts/* contains all the functions used to build docker containers and deploy them\n  - *bin/beluga* Contains the CLI to call the various scripts.\n  - *sample/* Example of BelugaFile.\n  - *lib/* Common functions used by the various deployment scripts.\n\n#### **Usage**\n\n    beluga [--build args] [--deploy args] [options]\n    -b Build the docker container and push to repository.\n    -p Connects via ssh to remote host and pulls the images.\n    -d Runs the build push and pull options.\n    -c Connects via ssh and removed all unused tags and containers.\n**Options:**\n  ```  -f path/to/BelugaFile Specify the BelugaFile to use.```\n\n\n##### **Configuration File**\n\nConfiguration related to deployment is stored in BelugaFile.\n\nThe structure used to grab all the infos related to a container is\n\n    LocalImageName;DockerFilePath;DockerImageName\n\nThey are stored in a array like shown in the following example:\n\n    IMAGES_TO_BUILD[0]=\"mrheaume/sample_project_web;.;sample_project_web\"\n    IMAGES_TO_BUILD[1]=\"mrheaume/sample_project_db;DockerPostgres/;sample_project_db\"\n    IMAGES_TO_BUILD[2]=\"mrheaume/sample_project_nginx;DockerNginx/;sample_project_nginx”\n\n### **Sample Docker Project with Docker Compose**\n\n    # docker-compose.yml\n    web:\n        image: my_repository:8080/my-awesome-app\n    ports:\n        - \"5000:5000\"\n    links:\n        - redis\n        - nginx\n    redis:\n        image: my_repository:8080/redis\n    nginx:\n        image: nginx:latest\n\n### **Contributing**\n\n  The project is currently verified with Shellcheck for bash compatibility (http://www.shellcheck.net/).\n  Feel free to ask for a Pull Request if you have awesome ideas for improvements or fixes to bugs.\n\n### **Main Contributors**\n\n  Mathieu Rhéaume <mrheaume@cortexstudio.com>\n\n  **Copyright (c) 2017 [Cortex](http://cortexstudio.com/) ([Mobile Development Studio](http://cortexstudio.com/))**\n\n  Licensed under the Apache License, Version 2.0 (the \"License\");\n  you may not use this file except in compliance with the License.\n  You may obtain a copy of the License at\n\n      http://www.apache.org/licenses/LICENSE-2.0\n\n  Unless required by applicable law or agreed to in writing, software\n  distributed under the License is distributed on an \"AS IS\" BASIS,\n  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n  See the License for the specific language governing permissions and\n  limitations under the License.\n"
  },
  {
    "path": "bin/beluga",
    "content": "#!/bin/bash\n#######################################################################\n# Copyright    2015 Cortex Media\n# Author    Mathieu Rhéaume <mrheaume@cortex.bz>\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n#######################################################################\n\nset -e\n\nset_base_directory() {\n  BASE_DIR=\"$(dirname \"$(readlink \"$0\")\")\"\n  DIR=\"$(cd \"$(dirname \"$0\")\" && pwd)\"\n  FULL_PATH=$(echo \"$DIR/$BASE_DIR\")\n  BASE_DIR=\"$(cd $FULL_PATH && pwd)\"\n  export BASE_DIR\n}\n\nshow_help() {\n  echo \"Copyright (c) 2015 Cortex (cortex.bz)\"\n  echo \"Beluga (0.0.1-alpha). Usage :\"\n  echo \"beluga [--build args] [--deploy args] [options]\"\n  echo \"-b Build the docker container and push to repository.\"\n  echo \"-p Connects via ssh to remote host and pulls the images.\"\n  echo \"-d Runs the build push and pull options.\"\n  echo \"-c Connects via ssh and removed all unused tags and containers.\"\n  echo \"Options:\"\n  echo \"-f path/to/BelugaFile Specify the BelugaFile to use.\"\n}\n\n####################\n# MAIN APP RUNTIME #\n####################\nset_base_directory\nfor ((i=1;i<=$#;i++))\ndo\n  case ${!i} in\n    -f)\n      ((i++)) \n      BELUGA_FILE=${!i}\n      export BELUGA_FILE\n      break\n      ;;\n  esac\ndone\n\nfor i in \"$@\"\ndo\n  case $i in\n    -b|-b=*|--build|--build=*)\n      \"$BASE_DIR/../scripts/BuildAndPush.sh\"\n      exit 0\n      shift\n      ;;\n    -p=*|-p|--pull|--pull=*)\n      \"$BASE_DIR/../scripts/Pull.sh\"\n      exit 0\n      shift\n      ;;\n    -c|-c=*|--clean|--clean=*)\n      \"$BASE_DIR/../scripts/Clean.sh\"\n      exit 0\n      shift\n      ;;\n    -d=*|-d|--deploy|--deploy=*)\n      \"$BASE_DIR/../scripts/BuildAndPush.sh\"\n      if [ $? -ne 0 ]; then\n        echo \"The build failed.\"\n        exit 1\n      else\n        \"$BASE_DIR/../scripts/Pull.sh\"\n      fi\n      exit 0\n      shift\n      ;;\n    *)\n      show_help\n      exit 0\n      ;;\n  esac\ndone\n\nshow_help\n"
  },
  {
    "path": "lib/BelugaDeployLib.sh",
    "content": "#!/bin/bash\n#######################################################################\n# Copyright    2015 Cortex Media\n# Author    Mathieu Rhéaume <mrheaume@cortex.bz>\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n#######################################################################\n# File : BelugaDeployLib.sh\n# Description : Beluga Docker Deployment lib.\n# A scavenge of bash functions used to deploy docker containers.\n#######################################################################\n# This is a list of index references to fit with IMAGES_TO_BUILD structure\n# So we don't specify numbers everywhere\n#######################################################################\nELOCALIMAGE=0\nEDOCKERFILE=1\nEDOCKERIMAGENAME=2\n\n################################################\n# Function: remove_untagged_containers()       #\n# Description : Remove the untagged containers #\n################################################\nremove_untagged_containers() {\n  # Print containers using untagged images: $1 is used with awk's print: 0=line, 1=column 1.\n  # NOTE: \"[0-9a-f]{12}\" does not work with GNU Awk 3.1.7 (RHEL6).\n  run_ssh_command \"docker ps -a | tail -n +2 | awk '\\$2 ~ \\\"^[0-9a-f]+$\\\" {print \\$1}' | xargs --no-run-if-empty docker rm\"\n}\n\n################################################\n# Function: remove_untagged_images()           #\n# Description : Remove the untagged images     #\n################################################\nremove_untagged_images() {\n  # Print untagged images: $1 is used with awk's print: 0=line, 3=column 3.\n  # NOTE: intermediate images (via -a) seem to only cause\n  # \"Error: Conflict, foobarid wasn't deleted\" messages.\n  # Might be useful sometimes when Docker messed things up?!\n  run_ssh_command \"docker images | tail -n +2 | awk '\\$1 == \\\"<none>\\\" {print \\$3}' | xargs --no-run-if-empty docker rmi\"\n}\n\n################################################\n# Function: stop_and_build_containers()        #\n# Description : This stops the running         #\n# containers and tries to build them if needed #\n################################################\nstop_and_build_containers() {\n  run_ssh_command \"cd $APP_DIRECTORY; docker-compose -f $DOCKER_COMPOSE_FILE stop; docker-compose -f $DOCKER_COMPOSE_FILE build \"\n}\n\n################################################\n# Function: start_containers_in_background()   #\n# Description : This starts the containers in  #\n# background.                                  #\n################################################\nstart_containers_in_background() {\n  run_ssh_command \"cd $APP_DIRECTORY; docker-compose -f $DOCKER_COMPOSE_FILE up -d \"\n}\n\n################################################\n# Function: rotate_containers()                #\n# Description : This stops and start the       #\n# containers and tries to build them if needed #\n################################################\nrotate_containers() {\n  echo \"Rotating docker-compose containers\"\n  stop_and_build_containers\n  start_containers_in_background\n}\n\n#############################################\n# Function: info_output()                   #\n# Description : Output a info text in green #\n# background.                               #\n#############################################\n# Arg 1 : Text to output as info            #\n#############################################\ninfo_output() {\n  if [ -z \"$1\" ]\n  then\n    echo \"YOU NEED TO PASS ME A TEXT\"\n  else\n    echo \"$(tput setaf 2)INFO: $1 $(tput sgr 0)\"\n  fi\n}\n\n#############################################\n# Function: run_ssh_command()               #\n# Description : Run a command over ssh with #\n# Configuration settings                    #\n#############################################\n# Arg 1 : Command to run                    #\n#############################################\nrun_ssh_command() {\n  if [ -z \"$1\" ]\n  then\n    echo \"YOU NEED TO SPECIFY A SSH COMMAND\"\n    exit 1\n  else\n      for i in \"${SERVER_IP[@]}\"\n      do\n        info_output \"SSH $i - $1\"\n        ssh \"$DOCKER_USER@$i\" $1\n      done\n  fi\n}\n\n##############################################\n# Function: pull_docker_image()              #\n# Description : Pull a docker container over #\n#               ssh on the target machine    #\n##############################################\n# Arg 1 : Container name                     #\n##############################################\npull_docker_image() {\n  if [ -z \"$1\" ]\n  then\n    echo \"YOU NEED TO SPECIFY A CONTAINER NAME\"\n    exit 1\n  else\n    run_ssh_command \"docker pull $1:latest\"\n  fi\n}\n\n#############################################\n# Function: sync_app_files()                #\n# Description : Sync files between host and #\n#               target machine              #\n#############################################\nsync_app_files() {\n      for i in \"${SERVER_IP[@]}\"\n      do\n        info_output \"RSYNC $i\"\n        rsync -r . \"$SERVER_USER@$i:$APP_DIRECTORY\"\n      done\n}\n\n##################################################################\n# Function: clean_untagged_images()                              #\n# Description : Removes all the old untagged docker containers.  #\n#               This will skip over the running containers...    #\n##################################################################\n# ARG 1 : run over ssh on remote hosts                           #\n##################################################################\nclean_untagged_images() {\n  if [ -z \"$1\" ]\n  then\n    run_ssh_command \"docker rmi $(docker images | grep '<none>' | awk '{print($3)}')\"\n  else\n    docker rmi \"$(docker images | grep '<none>' | awk '{print($3)}')\"\n  fi\n}\n\n#################################################\n# Function name: build_docker_image\n# Description : This function runs a docker build\n# on specified docker container name + docker container path\n#################################################\n# Arg 1 : Container name\n# Arg 2 : Recipe folder\n#################################################\nbuild_docker_image() {\n  if [ -z \"$1\" ] || [ -z \"$2\" ]\n  then\n    echo \"You need to specify a container name and receipe folder\"\n    exit 1\n  else\n    info_output \"Docker build $1 $2\"\n    docker build -t \"$1\" \"$2\"\n    if [ $? -ne 0 ]; then\n      echo \"Docker build failed. Check your stuff mang or ask Chuck Norris\"\n      exit 1\n    else\n      echo \"Just build $1 with $2\"\n    fi\n  fi\n}\n\n#################################################\n# Function name: tag_docker_image\n# Description : This function runs a docker tags\n# a container name with tag name\n#################################################\n# Arg 1 : Docker container name\n# Arg 2 : Docker tag name\n#################################################\ntag_docker_image() {\n  if [ -z \"$1\" ] || [ -z \"$2\" ]\n  then\n    echo \"You need to specify a container name and tag name\"\n    exit 1\n  else\n    info_output \"Docker tag $1 $2\"\n    docker tag -f \"$1\" \"$2\"\n    if [ $? -ne 0 ]; then\n      echo \"Docker tag failed. Check your stuff mang or ask Chuck Norris.\"\n      exit 1\n    else\n      echo \"Just tagged $1 with $2\"\n    fi\n  fi\n}\n\n#################################################\n# Function name: push_docker_image\n# Description : This function pushes the docker images\n#################################################\n# Arg 1 : Docker repository URL\n# Arg 2 : Docker container name\n#################################################\npush_docker_image() {\n  if [ -z \"$1\" ] || [ -z \"$2\" ]\n  then\n    echo \"You need to specify a container name and repository\"\n    exit 1\n  else\n    info_output \"Docker push $1 $2\"\n    docker push \"$1/$2\"\n    if [ $? -ne 0 ]; then\n      echo \"Docker push failed. Check your stuff mang or ask Chuck Norris.\"\n      exit 1\n    else\n      echo \"Just pushed $1 to $2\"\n    fi\n\n  fi\n}\n\n#################################################\n# Function name: get_config_parameter\n# Description : Extracts the selected parameter from string\n# Mostly useful for parsing CSV-like strings\n#################################################\n# Arg 1 : CSV Formatted string\n# Arg 2 : Index of item you want\n#################################################\nget_config_parameter() {\n  if [ -z \"$1\" ] || [ -z \"$2\" ]\n  then\n    echo \"You need to specify a configuration index and item index\"\n    exit 1\n  else\n    imageConfig=$1\n    IFS=$ARRAY_DELIMITER read -a parsedconfig <<< \"$imageConfig\"\n    echo \"${parsedconfig[$2]}\"\n  fi\n}\n\n#################################################\n# Function name: build_images\n# Description : Build the docker images\n#################################################\nbuild_images() {\n  echo \"Building images\"\n  for i in \"${IMAGES_TO_BUILD[@]}\"\n  do\n    imageToBuild=$(get_config_parameter \"$i\" $ELOCALIMAGE)\n    dockerFile=$(get_config_parameter \"$i\" $EDOCKERFILE)\n    build_docker_image \"$imageToBuild\" \"$dockerFile\"\n  done\n}\n\n#################################################\n# Function name: tag_images\n# Description : Tags the docker images\n#################################################\ntag_images() {\n  echo \"Tagging images\"\n  for i in \"${IMAGES_TO_BUILD[@]}\"\n  do\n    imageToBuild=$(get_config_parameter \"$i\" $ELOCALIMAGE)\n    dockerFile=$(get_config_parameter \"$i\" $EDOCKERIMAGENAME)\n    dockerTag=$REPOSITORY_URL\"/\"$dockerFile\n    tag_docker_image \"$imageToBuild\" \"$dockerTag\"\n  done\n}\n\n#################################################\n# Function name: push_images\n# Description : Pushes the docker images to repository\n#################################################\npush_images() {\n  echo \"Pushing images\"\n  for i in \"${IMAGES_TO_BUILD[@]}\"\n  do\n    dockerFile=$(get_config_parameter \"$i\" $EDOCKERIMAGENAME)\n    push_docker_image \"$REPOSITORY_URL\" \"$dockerFile\"\n  done\n}\n\n#################################################\n# Function name: pull_images\n# Description : Pulls the docker images to the target server from private repository\n#################################################\npull_images() {\n  echo \"Pulling images\"\n  for i in \"${IMAGES_TO_BUILD[@]}\"\n  do\n    dockerFile=$(get_config_parameter \"$i\" $EDOCKERIMAGENAME)\n    dockerTag=$REPOSITORY_URL\"/\"$dockerFile\n    pull_docker_image \"$dockerTag\"\n  done\n}\n\n"
  },
  {
    "path": "samples/BelugaFile",
    "content": "#!/bin/bash\n#######################################################################\n# Copyright    2015 Cortex Media\n# Author    Mathieu Rhéaume <mrheaume@cortex.bz>\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n#######################################################################\n# This config files contains the list of containers that\n# BuildDockerImagesAndPushToRepos and PullFromRepository will use\n# It keeps references between the docker repos and your own app\n# configuration.\n#######################################################################\n# Docker specific settings.\n#######################################################################\nREPOSITORY_URL=\"mydocker.repository.ca:8080\"\n\n#######################################################################\n# Server specific settings.\n#######################################################################\nAPP_DIRECTORY=\"/home/ubuntu/sample_app_directory\"\nDOCKER_COMPOSE_FILE=\"docker-compose-production.yml\"\n# User to RSync with\nSERVER_USER=\"ubuntu\"\n# User that runs docker-compose & containers\nDOCKER_USER=\"ubuntu\"\n\n#######################################################################\n# List of servers to deploy\n#######################################################################\n# Single server deployment (OLD Version)\n# SERVER_IP=\"99.999.99.99\"\n#######################################################################\n# Multiple server deployment (New Format)\n#######################################################################\ndeclare -a SERVER_IP\nSERVER_IP[0]=\"99.999.99.99\"\nSERVER_IP[1]=\"22.222.22.22\"\n\n#######################################################################\n# Docker specific settings.\n# Structure of config line is\n# \"LocalImageName;DockerFilePath;DockerImageName\"\n#######################################################################\nARRAY_DELIMITER=\";\"\ndeclare -a IMAGES_TO_BUILD\nIMAGES_TO_BUILD[0]=\"mrheaume/sample_project_web;.;sample_project_web\"\nIMAGES_TO_BUILD[1]=\"mrheaume/sample_project_db;DockerPostgres/;sample_project_db\"\nIMAGES_TO_BUILD[2]=\"mrheaume/sample_project_nginx;DockerNginx/;sample_project_nginx\"\n\n"
  },
  {
    "path": "scripts/BuildAndPush.sh",
    "content": "#!/bin/bash\n#######################################################################\n# Copyright    2015 Cortex Media\n# Author    Mathieu Rhéaume <mrheaume@cortex.bz>\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n#######################################################################\n# File import from config file\n#########################################################################\nset -e\n\nif [ -z \"$BELUGA_FILE\" ]; then\n  . \"./BelugaFile\"\nelse\n  . \"$BELUGA_FILE\"\nfi\n. \"$BASE_DIR/../lib/BelugaDeployLib.sh\"\n\n####################\n# MAIN APP RUNTIME\n####################\nbuild_images\ntag_images\npush_images\n\n"
  },
  {
    "path": "scripts/Clean.sh",
    "content": "#!/bin/bash\n#######################################################################\n# Copyright    2015 Cortex Media\n# Author    Mathieu Rhéaume <mrheaume@cortex.bz>\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n#########################################################################\n# File import from config file\n#########################################################################\nset -e\n\nif [ -z \"$BELUGA_FILE\" ]; then\n  . \"./BelugaFile\"\nelse\n  . \"$BELUGA_FILE\"\nfi\n. \"$BASE_DIR/../lib/BelugaDeployLib.sh\"\n\n####################\n# MAIN APP RUNTIME #\n####################\nremove_untagged_containers\nremove_untagged_images\n"
  },
  {
    "path": "scripts/Pull.sh",
    "content": "#!/bin/bash\n#######################################################################\n# Copyright    2015 Cortex Media\n# Author    Mathieu Rhéaume <mrheaume@cortex.bz>\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n###############################################################################\n# File import from config file                                                #\n###############################################################################\nset -e\n\nif [ -z \"$BELUGA_FILE\" ]; then\n  . \"./BelugaFile\"\nelse\n  . \"$BELUGA_FILE\"\nfi\n. \"$BASE_DIR/../lib/BelugaDeployLib.sh\"\n\n####################\n# MAIN APP RUNTIME\n####################\nsync_app_files\npull_images\nrotate_containers\n\necho \"Deployment done\"\n"
  }
]