Opsworks before-migrate.rb

Posted on Tue 09 June 2015 in aws

Opsworks is a convenient, powerful and free service provided by AWS to simplify the management of EC2 nodes.  The real power of the system is exposed through customizing various stages of the instance lifecycle by creating custom-tailored chef-solo recipes.

While Amazon provides a powerful deployment layer for PHP applications, it stops short once the PHP code has been checked out of git.  For Laravel or other composer apps, you’ll have to customize your deployment.  The most elegant and straightforward method is through custom deployment hooks.  Here’s how to build a before_migration.rb script to build a Laravel app.

At the top of your build, add a file deploy/before_migrate.rb like this:

[release_path].each do |path|
    log "before_migrate hook [#{path}]" do
        level :debug
    end
    # run composer install
    execute "composer install" do
        user "deploy"
        cwd path
        command "composer install --no-dev --no-interaction --optimize-autoloader"
        # only execute for composer projects
        only_if "test -f \"#{path}/composer.json\""
    end
    # correct permissions to allow apache to write
    execute "chown #{path}/app/storage" do
        cwd "#{path}/app/storage"
        command "chown -R deploy.www-data ."
    end
    execute "chmod #{path}/app/storage" do
        cwd "#{path}/app/storage"
        command "chmod -R u+rwX,g+rwX ."
    end