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.



16
17
18
19
# File 'lib/pipely/deploy/client.rb', line 16

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

Instance Method Details

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pipely/deploy/client.rb', line 67

def create_pipeline(pipeline_name, definition, tags={})
  definition_objects = JSON.parse(definition)['objects']

  unique_id = UUIDTools::UUID.random_create

  created_pipeline = @data_pipelines.pipelines.create(
    unique_id: unique_id,
    name: pipeline_name,
    tags: default_tags.merge(tags)
  )

  created_pipeline.put(definition_objects)
  created_pipeline.activate

  created_pipeline.id
end

#delete_pipeline(pipeline_id) ⇒ Object



84
85
86
# File 'lib/pipely/deploy/client.rb', line 84

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

#deploy_pipeline(pipeline_basename, definition) ⇒ Object



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

def deploy_pipeline(pipeline_basename, definition)
  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)
  @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

#existing_pipelines(pipeline_name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pipely/deploy/client.rb', line 52

def existing_pipelines(pipeline_name)
  ids = []

  begin
    result = Fog::AWS[:data_pipeline].list_pipelines

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

  end while (result['hasMoreResults'] && result['marker'])

  ids
end