Class: Dev::Template::Aws::Ci

Inherits:
BaseInterface show all
Defined in:
lib/firespring_dev_commands/templates/ci.rb

Overview

Class contains rake templates for managing your ci/cd resources

Instance Attribute Summary collapse

Attributes inherited from BaseInterface

#exclude

Instance Method Summary collapse

Methods inherited from BaseInterface

#create_tasks!

Constructor Details

#initialize(cloudformation, exclude: []) ⇒ Ci

Base interface template customized for codepipelines which require a pipeline pattern which will match the pipeline name



11
12
13
14
15
16
# File 'lib/firespring_dev_commands/templates/ci.rb', line 11

def initialize(cloudformation, exclude: [])
  @cloudformations = Array(cloudformation).sort_by(&:name)
  raise 'must specify an arry of cloudformation objects' unless @cloudformations.all?(Dev::Aws::Cloudformation)

  super(exclude:)
end

Instance Attribute Details

#cloudformationObject (readonly)

Returns the value of attribute cloudformation.



8
9
10
# File 'lib/firespring_dev_commands/templates/ci.rb', line 8

def cloudformation
  @cloudformation
end

Instance Method Details

#create_create_task!Object

Create the rake task for creating the codepipeline



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
# File 'lib/firespring_dev_commands/templates/ci.rb', line 19

def create_create_task!
  # Have to set a local variable to be accessible inside of the instance_eval block
  exclude = @exclude
  cloudformations = @cloudformations
  return if exclude.include?(:create)

  DEV_COMMANDS_TOP_LEVEL.instance_eval do
    namespace :ci do
      desc 'Create the ci cloudformation stack in aws'
      task create: %w(init ensure_aws_credentials show_account_info) do
        next if cloudformations.empty?

        names = cloudformations.map(&:name).join(', ')
        Dev::Common.new.exit_unless_confirmed("  This will create the #{names} pipelines. Continue?")

        # Start create on all stacks without waiting so they are created in parallel
        cloudformations.each do |cloudformation|
          next if cloudformation.exist?

          cloudformation.create(should_wait: false)
        end
        LOG.info 'Waiting for all stacks to finish create'

        # Wait until all stacks have finished creating
        cloudformations.each(&:create_wait)
        LOG.info "Stack create finished at #{Time.now.to_s.light_yellow}"
        LOG.info

        raise 'Some stacks failed to create' if cloudformations.any?(&:failed?)
      end
    end
  end
end

#create_delete_task!Object

Create the rake task for deleting the codepipeline



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/firespring_dev_commands/templates/ci.rb', line 89

def create_delete_task!
  # Have to set a local variable to be accessible inside of the instance_eval block
  exclude = @exclude
  cloudformations = @cloudformations
  return if exclude.include?(:delete)

  DEV_COMMANDS_TOP_LEVEL.instance_eval do
    namespace :ci do
      desc 'Delete the ci cloudformation stack in aws'
      task delete: %w(init ensure_aws_credentials show_account_info) do
        next if cloudformations.empty?

        names = cloudformations.map(&:name).join(', ')
        Dev::Common.new.exit_unless_confirmed("  This will delete the #{names} pipelines. Continue?")

        # Start delete on all stacks without waiting so they are deleted in parallel
        cloudformations.each do |cloudformation|
          next unless cloudformation.exist?

          cloudformation.delete(should_wait: false)
        end
        LOG.info 'Waiting for all stacks to finish delete'

        # Wait until all stacks have finished creating
        cloudformations.each(&:delete_wait)
        LOG.info "Stack delete finished at #{Time.now.to_s.light_yellow}"
        LOG.info

        raise 'Some stacks failed to update' if cloudformations.any?(&:failed?)
      end
    end
  end
end

#create_status_task!Object

Create the rake task for the aws codepipeline status method



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/firespring_dev_commands/templates/ci.rb', line 124

def create_status_task!
  # Have to set a local variable to be accessible inside of the instance_eval block
  exclude = @exclude
  cloudformations = @cloudformations
  return if exclude.include?(:status)

  DEV_COMMANDS_TOP_LEVEL.instance_eval do
    namespace :ci do
      desc 'Show the current status of the pipelines associated with your branch'
      task status: %w(init ensure_aws_credentials show_account_info) do
        next if cloudformations.empty?

        pattern = /#{cloudformations.map(&:name).join('|')}/
        pipelines = Dev::Aws::CodePipeline.new.pipelines(pattern).sort_by(&:name)
        LOG.info "No pipelines found matching #{pattern.source.gsub('|', ' OR ')}" if pipelines.empty?
        pipelines.each do |pipeline|
          Dev::Aws::CodePipeline.new.status(pipeline.name)
          LOG.info
        end
        LOG.info
      end
    end
  end
end

#create_update_task!Object

Create the rake task for updating the codepipeline



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/firespring_dev_commands/templates/ci.rb', line 54

def create_update_task!
  # Have to set a local variable to be accessible inside of the instance_eval block
  exclude = @exclude
  cloudformations = @cloudformations
  return if exclude.include?(:update)

  DEV_COMMANDS_TOP_LEVEL.instance_eval do
    namespace :ci do
      desc 'Update the ci cloudformation stack in aws'
      task update: %w(init ensure_aws_credentials show_account_info) do
        next if cloudformations.empty?

        names = cloudformations.map(&:name).join(', ')
        Dev::Common.new.exit_unless_confirmed("  This will update the #{names} pipelines. Continue?")

        # Start update on all stacks without waiting so they are updated in parallel
        cloudformations.each do |cloudformation|
          next unless cloudformation.exist?

          cloudformation.update(should_wait: false)
        end
        LOG.info 'Waiting for all stacks to finish update'

        # Wait until all stacks have finished creating
        cloudformations.each(&:update_wait)
        LOG.info "Stack update finished at #{Time.now.to_s.light_yellow}"
        LOG.info

        raise 'Some stacks failed to update' if cloudformations.any?(&:failed?)
      end
    end
  end
end