Class: Deployer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(deploy_all, project_path, account, artifact = nil, lambda = nil) ⇒ Deployer

Returns a new instance of Deployer.



14
15
16
17
18
19
20
21
22
# File 'lib/deployer.rb', line 14

def initialize(deploy_all, project_path, , artifact=nil, lambda=nil)
  @deploy_all = deploy_all
  @project_path  = project_path
  @account = 
  @project = Project.new("#{project_path}")
  @config = set_config
  @artifact =  artifact
  @lambda =  lambda
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



6
7
8
# File 'lib/deployer.rb', line 6

def 
  @account
end

#artifactObject

Returns the value of attribute artifact.



10
11
12
# File 'lib/deployer.rb', line 10

def artifact
  @artifact
end

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/deployer.rb', line 7

def config
  @config
end

#deploy_allObject

Returns the value of attribute deploy_all.



12
13
14
# File 'lib/deployer.rb', line 12

def deploy_all
  @deploy_all
end

#environmentsObject

Returns the value of attribute environments.



11
12
13
# File 'lib/deployer.rb', line 11

def environments
  @environments
end

#lambdaObject

Returns the value of attribute lambda.



9
10
11
# File 'lib/deployer.rb', line 9

def lambda
  @lambda
end

#projectObject

Returns the value of attribute project.



8
9
10
# File 'lib/deployer.rb', line 8

def project
  @project
end

#project_pathObject

Returns the value of attribute project_path.



5
6
7
# File 'lib/deployer.rb', line 5

def project_path
  @project_path
end

Instance Method Details

#archive_project(environment) ⇒ Object



99
100
101
102
103
# File 'lib/deployer.rb', line 99

def archive_project(environment)
  FileUtils.mkpath("#{project_path}/.history/")
  FileUtils.copy_entry("#{project_path}/config","#{project_path}/.history/config")
  FileUtils.copy_entry("#{project_path}/environments/#{environment}.yaml","#{project_path}/.history/environments/#{environment}.yaml")
end

#construct_s3_key(properties, env) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/deployer.rb', line 91

def construct_s3_key(properties, env)
  if properties['s3_key']
    return properties['s3_key']
  else
    return "#{config['environments'][env]['base_path']}/#{properties['artifact_name']}-#{properties['version']}.#{properties['extension']}"
  end
end

#deploy(environments) ⇒ Object



48
49
50
51
52
53
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
# File 'lib/deployer.rb', line 48

def deploy(environments)
   = project.[]
  env_region_map = project.env_region_map
  #Filter by account as the primary owner of envs
  lambda_env_map = project.get_lambdas
  unless deploy_all
    lambda_env_map = diff_projects(lambda_env_map)
    if lambda_env_map.empty?
      puts "No lambdas have been changed, skipping deploy"
      return
    end
  end
  lambda_env_map = get_deployable_lambdas(lambda_env_map)
  environments ||= project.environments
  .each do |env|
    #IF users has specified environments, skip if the environment is not a user specified one
    next unless !environments.nil? && environments.include?(env) && !lambda_env_map[env].nil?
    lambda_env_map[env].each do |lambda_name, properties|
      client = Client.new(env_region_map[env])

      s3_bucket = get_s3_bucket(properties, env)
      s3_key = construct_s3_key(properties, env)

      puts "ENV: #{env}"
      puts "Lambda Name: #{lambda_name}"
      puts "S3 Bucket: #{s3_bucket}"
      puts "S3 Key:  #{s3_key}"
      client.update_function_code(lambda_name,s3_bucket,s3_key)
    end
    archive_project(env)
  end
end

#diff_projects(new_project) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/deployer.rb', line 110

def diff_projects(new_project)
  historic = parse_archive
  historic.each do |environment, lambdas|
    lambdas.each do |lambda, configs|
      next if new_project[environment][lambda].nil?
      if new_project[environment][lambda].has_key?('sha1')
        if configs.has_key?('sha1')  && (configs['sha1'] == new_project[environment][lambda]['sha1'])
          new_project[environment].delete(lambda)
        end
      elsif new_project[environment][lambda].eql?(configs)
        new_project[environment].delete(lambda)
      end
    end
    #Delete empty environments as no lambdas changed so they all removed from bing deployable
    new_project.delete(environment) if new_project[environment].empty?
  end
  new_project
end

#get_deployable_lambdas(lambda_env_map) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/deployer.rb', line 28

def get_deployable_lambdas(lambda_env_map)

  if artifact
    lambda_env_map.each do |env, lambdas|
      lambdas.each do |lambda_name, properties|
        lambdas.delete(lambda_name) unless properties['artifact_name'] == artifact
      end
    end
  end

  if lambda
    lambda_env_map.each do |env, lambdas|
      lambdas.each do |lambda_name, properties|
        lambdas.delete(lambda_name) unless lambda_name == lambda
      end
    end
  end
  lambda_env_map
end

#get_s3_bucket(properties, env) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/deployer.rb', line 82

def get_s3_bucket(properties, env)
  if properties['s3_bucket']
    return properties['s3_bucket']
  else
    return config['environments'][env]['s3_bucket']
  end

end

#parse_archiveObject



105
106
107
108
# File 'lib/deployer.rb', line 105

def parse_archive
  archived_project = Project.new("#{project_path}/.history/")
  archived_project.get_lambdas
end

#set_configObject



24
25
26
# File 'lib/deployer.rb', line 24

def set_config
  @project.config
end