Class: Deploy::Environment

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, hash) ⇒ Environment

Returns a new instance of Environment.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/deploy/environment.rb', line 12

def initialize(name, hash)

	@name = name
	@config = { :host => '', :port => '',  :user => '', :private_key => '', :local => '', :remote => '' }
	@options = { :verbose => false, :sync => true }

	@config[:host] = hash['host']
	@config[:port] = hash['port'] || '22'
	
	@config[:user] = hash['user'] || ''
	@config[:private_key] = hash['privateKey'] || ''

	@config[:local] = hash['path']['local'].gsub(/\s+/, "")
	@config[:remote] = hash['path']['remote'].gsub(/\s+/, "")

	# Set to project and server root if local/remote are empty
	@config[:local] = '/' if @config[:local].empty?
	@config[:local].insert(0,'/') unless @config[:local].start_with?('/')
	@config[:remote] = '/' if @config[:remote].empty?
	@config[:remote].insert(0,'/') unless @config[:remote].start_with?('/')


	@config[:excludes] = hash['exclude'] || Array.new

	@options[:reverse] = false
	@options[:verbose] = hash['verbose'].to_s.empty? ? false : hash['verbose']
	@options[:sync] = hash['sync'].to_s.empty? ? true : hash['sync']

	validate

	# TODO: Find a way to do a setter for @config[:excludes] instead of
	# freezing the hash
	@config[:excludes].freeze

end

Instance Attribute Details

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/deploy/environment.rb', line 10

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/deploy/environment.rb', line 9

def name
  @name
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/deploy/environment.rb', line 10

def options
  @options
end

Instance Method Details

#deployObject

Runs the deploy based upon the current @config and @options settings Creates rsync command and creates exclude file then cleans up



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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/deploy/environment.rb', line 50

def deploy

	# Create excludes file if needed
	unless @config[:excludes].empty?
		tmp_exclude = Tempfile.new(['excludes','.txt'])
		@config[:excludes].each do |ex|
			tmp_exclude.puts ex
		end
		# Don't upload the deploy configuration file
		tmp_exclude.puts Deploy::CONFIG_PATH 
		tmp_exclude.puts ".git" # Never sync git files
		tmp_exclude.close
	end

	rsync_cmd = 'rsync -a'																																# Always keep permissions
	rsync_cmd += 'v' if @options[:verbose]																									# Verbose if requested
	rsync_cmd += 'z'																																			# Always zip files

	rsync_cmd += ' --progress'																														# Always show progress
	
	if (@options[:sync] && !@options[:reverse])
		rsync_cmd += ' --force --delete --delete-excluded'    																# Sync unless explicitly requested or downloading
	end
	rsync_cmd += " --exclude-from=#{tmp_exclude.path}" unless @config[:excludes].empty?			# Include exclude file if it exists
	
	rsync_cmd += " -e \"ssh "																															# Pass custom ssh options
	rsync_cmd += " -i #{@config[:private_key]} " unless @config[:private_key].empty?					# Include a custom private key if requested
	rsync_cmd += "-p #{@config[:port]}\" "																									# Choose port if specified

	unless @options[:reverse]
		rsync_cmd += `pwd`.gsub(/\s+/, "") + "#{@config[:local]} "														# The local path from the current directory
	end

	rsync_cmd += "#{@config[:user]}@" unless @config[:user].empty?													# Only add user if not empty
	rsync_cmd += "#{@config[:host]}:"																											# Add host
	rsync_cmd += "~#{@config[:remote]}"																										# Add remote (relative to remote user home)
	
	if @options[:reverse]
		rsync_cmd += " " + `pwd`.gsub(/\s+/, "") + "#{@config[:local]}"												# Put the local path after if we're downloading
	end
	
	# Run the command
	# puts rsync_cmd
	system(rsync_cmd)

	# Remove excludes file if needed
	tmp_exclude.unlink unless @config[:excludes].empty?

end