Buy

Symfony Console Commands

The project is working! Except... that it's not actually our project: this is the Symfony Standard Edition. Our cow customers are waiting: let's install MooTube!

Head over to https://github.com/knpuniversity/ansible to find the code behind this project. Copy the clone URL and open your editor. Find the spot where we clone the repo and use our new URL:

163 lines ansible/playbook.yml
---
- hosts: vb
... lines 3 - 9
tasks:
... lines 11 - 119
- name: Checkout Git repository
git:
repo: https://github.com/knpuniversity/ansible.git
... lines 123 - 163

You know the drill: run the playbook!

ansible-playbook ansible/playbook.yml -i ansible/hosts.ini

Our repository is public... which makes life easy. If you have a private repository, you'll need to make sure that your server has access to it. They talk about that a bit in the git module docs. You can also use a deploy key.

Using the Console

Once we have the code, we need to setup a few other things, like the database. The README.md file talks about these: after you download the composer dependencies, you can set up the database by running these three commands. Each runs through Symfony's console: an executable file in the bin/ directory.

This is a perfect situation for the command module... because... well, we literally just need to run 3 commands. Head to your playbook. Right above the handlers, add a comment: "Symfony Console Commands". We'll start with a task called "Create DB if not exists":

174 lines ansible/playbook.yml
---
- hosts: vb
... lines 3 - 10
tasks:
... lines 12 - 151
# Symfony console commands
- name: Create DB if not exists
... lines 154 - 174

Use the command module. For the value... we need to know the path to that bin/console file.

This is another good spot for a variable! Create a new one called symfony_console_path set to {{ symfony_root_dir }}/bin/console:

174 lines ansible/playbook.yml
---
- hosts: vb
vars:
... line 5
symfony_root_dir: /var/www/project
... lines 7 - 8
symfony_console_path: "{{ symfony_root_dir }}/bin/console"
... lines 10 - 174

Use that in the command: {{ symfony_console_path }} doctrine:database:create --if-not-exists:

174 lines ansible/playbook.yml
---
- hosts: vb
... lines 3 - 10
tasks:
... lines 12 - 151
# Symfony console commands
- name: Create DB if not exists
command: '{{ symfony_console_path }} doctrine:database:create --if-not-exists'
... lines 155 - 174

That last flag prevents an error if the database is already there.

Awesome! Copy that task to create the second one: "Execute migrations". Use doctrine:migrations:migrate --no-interaction:

174 lines ansible/playbook.yml
---
- hosts: vb
... lines 3 - 10
tasks:
... lines 12 - 151
# Symfony console commands
- name: Create DB if not exists
command: '{{ symfony_console_path }} doctrine:database:create --if-not-exists'
- name: Execute migrations
command: '{{ symfony_console_path }} doctrine:migrations:migrate --no-interaction'
... lines 158 - 174

And add one more: "Load data fixtures". This is something that we only want to run if this is a development machine, because it resets the database. We'll talk about controlling that later.

For this command, use hautelook_alice:doctrine:fixtures:load --no-interaction:

174 lines ansible/playbook.yml
---
- hosts: vb
... lines 3 - 10
tasks:
... lines 12 - 151
# Symfony console commands
- name: Create DB if not exists
command: '{{ symfony_console_path }} doctrine:database:create --if-not-exists'
- name: Execute migrations
command: '{{ symfony_console_path }} doctrine:migrations:migrate --no-interaction'
- name: Load data fixtures
command: '{{ symfony_console_path }} hautelook_alice:doctrine:fixtures:load --no-interaction'
... lines 161 - 174

Ok! The 3 commands are ready! Head back to the terminal. Woh! It exploded!

And actually... the reason is not that important: it says an error occurred during the cache:clear --no-warmup command. After we run composer install, Symfony runs several post install commands. One clears the cache. Changing from one project to an entirely different project temporarily put things in a weird state. This one time, in the virtual machine, just remove the cache manually:

rm -rf var/cache/*

Try the playbook now:

ansible-playbook ansible/playbook.yml -i ansible/hosts.ini

This time composer install should work and hopefully our new commands will setup the database. By the way! A Symfony 3 app reads its configuration from a parameters.yml file... which is not committed to the repository. So... in theory, that file should not yet exist... and none of this should work. But that file does exist! Why? Thanks to a special line in composer.json, after composer install finishes, the parameters.yml.dist file is copied to parameters.yml. And thanks to that dist file, Symfony will try to connect to MySQL using the root user and no password. If that's not right, just modify the file on the VM directly for now. Later, we'll talk about how we could properly update this file.

Yes! It worked! Notice: the three new tasks all say changed. That's because the command module isn't smart enough to know whether or not these actually changed anything. But, more on that soon!

Find your browser and refresh! Welcome to MooTube! The fact that it's showing these videos means our database is working. Now, let's talk about tags: a cool way to help us run only part of our playbook.

Leave a comment!

  • 2017-06-14 mlavrik

    Thanks! Adding version parameter helps.

  • 2017-06-06 Victor Bocharsky

    Hey mlavrik ,

    It's weird enough. Did you change the repo link to another repository? I think pointing the version could fix it. Try:


    - name: Checkout Git repository
    git:
    repo: https://github.com/knpuniversity/ansible.git
    dest: "{{ symfony_root_dir }}"
    force: yes
    version: master

    Where "master" is a branch which you want to checkout.

    If it doesn't help, please, try to connect to the remove host manually ("vagrant ssh" if you're using VM) and drop the repository folder at all with "rm -rf" command. Then run the playbook - Ansible try to pull the whole repo again without problem I think.

    Cheers!

  • 2017-06-05 mlavrik

    Hello guys. Problem with git checkout task. After changing repo url it fails with message "Could not determine remote revision for HEAD". Debug log - https://pastebin.com/DdF49Hqm

    What could be the reason?
    Thanks!

  • 2017-05-28 zacball

    Just blow out your projects dir in the VM and re-run Ansible. Ansible isnt actually pulling the new project files in. You can see that by looking at app/AppKernel.php. The migrations and haute bundles arent even registered because you still have the symfony standard code. Once you blow out the project dir it'll see the change, pull the new code and Composer will install the new packages.

  • 2017-05-24 Victor Bocharsky

    Hey Евгений,

    Don't you forget to run "composer update" after changing the composer.json file? Because if you change composer.json manually - then you still have outdated composer.lock file, so running "composer install" has no effect.

    Maybe there's a difference between versions. Could you try to install the latest version of it? To install and enable the latest version correctly you may follow the installation guide here: https://symfony.com/doc/cur... . And also try to clear the cache and run this command directly on your VM first.

    Cheers!

  • 2017-05-24 Евгений Явгель

    Hi guys. Task 'Execute migrations' give me an error: fatal: [192.168.33.10]: FAILED! => {"changed": true, "cmd": ["/var/www/ansible_project/bin/console", "doctrine:migrations:migrate", "--no-interaction"], "delta": "0:00:00.182210", "end": "2017-05-23 21:41:51.013178", "failed": true, "rc": 1, "start": "2017-05-23 21:41:50.830968", "stderr": "\n \n [Symfony\\Component\\Console\\Exception\\CommandNotFoundException] \n There are no commands defined in the \"doctrine:migrations\" namespace. \n Did you mean one of these? \n doctrine \n doctrine:cache \n doctrine:database \n doctrine:generate \n doctrine:mapping \n doctrine:query \n doctrine:schema \n ", "stderr_lines": ["", " ", " [Symfony\\Component\\Console\\Exception\\CommandNotFoundException] ", " There are no commands defined in the \"doctrine:migrations\" namespace. ", " Did you mean one of these? ", " doctrine ", " doctrine:cache ", " doctrine:database ", " doctrine:generate ", " doctrine:mapping ", " doctrine:query ", " doctrine:schema ", " "], "stdout": "", "stdout_lines": []}

    I edited the composer.json: "doctrine/doctrine-migrations-bundle": "1.0.0",
    "doctrine/migrations": "1.0.1" AND added in the AppKernel.php this line: new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(). How it resolve? Thanks

  • 2017-05-22 Victor Bocharsky

    Hey Jian,

    Hm, try to re-install the assets again with "./bin/console assets:install". Does it fix the CSS problem? Actually, this command executes during the "composer install", at the end of it. But if due to the some reason "composer install" command failed it won't be run. Also, try to clear the cache: dev/prod, depends on what environment you're using.

    Cheers!

  • 2017-05-19 jian su

    Hi guys: for some reason the CSS is off... Is it because we did not configure boostrap theme in ansible?

  • 2017-05-12 Victor Bocharsky

    Hey Chea,

    Glad you got it working by remove the cache and also remove the composer.lock. Btw, I think you just need to remove the cache dir, you can keep the composer.lock file.

    Cheers!

  • 2017-05-12 sokphea chea (ជា សុខភា)

    Hello, Victor here is the out put error in terminal


    TASK [Install Composer's dependencies]
    fatal: [192.168.33.10]: FAILED! => {"changed": false, "failed": true, "msg": "Loading composer repositories with package information Installing dependencies (including require-dev) from lock file Nothing to install or update Generating optimized autoload files > Incenteev\\ParameterHandler\\ScriptHandler::buildParameters > Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap > Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache Script Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception [RuntimeException] An error occurred when executing the \"'cache:clear --no-warmup'\" command: [Symfony\\Component\\Debug\\Exception\\ContextErrorException] Warning: Class __PHP_Incomplete_Class has no unserializer . install [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--no-custom-installers] [--no-autoloader] [--no-scripts] [--no-progress] [--no-suggest] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--ignore-platform-reqs] [--] [<packages>]...", "stdout": "Loading composer repositories with package information\nInstalling dependencies (including require-dev) from lock file\nNothing to install or update\nGenerating optimized autoload files\n> Incenteev\\ParameterHandler\\ScriptHandler::buildParameters\n> Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap\n> Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache\nScript Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception\n\n \n [RuntimeException] \n An error occurred when executing the \"'cache:clear --no-warmup'\" command: \n \n [Symfony\\Component\\Debug\\Exception\\ContextErrorException] \n Warning: Class __PHP_Incomplete_Class has no unserializer \n \n . \n \n\ninstall [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--no-custom-installers] [--no-autoloader] [--no-scripts] [--no-progress] [--no-suggest] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--ignore-platform-reqs] [--] [<packages>]...\n\n", "stdout_lines": ["Loading composer repositories with package information", "Installing dependencies (including require-dev) from lock file", "Nothing to install or update", "Generating optimized autoload files", "> Incenteev\\ParameterHandler\\ScriptHandler::buildParameters", "> Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", "> Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache", "Script Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception", "", " ", " [RuntimeException] ", " An error occurred when executing the \"'cache:clear --no-warmup'\" command: ", " ", " [Symfony\\Component\\Debug\\Exception\\ContextErrorException] ", " Warning: Class __PHP_Incomplete_Class has no unserializer ", " ", " . ", " ", "", "install [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--no-custom-installers] [--no-autoloader] [--no-scripts] [--no-progress] [--no-suggest] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--ignore-platform-reqs] [--] [<packages>]...", ""]}

  • 2017-05-10 Victor Bocharsky

    Hey Chea,

    May I see your full error output here? You can also wrap this output with pre/code tags in your comment to avoid getting to the spam folder - see Syntax Highlighting section in https://help.disqus.com/cus...

    Cheers!

  • 2017-05-09 sokphea chea (ជា សុខភា)

    Hello, I still get the cache error on TASK [Install Composer's dependencies] after I remove the cache, is there something I need to check? thanks