Class: Autodeploy::Deployer

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

Instance Method Summary collapse

Constructor Details

#initialize(yaml_config, jenkins_job) ⇒ Deployer

Returns a new instance of Deployer.



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

def initialize(yaml_config, jenkins_job)
	@config = yaml_config
	@job = jenkins_job
end

Instance Method Details

#acquire_lock_from_mysqlObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/autodeploy/deployer.rb', line 74

def acquire_lock_from_mysql
    Autodeploy.log "Attempting to acquire lock..."

	begin
		Models::DeployLock.create(job_name: @config['job'],
                                host: Socket.gethostname,
                                created_at: Time.now.to_i * 1000)
	rescue ActiveRecord::RecordNotUnique
		nil
	end
end

#build_full_file_path(deploy_build) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/autodeploy/deployer.rb', line 44

def build_full_file_path(deploy_build)
	artifact_file_name = @job['artifacts'][0]['fileName']

	Autodeploy.log "DeployBuild: #{deploy_build}"

	filename = [deploy_build.build_number, deploy_build.git_sha, artifact_file_name].compact.join('-')
	@filepath = File.join(@config['download_dir'], filename)
end

#deployObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/autodeploy/deployer.rb', line 32

def deploy
  Autodeploy.log "Lock acquired"

  begin
    move_artifact_to_deploy_dir

    record_successful_deployment
  ensure
    release_lock_from_mysql
  end
end

#get_build_to_deploy(job_name) ⇒ Object



53
54
55
# File 'lib/autodeploy/deployer.rb', line 53

def get_build_to_deploy(job_name)
	Models::DeployBuild.where(job_name: job_name).order("created_at DESC").first
end

#has_file_been_deployedObject



68
69
70
71
72
# File 'lib/autodeploy/deployer.rb', line 68

def has_file_been_deployed()
    Models::DeployAudit.where(host: Socket.gethostname,
                              build_number: @job['number'],
                              jenkins_build_timestamp: @job['timestamp']).exists?
end

#has_file_been_downloadedObject



57
58
59
# File 'lib/autodeploy/deployer.rb', line 57

def has_file_been_downloaded()
			File.exists?(@filepath)
end

#lock_and_deployObject



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

def lock_and_deploy
  locked = acquire_lock_from_mysql
  if locked
    deploy
  else
    Autodeploy.log "Waiting for lock..."
  end
end

#move_artifact_to_deploy_dirObject



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

def move_artifact_to_deploy_dir
    Autodeploy.log "Moving aritifact to deploy dir"

	cmd = "mv #{@filepath} #{@config['deploy_dir']}/#{@config['deploy_artifact_name']}"
	success = system(cmd)
end

#record_successful_deploymentObject



61
62
63
64
65
66
# File 'lib/autodeploy/deployer.rb', line 61

def record_successful_deployment()
  Models::DeployAudit.create(host: Socket.gethostname,
                             build_number: @job['number'],
                             jenkins_build_timestamp: @job['timestamp'],
                             created_at:  Time.now.to_i * 1000)
end

#release_lock_from_mysqlObject



86
87
88
89
90
# File 'lib/autodeploy/deployer.rb', line 86

def release_lock_from_mysql
    Autodeploy.log "Releasing lock..."
	lock = Models::DeployLock.where(job_name: @config['job'], host: Socket.gethostname).first
	lock.delete
end

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/autodeploy/deployer.rb', line 10

def run()
	deploy_build = get_build_to_deploy(@config['job'])
	build_full_file_path(deploy_build)

	file_downloaded = has_file_been_downloaded
	return unless file_downloaded
	
	file_deployed = has_file_been_deployed
	return if file_deployed

    lock_and_deploy
end