0

We are deploying a Laravel application on aws opsworks, everything is running great, however we need to do two other things:

1) On each deployment we want to run php artisan migrate to install the database updates.
2) We have a file (app/database/run.list) which contains a list of class names, for each line in the file we want to run php artisan db:seed --class={line from file}. e.g.

run.list contains

NewSystemSeed
NewUserSeed
CreateDefaultTemplatesSeed

we want to run

php artisan db:seed --class=NewSystemSeed
php artisan db:seed --class=NewUserSeed
php artisan db:seed --class=CreateDefaultTemplatesSeed

That parts not exactly difficult (although I am a bit stuck on the last one). The part that I am stuck on is, we only want to do this on the first instance in a specific layer (the php-app layer).

We obviously don't want to end up seeding the database for every instance!

Is there a way to automate this, or must we create another recipe, then after deployment manually trigger this recipe on the instance?

4

2 回答 2

2

您可以从应用层中的 Opsworks 实例列表中选择第一个实例,并始终在那里进行迁移,而不是将您的一个实例放在一个特殊层中,这会在该实例出现故障时产生单点故障。这使您可以保持配置不变,并确保您始终在正在运行的实例上运行迁移,因为失败的实例不会出现在列表中。

migrations_instance_hostname = node[:opsworks][:layers]['app-layer'][:instances].keys.sort.first

if migrations_instance_hostname == node[:opsworks][:instance][:hostname]
  # do migrations
end
于 2015-02-12T07:27:47.080 回答
0

您可以在 OpsWorks 中使用层作为标签来发送配方信号。

创建一个新层(可能是“db-seeder”)。不要在该层中添加新实例,而是从 PHP 层添加现有实例。

创建一个新的自定义配方,如下所示:

if node[:opsworks][:instance][:layers].include?("db-seeder")
  config_file = 'app/database/run.list'
  bash "migrate db" do
    code %Q^
      php artisan migrate
      for clz in `cat #{config_file}` do;
        php artisan db::seed --class=${clz};
      done^
  end
end

上面的配方只会在“db-seeder”层中的实例上运行。将此自定义配方添加到 PHP 应用程序层的“部署”事件。

于 2014-10-27T11:03:28.627 回答