Class: CfnPP::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/cfnpp/uploader.rb

Instance Method Summary collapse

Constructor Details

#initialize(bucketname, stackname) ⇒ Uploader

Returns a new instance of Uploader.



7
8
9
10
11
12
13
# File 'lib/cfnpp/uploader.rb', line 7

def initialize(bucketname, stackname)
  @bucket = AWS::S3.new.buckets[bucketname]
  @stackname = stackname
  @timestamp = new_timestamp
  @s3_path = "stacks/#{@stackname}/#{@timestamp}"
  @cfn = AWS::CloudFormation.new
end

Instance Method Details

#find_previous_ploy_update(opts) ⇒ Object



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

def find_previous_ploy_update(opts)
  stack_name = opts['StackName']
  launch_list = @bucket.objects.with_prefix("stacks/#{stack_name}").sort do |a,b|
    a.key <=> b.key
  end
  launch_list.each do |o|
    if o.key =~ /\/parameters.yml$/
      prev_opts = YAML::load(o.read)
      if prev_opts['TemplateSource'] == opts['TemplateSource']
        if prev_opts['TemplateGitRevision'] == opts['TemplateGitRevision']
          return prev_opts
        end
      end
    end
  end
  return nil
end

#new_timestampObject



15
16
17
# File 'lib/cfnpp/uploader.rb', line 15

def new_timestamp
  return Time.now.utc.strftime("%Y-%m-%dT%H.%M.%S.%LZ")
end

#opts_parameters(params, opts) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/cfnpp/uploader.rb', line 46

def opts_parameters(params, opts)
  opts_parameters = {}
  params.each do |param|
    key = param[:parameter_key]
    opts_parameters[key] = opts[key] if opts.has_key? key
  end
  return opts_parameters
end

#ploy_update_guard(name, opts) ⇒ Object



59
60
61
62
63
64
# File 'lib/cfnpp/uploader.rb', line 59

def ploy_update_guard(name, opts)
  if opts['TemplateSource'] == 'ploy'
    return find_previous_ploy_update(opts)
  end
  return nil
end

#s3_pathObject



19
20
21
# File 'lib/cfnpp/uploader.rb', line 19

def s3_path
  return @s3_path
end

#upload_parameters(name, opts_parameters) ⇒ Object



55
56
57
# File 'lib/cfnpp/uploader.rb', line 55

def upload_parameters(name, opts_parameters)
  return @bucket.objects.create("#{@s3_path}/#{name}/parameters.yml", opts_parameters.to_yaml)
end

#upload_template(template_result, opts) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cfnpp/uploader.rb', line 23

def upload_template(template_result, opts)
  r = { :url => nil, :error => nil, :validation_status => nil }

  already_updated = ploy_update_guard(template_result.name, opts)
  if already_updated
    r[:err] = 'already_updated'
    r[:prev_opts] = already_updated
    return r
  end

  obj = @bucket.objects.create("#{@s3_path}/#{template_result.name}/template.json", template_result.data.to_json)
  r[:url] = obj.public_url
  r[:validation_status] = @cfn.validate_template(r[:url])
  if r[:validation_status].has_key? :code #error condition
    r[:error] = 'validation_error'
  else
    tp = r[:validation_status].fetch(:parameters, [])
    r[:opts_parameters] = opts_parameters(tp, opts)
    upload_parameters(template_result.name, r[:opts_parameters])
  end
  return r
end