Sha256: 581bc4318d0074f0f66a3bfc10b1f8eca38c6bef3c37f6cc5c967246b8807e56

Contents?: true

Size: 1.82 KB

Versions: 27

Compression:

Stored size: 1.82 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)
          if @dry_run
            puts "#{self.class} will create CloudWatch log group #{group} in #{region}"
          else
            cloudwatch_logs(region).create_log_group(log_group_name: group)
            Hako.logger.info "Created CloudWatch log group #{group} in #{region}"
          end
        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

27 entries across 27 versions & 1 rubygems

Version Path
hako-2.17.0 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.16.0 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.15.1 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.15.0 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.14.0 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.13.0 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.12.0 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.11.1 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.11.0 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.10.0 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.9.2 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.9.1 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.9.0 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.8.0 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.7.0 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.6.2 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.6.1 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.6.0 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.5.1 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
hako-2.5.0 lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb