Class: Dploy::Deployer

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

Instance Method Summary collapse

Constructor Details

#initialize(deploy_data) ⇒ Deployer

Returns a new instance of Deployer.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dploy/deployer.rb', line 10

def initialize(deploy_data)
  @deploy_data = deploy_data
  @ec2 = EC2Client.new(
  DPLOY_CONFIG['ec2_access_key_id'],
  DPLOY_CONFIG['ec2_access_key'],
  DPLOY_CONFIG['ec2_endpoint'])
  @chef = ChefClient.new(
  DPLOY_CONFIG['chef_api_url'],
  DPLOY_CONFIG['chef_api_user'],
  DPLOY_CONFIG['chef_api_key'])

  #TODO validate deploy data

  @deploy_data[:id] = "#{@deploy_data[:project]}-#{@deploy_data[:environment]}-#{@deploy_data[:version].gsub('.','-')}-#{Time.now.to_i}"
  @log = Dploy::Logger.new(DPLOY_CONFIG['mongo_server'], @deploy_data[:id])
end

Instance Method Details

#deployObject



31
32
33
34
35
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
# File 'lib/dploy/deployer.rb', line 31

def deploy()
  begin
    #Get Instances
    search_tags = { 'project' => @deploy_data[:project], 'environment' => @deploy_data[:environment] }
    @log.debug("Searching For EC2 Instances")
    @log.debug("Search tags: #{search_tags}")
    instances = @ec2.get_instances_by_tags(search_tags)
    @log.debug("#{instances.count} matching instances found")

    instances.each do |instance|

      @log.debug("Starting deploy for instance: #{instance.id}")
      #TODO Remove from LB

      #update chef node
      node_name = "#{@deploy_data[:environment]}-#{@deploy_data[:project]}-#{instance.id}"
      @log.debug("Updating chef node: #{node_name}")
      attributes = { :dploy => { :target_version => @deploy_data[:version], :status => 'deploying' } }
      @chef.update_node_attributes(node_name, attributes)

      #Run Chef client
      @log.debug("Running Chef Client for #{node_name} (#{instance.dns_name})")
      ShellCmd.run("ssh  -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i #{DPLOY_CONFIG['ec2_ssh_key_path']} ubuntu@#{instance.dns_name} sudo chef-client", @log)
      

      #TODO Test
      
      @log.completion("Completed",{:status => "Success"})
    end
  rescue 
      @log.error("#{$!}")
      @log.completion("Failed",{:status => "failure"})
  end 
end

#deploy_dataObject



27
28
29
# File 'lib/dploy/deployer.rb', line 27

def deploy_data
  @deploy_data
end