From b8ac4b0bbb64d7f67cd18e5c7ce94b53d0430b30 Mon Sep 17 00:00:00 2001 From: Romain Soufflet Date: Mon, 31 Oct 2016 15:32:30 +0100 Subject: [PATCH] Add automatic deployment scripts (#362) * Add ansible scripts to deploy Nanocloud * Add a script and a new docker-compose file to deploy several PR * Fix directory detection so users can run this script from anywhere * Restore branch "master" after deploying a PR * Change guacamole.properties to call right backend * Create a new frontend volum for each PullRequest. * Add configuration file --- deployments/README.md | 70 ++++++++++++++++++ deployments/ansible.cfg | 4 ++ deployments/ansible_hosts | 2 + deployments/deploy-pr.sh | 66 +++++++++++++++++ deployments/group_vars/all | 3 + deployments/nanocloud.yml | 14 ++++ deployments/requirements.yml | 2 + deployments/roles/common/tasks/main.yml | 2 + .../nanocloud/files/nanocloud/config.env | 0 deployments/roles/nanocloud/tasks/main.yml | 43 +++++++++++ docker-compose-pr.yml | 71 +++++++++++++++++++ 11 files changed, 277 insertions(+) create mode 100644 deployments/README.md create mode 100644 deployments/ansible.cfg create mode 100644 deployments/ansible_hosts create mode 100755 deployments/deploy-pr.sh create mode 100644 deployments/group_vars/all create mode 100644 deployments/nanocloud.yml create mode 100644 deployments/requirements.yml create mode 100644 deployments/roles/common/tasks/main.yml create mode 100644 deployments/roles/nanocloud/files/nanocloud/config.env create mode 100644 deployments/roles/nanocloud/tasks/main.yml create mode 100644 docker-compose-pr.yml diff --git a/deployments/README.md b/deployments/README.md new file mode 100644 index 00000000..abad52f6 --- /dev/null +++ b/deployments/README.md @@ -0,0 +1,70 @@ +# Nanocloud deployments + +This directory is a collection of tools to deploy Nanocloud. + +## Install with ansible + +### Dependencies + +You need ansible 1.9 or above to use this installation + +Install dependencies + +``` +ansible-galaxy install franklinkim.docker +ansible-galaxy install franklinkim.docker-compose +``` + +> Note: You may need to have root privilege to install those dependencies. You +> won't need it to run deployment. + +### Configure + +Configure where *ansible* will deploy your installation by modifying the +*ansible_hosts* file. + +``` +[nanocloud] +api ansible_host=127.0.0.1 ansible_user=user +``` + +In this file, you can specify several servers and give name to them to deploy +multiple instances of Nanocloud: + +``` +[nanocloud] +api-instance1 ansible_host=10.0.0.2 ansible_user=user1 +api-customerX ansible_host=10.0.0.3 ansible_user=user2 +``` + +Use the file *deployments/roles/nanocloud/files/nanocloud/config.env* to +override configuration variable like you should normally do with the *config.env* +file + +### Run + +Run Playbook + +``` +ansible-playbook nanocloud.yml +``` + +## Run multiple instance on a same host + +The script *deploy-pr.sh* is here to help you deploy a specific Github Pull Request +on any environment in parallel. + +You can deploy several PR on a single machine. Each deployment will have its +own *database*, *frontend*, *backend* and *guacamole-client*. + +To use it, run the following command: + +``` +./deployments/deploy-pr.sh ${PR_NUMBER} +``` + +And if the remote is not called *origin* you can specify another name: + +``` +./deployments/deploy-pr.sh 175 base +``` diff --git a/deployments/ansible.cfg b/deployments/ansible.cfg new file mode 100644 index 00000000..46233e73 --- /dev/null +++ b/deployments/ansible.cfg @@ -0,0 +1,4 @@ +[defaults] +hostfile = ansible_hosts +remote_user = user +host_key_checking = False diff --git a/deployments/ansible_hosts b/deployments/ansible_hosts new file mode 100644 index 00000000..01404056 --- /dev/null +++ b/deployments/ansible_hosts @@ -0,0 +1,2 @@ +[nanocloud] +api ansible_host=127.0.0.1 ansible_user=user diff --git a/deployments/deploy-pr.sh b/deployments/deploy-pr.sh new file mode 100755 index 00000000..910d3228 --- /dev/null +++ b/deployments/deploy-pr.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +# Nanocloud turns any traditional software into a cloud solution, without +# changing or redeveloping existing source code. +# +# Copyright (C) 2016 Nanocloud Software +# +# This file is part of Nanocloud. +# +# Nanocloud is free software; you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# Nanocloud is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General +# Public License +# along with this program. If not, see +# . + +# Detect where is the absolute path to repository +NANOCLOUD_DEPLOY_SCRIPT="$(readlink -e "${0}")" +NANOCLOUD_DEPLOY_DIR=$(dirname "${NANOCLOUD_DEPLOY_SCRIPT}") +NANOCLOUD_REPO_DIR=$(dirname "${NANOCLOUD_DEPLOY_DIR}") + +PR_NUMBER=${1} +REMOTE_NAME=${2:-"origin"} + +BRANCH_NAME="PR-${PR_NUMBER}" +DOCKER_COMPOSE_YML="docker-compose-${PR_NUMBER}.yml" + +cd "${NANOCLOUD_REPO_DIR}" || exit 1 + +# Fetch pull request +git fetch "${REMOTE_NAME}" "pull/${PR_NUMBER}/head:${BRANCH_NAME}" +git checkout "${BRANCH_NAME}" + +# Custom configurations +cp proxy/nginx.conf proxy/nginx.pr.conf +sed -i "s/backend:/backend-42${PR_NUMBER}:/" "proxy/nginx.pr.conf" +sed -i "s/guacamole-client:/guacamole-client-42${PR_NUMBER}:/" "proxy/nginx.pr.conf" + +cp docker-compose-pr.yml "${DOCKER_COMPOSE_YML}" +sed -i "s/PRPORTS/42${PR_NUMBER}/" "${DOCKER_COMPOSE_YML}" + +cp guacamole-client/guac_home/guacamole.properties guacamole-client/guac_home/guacamole.properties.backup +sed -i "s/noauthlogged-server-url: backend/noauthlogged-server-url: backend-42${PR_NUMBER}/" "guacamole-client/guac_home/guacamole.properties" + +cp config/connections.js backupConnection.js +sed -i "s/host: 'postgres'/host: 'postgres-42${PR_NUMBER}'/" "config/connections.js" + +# Build and run containers +docker-compose build +docker-compose up -d storage guacd +docker-compose -f "${DOCKER_COMPOSE_YML}" up -d + +# Erase custom configurations +mv backupConnection.js config/connections.js +mv guacamole-client/guac_home/guacamole.properties.backup guacamole-client/guac_home/guacamole.properties +rm -rf proxy/nginx.pr.conf +rm -rf "${DOCKER_COMPOSE_YML}" +git checkout master diff --git a/deployments/group_vars/all b/deployments/group_vars/all new file mode 100644 index 00000000..bdef27d0 --- /dev/null +++ b/deployments/group_vars/all @@ -0,0 +1,3 @@ +repository: Nanocloud +branch: master +#pull_request: XXX diff --git a/deployments/nanocloud.yml b/deployments/nanocloud.yml new file mode 100644 index 00000000..6546dd14 --- /dev/null +++ b/deployments/nanocloud.yml @@ -0,0 +1,14 @@ +--- + +- hosts: all + remote_user: debian + become: yes + vars: + repository: Nanocloud + #pull_request: XX + branch: master + roles: + - common + - franklinkim.docker + - franklinkim.docker-compose + - nanocloud diff --git a/deployments/requirements.yml b/deployments/requirements.yml new file mode 100644 index 00000000..55f23fc9 --- /dev/null +++ b/deployments/requirements.yml @@ -0,0 +1,2 @@ +- src: franklinkim.docker +- src: franklinkim.docker-compose diff --git a/deployments/roles/common/tasks/main.yml b/deployments/roles/common/tasks/main.yml new file mode 100644 index 00000000..a10c9f35 --- /dev/null +++ b/deployments/roles/common/tasks/main.yml @@ -0,0 +1,2 @@ +- name: Update APT repositories + apt: update_cache=yes diff --git a/deployments/roles/nanocloud/files/nanocloud/config.env b/deployments/roles/nanocloud/files/nanocloud/config.env new file mode 100644 index 00000000..e69de29b diff --git a/deployments/roles/nanocloud/tasks/main.yml b/deployments/roles/nanocloud/tasks/main.yml new file mode 100644 index 00000000..198a233b --- /dev/null +++ b/deployments/roles/nanocloud/tasks/main.yml @@ -0,0 +1,43 @@ +- name: Install git + apt: name=git state=present + +- name: Clone nanocloud + git: + repo: git://github.com/{{repository}}/nanocloud + dest: /nanocloud + force: yes + +- name: Copy custom configuration + copy: src=roles/nanocloud/files/nanocloud/config.env dest=/nanocloud/config.env owner=root group=root mode=0644 + +- name: Deploy Pull Request {{pull_request}} + command: /nanocloud/deployments/deploy-pr.sh {{pull_request}} + when: pull_request is defined + +- name: Checkout specific branch + command: git checkout {{branch|default(master)}} + args: + chdir: /nanocloud + when: + - pull_request is undefined + +- name: Docker compose build + command: docker-compose build + args: + chdir: /nanocloud + when: + - pull_request is undefined + +- name: Docker compose build development containers + command: docker-compose -f docker-compose-dev.yml up -d + args: + chdir: /nanocloud + when: + - pull_request is undefined + +- name: Docker compose up + command: docker-compose -f docker-compose-dev.yml up -d + args: + chdir: /nanocloud + when: + - pull_request is undefined diff --git a/docker-compose-pr.yml b/docker-compose-pr.yml new file mode 100644 index 00000000..73a26b99 --- /dev/null +++ b/docker-compose-pr.yml @@ -0,0 +1,71 @@ +version: '2' + +services: + backend-PRPORTS: + build: + context: ./ + dockerfile: Dockerfile + image: nanocloud/backend + volumes: + - static-PRPORTS:/opt/back/assets/dist + env_file: + - config.env + networks: + - nanocloud + depends_on: + - postgres-PRPORTS + restart: always + container_name: backend-PRPORTS + + frontend-PRPORTS: + build: + context: ./assets + dockerfile: Dockerfile + image: nanocloud/frontend + volumes: + - static-PRPORTS:/opt/dist + networks: + - nanocloud + container_name: frontend-PRPORTS + + proxy-PRPORTS: + build: ./proxy + image: nanocloud/proxy + ports: + - PRPORTS:443 + depends_on: + - backend-PRPORTS + volumes: + - ./proxy/nginx.pr.conf:/etc/nginx/conf.d/default.conf:ro + networks: + - nanocloud + restart: always + container_name: proxy-PRPORTS + + postgres-PRPORTS: + image: postgres:9.5.3 + environment: + - PGDATA=/var/lib/postgresql/data/pgdata + - POSTGRES_USER=nanocloud + networks: + - nanocloud + restart: always + container_name: postgres-PRPORTS + + guacamole-client-PRPORTS: + build: ./guacamole-client + image: nanocloud/guacamole-client + networks: + - nanocloud + restart: always + container_name: guacamole-client-PRPORTS + +networks: + nanocloud: + driver: bridge + +volumes: + static-PRPORTS: + driver: local + plaza: + driver: local