From 6a436195161cdc109164b39efefa2a0bfc35ad62 Mon Sep 17 00:00:00 2001 From: Taku Nakajima Date: Wed, 28 Feb 2024 15:10:13 +0900 Subject: [PATCH 1/3] Add priority to hook --- app/models/district.rb | 5 +++- ...20240228032657_add_hook_order_to_plugin.rb | 5 ++++ db/schema.rb | 3 +- spec/models/district_spec.rb | 30 +++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20240228032657_add_hook_order_to_plugin.rb diff --git a/app/models/district.rb b/app/models/district.rb index 92e15101..098d3893 100644 --- a/app/models/district.rb +++ b/app/models/district.rb @@ -149,7 +149,10 @@ def container_instances end def hook_plugins(trigger, origin, arg = nil) - plugins.reverse.reduce(arg) do |a, plugin| + # call plugin by priority + # for same priority, by the reverse order of the registered + i = 0 + plugins.sort_by {|p| [p.hook_priority, i -= 1] }.reduce(arg) do |a, plugin| plugin.hook(trigger, origin, a) end end diff --git a/db/migrate/20240228032657_add_hook_order_to_plugin.rb b/db/migrate/20240228032657_add_hook_order_to_plugin.rb new file mode 100644 index 00000000..6e209947 --- /dev/null +++ b/db/migrate/20240228032657_add_hook_order_to_plugin.rb @@ -0,0 +1,5 @@ +class AddHookOrderToPlugin < ActiveRecord::Migration[5.2] + def change + add_column :plugins, :hook_priority, :integer, :default => 10 + end +end diff --git a/db/schema.rb b/db/schema.rb index 355dc116..c560a0bf 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_12_08_045503) do +ActiveRecord::Schema.define(version: 2024_02_28_032657) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -134,6 +134,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.text "plugin_attributes" + t.integer "hook_priority", default: 10 t.index ["district_id"], name: "index_plugins_on_district_id" end diff --git a/spec/models/district_spec.rb b/spec/models/district_spec.rb index 5c0fab47..d1c2b5d9 100644 --- a/spec/models/district_spec.rb +++ b/spec/models/district_spec.rb @@ -53,6 +53,36 @@ end end + describe "#hook_plugins" do + it "calls hook by the revserse of the registered order" do + user_data = InstanceUserData.new + district.save! + district.plugins.create(name: 'secure_instance') + district.plugins.create(name: 'datadog', plugin_attributes: { "api_key": 'abcdefg'}) + district.plugins.create(name: 'itamae', plugin_attributes: { "recipe_url": "s3://barcelona-district1-12345/itamae_recipes/recipe.tar.gz"}) + user_data = district.hook_plugins(:container_instance_user_data, self, user_data) + user_data_hash = YAML.load(Base64.decode64(user_data.build)) + + expect(user_data_hash['runcmd'].first).to include('ruby') # itamae + expect(user_data_hash['runcmd'][5]).to include('datadog') # datadog + expect(user_data_hash['runcmd'].last).to include('tmout.sh') # secure_instance + end + + it "calls hook by the specified order" do + user_data = InstanceUserData.new + district.save! + district.plugins.create(name: 'secure_instance') + district.plugins.create(name: 'datadog', plugin_attributes: { "api_key": 'abcdefg'}, 'hook_priority': 20) + district.plugins.create(name: 'itamae', plugin_attributes: { "recipe_url": "s3://barcelona-district1-12345/itamae_recipes/recipe.tar.gz"}) + user_data = district.hook_plugins(:container_instance_user_data, self, user_data) + user_data_hash = YAML.load(Base64.decode64(user_data.build)) + + expect(user_data_hash['runcmd'].first).to include('ruby') # itamae + expect(user_data_hash['runcmd'][6]).to include('clamav') # secure_instance + expect(user_data_hash['runcmd'].last).to include('datadog') # datadog + end + end + describe "#subnets" do before do allow(district.aws).to receive(:ec2) { ec2_mock } From 3cb09ed1dac6fa120dfd0c6bf0629b1309d32aa8 Mon Sep 17 00:00:00 2001 From: Taku Nakajima Date: Wed, 28 Feb 2024 15:12:01 +0900 Subject: [PATCH 2/3] Change instance type of bastion --- lib/barcelona/network/bastion_builder.rb | 2 +- spec/lib/barcelona/network/network_stack_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/barcelona/network/bastion_builder.rb b/lib/barcelona/network/bastion_builder.rb index b4653876..54ace022 100644 --- a/lib/barcelona/network/bastion_builder.rb +++ b/lib/barcelona/network/bastion_builder.rb @@ -100,7 +100,7 @@ def build_resources add_resource("AWS::AutoScaling::LaunchConfiguration", "BastionLaunchConfiguration") do |j| j.IamInstanceProfile ref("BastionProfile") j.ImageId AMI_IDS[district.region] - j.InstanceType "t3.micro" + j.InstanceType "t3.small" j.SecurityGroups [ref("SecurityGroupBastion")] j.AssociatePublicIpAddress true j.UserData user_data diff --git a/spec/lib/barcelona/network/network_stack_spec.rb b/spec/lib/barcelona/network/network_stack_spec.rb index 4a374d90..c19da817 100644 --- a/spec/lib/barcelona/network/network_stack_spec.rb +++ b/spec/lib/barcelona/network/network_stack_spec.rb @@ -374,7 +374,7 @@ "BastionLaunchConfiguration" => { "Type" => "AWS::AutoScaling::LaunchConfiguration", "Properties" => { - "InstanceType" => "t3.micro", + "InstanceType" => "t3.small", "MetadataOptions"=>{"HttpTokens"=>"required"}, "IamInstanceProfile" => {"Ref" => "BastionProfile"}, "ImageId" => kind_of(String), From 24e99547c9fd30972ac11d78c767a54a658ac4b8 Mon Sep 17 00:00:00 2001 From: Taku Nakajima Date: Wed, 28 Feb 2024 17:43:49 +0900 Subject: [PATCH 3/3] Make hook_priority as a plugin_attribute --- app/models/plugin.rb | 5 +++++ .../20240228032657_add_hook_order_to_plugin.rb | 5 ----- db/schema.rb | 3 +-- lib/barcelona/plugins/datadog_plugin.rb | 4 ++++ spec/models/district_spec.rb | 2 +- spec/models/plugin_spec.rb | 16 ++++++++++++++++ 6 files changed, 27 insertions(+), 8 deletions(-) delete mode 100644 db/migrate/20240228032657_add_hook_order_to_plugin.rb diff --git a/app/models/plugin.rb b/app/models/plugin.rb index f0b8c223..5857f968 100644 --- a/app/models/plugin.rb +++ b/app/models/plugin.rb @@ -30,6 +30,11 @@ def plugin klass.new(self) end + def hook_priority + attributes = self.plugin_attributes || {} + attributes['hook_priority'].to_i + end + private def default_attributes diff --git a/db/migrate/20240228032657_add_hook_order_to_plugin.rb b/db/migrate/20240228032657_add_hook_order_to_plugin.rb deleted file mode 100644 index 6e209947..00000000 --- a/db/migrate/20240228032657_add_hook_order_to_plugin.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddHookOrderToPlugin < ActiveRecord::Migration[5.2] - def change - add_column :plugins, :hook_priority, :integer, :default => 10 - end -end diff --git a/db/schema.rb b/db/schema.rb index c560a0bf..355dc116 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2024_02_28_032657) do +ActiveRecord::Schema.define(version: 2022_12_08_045503) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -134,7 +134,6 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.text "plugin_attributes" - t.integer "hook_priority", default: 10 t.index ["district_id"], name: "index_plugins_on_district_id" end diff --git a/lib/barcelona/plugins/datadog_plugin.rb b/lib/barcelona/plugins/datadog_plugin.rb index c433f2af..927692eb 100644 --- a/lib/barcelona/plugins/datadog_plugin.rb +++ b/lib/barcelona/plugins/datadog_plugin.rb @@ -1,6 +1,10 @@ module Barcelona module Plugins class DatadogPlugin < Base + # This plugin must be the last of the instalation order + # Usage sample: + # bcn district put-plugin -a api_key=8e53.... -a hook_priority=10 ec-staging datadog + def on_container_instance_user_data(_instance, user_data) add_files!(user_data) user_data.run_commands += [ diff --git a/spec/models/district_spec.rb b/spec/models/district_spec.rb index d1c2b5d9..37fc9e29 100644 --- a/spec/models/district_spec.rb +++ b/spec/models/district_spec.rb @@ -72,7 +72,7 @@ user_data = InstanceUserData.new district.save! district.plugins.create(name: 'secure_instance') - district.plugins.create(name: 'datadog', plugin_attributes: { "api_key": 'abcdefg'}, 'hook_priority': 20) + district.plugins.create(name: 'datadog', plugin_attributes: { "api_key": 'abcdefg', "hook_priority": 10}) district.plugins.create(name: 'itamae', plugin_attributes: { "recipe_url": "s3://barcelona-district1-12345/itamae_recipes/recipe.tar.gz"}) user_data = district.hook_plugins(:container_instance_user_data, self, user_data) user_data_hash = YAML.load(Base64.decode64(user_data.build)) diff --git a/spec/models/plugin_spec.rb b/spec/models/plugin_spec.rb index ffbba26c..b50fb551 100644 --- a/spec/models/plugin_spec.rb +++ b/spec/models/plugin_spec.rb @@ -32,4 +32,20 @@ end end end + + describe "#hook_priority" do + context "when not specified" do + let(:plugin) { Plugin.new(name: 'test') } + it "should be zero" do + expect(plugin.hook_priority).to eq(0) + end + end + + context "when specified" do + let(:plugin) { Plugin.new(name: 'test', plugin_attributes:{ "api_key": 'abcdefg', hook_priority: '10'}) } + it "should be the specified value" do + expect(plugin.hook_priority).to eq(10) + end + end + end end