| Class | BoxGrinder::SFTPPlugin |
| In: |
lib/boxgrinder-build/plugins/delivery/sftp/sftp-plugin.rb
lib/boxgrinder-build/plugins/delivery/sftp/sftp-plugin.rb |
| Parent: | BasePlugin |
# File lib/boxgrinder-build/plugins/delivery/sftp/sftp-plugin.rb, line 28
28: def after_init
29: set_default_config_value('overwrite', false)
30: set_default_config_value('default_permissions', 0644)
31:
32: register_deliverable(:package => "#{@appliance_config.name}-#{@appliance_config.version}.#{@appliance_config.release}-#{@appliance_config.os.name}-#{@appliance_config.os.version}-#{@appliance_config.hardware.arch}-#{current_platform}.tgz")
33: end
# File lib/boxgrinder-build/plugins/delivery/sftp/sftp-plugin.rb, line 28
28: def after_init
29: set_default_config_value('overwrite', false)
30: set_default_config_value('default_permissions', 0644)
31:
32: register_deliverable(:package => "#{@appliance_config.name}-#{@appliance_config.version}.#{@appliance_config.release}-#{@appliance_config.os.name}-#{@appliance_config.os.version}-#{@appliance_config.hardware.arch}-#{current_platform}.tgz")
33: end
# File lib/boxgrinder-build/plugins/delivery/sftp/sftp-plugin.rb, line 55
55: def connect
56: @log.info "Connecting to #{@plugin_config['host']}..."
57: @ssh = Net::SSH.start(@plugin_config['host'], @plugin_config['username'], {:password => @plugin_config['password']})
58: end
# File lib/boxgrinder-build/plugins/delivery/sftp/sftp-plugin.rb, line 55
55: def connect
56: @log.info "Connecting to #{@plugin_config['host']}..."
57: @ssh = Net::SSH.start(@plugin_config['host'], @plugin_config['username'], {:password => @plugin_config['password']})
58: end
# File lib/boxgrinder-build/plugins/delivery/sftp/sftp-plugin.rb, line 60
60: def connected?
61: return true if !@ssh.nil? and !@ssh.closed?
62: false
63: end
# File lib/boxgrinder-build/plugins/delivery/sftp/sftp-plugin.rb, line 60
60: def connected?
61: return true if !@ssh.nil? and !@ssh.closed?
62: false
63: end
# File lib/boxgrinder-build/plugins/delivery/sftp/sftp-plugin.rb, line 65
65: def disconnect
66: @log.info "Disconnecting from #{@plugin_config['host']}..."
67: @ssh.close if connected?
68: @ssh = nil
69: end
# File lib/boxgrinder-build/plugins/delivery/sftp/sftp-plugin.rb, line 65
65: def disconnect
66: @log.info "Disconnecting from #{@plugin_config['host']}..."
67: @ssh.close if connected?
68: @ssh = nil
69: end
# File lib/boxgrinder-build/plugins/delivery/sftp/sftp-plugin.rb, line 35
35: def execute( type = :sftp )
36: validate_plugin_config(['path', 'username', 'host'], 'http://boxgrinder.org/tutorials/boxgrinder-build-plugins/#SFTP_Delivery_Plugin')
37:
38: PackageHelper.new(@config, @appliance_config, :log => @log, :exec_helper => @exec_helper).package( File.dirname(@previous_deliverables[:disk]), @deliverables[:package] )
39:
40: @log.info "Uploading #{@appliance_config.name} appliance via SSH..."
41:
42: begin
43: #TODO move to a block
44: connect
45: upload_files(@plugin_config['path'], File.basename(@deliverables[:package]) => @deliverables[:package])
46: disconnect
47:
48: @log.info "Appliance #{@appliance_config.name} uploaded."
49: rescue => e
50: @log.error e
51: @log.error "An error occurred while uploading files."
52: end
53: end
# File lib/boxgrinder-build/plugins/delivery/sftp/sftp-plugin.rb, line 35
35: def execute( type = :sftp )
36: validate_plugin_config(['path', 'username', 'host'], 'http://boxgrinder.org/tutorials/boxgrinder-build-plugins/#SFTP_Delivery_Plugin')
37:
38: PackageHelper.new(@config, @appliance_config, :log => @log, :exec_helper => @exec_helper).package( File.dirname(@previous_deliverables[:disk]), @deliverables[:package] )
39:
40: @log.info "Uploading #{@appliance_config.name} appliance via SSH..."
41:
42: begin
43: #TODO move to a block
44: connect
45: upload_files(@plugin_config['path'], File.basename(@deliverables[:package]) => @deliverables[:package])
46: disconnect
47:
48: @log.info "Appliance #{@appliance_config.name} uploaded."
49: rescue => e
50: @log.error e
51: @log.error "An error occurred while uploading files."
52: end
53: end
# File lib/boxgrinder-build/plugins/delivery/sftp/sftp-plugin.rb, line 71
71: def upload_files(path, files = {})
72: return if files.size == 0
73:
74: raise "You're not connected to server" unless connected?
75:
76: @log.debug "Files to upload:"
77:
78: files.each do |remote, local|
79: @log.debug "#{File.basename(local)} => #{path}/#{remote}"
80: end
81:
82: global_size = 0
83:
84: files.each_value do |file|
85: global_size += File.size(file)
86: end
87:
88: global_size_kb = global_size / 1024
89: global_size_mb = global_size_kb / 1024
90:
91: @log.info "#{files.size} files to upload (#{global_size_mb > 0 ? global_size_mb.to_s + "MB" : global_size_kb > 0 ? global_size_kb.to_s + "kB" : global_size.to_s})"
92:
93: @ssh.sftp.connect do |sftp|
94: begin
95: sftp.stat!(path)
96: rescue Net::SFTP::StatusException => e
97: raise unless e.code == 2
98: @ssh.exec!("mkdir -p #{path}")
99: end
100:
101: nb = 0
102:
103: files.each do |key, local|
104: name = File.basename(local)
105: remote = "#{path}/#{key}"
106: size_b = File.size(local)
107: size_kb = size_b / 1024
108: nb_of = "#{nb += 1}/#{files.size}"
109:
110: begin
111: sftp.stat!(remote)
112:
113: unless @plugin_config['overwrite']
114:
115: local_md5_sum = `md5sum #{local} | awk '{ print $1 }'`.strip
116: remote_md5_sum = @ssh.exec!("md5sum #{remote} | awk '{ print $1 }'").strip
117:
118: if (local_md5_sum.eql?(remote_md5_sum))
119: @log.info "#{nb_of} #{name}: files are identical (md5sum: #{local_md5_sum}), skipping..."
120: next
121: end
122: end
123:
124: rescue Net::SFTP::StatusException => e
125: raise unless e.code == 2
126: end
127:
128: @ssh.exec!("mkdir -p #{File.dirname(remote) }")
129:
130: pbar = ProgressBar.new("#{nb_of} #{name}", size_b)
131: pbar.file_transfer_mode
132:
133: sftp.upload!(local, remote) do |event, uploader, * args|
134: case event
135: when :open then
136: when :put then
137: pbar.set(args[1])
138: when :close then
139: when :mkdir then
140: when :finish then
141: pbar.finish
142: end
143: end
144:
145: sftp.setstat(remote, :permissions => @plugin_config['default_permissions'])
146: end
147: end
148: end
# File lib/boxgrinder-build/plugins/delivery/sftp/sftp-plugin.rb, line 71
71: def upload_files(path, files = {})
72: return if files.size == 0
73:
74: raise "You're not connected to server" unless connected?
75:
76: @log.debug "Files to upload:"
77:
78: files.each do |remote, local|
79: @log.debug "#{File.basename(local)} => #{path}/#{remote}"
80: end
81:
82: global_size = 0
83:
84: files.each_value do |file|
85: global_size += File.size(file)
86: end
87:
88: global_size_kb = global_size / 1024
89: global_size_mb = global_size_kb / 1024
90:
91: @log.info "#{files.size} files to upload (#{global_size_mb > 0 ? global_size_mb.to_s + "MB" : global_size_kb > 0 ? global_size_kb.to_s + "kB" : global_size.to_s})"
92:
93: @ssh.sftp.connect do |sftp|
94: begin
95: sftp.stat!(path)
96: rescue Net::SFTP::StatusException => e
97: raise unless e.code == 2
98: @ssh.exec!("mkdir -p #{path}")
99: end
100:
101: nb = 0
102:
103: files.each do |key, local|
104: name = File.basename(local)
105: remote = "#{path}/#{key}"
106: size_b = File.size(local)
107: size_kb = size_b / 1024
108: nb_of = "#{nb += 1}/#{files.size}"
109:
110: begin
111: sftp.stat!(remote)
112:
113: unless @plugin_config['overwrite']
114:
115: local_md5_sum = `md5sum #{local} | awk '{ print $1 }'`.strip
116: remote_md5_sum = @ssh.exec!("md5sum #{remote} | awk '{ print $1 }'").strip
117:
118: if (local_md5_sum.eql?(remote_md5_sum))
119: @log.info "#{nb_of} #{name}: files are identical (md5sum: #{local_md5_sum}), skipping..."
120: next
121: end
122: end
123:
124: rescue Net::SFTP::StatusException => e
125: raise unless e.code == 2
126: end
127:
128: @ssh.exec!("mkdir -p #{File.dirname(remote) }")
129:
130: pbar = ProgressBar.new("#{nb_of} #{name}", size_b)
131: pbar.file_transfer_mode
132:
133: sftp.upload!(local, remote) do |event, uploader, * args|
134: case event
135: when :open then
136: when :put then
137: pbar.set(args[1])
138: when :close then
139: when :mkdir then
140: when :finish then
141: pbar.finish
142: end
143: end
144:
145: sftp.setstat(remote, :permissions => @plugin_config['default_permissions'])
146: end
147: end
148: end