Class: Kodiak::Transporter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, options) ⇒ Transporter

Returns a new instance of Transporter.



6
7
8
9
10
11
# File 'lib/kodiak/transporter.rb', line 6

def initialize(config, options)
	@config 	= config
	@files 		= @config.files
	@files 		= @config.files
	@options 	= options
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/kodiak/transporter.rb', line 4

def config
  @config
end

#filesObject

Returns the value of attribute files.



4
5
6
# File 'lib/kodiak/transporter.rb', line 4

def files
  @files
end

#ignoredObject

Returns the value of attribute ignored.



4
5
6
# File 'lib/kodiak/transporter.rb', line 4

def ignored
  @ignored
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/kodiak/transporter.rb', line 4

def options
  @options
end

#pushedObject

Returns the value of attribute pushed.



4
5
6
# File 'lib/kodiak/transporter.rb', line 4

def pushed
  @pushed
end

Instance Method Details

#ftpObject



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
99
100
101
102
# File 'lib/kodiak/transporter.rb', line 65

def ftp
	require 'net/ftp'
	credentials = @config.ftp

	puts "Connecting to #{credentials['server']}..."

  Net::FTP.open credentials['server'] do |ftp|
    ftp. credentials['username'], credentials['password']
    ftp.chdir credentials['path']

		@files.each do |file|
			source 			= file[:source]
			destination = file[:destination]

			if not File.directory? source
				folders = destination.split("/")
				filename = folders.pop

				folders.each do |folder|
					if not ftp.nlst.index(folder)
						puts "- /#{folder} not found. Creating."
						ftp.mkdir folder
					end
					puts "- opening directory /#{folder}"
					ftp.chdir folder
				end

				puts "+ pushing #{filename}"
				if File.directory? filename
					ftp.mkdir filename
				else
					ftp.put source, filename
				end
				folders.length.times { ftp.chdir("..") }
			end
		end
  end
end

#ignore(file, reason) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/kodiak/transporter.rb', line 117

def ignore(file, reason)
	file[:reason] = reason
	if @options[:verbose] 
 		Kodiak::Notification.new "Ignored #{file[:source]}\n     - #{file[:reason]}\n"
	else
 		puts "Ignored #{file[:source]}\n     - #{file[:reason]}\n"	
	end
	
	@ignored.push file
end

#localObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kodiak/transporter.rb', line 26

def local
	puts "\nPushing to [#{@options[:environment]}]"

	@files.each do |file|

		if @options[:force]
			push file

		elsif File.exists? file[:destination]
			source_changed 			= File.ctime(file[:source])
			destination_changed = File.ctime(file[:destination])

			# check if destination is older than source
			if (destination_changed <=> source_changed) == -1
				push file
			else
				ignore file, "Destination file was newer than source."
			end

		else
			push file
		end
	end

	if ! @options[:quiet]
  	Kodiak::Notification.new "\nPushed #{@pushed.length} files, Ignored #{@ignored.length} files\n\n", "success"
	else
		puts "\nPushed #{@pushed.length} files, Ignored #{@ignored.length} files\n\n"
	end

	if @options[:verbose] && @ignored.length > 0
		puts "Some files were ignored. Use --force to force overwrite.\n\n"
	end
	
	Kodiak.log :pushed => @pushed, :ignored => @ignored
	
end

#push(file) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/kodiak/transporter.rb', line 105

def push(file)
	FileUtils.cp_r file[:source], file[:destination], :remove_destination => true
	
	if @options[:verbose] 
  	Kodiak::Notification.new "Pushed #{file[:source]} --> #{file[:destination]}\n"
	else
		puts " - Pushed #{file[:source]} --> #{file[:destination]}\n"
	end

	@pushed.push file
end

#transport(files = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/kodiak/transporter.rb', line 14

def transport(files = nil)
	@pushed = []
	@ignored = []
	@files = files || @files
	if @config.ftp?
		ftp
	else
		local
	end
end