Sha256: 552a17ae6061d2ba2c8b0f21ea2c19ed22f6cefd16ce058712795c7cd2551520

Contents?: true

Size: 1.68 KB

Versions: 9

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

require 'aws-sdk-cloudwatchlogs'
require 'hako'
require 'hako/script'

module Hako
  module Scripts
    class CreateAwsCloudWatchLogsLogGroup < Script
      # @param [Hash<String, Container>] containers
      # @return [nil]
      def deploy_starting(containers)
        containers.each_value do |container|
          log_configuration = container.log_configuration
          unless log_configuration
            next
          end

          if log_configuration[:log_driver] == 'awslogs'
            create_log_group_if_not_exist(log_configuration.fetch(:options))
          end
        end
      end

      alias_method :oneshot_starting, :deploy_starting

      private

      # @param [Hash] options
      # @return [nil]
      def create_log_group_if_not_exist(options)
        group = options.fetch('awslogs-group')
        region = options.fetch('awslogs-region')

        unless log_group_exist?(group, region: region)
          cloudwatch_logs(region).create_log_group(log_group_name: group)
          Hako.logger.info "Created CloudWatch log group #{group} in #{region}"
        end
      end

      # @param [String] region
      # @return [Aws::CloudWatchLogs::Client]
      def cloudwatch_logs(region)
        @cloudwatch_logs ||= {}
        @cloudwatch_logs[region] ||= Aws::CloudWatchLogs::Client.new(region: region)
      end

      # @param [String] group
      # @param [String] region
      # @return [Boolean]
      def log_group_exist?(group, region:)
        cloudwatch_logs(region).describe_log_groups(log_group_name_prefix: group).any? do |page|
          page.log_groups.any? { |log_group| log_group.log_group_name == group }
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
hako-2.0.2 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.0.1 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.0.0 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-1.9.0 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-1.8.4 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-1.8.3 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-1.8.2 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-1.8.1 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-1.8.0 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb