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



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

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



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

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



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

def deploy
  Autodeploy.log "Lock acquired"

  begin
    return unless move_artifact_to_deploy_dir

    record_successful_deployment
  ensure
    release_lock_from_mysql
  end
end

#get_build_to_deploy(job_name) ⇒ Object



59
60
61
# File 'lib/autodeploy/deployer.rb', line 59

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

#has_file_been_deployedObject



74
75
76
77
78
# File 'lib/autodeploy/deployer.rb', line 74

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



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

def has_file_been_downloaded
			File.exists?(@filepath)
end

#lock_and_deployObject



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

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



98
99
100
101
102
103
104
105
106
107
# File 'lib/autodeploy/deployer.rb', line 98

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

	cmd = "mv -f #{@filepath} #{@config['deploy_dir']}/#{@config['deploy_artifact_name']}"
	output = system(cmd)

    Autodeploy.log output

    output
end

#ready_to_deploy?(job_name) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/autodeploy/deployer.rb', line 55

def ready_to_deploy?(job_name)
  Models::DeployBuild.where(job_name: job_name).order("created_at DESC").exists?
end

#record_successful_deploymentObject



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

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



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

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
22
23
# File 'lib/autodeploy/deployer.rb', line 10

def run
    return unless ready_to_deploy?(@config['job'])

	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