Class: Autodeploy::Job

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

Instance Method Summary collapse

Constructor Details

#initialize(configfile) ⇒ Job

Returns a new instance of Job.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/autodeploy.rb', line 23

def initialize(configfile)
  @config = YAML.load_file(configfile)
  @client = JenkinsApi::Client.new(@config)

  unless Dir.exist? @config['download_dir']
    Autodeploy.log "Creating #{@config['download_dir']} in #{Dir.pwd}"
    Dir.mkdir @config['download_dir']
  end

  ActiveRecord::Base.establish_connection(@config['database'])
end

Instance Method Details

#daemonize(time = 10) ⇒ Object



84
85
86
87
88
89
# File 'lib/autodeploy.rb', line 84

def daemonize(time=10)
  loop do
    run
    sleep time
  end
end

#jobObject



39
40
41
# File 'lib/autodeploy.rb', line 39

def job
  @job ||= @client.job.get_build_details(@config['job'], job_number)
end

#job_numberObject



35
36
37
# File 'lib/autodeploy.rb', line 35

def job_number
  @job_number ||= @client.job.get_current_build_number(@config['job'])
end

#runObject



43
44
45
46
47
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
80
81
82
# File 'lib/autodeploy.rb', line 43

def run
  @deployer = Autodeploy::Deployer.new(@config, job)
  @deployer.run()

  return unless job['result'] == 'SUCCESS'

  build_action = job['actions'].detect{|action| action.include?('lastBuiltRevision')}
  sha = build_action.nil? ? nil : build_action['lastBuiltRevision']['SHA1']

  filename = [job_number, sha, job['artifacts'][0]['fileName']].compact.join('-')
  filepath = File.join(@config['download_dir'], filename)

  if File.exists?(filepath)
    Autodeploy.log "#{filepath} already exists"
    return
  end

  uri = URI("#{job['url']}artifact/#{job['artifacts'][0]['relativePath']}")

  Autodeploy.log "Starting download: #{uri.path}"

  Net::HTTP.start(uri.host, uri.port, use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
    request = Net::HTTP::Get.new uri.request_uri
    request.basic_auth @config['username'], @config['password']

    resp = http.request(request)

    if resp.code != "200"
      Autodeploy.log "Error downloading artifact: #{resp.code} - #{resp.message}"
      return
    end

    Autodeploy.log "Saving #{filename}"

    open("downloads/#{filename}", "wb") do |file|
      file.write(resp.body)
      Autodeploy.log "Completed download - #{filename}"
    end
  end
end