Class: HooplaSalesforce::Deployer

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

Overview

Deploys and retreives zip files from salesforce using the metadata API. Use ENV = ‘test.salesforce.com’ to use this on sandbox organizations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, token, enterprise_wsdl, metadata_wsdl) ⇒ Deployer

Returns a new instance of Deployer.



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

def initialize(username, password, token, enterprise_wsdl, )
  @username = username
  @password = password
  @token    = token
  @enterprise_wsdl = enterprise_wsdl
  @metadata_wsdl   = 
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

#headerObject

Returns the value of attribute header.



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

def header
  @header
end

#metaclientObject

Returns the value of attribute metaclient.



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

def metaclient
  @metaclient
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#build_clientsObject



30
31
32
33
# File 'lib/hoopla_salesforce/deployer.rb', line 30

def build_clients
  @client     = Savon::Client.new @enterprise_wsdl
  @metaclient = Savon::Client.new @metadata_wsdl
end

#deploy(zipfile, options = {}) ⇒ Object



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
80
81
82
# File 'lib/hoopla_salesforce/deployer.rb', line 52

def deploy(zipfile, options = {})
  

  data = Base64.encode64(File.read(zipfile))

  response = metaclient.deploy do |soap, wsse|
    soap.header = header
    soap.body = { "wsdl:ZipFile" => data,
                  "wsdl:DeployOptions" => options,
                  :order! => ['wsdl:ZipFile', 'wsdl:DeployOptions']
                }
  end.to_hash[:deploy_response][:result]
   
  puts "Deployment requested, awaiting completion of job #{response[:id]}..." 
  while !response[:done]
    begin
      response = metaclient.check_status do |soap, wsse|
        soap.header = header
        soap.body = { "wsdl:asyncProcessId" => response[:id] }
      end.to_hash[:check_status_response][:result]
    rescue Timeout::Error
    end
  end 

  response = metaclient.check_deploy_status do |soap, wsse|
    soap.header = header
    soap.body = { "wsdl:asyncProcessId" => response[:id] }
  end.to_hash[:check_deploy_status_response][:result]

  error_out unless TextReporter.new(response).report
end

#error_outObject



84
85
86
# File 'lib/hoopla_salesforce/deployer.rb', line 84

def error_out
  raise "Deployment errors. See report for details."
end

#loginObject



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

def 
  build_clients
  set_certs
  setup_metaclient_and_store_header
end

#retrieve(request) ⇒ Object



88
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/hoopla_salesforce/deployer.rb', line 88

def retrieve(request)
  

  response = metaclient.retrieve do |soap, wsse|
    soap.header = @header
    soap.body = { "wsdl:retrieveRequest" => request }
  end.to_hash[:retrieve_response][:result]
 
  puts "Retrieve requested, awaiting completion of job #{response[:id]}..." 
  while !response[:done]
    response = metaclient.check_status do |soap, wsse|
      soap.header = @header
      soap.body = { "wsdl:asyncProcessId" => response[:id] }
    end.to_hash[:check_status_response][:result]
  end
  
  response = metaclient.check_retrieve_status do |soap, wsse|
    soap.header = @header
    soap.body = { "wsdl:asyncProcessId" => response[:id] }
  end.to_hash[:check_retrieve_status_response][:result]
  
  File.open('retrieved.zip', 'w') { |f| f.print Base64.decode64(response[:zip_file]) }
  puts "Wrote retrieved contents to retrieved.zip."
end

#set_certsObject



35
36
37
38
# File 'lib/hoopla_salesforce/deployer.rb', line 35

def set_certs
  # TODO figure out how to get Savon to validate SF's cert
  [@client, @metaclient].each { |c| c.request.http.ssl_client_auth(:verify_mode => OpenSSL::SSL::VERIFY_NONE) }
end

#setup_metaclient_and_store_headerObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hoopla_salesforce/deployer.rb', line 40

def setup_metaclient_and_store_header
  client.wsdl.soap_endpoint.host = ENV['LOGIN_HOST'] if ENV['LOGIN_HOST']

  response = client. do |soap, wsse|
    soap.body = { "wsdl:username" => username, "wsdl:password" => password + token}
  end.to_hash[:login_response][:result]

  metaclient.wsdl.soap_endpoint = response[:metadata_server_url]

  @header = { "wsdl:SessionHeader" => { "wsdl:sessionId" => response[:session_id] } }
end