Deploying Symfony2 app with Ansible

download Deploying Symfony2 app with Ansible

of 60

  • date post

    14-Jul-2015
  • Category

    Engineering

  • view

    980
  • download

    5

Embed Size (px)

Transcript of Deploying Symfony2 app with Ansible

Roman R.About meSoftware Engineer at EPAM@co-organizer Lviv GDG@founder 2enota

Roman Rodomanskyitspoma@gmail.comgithub.com/itspomaskype: roman.rodomanskyylinkedin.com/in/rodomansky

3y epamAnsible overviewAnsible architecture and conceptsWhat is deploy?Deploying Symfony2 app with AnsibleAgenda

python-powered redically simple IT automation toolis optimized for easy automation, review, editing, & auditabilityfree, open sourcesimplyclear (anyone)fast (to learn, to setup)complete (modules)efficient (runs on OpenSSH)secure (without agents)What is Ansible?

yet another , - , you can get started in minuteslocated on githubany configuration managementapplication deploymentmulti-tier orchestrationcloud provisioning

For what Ansible?it can configure systemsand deploy the applicationsand orchestrate more advanced () IT tasks:such as continuous deploymentsor zero downtime rolling updates+ -agentless architecturemanagement over SSH (no custom PKI-SSH-based, no external databases, no daemons, does not leave software installed)developer friendly (configuration as data, not code)batteries-included (usefull modules)dead simplerelease cycles are usually about two months long

Ansible featuresconfigurations are textIt reads like Englishuses SSH to execute modules on remote machines without having to install any systems management softwarecomes with a large selection of modules for automating common tasksmodules can be written in any language -- if you would like to add extensions in bash, Python, Ruby, or even C, you are welcome to do soWho uses Ansible?

Jira, Confluence, HipChatfrom 2012, downloaded >1kkWho uses Ansible?

20.01.2014

top 10 python projects on github, new contributor added every ~1.3 days7 commits to dev-branch every dayfabric (python library)capistrano (capifony)puppetchefsaltstack

idephix, magellanes,deployer, rocketeer (php)Other tools

some for deploy, some for system tasksfabric is a Python (2.5-2.7) library for application deployment or systems administration tasks over SSHIt provides a basic suite of operations, and uploading/downloading filespython syntax (from fabric.api import run)capistrano: pre-post hooks (beforeX / afterX)rollbackant/phingrequired Python 2.6or Python 2.5 (with additional paramiko, PyYAML, python-jinja2 and httplib2 modules)Windows isnt supported for the control machine (starting with 1.8 will be fully support Windows)includes Red Hat, Debian, CentOS, OS X, any of the BSDs, and so onControl Machine Requirements

client, local-machinerequirements () for Ansible are extremely minimal ( )ansible runs on a central computerPython 2.5 + paramiko / PyYAML / python-jinja2 / httplib2Python 2.4 or laterif Python 2.5, then with python-simplejson modulesansible_python_interpreter to point at your 2.x Pythonstarting in version 1.7, ansible contains support for managing windows machinesManaged Node Requirements

raw module do not need python-simplejson module more

1.9-dev Dancing In the Street1.8 You Really Got Me Nov 26, 2014new Jinja2 filters, fixed a log of modules bugs, new system, variables, new modules, docker support, etc1.7 Summer Nights Sep 24, 2014

Versions1.9 = // stable release26 201424 2014every 2 month release

Ansible Galaxy , analogue: packagist, npmjs, rubygems.org

Ansible Towersaas service => software as a service =>

Ansible Towerdemo freebasic = 100$/month, up to 100 nodes, annual contract onlyenterprise = 50$/host/per-year, 8x5 supportpremium = 70$/host/per-year, 24x7 support

10 hosts = premium = 60$/per-month

from gitfrom os packages (recommend If you are wishing to run the latest released version)from pip (recommended to use Python package manager for other cases)Install & Configurefrom git == to get all the latest features ( ), keep up to date with the developmentrelease cycles are usually about two months long

Paramiko (python ssh module)SSH (OpenSSH)local

Connection typesit's important to understand how Ansible is communicating with remote machines over SSHby default => Ansible 1.3 try to use native OpenSSH when possibleas fallback => high-quality ( ) Python implementation of OpenSSH called paramikoIn Ansible 1.2 and before - defalut is ParamikoWhen speaking with remote machines, Ansible will by default assume () you are using SSH keyslocal => when node == control machineAnsible architecture

Inventory can be sourced from simple text files, the cloud, or configuration management databases

[web]webserver-1.example.comwebserver-2.example.com

[db]dbserver-1.example.com

Host Inventory: Basics with hostsdescribe infrastructure of your app serversthe things in brackets are group names, used for classifying systems, are controlling for what purposeIt is ok to put systems in more than one group, for instance a server could be both a webserver and a dbserver[web]webserver-[01:25].example.comwebserver-2.example.com

[db]dbserver-[a:f].example.com

Host Inventory: Rangespattern[all:children] all-local all-stage

[all-local:children] web-local db-local

[all-stage:children] web-stage db-stage

Host Inventory: child groups[web-stage:children] web-stage-testing web-stage-production

[db-stage:children] db-stage-testing db-stage-production

[web-stage-testing] testing-red

[web-stage-production] production

[web-local] vagrant

[db-local] vagrant

non standart SSH-ports:webserver-3.example.com:2222

SSH tunnel:myhost ansible_ssh_port=5555 ansible_ssh_host=192.168.0.1

Host Inventory: Morecustom connection settingsgroup-vars / host-varsansible [options]

vm$ cd demo1/vm$ ansible all -m pingvm$ ansible all -m setupvm$ ansible all -a "grep -c processor /proc/cpuinfo"vm$ ansible all -a "uptime"vm$ ansible all -a "uptime" -f 10

Demodefault: /etc/ansible/(!!!) ansible is NOT just about running commands, it also has powerful configuration management and deployment featuresplaybooksplaystasks and handlersmodulesvariables

Ansible concepts,

playbooks contains playsplays contains taskstasks contains modules

handels can be triggered by tasks,and will run at the end, oncePlaybooksplaybooks define configuration policy and orchestration workflowsYAML - , a tasks calls a module,and may have parameters

Tasks

Modules

May 2013 - 72, October 2014 - 175,February 2015 - 1933 modules on Galaxy , , , , , , , . 2013 = 72, = 175, 2015 = 2000

Modules list

package management: yum, aptremove execution: command, shellservice management: servicefile handling: copy, templatescm: git, subversion

Modules examplesmonitoring: monit, nagios, haproxy, etcdevelopment: jenkins, drush, solr, scala, maven, etcweb: Varnish, apache, composer, tomcat, symfony2, etcnetworking: tor, RabbitMQ, iptables, etccloud: stash-docker, OpenStack, etc

Modules examples #2over2000 modules on Galaxy

Module: copy and template

Module: apt and yum

Simple playbook

tagsplaybooksinventory (group vars, host vars)command line (ansible-playbook -e uservar=vagrant)discovered variables (facts)

Variables

Ansible Directory Structure

ls demo2-*/ls demo3-*/

DemoAnsible , inventory playbookdefault: /etc/ansible/Ansible is NOT just about running commands, it also has powerful configuration management and deployment features

Variables

Factsdiscovered variables about systemsansible -m setup

Using facts

Variables (example of group-var)

Variables (example of host-vars)

best practiceproject organization toolreusable componentsdefined filesystem structureshow: parameterized roles

Roles

Roles

failed_whenchanged_whenuntilignore_errors{{ lookup(file, test.pub) }}etc

Advanced playbook featuresUsage: ansible-vault [create|decrypt|edit|encrypt|rekey|view] [--help] [options] file_name

Ansible vaultfeature from ansible 1.5allows keeping encrypted data (in source control)App deploy strategiesbasic file transfer (via ftp/scp)using Source Controlusing Build Scripts and other Tools

http://symfony.com/doc/current/cookbook/deployment/tools.html

Upload your modified codeUpdate your vendor dependencies (composer)Running database migrationsUpdated assetic assetsClearing your cacheOther thingsSymfony deployment

other things:Tagging a particular version of your codeRunning any testsRemoval of any unnecessary filesClearing of external cache systemscron tasks

Symfony deployment$ git pull

$ php composer.phar install

$ php app/console doctrine:migration:migrate --no-iteraction

$ php app/console assets:install web --symlink$ php app/console assets:dump --env=prod

$ php app/console cache:clear

Directory structure

time to implementationUpload your modified code

- name: Pull sources from the repository. git: repo={{repo}} dest={{dest}} version={{branch}} when: project_deploy_strategy == git

module synchronize for rsyncSymfony deployment

Tagging a particular version of your codeRunning any testsRemoval of any unnecessary filesClearing of external cache systemscron tasks2) Update your vendor dependencies (composer)

- name: Install composer get_url: url=https://getcomposer.org/composer.phar dest={{project_root}}/composer.phar mode=0755 validate_certs=no

- name: Run composer install shell: cd {{project_root}}/releases/{{release}} && {{path}} {{project_root}}/composer.phar install {{project_composer_opts}}

Symfony deployment

Tagging a particular version of