0

I can't understand how am I supposed to handle 'Deploy' event that happens on instance start. The stack has two layers (Node.js and Rails) and two apps with distinct recipes to handle 'Deploy' events. The problems is, when the instance is started, OpsWorks tries to deploy both these apps to an instance and obviously fails. Currently the deployment recipe looks like this:

search('aws_opsworks_app').each do |app|
  Chef::Log.info("Deploying app #{app.name}")
  app_dir = '/srv/www/js_app'

  application app_dir do
    git app['app_source']['url'] do
      revision app['app_source']['revision']
      deploy_key app['app_source']['ssh_key']
    end

    execute 'Install dependencies' do
      command 'npm install --dev'
      cwd app_dir
    end

    execute 'Build' do
      command 'npm run build'
      cwd app_dir
    end

    npm_start
  end
end
synapse
  • 489
  • 1
  • 6
  • 14

1 Answers1

1

Your recipe needs to be able to work for both apps. Opsworks does this by design, if an instance is off and is subsequently started (however that happens), it needs to be able to synchronize the instance up with the latest versions of any apps, attributes, etc that the instance requires for the layer that its in before it brings it online.

The simplest thing you can do to fix your recipe, assuming that it's app_dir that's causing you grief, you could look at setting app_dir to something unique for each app, for example /srv/www/#{app['shortname']}. shortname is an underscored, lower-cased version of your application's name which is suitable to create a directory with.

If your applications vary in a way that, for example, one application runs npm install and the other doesn't, you can use the app['environment'] hash to pass additional parameters about your application which you can then use in your recipe to determine whether the run a specific action or not. For example;

execute "Install dependencies" do
  command "npm install --dev"
  cwd app_dir
  only_if do
    app['environment'].key?('do_npm_install')
  end
end

Then go into your app's settings in the Opsworks console, add a key "do_npm_install" and set its value to anything (doesn't matter what it is, the example above only checks for the presence of a key, not what its value is), while leaving the other app in its original condition. This will then only run the execute block for apps that have do_npm_install in their environment.

dannosaur
  • 953
  • 5
  • 15