Skip to content

Commit

Permalink
Merge pull request #30 from servergrove/2.0
Browse files Browse the repository at this point in the history
2.0
  • Loading branch information
maschmann committed Nov 7, 2015
2 parents 3ac76bb + a6c2020 commit b9c5fdd
Show file tree
Hide file tree
Showing 14 changed files with 283 additions and 120 deletions.
13 changes: 9 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
language: python
python: "2.7"

before_install:
# Make sure everything's up to date.
- sudo apt-get update -qq
sudo: false

addons:
apt:
packages:
- php5-cli
- php5-common
- php5-curl

install:
# Install Ansible.
Expand All @@ -19,4 +24,4 @@ install:
script:
# Check the role/playbook's syntax.
- "ansible-playbook -i tests/inventory tests/test.yml --syntax-check"
- "ansible-playbook -i tests/inventory tests/test.yml --connection=local --sudo"
- "ansible-playbook -i tests/inventory tests/test.yml --connection=local"
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changelog

## v2.0
reworked and more flexible configuration

* creates release version in a file per release
* more control over shared folders with additional config options
* possibilities for shallow git copy
* optional cache warmup for sf2
* added tags for specific tasks to allow e.g. cache flush only in later versions
* minified composer.json read overhead
* allow for role hooks

## v1.0

* created stable release for later refactoring

## v0.3

* working travis-ci tests
Expand Down
119 changes: 83 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

Ansible role to easily deploy Symfony2 applications.
It will clone a git repository, a specific branch or a tag, download and run composer install, and run assetic:dump when finished.
The resulting directory structure is similar to what capifony creates:
The resulting directory structure is similar to what capifony/capistrano creates:

```
project
Expand Down Expand Up @@ -45,20 +45,24 @@ This playbook is taken from the travis testcase. You can always pass these value
- ansible-symfony2

vars:
symfony2_project_root: /test_app
symfony2_project_name: travis-test
symfony2_project_composer_path: /test_app/shared
symfony2_project_repo: https://github.com/symfony/symfony-standard.git
symfony2_project_branch: "2.6"
symfony2_project_php_path: php
symfony2_project_env: prod
symfony2_project_console_opts: '--no-debug'
symfony2_project_composer_opts: '--no-dev --optimize-autoloader --no-interaction'
symfony2_project_keep_releases: 5
symfony2_project_clean_versioning: true
symfony_project_root: /tmp/test_app
symfony_project_name: travis-test
symfony_project_composer_path: /tmp/test_app/shared/composer.phar
symfony_project_repo: https://github.com/symfony/symfony-standard.git
symfony_project_env: prod

symfony_project_console_opts: '--no-debug'
symfony_project_keep_releases: 5

symfony_project_branch: "2.6"
symfony_project_php_path: php
symfony_project_keep_releases: 5

symfony_project_manage_composer: True
symfony_project_composer_opts: '--no-dev --optimize-autoloader --no-interaction'
```
Commandline: ```~$ ansible-playbook -i inventory --extra-vars "symfony2_project_release=20150417142505,symfony2_project_branch=master" test.yml```
Commandline: ```~$ ansible-playbook -i inventory --extra-vars "symfony_project_release=20150417142505,symfony_project_branch=master" test.yml```

## Role Variables

Expand All @@ -69,20 +73,35 @@ These are the possible role variables - you only need to have a small set define
```yaml
---
- vars:
symfony2_project_root: Path where application will be deployed on server.
symfony2_project_name: Name of project.
symfony2_project_composer_path: path where composer.phar will be stored (e.g. project_root/shared)
symfony2_project_repo: URL of git repository.
symfony2_project_release: Release number, can be numeric, we recommend to set it to release date/time, 20140327100911
symfony2_project_branch: git branch to deploy.
symfony2_project_php_path: /usr/local/php54/bin/php
symfony2_project_env: prod
symfony2_project_console_opts: ''
symfony2_project_composer_opts: '--no-dev --optimize-autoloader --no-interaction'
symfony2_project_keep_releases: 5
symfony2_project_clean_versioning: true
symfony2_fire_schema_update: false # Runs doctrine:mongodb:schema:update
symfony2_fire_migrations: false # Runs doctrine migrations script
# necessary project vars
symfony_project_root: Path where application will be deployed on server.
symfony_project_composer_path: path where composer.phar will be stored (e.g. project_root/shared)
symfony_project_repo: URL of git repository.
symfony_project_release: Release number, can be numeric, we recommend to set it to release date/time, 20140327100911
symfony_project_env: prod
# optional parameters, covered by defaults
symfony_project_post_folder_creation_tasks: task hook after folder creation
symfony_project_pre_cache_warmup_tasks: after cache warmup
symfony_project_pre_live_switch_tasks: before live symlink is switched
symfony_project_post_live_switch_tasks: after live symlink is switched
symfony_project_branch: git branch, commit hash or version tag to deploy - defaults to master
symfony_project_php_path: php
symfony_project_keep_releases: 5
symfony_project_git_clone_depth: 1 # uses git shallow copy
symfony_project_console_opts: ''
symfony_project_parameters_file: parameters.yml # optional fixed parameters file in shared
symfony_project_cache_command: cache:warmup
symfony_project_manage_composer: True
symfony_project_composer_opts: '--no-dev --optimize-autoloader --no-interaction'
symfony_project_composer_run_install: True
symfony_project_enable_cache_warmup: True warmup symfony cache, check out cache command!
symfony_project_fire_schema_update: False # rund mongodb schema update if installed
symfony_project_fire_migrations: run doctrine migrations, if installed
symfony_project_symlink_assets: run assets:create with symlink options
```

### Role variable defaults
Expand All @@ -92,16 +111,44 @@ As you can see, the release number default is the current date/time with seconds
```yaml
---
- vars
symfony2_project_release: <datetime> # internally replaced with YmdHis
symfony2_project_branch: master
symfony2_project_php_path: /usr/bin/php
symfony2_project_keep_releases: 5
symfony2_project_clean_versioning: true
symfony2_project_console_opts: ''
symfony2_project_composer_opts: '--no-dev --optimize-autoloader --no-interaction'
symfony2_fire_schema_update: false
symfony2_fire_migrations: false
symfony_project_release: <datetime> # internally replaced with YmdHis
symfony_project_branch: master
symfony_project_php_path: /usr/bin/php
symfony_project_keep_releases: 5
symfony_project_git_clone_depth: 1
symfony_project_console_opts: ''
symfony_project_composer_opts: '--no-dev --optimize-autoloader --no-interaction'
symfony_project_fire_migrations: False
symfony_project_symlink_assets: True
```

## hooks
If you need any more tasks and stuff in your deployment, you now have the option to include hook scripts.
In my projects there's often e.g. a gulp task that has to be started before finishing the release. You're also free to create more folders yourself or do whatever you need.
As an additional goodie, you can use the internal dynamically created facts from main role:

```
symfony_project_release # release timestamp
symfony_current_release # release name
symfony_current_release_dir # fully qualified path to release
symfony_shared_dir # shared folder base path
symfony_console # fully qualified console command path
```
possible hooks:

```
symfony_project_post_folder_creation_tasks: task hook after folder creation
symfony_project_pre_cache_warmup_tasks: after cache warmup
symfony_project_pre_live_switch_tasks: before live symlink is switched
symfony_project_post_live_switch_tasks: after live symlink is switched
```

These hooks trigger an include when defined.
Define hooks:
```
symfony_project_post_folder_creation_tasks: "{{ playbook_dir }}/hooks/post_folder_creation.yml"
```
The "hooks" dir should be in your deployment project as a subfolder. I'd recommend to use this name as a convention. Also it's convinient to use the name of the hook task as a yml name.

## Dependencies

Expand Down
42 changes: 30 additions & 12 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
---
# defaults file for symfony2

symfony2_project_release: ~
symfony2_project_branch: master
symfony2_project_php_path: php
symfony2_project_keep_releases: 5
symfony2_project_clean_versioning: true
symfony2_project_console_opts: ''
symfony2_project_maintain_composer: true
symfony2_project_composer_opts: '--no-dev --optimize-autoloader --no-interaction'
symfony2_fire_schema_update: false
symfony2_fire_migrations: false
# necessary project vars

#symfony_project_root: ''
#symfony_project_composer_path: ''
#symfony_project_repo: ''
#symfony_project_env: ''

# hooks
symfony_project_post_folder_creation_tasks: ~
symfony_project_pre_cache_warmup_tasks: ~
symfony_project_pre_live_switch_tasks: ~
symfony_project_post_live_switch_tasks: ~

symfony_project_release: ~
symfony_project_branch: master
symfony_project_php_path: php
symfony_project_keep_releases: 5
symfony_project_git_clone_depth: 1
symfony_project_console_opts: ''
symfony_project_parameters_file: parameters.yml
symfony_project_cache_command: cache:warmup

symfony_project_manage_composer: True
symfony_project_composer_opts: '--no-dev --optimize-autoloader --no-interaction'
symfony_project_composer_run_install: True

symfony_project_enable_cache_warmup: True
symfony_project_fire_schema_update: False
symfony_project_fire_migrations: False
symfony_project_symlink_assets: True
8 changes: 8 additions & 0 deletions tasks/assets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: Dump assetic assets.
shell: cd {{symfony_current_release_dir}} && {{symfony_project_php_path}} {{symfony_console}} assetic:dump --env={{symfony_project_env}} {{symfony_project_console_opts}}
when: composer_content.stdout.find('assetic-bundle') != -1

- name: Symlink/install assets.
shell: cd {{symfony_current_release_dir}} && {{symfony_project_php_path}} {{symfony_console}} assets:install --symlink --env={{symfony_project_env}} {{symfony_project_console_opts}}
when: symfony_project_symlink_assets == True
12 changes: 12 additions & 0 deletions tasks/cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- name: Remove cache dir manually.
file: state=absent path={{symfony_current_release_dir}}/app/cache/{{symfony_project_env}}
when: symfony_project_enable_cache_warmup == True
tags:
- cache

- name: Warmup sf cache.
shell: cd {{symfony_current_release_dir}} && export SYMFONY_ENV={{symfony_project_env}}; {{symfony_project_php_path}} {{symfony_console}} {{symfony_project_cache_command}} --env={{symfony_project_env}} {{symfony_project_console_opts}}
when: symfony_project_enable_cache_warmup == True
tags:
- cache
4 changes: 4 additions & 0 deletions tasks/clean_releases.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- name: Cleanup releases.
shell: "cd {{symfony_project_root}}/releases && ls -t1 | tail -n +$(({{symfony_project_keep_releases}}+1)) | xargs -n1 rm -rf"
when: symfony_project_keep_releases
21 changes: 21 additions & 0 deletions tasks/composer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
- name: Check if composer exists.
stat: path={{symfony_project_composer_path}}
register: composer_file
when: symfony_project_manage_composer == True

- name: Install composer.
get_url: url=https://getcomposer.org/composer.phar dest={{symfony_project_composer_path}} mode=0755 validate_certs=no
when: symfony_project_manage_composer == True and composer_file.stat.exists == False

- name: Update composer if already exists.
shell: "{{symfony_project_composer_path}} selfupdate"
when: composer_file.stat.exists == True and symfony_project_manage_composer == True

- name: Link composer to project dir.
file: state=link src={{symfony_project_composer_path}} path={{symfony_current_release_dir}}/composer
when: symfony_project_manage_composer == True

- name: Run composer install.
shell: cd {{symfony_current_release_dir}} && export SYMFONY_ENV={{symfony_project_env}}; {{symfony_project_php_path}} {{symfony_project_composer_path}} install {{symfony_project_composer_opts}}
when: symfony_project_composer_run_install == True
16 changes: 16 additions & 0 deletions tasks/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
- name: Check if config dir exists.
stat: path={{symfony_current_release_dir}}/app/config
register: config_dir

- name: Link configs dir if not yet exists.
file: state=link src={{symfony_shared_dir}}/app/config path={{symfony_current_release_dir}}/app/config
when: config_dir.stat.exists == false

- name: Check if parameters file exists.
stat: path={{symfony_shared_dir}}/app/config/{{symfony_project_parameters_file}}
register: symfony_parameters

- name: Create symlink for parameters file from shared directory.
file: state=link src={{symfony_project_root}}/shared/app/config/{{symfony_project_parameters_file}} path={{symfony_current_release_dir}}/app/config/{{symfony_project_parameters_file}} creates={{symfony_current_release_dir}}/app/config/{{symfony_project_parameters_file}}
when: symfony_parameters.stat.exists
1 change: 1 addition & 0 deletions tasks/empty.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
18 changes: 18 additions & 0 deletions tasks/facts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
# simplify folder and command vars

- name: Check/define release name.
set_fact: symfony_project_release={{ lookup('pipe', 'date +%Y%m%d%H%M%S') }}
when: symfony_project_release == None

- name: Set symfony_current_release.
set_fact: symfony_current_release={{symfony_project_release}}_{{symfony_project_branch}}

- name: Set symfony_current_release_dir.
set_fact: symfony_current_release_dir={{symfony_project_root}}/releases/{{symfony_current_release}}

- name: Set symfony_shared_dir.
set_fact: symfony_shared_dir={{symfony_project_root}}/shared

- name: Set symfony_console.
set_fact: symfony_console={{symfony_current_release_dir}}/app/console
Loading

0 comments on commit b9c5fdd

Please sign in to comment.