From dfeb0bb0350fabd5f66badc3393bf486d930c646 Mon Sep 17 00:00:00 2001 From: Michal Wojtowicz Date: Sun, 24 Dec 2017 01:24:03 +0100 Subject: [PATCH] Initial commit --- README.md | 74 +++++++++++++++++++++++++++++++++++++++++++++++ defaults/main.yml | 16 ++++++++++ meta/main.yml | 29 +++++++++++++++++++ tasks/main.yml | 70 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 189 insertions(+) create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 meta/main.yml create mode 100644 tasks/main.yml diff --git a/README.md b/README.md new file mode 100644 index 0000000..80525da --- /dev/null +++ b/README.md @@ -0,0 +1,74 @@ +# Ansible Role: Deployment + +An Ansible role that deploy your application just the same way like Capistrano. + +## What means _Capistrano way_? + +Capistrano is software written in Ruby, which allows you to deploy your application on remote node. +Here is how it works: + +1. On remote node you should have in project workdir directories `/shared`, `/repository` and `/deployments`. +2. In `/repository` directory there is a copy of your source repo (GIT/SVN etc.) +3. When you do deploy, capistrano copies repository directory into `/deployments` dir. + Name of copied directory is different - it's always current timestamp (like `/deployments/20171108123453`). +4. Next step is to make symlinks of all directories/files from `/shared` directory into the new deployment directory. + It allows you to share upload dir or logs directory between deployments. +5. Last step is to create or change symlink `/current` to point to the latest deployment from `/deployments` + +## Requirements + +There aren't any requirements for this role. + +## Role variables + +Available variables are listed below, along with default values (see `defaults/main.yml`). + +##### Workdir for the role. Inside there will be deployed all applications. It can be also customised per project. + + deployment_default_workdir: "" + +##### Project owner and group. It can be also customised per project. + + deployment_default_owner: "" + deployment_default_group: "" + +##### Project deployments data + + deployment_projects: [] + +##### Example of project deployed with almost all defaults + deployment_projects: [] + - name: project.name + repository: git@github.com:some/repo + +##### Example of project deployed with all options customized + deployment_projects: [] + - name: project.name + workdir: /var/www + repository: git@github.com:some/repo + version: "HEAD" + owner: apache + group: apache + shared_files: + - any/path/to/mount/in/dir/deployment/dir + ssh_opts: "" + +## Dependencies + +None. + +## Example Playbook + + - hosts: webservers + vars_files: + - vars/main.yml + roles: + - { role: MWojtowicz.deployment } + +## License + +MIT / BSD + +## Author Information + +This role was created in 2017 by [Michal Wojtowicz](https://mwojtowicz.it/). \ No newline at end of file diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..709912e --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,16 @@ +--- +deployment_default_workdir: "" + +deployment_default_owner: "" +deployment_default_group: "" + +deployment_projects: [] +# - name: project.name +# workdir: /var/www +# repository: git@github.com:some/repo +# version: "HEAD" +# owner: apache +# group: apache +# shared_files: +# - any/path/to/mount/in/dir/deployment/dir +# ssh_opts: "" \ No newline at end of file diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..bad4c5f --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,29 @@ +galaxy_info: + author: Michał Wójtowicz + description: An Ansible role that deploy your application just the same way like Capistrano + company: mwojtowicz.it + + license: MIT + + min_ansible_version: 2.4 + + github_branch: master + + galaxy_tags: + - deployment + - apache + - capistrano + + platforms: + - name: EL + versions: + - all + - name: Debian + versions: + - all + - name: Ubuntu + versions: + - all + - name: Amazon + versions: + - all \ No newline at end of file diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..b408e15 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,70 @@ +--- +- name: "Ensure deployment workdir exists" + file: + path: "{{ item.workdir|default(deployment_default_workdir) }}" + state: directory + owner: "{{ item.owner|default(deployment_default_owner) }}" + group: "{{ item.group|default(deployment_default_group) }}" + with_items: "{{ deployment_projects }}" + +- name: "Create deployments directory" + file: + path: "{{ item.workdir|default(deployment_default_workdir) }}/{{ item.name }}/deployments" + state: directory + owner: "{{ item.owner|default(deployment_default_owner) }}" + group: "{{ item.group|default(deployment_default_group) }}" + with_items: "{{ deployment_projects }}" + +- name: "Create shared directory" + file: + path: "{{ item.workdir|default(deployment_default_workdir) }}/{{ item.name }}/shared" + state: directory + owner: "{{ item.owner|default(deployment_default_owner) }}" + group: "{{ item.group|default(deployment_default_group) }}" + with_items: "{{ deployment_projects }}" + +- name: "Clone git repository" + git: + repo: "{{ item.repository }}" + dest: "{{ item.workdir|default(deployment_default_workdir) }}/{{ item.name }}/repository" + force: yes + ssh_opts: "{{ item.ssh_opts|default('') }}" + version: "{{ item.version|default('HEAD') }}" + with_items: "{{ deployment_projects }}" + +- name: "Register deployment timestamp" + set_fact: + deploymentDate: "{{lookup('pipe','date +%Y%m%d%H%M%S')}}" + +- name: "Create deployment" + command: > + /bin/cp -r + {{ item.workdir|default(deployment_default_workdir) }}/{{ item.name }}/repository + {{ item.workdir|default(deployment_default_workdir) }}/{{ item.name }}/deployments/{{ deploymentDate }} + with_items: "{{ deployment_projects }}" + +- name: "Remove git tracking from deployment" + command: > + rm -rf {{ item.workdir|default(deployment_default_workdir) }}/{{ item.name }}/deployments/{{ deploymentDate }}/.git + with_items: "{{ deployment_projects }}" + +- name: "Create symlinks to shared files & directories" + file: + src: "{{ item.0.workdir|default(deployment_default_workdir) }}/{{ item.0.name }}/shared/{{ item.1 }}" + dest: "{{ item.0.workdir|default(deployment_default_workdir) }}/{{ item.0.name }}/deployments/{{ deploymentDate }}/{{ item.1 }}" + state: link + force: yes + owner: "{{ item.0.owner|default(deployment_default_owner) }}" + group: "{{ item.0.group|default(deployment_default_group) }}" + with_subelements: + - "{{ deployment_projects }}" + - shared_files + +- name: "Current deployment symlink" + file: + src: "{{ item.workdir|default(deployment_default_workdir) }}/{{ item.name }}/deployments/{{ deploymentDate }}" + dest: "{{ item.workdir|default(deployment_default_workdir) }}/{{ item.name }}/current" + state: link + owner: "{{ item.owner|default(deployment_default_owner) }}" + group: "{{ item.group|default(deployment_default_group) }}" + with_items: "{{ deployment_projects }}"