Class: Hako::Scripts::CreateAwsCloudWatchLogsLogGroup

Inherits:
Hako::Script
  • Object
show all
Defined in:
lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb

Instance Method Summary collapse

Methods inherited from Hako::Script

#after_remove, #configure, #deploy_failed, #deploy_finished, #deploy_started, #initialize, #oneshot_finished, #oneshot_started, #remove_starting, #rollback_finished, #rollback_started, #rollback_starting

Constructor Details

This class inherits a constructor from Hako::Script

Instance Method Details

#cloudwatch_logs(region) ⇒ Aws::CloudWatchLogs::Client (private)

Parameters:

  • region (String)

Returns:

  • (Aws::CloudWatchLogs::Client)


47
48
49
50
# File 'lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb', line 47

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

#create_log_group_if_not_exist(options) ⇒ nil (private)

Parameters:

  • options (Hash)

Returns:

  • (nil)


31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb', line 31

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

#deploy_starting(containers) ⇒ nil Also known as: oneshot_starting

Parameters:

Returns:

  • (nil)


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb', line 12

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

#log_group_exist?(group, region:) ⇒ Boolean (private)

Parameters:

  • group (String)
  • region (String)

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb', line 55

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