├── .gitignore ├── README.md ├── deploy.yml ├── destroy.yml ├── handlers.yml ├── hosts ├── hosts.dist ├── provision.yml ├── roles └── VCS │ └── tasks │ └── main.yml ├── templates ├── parameters.yml.js2.dist └── vhost-configuration.js2 └── vars.yml.dist /.gitignore: -------------------------------------------------------------------------------- 1 | /templates/parameters.yml.js2 2 | /vars.yml -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This repository contain 3 Ansible playbooks that can help you with Symfony2 application 2 | installation and later deploys. 3 | 4 | #Usage 5 | 6 | First of all you should install [Ansible](http://www.ansible.com/home) on your machine, 7 | official [docs](http://docs.ansible.com/intro_installation.html) should help you with that. 8 | 9 | Now you need create host file (``cp hosts.dist hosts``). Basically it's enough to put there server 10 | host where your application should be deployed/installed. 11 | 12 | Project variables - everything that is related to your project should be configured in 2 files. 13 | ``vars.yml`` and ``templates/parameters.yml.js2``. Just copy ``vars.yml.dist`` to ``vars.yml`` and 14 | ``templates/parameters.yml.js2.dist`` to ``templates/parameters.yml.js2``. 15 | Every single variable in those files should be self descriptive. 16 | 17 | Ok now you should have everything to install project at production server with ``ansible-playbook -i hosts provision.yml`` 18 | but before doing it you should check playbook with ``ansible-playbook -i hosts provision.yml --check`` 19 | 20 | ## Provision 21 | 22 | This command should be used only once. it's basically a application installation playbook. 23 | 24 | ``` 25 | $ ansible-playbook -i hosts provision.yml 26 | ``` 27 | 28 | ## Deploy 29 | 30 | Use every single time when you need to update your application. 31 | This playbook will not remove current application code from server. It will copy application source to 32 | temp location, do deploy stuff and replace source app with temp. 33 | 34 | ``` 35 | $ ansible-playbook -i hosts deploy.yml 36 | ``` 37 | 38 | ## Destroy 39 | 40 | Use only when you need to remove application from server. It will remove application sources, 41 | databases and apache configuration from server. 42 | 43 | ``` 44 | $ ansible-playbook -i hosts destroy.yml 45 | ``` 46 | -------------------------------------------------------------------------------- /deploy.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Deploy application 3 | hosts: all 4 | remote_user: root 5 | vars_files: 6 | - vars.yml 7 | handlers: 8 | - include: handlers.yml 9 | gather_facts: false 10 | 11 | tasks: 12 | - name: Register timestamp variable 13 | shell: date +%Y_%m_%d_%H_%M_%S 14 | register: timestamp 15 | 16 | - name: Copy project source to temp folder 17 | shell: rsync -r --copy-links --exclude 'app/cache/*' --exclude 'app/logs/*' {{ project_root }}/ {{ project_root }}_{{ timestamp.stdout }}/ 18 | remote_user: "{{ remote_user }}" 19 | 20 | - name: Fetch project origin 21 | shell: cd {{ project_root }}_{{ timestamp.stdout }} && git fetch origin 22 | remote_user: "{{ remote_user }}" 23 | 24 | - name: Merge project origin master branch 25 | shell: cd {{ project_root }}_{{ timestamp.stdout }} && git merge origin/master 26 | remote_user: "{{ remote_user }}" 27 | 28 | - name: Set cache folder permissions 29 | shell: > 30 | APACHEUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data' | grep -v root | head -1 | cut -d\ -f1` && 31 | setfacl -R -m u:"$APACHEUSER":rwX -m u:`whoami`:rwX {{ project_root }}_{{ timestamp.stdout }}/app/cache {{ project_root }}_{{ timestamp.stdout }}/app/logs && 32 | setfacl -dR -m u:"$APACHEUSER":rwX -m u:`whoami`:rwX {{ project_root }}_{{ timestamp.stdout }}/app/cache {{ project_root }}_{{ timestamp.stdout }}/app/logs 33 | 34 | - name: Install composer dependencies 35 | shell: cd {{ project_root }}_{{ timestamp.stdout }} && composer install --no-interaction --prefer-source --optimize-autoloader --no-dev 36 | remote_user: "{{ remote_user }}" 37 | 38 | - name: Set application parameters 39 | template: 40 | src: templates/parameters.yml.js2 41 | dest: "{{ project_root }}_{{ timestamp.stdout }}/app/config/parameters.yml" 42 | remote_user: "{{ remote_user }}" 43 | 44 | - name: Clear application cache 45 | shell: php {{ project_root }}_{{ timestamp.stdout }}/app/console ca:cl --no-debug --env=prod 46 | remote_user: "{{ remote_user }}" 47 | 48 | - name: Update database schema 49 | shell: php {{ project_root }}_{{ timestamp.stdout }}/app/console doctrine:schema:update --force --env=prod 50 | remote_user: "{{ remote_user }}" 51 | 52 | - name: Remove link to old application code 53 | file: 54 | path: "{{ project_root }}" 55 | state: absent 56 | remote_user: "{{ remote_user }}" 57 | 58 | - name: Create link to shiny new application code 59 | file: 60 | src: "{{ project_root }}_{{ timestamp.stdout }}" 61 | dest: "{{ project_root }}" 62 | state: link 63 | remote_user: "{{ remote_user }}" 64 | -------------------------------------------------------------------------------- /destroy.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Remove application from server 3 | hosts: all 4 | remote_user: root 5 | vars_files: 6 | - vars.yml 7 | handlers: 8 | - include: handlers.yml 9 | gather_facts: false 10 | 11 | tasks: 12 | - name: Delete all project source 13 | file: 14 | path: "{{ project_path }}" 15 | state: absent 16 | 17 | - name: Disable apache2 vhosts 18 | file: 19 | path: "/etc/apache2/sites-enabled/{{ project_name }}-configuration.conf" 20 | state: absent 21 | 22 | - name: Remove apache2 configuration 23 | file: 24 | path: "/etc/apache2/sites-available/{{ project_name }}-configuration.conf" 25 | state: absent 26 | notify: restart apache 27 | 28 | - name: Remove database 29 | mysql_db: 30 | name: "{{ db_name }}" 31 | state: absent 32 | 33 | - name: Remove database user 34 | mysql_user: 35 | name: "{{ db_user }}" 36 | state: absent 37 | -------------------------------------------------------------------------------- /handlers.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: restart apache 3 | service: name=apache2 state=restarted 4 | -------------------------------------------------------------------------------- /hosts: -------------------------------------------------------------------------------- 1 | thor.ndevops.pl 2 | -------------------------------------------------------------------------------- /hosts.dist: -------------------------------------------------------------------------------- 1 | example.com 2 | -------------------------------------------------------------------------------- /provision.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Install application at server 3 | hosts: all 4 | remote_user: root 5 | vars_files: 6 | - vars.yml 7 | handlers: 8 | - include: handlers.yml 9 | gather_facts: false 10 | roles: 11 | - VCS 12 | tasks: 13 | - name: Ensure that project path exists 14 | file: 15 | path: "{{ project_path }}" 16 | state: directory 17 | remote_user: "{{ remote_user }}" 18 | 19 | - name: Clone project 20 | git: 21 | repo: "{{ project_repo }}" 22 | dest: "{{ project_root }}_source" 23 | remote_user: "{{ remote_user }}" 24 | 25 | - name: Set cache folder permissions 26 | shell: > 27 | APACHEUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data' | grep -v root | head -1 | cut -d\ -f1` && 28 | setfacl -R -m u:"$APACHEUSER":rwX -m u:`whoami`:rwX {{ project_root }}_source/app/cache {{ project_root }}_source/app/logs && 29 | setfacl -dR -m u:"$APACHEUSER":rwX -m u:`whoami`:rwX {{ project_root }}_source/app/cache {{ project_root }}_source/app/logs 30 | 31 | - name: Install composer deps 32 | shell: cd {{ project_root }}_source && composer install --no-interaction --prefer-source --optimize-autoloader --no-dev 33 | remote_user: "{{ remote_user }}" 34 | 35 | - name: Set application parameters 36 | template: 37 | src: templates/parameters.yml.js2 38 | dest: "{{ project_root }}_source/app/config/parameters.yml" 39 | remote_user: "{{ remote_user }}" 40 | 41 | - name: Create a new database 42 | mysql_db: 43 | name: "{{ db_name }}" 44 | state: present 45 | collation: utf8_general_ci 46 | 47 | - name: Create a database user 48 | mysql_user: 49 | name: "{{ db_user }}" 50 | password: "{{ db_user_pass }}" 51 | priv: "{{ db_name }}.*:ALL" 52 | host: "{{ db_host }}" 53 | state: present 54 | 55 | - name: Clear application cache 56 | shell: php {{ project_root }}_source/app/console ca:cl --env=prod 57 | remote_user: "{{ remote_user }}" 58 | 59 | - name: Update database schema 60 | shell: php {{ project_root }}_source/app/console doctrine:schema:update --force --env=prod 61 | remote_user: "{{ remote_user }}" 62 | 63 | - name: Create link to application source 64 | file: 65 | src: "{{ project_root }}_source" 66 | dest: "{{ project_root }}" 67 | state: link 68 | 69 | - name: Create apache2 configuration 70 | template: 71 | src: templates/vhost-configuration.js2 72 | dest: /etc/apache2/sites-available/{{ project_name }}-configuration.conf 73 | 74 | - name: Enable apache2 vhosts 75 | file: 76 | src: /etc/apache2/sites-available/{{ project_name }}-configuration.conf 77 | dest: /etc/apache2/sites-enabled/{{ project_name }}-configuration.conf 78 | state: link 79 | notify: restart apache -------------------------------------------------------------------------------- /roles/VCS/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Add 'bitbucket.org' to ssh known hosts 3 | lineinfile: 4 | dest: ~/.ssh/known_hosts 5 | state: present 6 | create: yes 7 | line: 'bitbucket.org ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAubiN81eDcafrgMeLzaFPsw2kNvEcqTKl/VqLat/MaB33pZy0y3rJZtnqwR2qOOvbwKZYKiEO1O6VqNEBxKvJJelCq0dTXWT5pbO2gDXC6h6QDXCaHo6pOHGPUy+YBaGQRGuSusMEASYiWunYN0vCAI8QaXnWMXNMdFP3jHAJH0eDsoiGnLPBlBp4TNm6rYI74nMzgz3B9IikW4WVK+dc8KZJZWYjAuORU3jc1c/NPskD2ASinf8v3xnfXeukU0sJ5N6m5E8VLjObPEO+mN2t/FZTMZLiFqPWc/ALSqnMnnhwrNi2rbfg/rd/IpL8Le3pSBne8+seeFVBoGqzHM9yXw==' 8 | remote_user: "{{ remote_user }}" 9 | 10 | - name: Add 'github.com' to ssh known hosts 11 | lineinfile: 12 | dest: ~/.ssh/known_hosts 13 | state: present 14 | create: yes 15 | line: 'github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==' 16 | remote_user: "{{ remote_user }}" 17 | 18 | -------------------------------------------------------------------------------- /templates/parameters.yml.js2.dist: -------------------------------------------------------------------------------- 1 | parameters: 2 | database_driver: pdo_mysql 3 | database_host: {{ db_host }} 4 | database_port: null 5 | database_name: {{ db_name }} 6 | database_user: {{ db_user }} 7 | database_password: {{ db_user_pass }} 8 | mailer_transport: smtp 9 | mailer_host: 127.0.0.1 10 | mailer_user: null 11 | mailer_password: null 12 | secret: ThisIsNotASecret 13 | locale: en 14 | -------------------------------------------------------------------------------- /templates/vhost-configuration.js2: -------------------------------------------------------------------------------- 1 | 2 | ServerAdmin webmaster@{{ project_name }} 3 | DocumentRoot {{ project_web_root }} 4 | ServerName {{ project_name }} 5 | ServerAlias www.{{ project_name }} 6 | 7 | ErrorLog /var/log/apache2/{{ project_name }}-error_log 8 | CustomLog /var/log/apache2/{{ project_name }}-access_log common 9 | 10 | -------------------------------------------------------------------------------- /vars.yml.dist: -------------------------------------------------------------------------------- 1 | --- 2 | project_name: symfony-standard.com 3 | project_path: /var/www/symfony 4 | project_root: "{{ project_path }}/{{ project_name }}" 5 | project_web_root: "{{ project_root }}/web" 6 | project_repo: git@github.com:symfony/symfony-standard.git 7 | db_name: symfony 8 | db_host: localhost 9 | db_user: symfony 10 | db_user_pass: this_is_not_a_good_password 11 | 12 | remote_user: www-data # remote user used to clear application cache, update databases install dependencies etc. --------------------------------------------------------------------------------