Class: Pipely::Deploy::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pipely/deploy/client.rb

Overview

Client for managing deployment of rendered definitions.

Defined Under Namespace

Classes: PipelineDeployerError

Instance Method Summary collapse

Constructor Details

#initialize(log = nil) ⇒ Client

Returns a new instance of Client.



30
31
32
33
34
# File 'lib/pipely/deploy/client.rb', line 30

def initialize(log=nil)
  @log = log || Logger.new(STDOUT)
  @data_pipelines = Fog::AWS::DataPipeline.new
  @aws = AWS::DataPipeline.new.client
end

Instance Method Details

#activate_pipeline(response, pipeline) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/pipely/deploy/client.rb', line 113

def activate_pipeline(response, pipeline)
  if response[:errored]
    @log.error("Failed to put pipeline definition.")
    @log.error(response[:validation_errors].inspect)
    false
  else
    @aws.activate_pipeline(pipeline_id: pipeline.id)
    pipeline.id
  end
end

#create_pipeline(pipeline_name, definition, tags = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/pipely/deploy/client.rb', line 89

def create_pipeline(pipeline_name, definition, tags={})
  # Use Fog gem, instead of aws-sdk gem, to create pipeline with tags.
  #
  # TODO: Consolidate on aws-sdk when tagging support is added.
  #
  created_pipeline = @data_pipelines.pipelines.create(
    unique_id: SecureRandom.uuid,
    name: pipeline_name,
    tags: default_tags.merge(tags)
  )

  definition ||= yield(created_pipeline.id) if block_given?

  # Use aws-sdk gem, instead of Fog, to put definition and activate
  # pipeline, for improved reporting of validation errors.
  #
  response = @aws.put_pipeline_definition(
    pipeline_id: created_pipeline.id,
    pipeline_objects: JSONDefinition.parse(definition)
  )

  activate_pipeline(response, created_pipeline)
end

#delete_pipeline(pipeline_id) ⇒ Object



124
125
126
# File 'lib/pipely/deploy/client.rb', line 124

def delete_pipeline(pipeline_id)
  @data_pipelines.pipelines.get(pipeline_id).destroy
end

#deploy_pipeline(pipeline_basename, definition = nil, &block) ⇒ Object



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
# File 'lib/pipely/deploy/client.rb', line 36

def deploy_pipeline(pipeline_basename, definition=nil, &block)
  pipeline_name = [
    ('P' if ENV['env'] == 'production'),
    ENV['USER'],
    pipeline_basename
  ].compact.join(':')

  tags = { "basename" => pipeline_basename }

  # Get a list of all existing pipelines
  pipeline_ids = existing_pipelines(pipeline_name)
  @log.info("#{pipeline_ids.count} existing pipelines: #{pipeline_ids}")

  # Create new pipeline
  created_pipeline_id = create_pipeline(
    pipeline_name, definition, tags, &block
  )

  if created_pipeline_id
    @log.info("Created pipeline id '#{created_pipeline_id}'")

    # Delete old pipelines
    pipeline_ids.each do |pipeline_id|
      begin
        delete_pipeline(pipeline_id)
        @log.info("Deleted pipeline '#{pipeline_id}'")

      rescue PipelineDeployerError => error
        @log.warn(error)
      end
    end
  end

  created_pipeline_id
end

#existing_pipelines(pipeline_name) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pipely/deploy/client.rb', line 72

def existing_pipelines(pipeline_name)
  ids = []
  marker = nil

  begin
    options = marker ? { marker: marker } : {}
    result = @aws.list_pipelines(options)

    ids += result[:pipeline_id_list].
             select { |p| p[:name] == pipeline_name }.
             map { |p| p[:id] }

  end while (result[:has_more_results] && marker = result[:marker])

  ids
end