Class: Pipedream::Stack

Inherits:
Object
  • Object
show all
Includes:
AwsServices
Defined in:
lib/pipedream/stack.rb

Direct Known Subclasses

Create, Deploy, Update

Instance Method Summary collapse

Methods included from AwsServices

#cfn, #codepipeline, #s3

Methods included from AwsServices::Helpers

#are_you_sure?, #inferred_pipeline_name, #inferred_stack_name, #pipeline_name_convention, #stack_exists?

Constructor Details

#initialize(options) ⇒ Stack

Returns a new instance of Stack.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/pipedream/stack.rb', line 7

def initialize(options)
  @options = options
  @pipeline_name = @options[:pipeline_name] || inferred_pipeline_name
  @stack_name = options[:stack_name] || inferred_stack_name(@pipeline_name)

  @full_pipeline_name = pipeline_name_convention(@pipeline_name)
  @template = {
    "Description" => "CodePipeline Project: #{@full_pipeline_name}",
    "Resources" => {}
  }
end

Instance Method Details

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pipedream/stack.rb', line 19

def run
  options = @options.merge(
    pipeline_name: @pipeline_name,
    full_pipeline_name: @full_pipeline_name,
  )

  pipeline_builder = Pipeline.new(options)
  unless pipeline_builder.exist?
    puts "ERROR: pipeline does not exist: #{pipeline_builder.pipeline_path}".color(:red)
    exit 1
    return
  end
  pipeline = pipeline_builder.run
  @template["Resources"].merge!(pipeline)

  if pipeline["Pipeline"]["Properties"]["RoleArn"] == {"Fn::GetAtt"=>"IamRole.Arn"}
    role = Role.new(options).run
    @template["Resources"].merge!(role)
  end

  if sns_topic?(pipeline)
    role = Sns.new(options).run
    @template["Resources"].merge!(role)
  end

  webhook = Webhook.new(options).run
  @template["Resources"].merge!(webhook) if webhook

  schedule = Schedule.new(options).run
  @template["Resources"].merge!(schedule) if schedule

  template_path = "/tmp/codepipeline.yml"
  FileUtils.mkdir_p(File.dirname(template_path))
  IO.write(template_path, YAML.dump(@template))
  puts "Generated CloudFormation template at #{template_path.color(:green)}"
  return if @options[:noop]
  puts "Deploying stack #{@stack_name.color(:green)} with CodePipeline project #{@full_pipeline_name.color(:green)}"

  begin
    perform
    url_info
    return unless @options[:wait]
    status.wait
    exit 2 unless status.success?
  rescue Aws::CloudFormation::Errors::ValidationError => e
    if e.message.include?("No updates") # No updates are to be performed.
      puts "WARN: #{e.message}".color(:yellow)
    else
      puts "ERROR ValidationError: #{e.message}".color(:red)
      exit 1
    end
  end
end