class Codepipe::Schedule

Public Class Methods

new(options={}) click to toggle source
# File lib/codepipe/schedule.rb, line 6
def initialize(options={})
  @options = options
  @schedule_path = options[:schedule_path] || get_schedule_path
  @properties = default_properties
end

Public Instance Methods

default_properties() click to toggle source
# File lib/codepipe/schedule.rb, line 45
def default_properties
  description = "CodePipeline #{@options[:full_pipeline_name]}"
  name = description.gsub(" ", "-").downcase
  {
    description: description,
    # event_pattern: ,
    name: name,
    # schedule_expression: ,
    state: "ENABLED",
    targets: [{
      arn: "arn:aws:codepipeline:#{aws.region}:#{aws.account}:#{@options[:full_pipeline_name]}",
      role_arn: { "Fn::GetAtt": "EventsRuleRole.Arn" }, # required for specific CodePipeline target.
      id: "CodePipelineTarget",
    }]
  }
end
run() click to toggle source
# File lib/codepipe/schedule.rb, line 12
def run
  return unless File.exist?(@schedule_path)

  old_properties = @properties.clone
  evaluate(@schedule_path)

  @properties[:schedule_expression] = @schedule_expression if @schedule_expression
  set_rule_event! if @rule_event_props
  return if old_properties == @properties # empty schedule.rb file

  resource = {
    events_rule: {
      type: "AWS::Events::Rule",
      properties: @properties
    },
    events_rule_role: events_rule_role,
  }
  CfnCamelizer.transform(resource)
end
set_rule_event!() click to toggle source
# File lib/codepipe/schedule.rb, line 32
def set_rule_event!
  props = @rule_event_props
  if props.key?(:detail)
    description = props.key?(:description) ? props.delete(:description) : rule_description
    rule_props = { event_pattern: props, description: description }
  else # if props.key?(:event_pattern)
    props[:description] ||= rule_description
    rule_props = props
  end

  @properties.merge!(rule_props)
end

Private Instance Methods

aws() click to toggle source
# File lib/codepipe/schedule.rb, line 95
def aws
  @aws ||= AwsData.new
end
events_rule_role() click to toggle source
# File lib/codepipe/schedule.rb, line 67
def events_rule_role
  {
    type: "AWS::IAM::Role",
    properties: {
      assume_role_policy_document: {
        statement: [{
          action: [ "sts:AssumeRole" ],
          effect: "Allow",
          principal: { service: [ "events.amazonaws.com" ] }
        }],
        version: "2012-10-17"
      },
      path: "/",
      policies: [{
        policy_name: "CodePipelineAccess",
        policy_document: {
          version: "2012-10-17",
          statement: [{
            action: "codepipeline:StartPipelineExecution",
            effect: "Allow",
            resource: "arn:aws:codepipeline:#{aws.region}:#{aws.account}:#{@options[:full_pipeline_name]}"
          }]
        }
      }]
    }
  }
end
get_schedule_path() click to toggle source
# File lib/codepipe/schedule.rb, line 63
def get_schedule_path
  lookup_codepipeline_file("schedule.rb")
end