简体   繁体   English

如何使用opsworks在部署的特定层中仅在第一个实例上运行命令?

[英]How to run a command on only the first instance in a specific layer on deployment with opsworks?

We are deploying a Laravel application on aws opsworks, everything is running great, however we need to do two other things: 我们正在aws opsworks上部署Laravel应用程序,一切运行良好,但是我们需要做另外两件事:

1) On each deployment we want to run php artisan migrate to install the database updates. 1)在每个部署上,我们要运行php artisan migrate来安装数据库更新。
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} . 2)我们有一个文件(app / database / run.list),其中包含类名列表,对于文件中的每一行,我们要运行php artisan db:seed --class={line from file} eg 例如

run.list contains run.list包含

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). 我停留的部分是,我们只想在特定层(php-app层)的第一个实例上执行此操作。

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? 有没有一种方法可以自动执行此操作,或者必须创建另一个配方,然后在部署后在实例上手动触发该配方?

Rather than put one of your instances in a special layer, which creates a single point of failure if that instance goes down, you could choose the first instance from the list of Opsworks instances in your app layer and always do your migrations there. 您可以将实例从出现在应用程序层的Opsworks实例列表中选择第一个实例,然后始终在其中进行迁移,而不是将一个实例放在一个特殊的层中(如果该实例出现故障将创建一个单点故障)。 This allows you to keep your configuration as-is and ensures that you always run the migrations on a running instance since failed instances won't be in the list. 这使您可以保持配置不变,并确保始终在运行中的实例上运行迁移,因为失败的实例将不在列表中。

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

if migrations_instance_hostname == node[:opsworks][:instance][:hostname]
  # do migrations
end

You can use Layers as tags in OpsWorks to signal recipes. 您可以在OpsWorks中将“图层”用作标签来指示配方。

Create a new Layer (perhaps "db-seeder"). 创建一个新的图层(也许是“ db-seeder”)。 Instead of adding a new instance in that layer, add an existing instance from your PHP layer. 无需在该层中添加新实例,而是从PHP层中添加现有实例。

Create a new custom recipe that looks like this: 创建一个新的自定义配方,如下所示:

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

The above recipe will only operate on an instance that is in the "db-seeder" Layer. 上面的配方仅适用于“ db-seeder”层中的实例。 Add this custom recipe to the "deploy" event for the PHP app layer. 将此自定义配方添加到PHP应用程序层的“ deploy”事件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使php脚本运行特定命令 - How to make a php script run a specific command 如何在PHP中以特定用户身份运行外部命令 - How to run an external command as a specific user in PHP 如何在页面加载时运行 ajax jquery 的第一个实例 - How to run a first instance of ajax jquery when page loads 如何仅获取条件为真的数组的第一个实例 - How to get only the first instance of an array that is true of a condition 仅替换子字符串的第一个实例 - replace only the first instance of a substring AWS OpsWorks应用程序层:无法从PHP应用程序访问环境变量 - AWS OpsWorks app layer: environment variables not accessible from PHP app 如何仅在PHP中获取第一行的特定列数据SQL? - How to get specific column data SQL for first row only in PHP? 如何使PHP函数仅在首次触发后运行? - How to make PHP function run only after triggered first time? Bash脚本来选择一个文件并运行它的第一个实例 - Bash Script to select a file and run the first instance of it 当仅在命令行上调用一次PHP脚本时,它将执行多次。 仅首次运行的输出发送到stdout - PHP script is executing multiple times when only invoked once on the command line. Only output of first run is sent to stdout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM