From b7a20403c27dfb7a47d0aa6e06753ce74190c891 Mon Sep 17 00:00:00 2001 From: Ryo Takaishi Date: Wed, 6 Sep 2023 23:04:16 +0900 Subject: [PATCH 1/9] =?UTF-8?q?MediaPackageV2=E3=83=AA=E3=82=BD=E3=83=BC?= =?UTF-8?q?=E3=82=B9=E3=82=92=E6=89=B1=E3=81=88=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 1 + Gemfile.lock | 12 +- app/helpers/media_package_v2_helper.rb | 44 +++++++ .../create_streaming_aws_resources_job.rb | 26 +++++ .../delete_streaming_aws_resources_job.rb | 23 ++++ app/models/media_package_v2_channel.rb | 77 +++++++++++++ app/models/media_package_v2_channel_group.rb | 53 +++++++++ .../media_package_v2_origin_endpoint.rb | 109 ++++++++++++++++++ app/models/streaming.rb | 7 ++ app/views/admin/streamings/index.html.erb | 50 ++++++++ .../20230906132249_create_media_package_v2.rb | 30 +++++ db/schema.rb | 30 ++++- spec/factories/media_package_v2_channel.rb | 26 +++++ .../media_package_v2_channel_group.rb | 26 +++++ .../media_package_v2_origin_endpoint.rb | 26 +++++ spec/requests/admin/streamss_spec.rb | 11 +- 16 files changed, 545 insertions(+), 6 deletions(-) create mode 100644 app/helpers/media_package_v2_helper.rb create mode 100644 app/models/media_package_v2_channel.rb create mode 100644 app/models/media_package_v2_channel_group.rb create mode 100644 app/models/media_package_v2_origin_endpoint.rb create mode 100644 db/migrate/20230906132249_create_media_package_v2.rb create mode 100644 spec/factories/media_package_v2_channel.rb create mode 100644 spec/factories/media_package_v2_channel_group.rb create mode 100644 spec/factories/media_package_v2_origin_endpoint.rb diff --git a/Gemfile b/Gemfile index e99a7983c..2b84cde4a 100644 --- a/Gemfile +++ b/Gemfile @@ -112,6 +112,7 @@ gem 'psych', '< 6' gem "aws-sdk-ivs" gem "aws-sdk-medialive" gem "aws-sdk-mediapackage" +gem "aws-sdk-mediapackagev2" gem "aws-sdk-ssm" gem 'active_hash' diff --git a/Gemfile.lock b/Gemfile.lock index 5fba89d5a..371ad6538 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -84,13 +84,13 @@ GEM rake (>= 10.4, < 14.0) ast (2.4.2) aws-eventstream (1.2.0) - aws-partitions (1.628.0) + aws-partitions (1.817.0) aws-record (2.7.0) aws-sdk-dynamodb (~> 1.18) - aws-sdk-core (3.144.0) + aws-sdk-core (3.181.0) aws-eventstream (~> 1, >= 1.0.2) - aws-partitions (~> 1, >= 1.525.0) - aws-sigv4 (~> 1.1) + aws-partitions (~> 1, >= 1.651.0) + aws-sigv4 (~> 1.5) jmespath (~> 1, >= 1.6.1) aws-sdk-dynamodb (1.76.0) aws-sdk-core (~> 3, >= 3.127.0) @@ -107,6 +107,9 @@ GEM aws-sdk-mediapackage (1.56.0) aws-sdk-core (~> 3, >= 3.127.0) aws-sigv4 (~> 1.1) + aws-sdk-mediapackagev2 (1.5.0) + aws-sdk-core (~> 3, >= 3.177.0) + aws-sigv4 (~> 1.1) aws-sdk-rails (3.6.3) aws-record (~> 2) aws-sdk-ses (~> 1) @@ -702,6 +705,7 @@ DEPENDENCIES aws-sdk-ivs aws-sdk-medialive aws-sdk-mediapackage + aws-sdk-mediapackagev2 aws-sdk-rails aws-sdk-s3 (~> 1.14) aws-sdk-ssm diff --git a/app/helpers/media_package_v2_helper.rb b/app/helpers/media_package_v2_helper.rb new file mode 100644 index 000000000..c8c9c7c29 --- /dev/null +++ b/app/helpers/media_package_v2_helper.rb @@ -0,0 +1,44 @@ +require 'aws-sdk-mediapackagev2' + +module MediaPackageV2Helper + def media_package_v2_client + creds = Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']) + @client ||= if creds.set? + Aws::MediaPackageV2::Client.new(region: AWS_LIVE_STREAM_REGION, credentials: creds) + else + Aws::MediaPackageV2::Client.new(region: AWS_LIVE_STREAM_REGION) + end + end + + def channel_group_name + conference = streaming.conference + + if review_app? + "review_app_#{review_app_number}_#{conference.abbr}" + else + "#{env_name}_#{conference.abbr}" + end + end + + def channel_name + conference = streaming.conference + track = streaming.track + + if review_app? + "review_app_#{review_app_number}_#{conference.abbr}_track#{track.name}" + else + "#{env_name}_#{conference.abbr}_track#{track.name}" + end + end + + def origin_endpoint_name + conference = streaming.conference + track = streaming.track + + if review_app? + "review_app_#{review_app_number}_#{conference.abbr}_track#{track.name}" + else + "#{env_name}_#{conference.abbr}_track#{track.name}" + end + end +end diff --git a/app/jobs/create_streaming_aws_resources_job.rb b/app/jobs/create_streaming_aws_resources_job.rb index 499beaa11..9fc2e0301 100644 --- a/app/jobs/create_streaming_aws_resources_job.rb +++ b/app/jobs/create_streaming_aws_resources_job.rb @@ -1,14 +1,23 @@ class CreateStreamingAwsResourcesJob < ApplicationJob include EnvHelper + include MediaPackageV2Helper include LogoutHelper # queue_as :default self.queue_adapter = :async + attr_reader :conference + attr_reader :track + attr_reader :streaming + def perform(*args) # Rails.logger.level = Logger::DEBUG logger.info('Perform CreateMediaPackageV2Job') @streaming = args[0] + @conference = @streaming.conference + @track = @streaming.track + + create_media_package_v2_resources @streaming.update!(status: 'created') rescue => e @@ -16,4 +25,21 @@ def perform(*args) logger.error(e.message) logger.error(e.backtrace.join("\n")) end + + def create_media_package_v2_resources + logger.info('Perform CreateMediaPackageV2Job') + + + channel_group = MediaPackageV2ChannelGroup.find_or_create_by(streaming_id: @streaming.id, name: channel_group_name) + logger.info("channel group: #{channel_group}") + channel_group.create_aws_resource + + channel = MediaPackageV2Channel.find_or_create_by(streaming_id: @streaming.id, media_package_v2_channel_group_id: channel_group.id, name: channel_name) + logger.info("channel: #{channel}") + channel.create_aws_resource + + origin_endpoint = MediaPackageV2OriginEndpoint.find_or_create_by(streaming_id: @streaming.id, media_package_v2_channel_id: channel.id, name: origin_endpoint_name) + logger.info("origin endpoint: #{origin_endpoint}") + origin_endpoint.create_aws_resource + end end diff --git a/app/jobs/delete_streaming_aws_resources_job.rb b/app/jobs/delete_streaming_aws_resources_job.rb index 9a9c50e2b..19c895b02 100644 --- a/app/jobs/delete_streaming_aws_resources_job.rb +++ b/app/jobs/delete_streaming_aws_resources_job.rb @@ -10,9 +10,32 @@ def perform(*args) logger.info('Perform DeleteStreamingAwsResourcesJob') @streaming = args[0] + delete_media_package_v2_resources + @streaming.update!(status: 'deleted') rescue => e logger.error(e.message) logger.error(e.backtrace.join("\n")) end + + def delete_media_package_v2_resources + logger.info('Deleting MediaPackageV2 resources...') + + if @streaming.media_package_v2_origin_endpoint + @streaming.media_package_v2_origin_endpoint.delete_aws_resource + @streaming.media_package_v2_origin_endpoint.destroy! + end + + if @streaming.media_package_v2_channel + @streaming.media_package_v2_channel.delete_aws_resource + @streaming.media_package_v2_channel.destroy! + end + + if @streaming.media_package_v2_channel_group + @streaming.media_package_v2_channel_group.delete_aws_resource + @streaming.media_package_v2_channel_group.destroy! + end + + logger.info('Deleted MediaPackageV2 resources...') + end end diff --git a/app/models/media_package_v2_channel.rb b/app/models/media_package_v2_channel.rb new file mode 100644 index 000000000..46fa751bd --- /dev/null +++ b/app/models/media_package_v2_channel.rb @@ -0,0 +1,77 @@ +# == Schema Information +# +# Table name: media_package_v2_channels +# +# id :string(255) not null, primary key +# name :string(255) +# media_package_v2_channel_group_id :string(255) +# streaming_id :string(255) not null +# +# Indexes +# +# index_channels_on_channel_group_id (media_package_v2_channel_group_id) +# index_media_package_v2_channels_on_name (name) UNIQUE +# index_media_package_v2_channels_on_streaming_id (streaming_id) +# +# Foreign Keys +# +# fk_rails_... (streaming_id => streamings.id) +# + +require 'aws-sdk-mediapackagev2' + +class MediaPackageV2Channel < ApplicationRecord + include EnvHelper + include MediaPackageV2Helper + + before_create :set_uuid + + belongs_to :streaming + belongs_to :channel_group, class_name: 'MediaPackageV2ChannelGroup', foreign_key: :media_package_v2_channel_group_id + has_one :origin_endpoint, class_name: 'MediaPackageV2OriginEndpoint' + + def create_aws_resource + unless exists_aws_resource? + resp = media_package_v2_client.create_channel(channel_group_name:, channel_name:) + update!(name: resp.channel_name) + end + end + + def delete_aws_resource + media_package_v2_client.delete_channel(channel_group_name:, channel_name:) if exists_aws_resource? + loop do + break unless exists_aws_resource? + end + update!(name: '') + end + + def exists_aws_resource? + media_package_v2_client.get_channel(channel_group_name:, channel_name:) + true + rescue Aws::MediaPackageV2::Errors::NotFoundException + false + rescue => e + logger.error(e.message) + false + end + + def channel + @channel ||= media_package_v2_client.get_channel(channel_group_name:, channel_name:) + rescue => e + logger.error(e.message.to_s) + end + + def ingest_endpoint_url + channel.ingest_endpoints[0].url + end + + private + + def conference + streaming.conference + end + + def track + streaming.track + end +end diff --git a/app/models/media_package_v2_channel_group.rb b/app/models/media_package_v2_channel_group.rb new file mode 100644 index 000000000..c382c005f --- /dev/null +++ b/app/models/media_package_v2_channel_group.rb @@ -0,0 +1,53 @@ +# == Schema Information +# +# Table name: media_package_v2_channel_groups +# +# id :string(255) not null, primary key +# name :string(255) +# streaming_id :string(255) not null +# +# Indexes +# +# index_media_package_v2_channel_groups_on_name (name) UNIQUE +# index_media_package_v2_channel_groups_on_streaming_id (streaming_id) +# +# Foreign Keys +# +# fk_rails_... (streaming_id => streamings.id) +# +require 'aws-sdk-mediapackagev2' + +class MediaPackageV2ChannelGroup < ApplicationRecord + include EnvHelper + include MediaPackageV2Helper + + before_create :set_uuid + + belongs_to :streaming + has_one :channel, class_name: 'MediaPackageV2Channel' + + def create_aws_resource + unless exists_aws_resource? + resp = media_package_v2_client.create_channel_group(channel_group_name:) + update!(name: resp.channel_group_name) + end + end + + def delete_aws_resource + media_package_v2_client.delete_channel_group(channel_group_name:) if exists_aws_resource? + loop do + break unless exists_aws_resource? + end + update!(name: '') + end + + def exists_aws_resource? + media_package_v2_client.get_channel_group(channel_group_name:) + true + rescue Aws::MediaPackageV2::Errors::NotFoundException + false + rescue => e + logger.error(e.message) + false + end +end diff --git a/app/models/media_package_v2_origin_endpoint.rb b/app/models/media_package_v2_origin_endpoint.rb new file mode 100644 index 000000000..f01a98630 --- /dev/null +++ b/app/models/media_package_v2_origin_endpoint.rb @@ -0,0 +1,109 @@ +# == Schema Information +# +# Table name: media_package_v2_origin_endpoints +# +# id :string(255) not null, primary key +# name :string(255) +# media_package_v2_channel_id :string(255) +# streaming_id :string(255) not null +# +# Indexes +# +# index_media_package_v2_origin_endpoints_on_name (name) UNIQUE +# index_media_package_v2_origin_endpoints_on_streaming_id (streaming_id) +# index_origin_endpoints_on_channel_id (media_package_v2_channel_id) +# +# Foreign Keys +# +# fk_rails_... (streaming_id => streamings.id) +# +require 'aws-sdk-mediapackagev2' + +class MediaPackageV2OriginEndpoint < ApplicationRecord + include EnvHelper + include MediaPackageV2Helper + + before_create :set_uuid + + belongs_to :streaming + belongs_to :channel, class_name: 'MediaPackageV2Channel', foreign_key: :media_package_v2_channel_id + + def create_aws_resource + unless exists_aws_resource? + resp = media_package_v2_client.create_origin_endpoint( + { + channel_group_name:, + channel_name:, + origin_endpoint_name:, + container_type: 'TS', + segment: { + segment_duration_seconds: 1, + segment_name: 'segment', + ts_use_audio_rendition_group: false, + include_iframe_only_streams: false, + ts_include_dvb_subtitles: false + }, + startover_window_seconds: 60, + low_latency_hls_manifests: [ + { + manifest_name: 'll-hls-index', + manifest_window_seconds: 30, + program_date_time_interval_seconds: 1 + } + ] + } + ) + media_package_v2_client.put_origin_endpoint_policy( + { + channel_group_name:, + channel_name:, + origin_endpoint_name:, + policy: <<~EOS + { + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "AllowUser", + "Effect": "Allow", + "Principal": "*", + "Action": "mediapackagev2:GetObject", + "Resource": "arn:aws:mediapackagev2:us-east-1:607167088920:channelGroup/#{channel_group_name}/channel/#{channel_name}/originEndpoint/#{origin_endpoint_name}" + } + ] + } + EOS + } + ) + update!(name: resp.origin_endpoint_name) + end + end + + def exists_aws_resource? + media_package_v2_client.get_origin_endpoint(channel_group_name:, channel_name:, origin_endpoint_name:) + true + rescue Aws::MediaPackageV2::Errors::NotFoundException + false + rescue => e + logger.error(e.message) + false + end + + def delete_aws_resource + media_package_v2_client.delete_origin_endpoint_policy(channel_group_name:, channel_name:, origin_endpoint_name:) + media_package_v2_client.delete_origin_endpoint(channel_group_name:, channel_name:, origin_endpoint_name:) + loop do + break unless exists_aws_resource? + end + update!(name: '') + end + + def aws_resource + @aws_resource = media_package_v2_client.get_origin_endpoint(channel_group_name:, channel_name:, origin_endpoint_name:) + end + + def playback_url + aws_resource&.low_latency_hls_manifests&.first&.url + rescue Aws::MediaPackageV2::Errors::NotFoundException + '' + end +end diff --git a/app/models/streaming.rb b/app/models/streaming.rb index fe42c83fb..54e8836f5 100644 --- a/app/models/streaming.rb +++ b/app/models/streaming.rb @@ -26,9 +26,16 @@ class Streaming < ApplicationRecord belongs_to :conference belongs_to :track + has_one :media_package_v2_channel_group + has_one :media_package_v2_channel + has_one :media_package_v2_origin_endpoint STATUS_CREATING = 'creating'.freeze STATUS_CRTEATED = 'created'.freeze STATUS_DELETING = 'deleting'.freeze STATUS_DELETED = 'deleted'.freeze + + def playback_url + media_package_v2_origin_endpoint&.playback_url + end end diff --git a/app/views/admin/streamings/index.html.erb b/app/views/admin/streamings/index.html.erb index 43c161621..abb16779a 100644 --- a/app/views/admin/streamings/index.html.erb +++ b/app/views/admin/streamings/index.html.erb @@ -56,4 +56,54 @@ + +
+
+

配信用URL一覧

+
+
+ +
+
+ + + + + + + + + <% @tracks.each do |track| %> + + + + + <% end %> + +
Track Name (ID)Playback URL
<%= track.name %> (<%= track.id %>)<%= track.streaming&.playback_url %>
+
+
+ + <% @tracks.each do |track| %> +
+
+

Track <%= track.name %>: リソース詳細

+
+
+ +
+
+ + + + + + + + + +
Track ID<%= track.id %>
MediaPackage V2 ChannelGroup<%= track.streaming&.media_package_v2_channel_group&.name %>
+
+
+ <% end %> <% end %> diff --git a/db/migrate/20230906132249_create_media_package_v2.rb b/db/migrate/20230906132249_create_media_package_v2.rb new file mode 100644 index 000000000..f7a69ca18 --- /dev/null +++ b/db/migrate/20230906132249_create_media_package_v2.rb @@ -0,0 +1,30 @@ +class CreateMediaPackageV2 < ActiveRecord::Migration[7.0] + def up + create_table :media_package_v2_channel_groups, id: :string do |t| + t.belongs_to :streaming, null: false, foreign_key: true, type: :string + t.string :name, null: true + t.index :name, unique: true + end unless ActiveRecord::Base.connection.table_exists?(:media_package_v2_channel_groups) + + create_table :media_package_v2_channels, id: :string do |t| + t.belongs_to :streaming, null: false, foreign_key: true, type: :string + t.belongs_to :media_package_v2_channel_group, null: true, type: :string, index: {name: 'index_channels_on_channel_group_id'} + + t.string :name, null: true + t.index :name, unique: true + end unless ActiveRecord::Base.connection.table_exists?(:media_package_v2_channels) + + create_table :media_package_v2_origin_endpoints, id: :string do |t| + t.belongs_to :streaming, null: false, foreign_key: true, type: :string + t.belongs_to :media_package_v2_channel, null: true, type: :string, index: {name: 'index_origin_endpoints_on_channel_id'} + t.string :name, null: true + t.index :name, unique: true + end unless ActiveRecord::Base.connection.table_exists?(:media_package_v2_origin_endpoints) + end + + def down + drop_table :media_package_v2_origin_endpoints + drop_table :media_package_v2_channels + drop_table :media_package_v2_channel_groups + end +end diff --git a/db/schema.rb b/db/schema.rb index 34554059c..0711b1d93 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[7.0].define(version: 2023_06_27_103419) do +ActiveRecord::Schema[7.0].define(version: 2023_09_06_132249) do create_table "access_logs", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "name" t.string "sub" @@ -192,6 +192,31 @@ t.index ["media_package_channel_id"], name: "index_media_package_origin_endpoints_on_media_package_channel_id" end + create_table "media_package_v2_channel_groups", id: :string, charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| + t.string "streaming_id", null: false + t.string "name" + t.index ["name"], name: "index_media_package_v2_channel_groups_on_name", unique: true + t.index ["streaming_id"], name: "index_media_package_v2_channel_groups_on_streaming_id" + end + + create_table "media_package_v2_channels", id: :string, charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| + t.string "streaming_id", null: false + t.string "media_package_v2_channel_group_id" + t.string "name" + t.index ["media_package_v2_channel_group_id"], name: "index_channels_on_channel_group_id" + t.index ["name"], name: "index_media_package_v2_channels_on_name", unique: true + t.index ["streaming_id"], name: "index_media_package_v2_channels_on_streaming_id" + end + + create_table "media_package_v2_origin_endpoints", id: :string, charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| + t.string "streaming_id", null: false + t.string "media_package_v2_channel_id" + t.string "name" + t.index ["media_package_v2_channel_id"], name: "index_origin_endpoints_on_channel_id" + t.index ["name"], name: "index_media_package_v2_origin_endpoints_on_name", unique: true + t.index ["streaming_id"], name: "index_media_package_v2_origin_endpoints_on_streaming_id" + end + create_table "messages", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "content" t.string "text" @@ -559,6 +584,9 @@ add_foreign_key "media_package_harvest_jobs", "talks" add_foreign_key "media_package_origin_endpoints", "conferences" add_foreign_key "media_package_origin_endpoints", "media_package_channels" + add_foreign_key "media_package_v2_channel_groups", "streamings" + add_foreign_key "media_package_v2_channels", "streamings" + add_foreign_key "media_package_v2_origin_endpoints", "streamings" add_foreign_key "orders", "profiles" add_foreign_key "orders_tickets", "orders" add_foreign_key "orders_tickets", "tickets" diff --git a/spec/factories/media_package_v2_channel.rb b/spec/factories/media_package_v2_channel.rb new file mode 100644 index 000000000..47b6a2e7f --- /dev/null +++ b/spec/factories/media_package_v2_channel.rb @@ -0,0 +1,26 @@ +# == Schema Information +# +# Table name: streamings +# +# id :string(255) not null, primary key +# status :string(255) not null +# created_at :datetime not null +# updated_at :datetime not null +# conference_id :bigint not null +# track_id :bigint not null +# +# Indexes +# +# index_streamings_on_conference_id (conference_id) +# index_streamings_on_conference_id_and_track_id (conference_id,track_id) UNIQUE +# index_streamings_on_track_id (track_id) +# +# Foreign Keys +# +# fk_rails_... (conference_id => conferences.id) +# fk_rails_... (track_id => tracks.id) +# + +FactoryBot.define do + factory :media_package_v2_channel, class: MediaPackageV2Channel +end diff --git a/spec/factories/media_package_v2_channel_group.rb b/spec/factories/media_package_v2_channel_group.rb new file mode 100644 index 000000000..7d48f4b9e --- /dev/null +++ b/spec/factories/media_package_v2_channel_group.rb @@ -0,0 +1,26 @@ +# == Schema Information +# +# Table name: streamings +# +# id :string(255) not null, primary key +# status :string(255) not null +# created_at :datetime not null +# updated_at :datetime not null +# conference_id :bigint not null +# track_id :bigint not null +# +# Indexes +# +# index_streamings_on_conference_id (conference_id) +# index_streamings_on_conference_id_and_track_id (conference_id,track_id) UNIQUE +# index_streamings_on_track_id (track_id) +# +# Foreign Keys +# +# fk_rails_... (conference_id => conferences.id) +# fk_rails_... (track_id => tracks.id) +# + +FactoryBot.define do + factory :media_package_v2_channel_group, class: MediaPackageV2ChannelGroup +end diff --git a/spec/factories/media_package_v2_origin_endpoint.rb b/spec/factories/media_package_v2_origin_endpoint.rb new file mode 100644 index 000000000..f73ed2445 --- /dev/null +++ b/spec/factories/media_package_v2_origin_endpoint.rb @@ -0,0 +1,26 @@ +# == Schema Information +# +# Table name: streamings +# +# id :string(255) not null, primary key +# status :string(255) not null +# created_at :datetime not null +# updated_at :datetime not null +# conference_id :bigint not null +# track_id :bigint not null +# +# Indexes +# +# index_streamings_on_conference_id (conference_id) +# index_streamings_on_conference_id_and_track_id (conference_id,track_id) UNIQUE +# index_streamings_on_track_id (track_id) +# +# Foreign Keys +# +# fk_rails_... (conference_id => conferences.id) +# fk_rails_... (track_id => tracks.id) +# + +FactoryBot.define do + factory :media_package_v2_origin_endpoint, class: MediaPackageV2OriginEndpoint +end diff --git a/spec/requests/admin/streamss_spec.rb b/spec/requests/admin/streamss_spec.rb index 62b6652b2..1951b1708 100644 --- a/spec/requests/admin/streamss_spec.rb +++ b/spec/requests/admin/streamss_spec.rb @@ -6,11 +6,19 @@ before do conference = create(:cndt2020) - create(:streaming, status: 'created', conference:, track: conference.tracks.first) + streaming = create(:streaming, status: 'created', conference:, track: conference.tracks.first) + channel_group = create(:media_package_v2_channel_group, streaming:) + channel = create(:media_package_v2_channel, streaming:, channel_group:) + create(:media_package_v2_origin_endpoint, streaming:, channel:) end context 'user logged in' do before do + Streaming.define_method(:original, Streaming.instance_method(:playback_url)) + allow_any_instance_of(Streaming).to(receive(:playback_url) do |*_arg| + 'https://example.cloudnativedays.jp/index.m3u8' + end) + ActionDispatch::Request::Session.define_method(:original, ActionDispatch::Request::Session.instance_method(:[])) allow_any_instance_of(ActionDispatch::Request::Session).to(receive(:[]) do |*arg| if arg[1] == :userinfo @@ -34,6 +42,7 @@ expect(response).to(have_http_status('200')) expect(response.body).to(include('created')) + expect(response.body).to(include('https://example.cloudnativedays.jp/index.m3u8')) end end end From 910fbcf6ea784ed5db641f298ffb4e08e21cfe21 Mon Sep 17 00:00:00 2001 From: Ryo Takaishi Date: Sat, 9 Sep 2023 13:09:10 +0900 Subject: [PATCH 2/9] =?UTF-8?q?MediaPackage=E3=83=AA=E3=82=BD=E3=83=BC?= =?UTF-8?q?=E3=82=B9=E3=82=92Streaming=E3=81=AE=E7=AE=A1=E7=90=86=E4=B8=8B?= =?UTF-8?q?=E3=81=AB=E7=A7=BB=E8=A1=8C=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/helpers/media_package_helper.rb | 11 ++++ app/jobs/create_media_package_job.rb | 4 +- .../create_streaming_aws_resources_job.rb | 17 +++++ app/jobs/delete_media_package_job.rb | 6 +- .../delete_streaming_aws_resources_job.rb | 23 +++++++ app/models/media_package_channel.rb | 57 +++++++--------- app/models/media_package_origin_endpoint.rb | 38 +++++------ app/models/media_package_parameter.rb | 65 +++++++++++++++++++ app/models/streaming.rb | 3 + app/models/track.rb | 1 - app/views/admin/streamings/index.html.erb | 3 + ...ming_id_columns_to_media_package_tables.rb | 31 +++++++++ db/schema.rb | 29 ++++++--- 13 files changed, 218 insertions(+), 70 deletions(-) create mode 100644 app/models/media_package_parameter.rb create mode 100644 db/migrate/20230909035236_add_streaming_id_columns_to_media_package_tables.rb diff --git a/app/helpers/media_package_helper.rb b/app/helpers/media_package_helper.rb index d8cc0d32b..b018d425a 100644 --- a/app/helpers/media_package_helper.rb +++ b/app/helpers/media_package_helper.rb @@ -22,4 +22,15 @@ def get_media_package_channels_from_aws(channel_ids = []) channels.select { |channel| channel_ids.include?(channel.id) } end + + def resource_name + conference = streaming.conference + track = streaming.track + + if review_app? + "review_app_#{review_app_number}_#{conference.abbr}_track#{track.name}" + else + "#{env_name}_#{conference.abbr}_track#{track.name}" + end + end end diff --git a/app/jobs/create_media_package_job.rb b/app/jobs/create_media_package_job.rb index 103ed5d38..923b36278 100644 --- a/app/jobs/create_media_package_job.rb +++ b/app/jobs/create_media_package_job.rb @@ -10,11 +10,11 @@ def perform(*args) channel = MediaPackageChannel.new(conference:, track:) logger.error("Failed to create MediaPackageChannel: #{channel.errors}") unless channel.save - channel.create_media_package_resources + channel.create_aws_resource endpoint = MediaPackageOriginEndpoint.new(conference:, media_package_channel: channel) logger.error("Failed to create MediaPackageChannel: #{endpoint.errors}") unless endpoint.save - endpoint.create_media_package_resources + endpoint.create_aws_resource rescue => e logger.error(e.message) end diff --git a/app/jobs/create_streaming_aws_resources_job.rb b/app/jobs/create_streaming_aws_resources_job.rb index 9fc2e0301..7dce296c6 100644 --- a/app/jobs/create_streaming_aws_resources_job.rb +++ b/app/jobs/create_streaming_aws_resources_job.rb @@ -18,6 +18,7 @@ def perform(*args) @track = @streaming.track create_media_package_v2_resources + create_media_package_resources @streaming.update!(status: 'created') rescue => e @@ -42,4 +43,20 @@ def create_media_package_v2_resources logger.info("origin endpoint: #{origin_endpoint}") origin_endpoint.create_aws_resource end + + def create_media_package_resources + logger.info('Perform CreateMediaPackageJob') + + channel = MediaPackageChannel.find_or_create_by(streaming_id: @streaming.id) + logger.info("channel: #{channel}") + channel.create_aws_resource + + parameter = MediaPackageParameter.find_or_create_by(streaming_id: @streaming.id, media_package_channel_id: channel.id) + logger.info("parameter: #{parameter}") + parameter.create_aws_resources + + endpoint = MediaPackageOriginEndpoint.find_or_create_by(streaming_id: @streaming.id, media_package_channel_id: channel.id) + logger.info("endpoint: #{endpoint}") + endpoint.create_aws_resource + end end diff --git a/app/jobs/delete_media_package_job.rb b/app/jobs/delete_media_package_job.rb index b1682bd9e..9dbd05721 100644 --- a/app/jobs/delete_media_package_job.rb +++ b/app/jobs/delete_media_package_job.rb @@ -7,8 +7,12 @@ class DeleteMediaPackageJob < ApplicationJob def perform(*args) logger.info('Perform DeleteMediaPackageJob') channel = args[0] - channel.media_package_origin_endpoints.each(&:destroy) + channel.media_package_origin_endpoints.each do |oe| + oe.delete_aws_resource + oe.destroy + end channel.media_package_harvest_jobs.each(&:destroy) + channel.delete_aws_resource channel.destroy rescue => e logger.error(e.message) diff --git a/app/jobs/delete_streaming_aws_resources_job.rb b/app/jobs/delete_streaming_aws_resources_job.rb index 19c895b02..4fa4f3db3 100644 --- a/app/jobs/delete_streaming_aws_resources_job.rb +++ b/app/jobs/delete_streaming_aws_resources_job.rb @@ -11,6 +11,7 @@ def perform(*args) @streaming = args[0] delete_media_package_v2_resources + delete_media_package_resources @streaming.update!(status: 'deleted') rescue => e @@ -38,4 +39,26 @@ def delete_media_package_v2_resources logger.info('Deleted MediaPackageV2 resources...') end + + def delete_media_package_resources + logger.info('Deleting MediaPackage resources...') + + if @streaming.media_package_origin_endpoint + @streaming.media_package_origin_endpoint.delete_aws_resource + @streaming.media_package_origin_endpoint.destroy! + end + + if @streaming.media_package_parameter + @streaming.media_package_parameter.delete_aws_resource + @streaming.media_package_parameter.destroy! + end + + if @streaming.media_package_channel + @streaming.media_package_channel.media_package_harvest_jobs&.each(&:destroy) + @streaming.media_package_channel.delete_aws_resource + @streaming.media_package_channel.destroy! + end + + logger.info('Deleted MediaPackage resources...') + end end diff --git a/app/models/media_package_channel.rb b/app/models/media_package_channel.rb index 45b376bef..318fff862 100644 --- a/app/models/media_package_channel.rb +++ b/app/models/media_package_channel.rb @@ -2,40 +2,30 @@ # # Table name: media_package_channels # -# id :bigint not null, primary key -# channel_id :string(255) default("") -# conference_id :bigint not null -# track_id :bigint not null +# id :bigint not null, primary key +# channel_id :string(255) default("") +# streaming_id :string(255) # # Indexes # -# index_media_package_channels_on_channel_id (channel_id) -# index_media_package_channels_on_conference_id (conference_id) -# index_media_package_channels_on_track_id (track_id) +# index_media_package_channels_on_channel_id (channel_id) +# index_media_package_channels_on_streaming_id (streaming_id) # # Foreign Keys # -# fk_rails_... (conference_id => conferences.id) -# fk_rails_... (track_id => tracks.id) +# fk_rails_... (streaming_id => streamings.id) # class MediaPackageChannel < ApplicationRecord include MediaPackageHelper include EnvHelper - belongs_to :conference - belongs_to :track + belongs_to :streaming has_many :media_package_origin_endpoints has_many :media_package_harvest_jobs - before_destroy do - delete_media_package_resources - rescue => e - logger.error(e.message) - end - def channel - @channel ||= media_package_client.describe_channel(id: channel_id) + @channel = media_package_client.describe_channel(id: channel_id) rescue => e logger.error(e.message.to_s) end @@ -56,18 +46,25 @@ def ingest_endpoint_password ingest_endpoints[0]['password'] end - def create_media_package_resources - resp = media_package_client.create_channel(create_channel_params) - update!(channel_id: resp.id) - rescue => e - logger.error(e.message) - delete_media_package_resources + def create_aws_resource + unless exists_aws_resource? + resp = media_package_client.create_channel(create_channel_params) + update!(channel_id: resp.id) + end end - def delete_media_package_resources - media_package_client.delete_channel(id: channel_id) + def exists_aws_resource? + media_package_client.describe_channel(id: channel_id) + true + rescue Aws::MediaPackage::Errors::NotFoundException + false rescue => e logger.error(e.message.to_s) + false + end + + def delete_aws_resource + media_package_client.delete_channel(id: channel_id) end private @@ -85,12 +82,4 @@ def tags tags['ReviewAppNumber'] = review_app_number.to_s if ENV['DREAMKAST_NAMESPACE'] tags end - - def resource_name - if review_app? - "review_app_#{review_app_number}_#{conference.abbr}_track#{track.name}" - else - "#{env_name}_#{conference.abbr}_track#{track.name}" - end - end end diff --git a/app/models/media_package_origin_endpoint.rb b/app/models/media_package_origin_endpoint.rb index d0a5eb26f..74b0fc021 100644 --- a/app/models/media_package_origin_endpoint.rb +++ b/app/models/media_package_origin_endpoint.rb @@ -3,50 +3,52 @@ # Table name: media_package_origin_endpoints # # id :bigint not null, primary key -# conference_id :bigint not null # endpoint_id :string(255) # media_package_channel_id :bigint not null +# streaming_id :string(255) # # Indexes # -# index_media_package_origin_endpoints_on_conference_id (conference_id) # index_media_package_origin_endpoints_on_media_package_channel_id (media_package_channel_id) +# index_media_package_origin_endpoints_on_streaming_id (streaming_id) # # Foreign Keys # -# fk_rails_... (conference_id => conferences.id) # fk_rails_... (media_package_channel_id => media_package_channels.id) +# fk_rails_... (streaming_id => streamings.id) # class MediaPackageOriginEndpoint < ApplicationRecord include MediaPackageHelper include EnvHelper - belongs_to :conference belongs_to :media_package_channel + belongs_to :streaming - before_destroy do - delete_media_package_resources - end def origin_endpoint - @origin_endpoint ||= media_package_client.describe_origin_endpoint(id: endpoint_id) + @origin_endpoint = media_package_client.describe_origin_endpoint(id: endpoint_id) rescue => e logger.error(e.message.to_s) end - def create_media_package_resources + def create_aws_resource resp = media_package_client.create_origin_endpoint(create_params) update!(endpoint_id: resp.id) - rescue => e - logger.error(e.message) - delete_media_package_resources end - def delete_media_package_resources - media_package_client.delete_origin_endpoint(id: endpoint_id) + def exists_aws_resource? + media_package_client.describe_origin_endpoint(id: endpoint_id) + true + rescue Aws::MediaPackage::Errors::NotFoundException + false rescue => e logger.error(e.message.to_s) + false + end + + def delete_aws_resource + media_package_client.delete_origin_endpoint(id: endpoint_id) end private @@ -84,12 +86,4 @@ def tags tags['ReviewAppNumber'] = review_app_number.to_s if ENV['DREAMKAST_NAMESPACE'] tags end - - def resource_name - if review_app? - "review_app_#{review_app_number}_#{conference.abbr}_track#{media_package_channel.track.name}" - else - "#{env_name}_#{conference.abbr}_track#{media_package_channel.track.name}" - end - end end diff --git a/app/models/media_package_parameter.rb b/app/models/media_package_parameter.rb new file mode 100644 index 000000000..ec768538d --- /dev/null +++ b/app/models/media_package_parameter.rb @@ -0,0 +1,65 @@ +# == Schema Information +# +# Table name: media_package_parameters +# +# id :string(255) not null, primary key +# name :string(255) +# created_at :datetime not null +# updated_at :datetime not null +# media_package_channel_id :bigint not null +# streaming_id :string(255) not null +# +# Indexes +# +# index_media_package_parameters_on_media_package_channel_id (media_package_channel_id) +# index_media_package_parameters_on_streaming_id (streaming_id) +# +# Foreign Keys +# +# fk_rails_... (media_package_channel_id => media_package_channels.id) +# fk_rails_... (streaming_id => streamings.id) +# +require 'aws-sdk-medialive' + +class MediaPackageParameter < ApplicationRecord + include MediaLiveHelper + include SsmHelper + include EnvHelper + + before_create :set_uuid + + belongs_to :streaming + belongs_to :media_package_channel + + def create_aws_resources + unless exists_aws_resource? + create_parameter("/medialive/#{resource_name}", media_package_channel.ingest_endpoint_password) + update!(name: "/medialive/#{resource_name}") + end + rescue => e + logger.error(e.message) + end + + def exists_aws_resource? + ssm_client.describe_parameters(filters: [{ key: 'Name', values: ["/medialive/#{resource_name}"] }]).parameters.any? + rescue => e + logger.error(e.message) + false + end + + def delete_aws_resource + delete_parameter("/medialive/#{resource_name}") + rescue => e + logger.error(e.message.to_s) + end + + private + + def resource_name + if review_app? + "review_app_#{review_app_number}_#{conference.abbr}_track#{track.name}" + else + "#{env_name}_#{conference.abbr}_track#{track.name}" + end + end +end diff --git a/app/models/streaming.rb b/app/models/streaming.rb index 54e8836f5..fad4e4cf2 100644 --- a/app/models/streaming.rb +++ b/app/models/streaming.rb @@ -29,6 +29,9 @@ class Streaming < ApplicationRecord has_one :media_package_v2_channel_group has_one :media_package_v2_channel has_one :media_package_v2_origin_endpoint + has_one :media_package_channel + has_one :media_package_origin_endpoint + has_one :media_package_parameter STATUS_CREATING = 'creating'.freeze STATUS_CRTEATED = 'created'.freeze diff --git a/app/models/track.rb b/app/models/track.rb index 74fdd27ea..f80a759d6 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -21,7 +21,6 @@ class Track < ApplicationRecord has_many :talks has_one :live_stream_ivs has_one :live_stream_media_live - has_one :media_package_channel has_one :streaming belongs_to :room, optional: true diff --git a/app/views/admin/streamings/index.html.erb b/app/views/admin/streamings/index.html.erb index abb16779a..57045944f 100644 --- a/app/views/admin/streamings/index.html.erb +++ b/app/views/admin/streamings/index.html.erb @@ -101,6 +101,9 @@ MediaPackage V2 ChannelGroup<%= track.streaming&.media_package_v2_channel_group&.name %> + + MediaPackage Channel<%= track.streaming&.media_package_channel&.channel_id %> + diff --git a/db/migrate/20230909035236_add_streaming_id_columns_to_media_package_tables.rb b/db/migrate/20230909035236_add_streaming_id_columns_to_media_package_tables.rb new file mode 100644 index 000000000..a6af63f78 --- /dev/null +++ b/db/migrate/20230909035236_add_streaming_id_columns_to_media_package_tables.rb @@ -0,0 +1,31 @@ +class AddStreamingIdColumnsToMediaPackageTables < ActiveRecord::Migration[7.0] + def up + create_table :media_package_parameters, id: :string do |t| + t.belongs_to :streaming, null: false, foreign_key: true, type: :string + t.belongs_to :media_package_channel, null: false, foreign_key: true, type: :bigint + + t.string :name, null: true + t.timestamps + end unless ActiveRecord::Base.connection.table_exists?(:media_package_parameters) + + add_belongs_to :media_package_channels, :streaming, null: true, foreign_key: true, type: :string + add_belongs_to :media_package_origin_endpoints, :streaming, null: true, foreign_key: true, type: :string + + remove_belongs_to :media_package_channels, :conference + remove_belongs_to :media_package_channels, :track + + remove_belongs_to :media_package_origin_endpoints, :conference + end + + def down + add_belongs_to :media_package_origin_endpoints, :conference, null: false, foreign_key: true, type: :bigint + + add_belongs_to :media_package_channels, :track, null: false, foreign_key: true, type: :bigint + add_belongs_to :media_package_channels, :conference, null: false, foreign_key: true, type: :bigint + + remove_belongs_to :media_package_channels, :streaming + remove_belongs_to :media_package_origin_endpoints, :streaming + + drop_table :media_package_parameters + end +end diff --git a/db/schema.rb b/db/schema.rb index 0711b1d93..347bbb35b 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[7.0].define(version: 2023_09_06_132249) do +ActiveRecord::Schema[7.0].define(version: 2023_09_09_035236) do create_table "access_logs", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "name" t.string "sub" @@ -163,12 +163,10 @@ end create_table "media_package_channels", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| - t.bigint "conference_id", null: false - t.bigint "track_id", null: false t.string "channel_id", default: "" + t.string "streaming_id" t.index ["channel_id"], name: "index_media_package_channels_on_channel_id" - t.index ["conference_id"], name: "index_media_package_channels_on_conference_id" - t.index ["track_id"], name: "index_media_package_channels_on_track_id" + t.index ["streaming_id"], name: "index_media_package_channels_on_streaming_id" end create_table "media_package_harvest_jobs", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| @@ -185,11 +183,21 @@ end create_table "media_package_origin_endpoints", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| - t.bigint "conference_id", null: false t.bigint "media_package_channel_id", null: false t.string "endpoint_id" - t.index ["conference_id"], name: "index_media_package_origin_endpoints_on_conference_id" + t.string "streaming_id" t.index ["media_package_channel_id"], name: "index_media_package_origin_endpoints_on_media_package_channel_id" + t.index ["streaming_id"], name: "index_media_package_origin_endpoints_on_streaming_id" + end + + create_table "media_package_parameters", id: :string, charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| + t.string "streaming_id", null: false + t.bigint "media_package_channel_id", null: false + t.string "name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["media_package_channel_id"], name: "index_media_package_parameters_on_media_package_channel_id" + t.index ["streaming_id"], name: "index_media_package_parameters_on_streaming_id" end create_table "media_package_v2_channel_groups", id: :string, charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| @@ -577,13 +585,14 @@ add_foreign_key "links", "conferences" add_foreign_key "live_streams", "conferences" add_foreign_key "live_streams", "tracks" - add_foreign_key "media_package_channels", "conferences" - add_foreign_key "media_package_channels", "tracks" + add_foreign_key "media_package_channels", "streamings" add_foreign_key "media_package_harvest_jobs", "conferences" add_foreign_key "media_package_harvest_jobs", "media_package_channels" add_foreign_key "media_package_harvest_jobs", "talks" - add_foreign_key "media_package_origin_endpoints", "conferences" add_foreign_key "media_package_origin_endpoints", "media_package_channels" + add_foreign_key "media_package_origin_endpoints", "streamings" + add_foreign_key "media_package_parameters", "media_package_channels" + add_foreign_key "media_package_parameters", "streamings" add_foreign_key "media_package_v2_channel_groups", "streamings" add_foreign_key "media_package_v2_channels", "streamings" add_foreign_key "media_package_v2_origin_endpoints", "streamings" From 83422efeb11b7497d370814ec48076be107f268a Mon Sep 17 00:00:00 2001 From: Ryo Takaishi Date: Sun, 10 Sep 2023 18:29:53 +0900 Subject: [PATCH 3/9] =?UTF-8?q?=E5=89=8A=E9=99=A4=E5=89=8D=E3=81=AB?= =?UTF-8?q?=E3=83=AA=E3=82=BD=E3=83=BC=E3=82=B9=E3=81=8C=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=81=8B=E3=83=81=E3=82=A7=E3=83=83=E3=82=AF?= =?UTF-8?q?=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/media_package_channel.rb | 2 +- app/models/media_package_origin_endpoint.rb | 2 +- app/models/media_package_parameter.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/media_package_channel.rb b/app/models/media_package_channel.rb index 318fff862..91e6b2817 100644 --- a/app/models/media_package_channel.rb +++ b/app/models/media_package_channel.rb @@ -64,7 +64,7 @@ def exists_aws_resource? end def delete_aws_resource - media_package_client.delete_channel(id: channel_id) + media_package_client.delete_channel(id: channel_id) if exists_aws_resource? end private diff --git a/app/models/media_package_origin_endpoint.rb b/app/models/media_package_origin_endpoint.rb index 74b0fc021..9c00ff032 100644 --- a/app/models/media_package_origin_endpoint.rb +++ b/app/models/media_package_origin_endpoint.rb @@ -48,7 +48,7 @@ def exists_aws_resource? end def delete_aws_resource - media_package_client.delete_origin_endpoint(id: endpoint_id) + media_package_client.delete_origin_endpoint(id: endpoint_id) if exists_aws_resource? end private diff --git a/app/models/media_package_parameter.rb b/app/models/media_package_parameter.rb index ec768538d..f76455249 100644 --- a/app/models/media_package_parameter.rb +++ b/app/models/media_package_parameter.rb @@ -48,7 +48,7 @@ def exists_aws_resource? end def delete_aws_resource - delete_parameter("/medialive/#{resource_name}") + delete_parameter("/medialive/#{resource_name}") if exists_aws_resource? rescue => e logger.error(e.message.to_s) end From 4a6480461d2135928a8eca9621610e933c22b5e5 Mon Sep 17 00:00:00 2001 From: Ryo Takaishi Date: Sun, 10 Sep 2023 18:32:06 +0900 Subject: [PATCH 4/9] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AA=E4=BE=8B?= =?UTF-8?q?=E5=A4=96=E3=81=AE=E6=8F=A1=E3=82=8A=E3=81=A4=E3=81=B6=E3=81=97?= =?UTF-8?q?=E3=82=92=E6=B6=88=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/media_package_channel.rb | 2 -- app/models/media_package_origin_endpoint.rb | 2 -- app/models/media_package_parameter.rb | 4 ---- 3 files changed, 8 deletions(-) diff --git a/app/models/media_package_channel.rb b/app/models/media_package_channel.rb index 91e6b2817..f071febb2 100644 --- a/app/models/media_package_channel.rb +++ b/app/models/media_package_channel.rb @@ -26,8 +26,6 @@ class MediaPackageChannel < ApplicationRecord def channel @channel = media_package_client.describe_channel(id: channel_id) - rescue => e - logger.error(e.message.to_s) end def ingest_endpoints diff --git a/app/models/media_package_origin_endpoint.rb b/app/models/media_package_origin_endpoint.rb index 9c00ff032..c91ee284b 100644 --- a/app/models/media_package_origin_endpoint.rb +++ b/app/models/media_package_origin_endpoint.rb @@ -28,8 +28,6 @@ class MediaPackageOriginEndpoint < ApplicationRecord def origin_endpoint @origin_endpoint = media_package_client.describe_origin_endpoint(id: endpoint_id) - rescue => e - logger.error(e.message.to_s) end def create_aws_resource diff --git a/app/models/media_package_parameter.rb b/app/models/media_package_parameter.rb index f76455249..d33766532 100644 --- a/app/models/media_package_parameter.rb +++ b/app/models/media_package_parameter.rb @@ -36,8 +36,6 @@ def create_aws_resources create_parameter("/medialive/#{resource_name}", media_package_channel.ingest_endpoint_password) update!(name: "/medialive/#{resource_name}") end - rescue => e - logger.error(e.message) end def exists_aws_resource? @@ -49,8 +47,6 @@ def exists_aws_resource? def delete_aws_resource delete_parameter("/medialive/#{resource_name}") if exists_aws_resource? - rescue => e - logger.error(e.message.to_s) end private From ebb61f8591b6cdf39d1f5a1a8ef44b7f177b5aa9 Mon Sep 17 00:00:00 2001 From: Ryo Takaishi Date: Sun, 10 Sep 2023 18:58:07 +0900 Subject: [PATCH 5/9] =?UTF-8?q?AWS=E3=83=AA=E3=82=BD=E3=83=BC=E3=82=B9?= =?UTF-8?q?=E3=81=8C=E3=81=82=E3=82=8B=E5=A0=B4=E5=90=88=E3=81=AE=E3=81=BF?= =?UTF-8?q?destroy!=E6=99=82=E3=81=ABAWS=E3=83=AA=E3=82=BD=E3=83=BC?= =?UTF-8?q?=E3=82=B9=E3=82=82=E6=B6=88=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../delete_streaming_aws_resources_job.rb | 19 ++++--------------- app/models/media_package_channel.rb | 4 ++++ app/models/media_package_origin_endpoint.rb | 4 ++++ app/models/media_package_parameter.rb | 4 ++++ 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/app/jobs/delete_streaming_aws_resources_job.rb b/app/jobs/delete_streaming_aws_resources_job.rb index 4fa4f3db3..21a596705 100644 --- a/app/jobs/delete_streaming_aws_resources_job.rb +++ b/app/jobs/delete_streaming_aws_resources_job.rb @@ -43,21 +43,10 @@ def delete_media_package_v2_resources def delete_media_package_resources logger.info('Deleting MediaPackage resources...') - if @streaming.media_package_origin_endpoint - @streaming.media_package_origin_endpoint.delete_aws_resource - @streaming.media_package_origin_endpoint.destroy! - end - - if @streaming.media_package_parameter - @streaming.media_package_parameter.delete_aws_resource - @streaming.media_package_parameter.destroy! - end - - if @streaming.media_package_channel - @streaming.media_package_channel.media_package_harvest_jobs&.each(&:destroy) - @streaming.media_package_channel.delete_aws_resource - @streaming.media_package_channel.destroy! - end + @streaming.media_package_origin_endpoint&.destroy! + @streaming.media_package_parameter&.destroy! + @streaming.media_package_channel&.media_package_harvest_jobs&.each(&:destroy) + @streaming.media_package_channel&.destroy! logger.info('Deleted MediaPackage resources...') end diff --git a/app/models/media_package_channel.rb b/app/models/media_package_channel.rb index f071febb2..56d06f2a5 100644 --- a/app/models/media_package_channel.rb +++ b/app/models/media_package_channel.rb @@ -20,6 +20,10 @@ class MediaPackageChannel < ApplicationRecord include MediaPackageHelper include EnvHelper + before_destroy do + delete_aws_resource + end + belongs_to :streaming has_many :media_package_origin_endpoints has_many :media_package_harvest_jobs diff --git a/app/models/media_package_origin_endpoint.rb b/app/models/media_package_origin_endpoint.rb index c91ee284b..cd91e3414 100644 --- a/app/models/media_package_origin_endpoint.rb +++ b/app/models/media_package_origin_endpoint.rb @@ -22,6 +22,10 @@ class MediaPackageOriginEndpoint < ApplicationRecord include MediaPackageHelper include EnvHelper + before_destroy do + delete_aws_resource + end + belongs_to :media_package_channel belongs_to :streaming diff --git a/app/models/media_package_parameter.rb b/app/models/media_package_parameter.rb index d33766532..e41d83e7f 100644 --- a/app/models/media_package_parameter.rb +++ b/app/models/media_package_parameter.rb @@ -26,6 +26,10 @@ class MediaPackageParameter < ApplicationRecord include SsmHelper include EnvHelper + before_destroy do + delete_aws_resource + end + before_create :set_uuid belongs_to :streaming From b35b98fa1e6d5c7e5e93638d7ccb4b783e3fe1c5 Mon Sep 17 00:00:00 2001 From: Ryo Takaishi Date: Sun, 10 Sep 2023 18:59:32 +0900 Subject: [PATCH 6/9] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AA=E4=BE=8B?= =?UTF-8?q?=E5=A4=96=E3=81=AE=E6=8F=A1=E3=82=8A=E3=81=A4=E3=81=B6=E3=81=97?= =?UTF-8?q?=E3=82=92=E3=82=84=E3=82=81=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/media_package_v2_channel.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/models/media_package_v2_channel.rb b/app/models/media_package_v2_channel.rb index 46fa751bd..8928c5ba2 100644 --- a/app/models/media_package_v2_channel.rb +++ b/app/models/media_package_v2_channel.rb @@ -57,8 +57,6 @@ def exists_aws_resource? def channel @channel ||= media_package_v2_client.get_channel(channel_group_name:, channel_name:) - rescue => e - logger.error(e.message.to_s) end def ingest_endpoint_url From 5e94fe21c04f39446a6dbf608966d97050d60e1d Mon Sep 17 00:00:00 2001 From: Ryo Takaishi Date: Sun, 10 Sep 2023 19:07:16 +0900 Subject: [PATCH 7/9] =?UTF-8?q?=E3=83=AC=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?=E5=89=8A=E9=99=A4=E6=99=82=E3=81=ABAWS=E3=83=AA=E3=82=BD?= =?UTF-8?q?=E3=83=BC=E3=82=B9=E3=82=82=E5=89=8A=E9=99=A4=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/delete_streaming_aws_resources_job.rb | 17 +++-------------- app/models/media_package_v2_channel.rb | 11 +++++++---- app/models/media_package_v2_channel_group.rb | 11 +++++++---- app/models/media_package_v2_origin_endpoint.rb | 11 +++++++---- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/app/jobs/delete_streaming_aws_resources_job.rb b/app/jobs/delete_streaming_aws_resources_job.rb index 19c895b02..8d378d040 100644 --- a/app/jobs/delete_streaming_aws_resources_job.rb +++ b/app/jobs/delete_streaming_aws_resources_job.rb @@ -21,20 +21,9 @@ def perform(*args) def delete_media_package_v2_resources logger.info('Deleting MediaPackageV2 resources...') - if @streaming.media_package_v2_origin_endpoint - @streaming.media_package_v2_origin_endpoint.delete_aws_resource - @streaming.media_package_v2_origin_endpoint.destroy! - end - - if @streaming.media_package_v2_channel - @streaming.media_package_v2_channel.delete_aws_resource - @streaming.media_package_v2_channel.destroy! - end - - if @streaming.media_package_v2_channel_group - @streaming.media_package_v2_channel_group.delete_aws_resource - @streaming.media_package_v2_channel_group.destroy! - end + @streaming.media_package_v2_origin_endpoint&.destroy! + @streaming.media_package_v2_channel&.destroy! + @streaming.media_package_v2_channel_group&.destroy! logger.info('Deleted MediaPackageV2 resources...') end diff --git a/app/models/media_package_v2_channel.rb b/app/models/media_package_v2_channel.rb index 8928c5ba2..7fffc9982 100644 --- a/app/models/media_package_v2_channel.rb +++ b/app/models/media_package_v2_channel.rb @@ -25,6 +25,7 @@ class MediaPackageV2Channel < ApplicationRecord include MediaPackageV2Helper before_create :set_uuid + before_destroy :delete_aws_resource belongs_to :streaming belongs_to :channel_group, class_name: 'MediaPackageV2ChannelGroup', foreign_key: :media_package_v2_channel_group_id @@ -38,11 +39,13 @@ def create_aws_resource end def delete_aws_resource - media_package_v2_client.delete_channel(channel_group_name:, channel_name:) if exists_aws_resource? - loop do - break unless exists_aws_resource? + if exists_aws_resource? + media_package_v2_client.delete_channel(channel_group_name:, channel_name:) + loop do + break unless exists_aws_resource? + end + update!(name: '') end - update!(name: '') end def exists_aws_resource? diff --git a/app/models/media_package_v2_channel_group.rb b/app/models/media_package_v2_channel_group.rb index c382c005f..2cd17440c 100644 --- a/app/models/media_package_v2_channel_group.rb +++ b/app/models/media_package_v2_channel_group.rb @@ -22,6 +22,7 @@ class MediaPackageV2ChannelGroup < ApplicationRecord include MediaPackageV2Helper before_create :set_uuid + before_destroy :delete_aws_resource belongs_to :streaming has_one :channel, class_name: 'MediaPackageV2Channel' @@ -34,11 +35,13 @@ def create_aws_resource end def delete_aws_resource - media_package_v2_client.delete_channel_group(channel_group_name:) if exists_aws_resource? - loop do - break unless exists_aws_resource? + if exists_aws_resource? + media_package_v2_client.delete_channel_group(channel_group_name:) + loop do + break unless exists_aws_resource? + end + update!(name: '') end - update!(name: '') end def exists_aws_resource? diff --git a/app/models/media_package_v2_origin_endpoint.rb b/app/models/media_package_v2_origin_endpoint.rb index f01a98630..cb90e57c2 100644 --- a/app/models/media_package_v2_origin_endpoint.rb +++ b/app/models/media_package_v2_origin_endpoint.rb @@ -24,6 +24,7 @@ class MediaPackageV2OriginEndpoint < ApplicationRecord include MediaPackageV2Helper before_create :set_uuid + before_destroy :delete_aws_resource belongs_to :streaming belongs_to :channel, class_name: 'MediaPackageV2Channel', foreign_key: :media_package_v2_channel_id @@ -89,10 +90,12 @@ def exists_aws_resource? end def delete_aws_resource - media_package_v2_client.delete_origin_endpoint_policy(channel_group_name:, channel_name:, origin_endpoint_name:) - media_package_v2_client.delete_origin_endpoint(channel_group_name:, channel_name:, origin_endpoint_name:) - loop do - break unless exists_aws_resource? + if exists_aws_resource? + media_package_v2_client.delete_origin_endpoint_policy(channel_group_name:, channel_name:, origin_endpoint_name:) + media_package_v2_client.delete_origin_endpoint(channel_group_name:, channel_name:, origin_endpoint_name:) + loop do + break unless exists_aws_resource? + end end update!(name: '') end From ac5fd73254520c3d94da22d0d295674a7f2e675c Mon Sep 17 00:00:00 2001 From: Ryo Takaishi Date: Sun, 10 Sep 2023 19:08:44 +0900 Subject: [PATCH 8/9] =?UTF-8?q?1=E8=A1=8C=E3=81=A7=E6=9B=B8=E3=81=91?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/media_package_channel.rb | 4 +--- app/models/media_package_origin_endpoint.rb | 4 +--- app/models/media_package_parameter.rb | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/app/models/media_package_channel.rb b/app/models/media_package_channel.rb index 56d06f2a5..9a8587f20 100644 --- a/app/models/media_package_channel.rb +++ b/app/models/media_package_channel.rb @@ -20,9 +20,7 @@ class MediaPackageChannel < ApplicationRecord include MediaPackageHelper include EnvHelper - before_destroy do - delete_aws_resource - end + before_destroy :delete_aws_resource belongs_to :streaming has_many :media_package_origin_endpoints diff --git a/app/models/media_package_origin_endpoint.rb b/app/models/media_package_origin_endpoint.rb index cd91e3414..dd6390295 100644 --- a/app/models/media_package_origin_endpoint.rb +++ b/app/models/media_package_origin_endpoint.rb @@ -22,9 +22,7 @@ class MediaPackageOriginEndpoint < ApplicationRecord include MediaPackageHelper include EnvHelper - before_destroy do - delete_aws_resource - end + before_destroy :delete_aws_resource belongs_to :media_package_channel belongs_to :streaming diff --git a/app/models/media_package_parameter.rb b/app/models/media_package_parameter.rb index e41d83e7f..b9a15aafe 100644 --- a/app/models/media_package_parameter.rb +++ b/app/models/media_package_parameter.rb @@ -26,9 +26,7 @@ class MediaPackageParameter < ApplicationRecord include SsmHelper include EnvHelper - before_destroy do - delete_aws_resource - end + before_destroy :delete_aws_resource before_create :set_uuid From 3d7c7e28edb7b18bce48fb04e8910ec8aac7a3d6 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Tue, 12 Sep 2023 09:48:08 +0000 Subject: [PATCH 9/9] Update all dependencies on GitHub Actions --- .github/workflows/build.yml | 14 +++++++------- .github/workflows/gitops-prd.yml | 4 ++-- .github/workflows/gitops-stg.yml | 4 ++-- .github/workflows/make-swagger-artifacts.yml | 4 ++-- .github/workflows/push-tag-by-releasebot.yml | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2b9bc11fd..5e94e5e9d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -18,10 +18,10 @@ jobs: - name: Set up Docker Buildx id: buildx - uses: docker/setup-buildx-action@v2 + uses: docker/setup-buildx-action@v3 - name: Configure AWS Credentials - uses: aws-actions/configure-aws-credentials@v3 + uses: aws-actions/configure-aws-credentials@v4 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} @@ -33,7 +33,7 @@ jobs: - name: Docker meta id: meta - uses: docker/metadata-action@v4 + uses: docker/metadata-action@v5 with: images: ${{ steps.login-ecr.outputs.registry }}/dreamkast-ecs tags: | @@ -54,7 +54,7 @@ jobs: - name: Build id: docker_build - uses: docker/build-push-action@v4 + uses: docker/build-push-action@v5 with: context: ./ file: ./Dockerfile @@ -71,10 +71,10 @@ jobs: needs: ["build"] steps: - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 + uses: docker/setup-buildx-action@v3 - name: Configure AWS Credentials - uses: aws-actions/configure-aws-credentials@v3 + uses: aws-actions/configure-aws-credentials@v4 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} @@ -86,7 +86,7 @@ jobs: - name: Docker meta id: meta - uses: docker/metadata-action@v4 + uses: docker/metadata-action@v5 with: images: ${{ steps.login-ecr.outputs.registry }}/dreamkast-ecs tags: | diff --git a/.github/workflows/gitops-prd.yml b/.github/workflows/gitops-prd.yml index 930774ebd..09a2a9b2f 100644 --- a/.github/workflows/gitops-prd.yml +++ b/.github/workflows/gitops-prd.yml @@ -14,13 +14,13 @@ jobs: - name: Generate token id: generate_token - uses: tibdex/github-app-token@v1 + uses: tibdex/github-app-token@v2 with: app_id: ${{ secrets.APP_ID }} private_key: ${{ secrets.PRIVATE_KEY }} - name: Configure AWS Credentials - uses: aws-actions/configure-aws-credentials@v3 + uses: aws-actions/configure-aws-credentials@v4 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} diff --git a/.github/workflows/gitops-stg.yml b/.github/workflows/gitops-stg.yml index 9db03a4d5..c1892a9e3 100644 --- a/.github/workflows/gitops-stg.yml +++ b/.github/workflows/gitops-stg.yml @@ -14,13 +14,13 @@ jobs: - name: Generate token id: generate_token - uses: tibdex/github-app-token@v1 + uses: tibdex/github-app-token@v2 with: app_id: ${{ secrets.APP_ID }} private_key: ${{ secrets.PRIVATE_KEY }} - name: Configure AWS Credentials - uses: aws-actions/configure-aws-credentials@v3 + uses: aws-actions/configure-aws-credentials@v4 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} diff --git a/.github/workflows/make-swagger-artifacts.yml b/.github/workflows/make-swagger-artifacts.yml index 1a492a824..09c879571 100644 --- a/.github/workflows/make-swagger-artifacts.yml +++ b/.github/workflows/make-swagger-artifacts.yml @@ -23,13 +23,13 @@ jobs: - name: Generate token id: generate_token - uses: tibdex/github-app-token@v1 + uses: tibdex/github-app-token@v2 with: app_id: ${{ secrets.APP_ID }} private_key: ${{ secrets.PRIVATE_KEY }} - name: Configure AWS Credentials - uses: aws-actions/configure-aws-credentials@v3 + uses: aws-actions/configure-aws-credentials@v4 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} diff --git a/.github/workflows/push-tag-by-releasebot.yml b/.github/workflows/push-tag-by-releasebot.yml index a3c9c237f..ba0ed31ee 100644 --- a/.github/workflows/push-tag-by-releasebot.yml +++ b/.github/workflows/push-tag-by-releasebot.yml @@ -11,7 +11,7 @@ jobs: steps: - name: Generate token id: generate_token - uses: tibdex/github-app-token@v1 + uses: tibdex/github-app-token@v2 with: app_id: ${{ secrets.APP_ID }} private_key: ${{ secrets.PRIVATE_KEY }}