1 2 3 4 5 6 7 8 9 10 11 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 47 48 49 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
# = UploadUtils
#
# Upload files to host. These means of uploading are current
# supported: ftp, sftp, scp and rsync.
#
# user Username for host.
# host Host server's domain name.
# root Document root path on host.
# copy Directory of files to publish, or
# Files to publish using from and to.
#
# dryrun If true only pretend to upload.
# quiet Supress all output.
# verbose Provide extra details.
#
# The copy parameter allows you to simply specify a file
# or directory which will be published to host's document
# root location.
#
# If you need more control over which files to publish
# where, you can use the copy parameter instead. Provide
# an array of pattern strings in the form of "{from} {to}".
# If the desitination is the host's document root you do
# not need to specify the {to} part. For example:
#
# copy = [ 'web/*', 'doc/api/* doc/api' ]
#
# The first copies the files under your project's web directory
# to the host's document root. The second copies your projects
# doc/api files to the doc/api location on the host.
#
# The internal template used for the outbound destination
# is 'username@host:root/'.
#
# == AUTHORS
#
# * Thomas Sawyer
#
# == TODOs
#
# * Needs general improvements.
# * Reduce shelling-out.
# * Incorporate password into scp and ftp ?
# * rsync needs --delete option
#
# == COPYRIGHT
#
# Copyright (c) 2006 Thomas Sawyer
#
# == LICENSE
#
# Ruby License
#
# This module is free software. You may use, modify, and/or redistribute this
# software under the same terms as Ruby.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.
warn "WARNING: facets/uploadutils.rb will be deprecated. Use Folio or alternate solution for future versions."
require 'openssl'
require 'shellwords'
require 'tmpdir'
require 'facets/openobject'
# = UploadUtils
#
# Upload files to host. These means of uploading are current
# supported: ftp, sftp, scp and rsync.
#
# user Username for host.
# host Host server's domain name.
# root Document root path on host.
# copy Directory of files to publish, or
# Files to publish using from and to.
#
# dryrun If true only pretend to upload.
# quiet Supress all output.
# verbose Provide extra details.
#
# The copy parameter allows you to simply specify a file
# or directory which will be published to host's document
# root location.
#
# If you need more control over which files to publish
# where, you can use the copy parameter instead. Provide
# an array of pattern strings in the form of "{from} {to}".
# If the desitination is the host's document root you do
# not need to specify the {to} part. For example:
#
# copy = [ 'web/*', 'doc/api/* doc/api' ]
#
# The first copies the files under your project's web directory
# to the host's document root. The second copies your projects
# doc/api files to the doc/api location on the host.
#
# The internal template used for the outbound destination
# is 'username@host:root/'.
module UploadUtils
module_function
#
# Upload via given protocol.
#
def upload( protocol, opts )
send(protocol.to_s.downcase,opts)
end
#
# Use ftp to upload files.
#
def ftp( keys )
keys = upload_parameters(keys)
# set transfer rules
if keys.stage
trans = stage_transfer(keys.stage)
else
files(keys.dir, keys.copy).each do |from|
trans << [from,from]
end
end
# append location of publication dir to from
dir = keys.dir
trans.collect!{ |from,to| [File.join(dir,from), to] }
if keys.dryrun
puts "ftp open #{keys.user}@#{keys.host}:#{keys.root}/"
keys.trans.each do |f, t|
puts "ftp put #{f} #{t}"
end
else
require 'net/ftp'
Net::FTP.open(keys.host) do |ftp|
ftp.login(keys.user) #password?
ftp.chdir(keys.root)
keys.trans.each do |f, t|
puts "ftp #{f} #{t}" unless keys.quiet
ftp.putbinaryfile( f, t, 1024 )
end
end
end
end
#
# Use sftp to upload files.
#
def sftp( keys )
keys = upload_parameters(keys)
# set transfer rules
if keys.stage
trans = stage_transfer(keys.stage)
else
files(keys.dir, keys.copy).each do |from|
trans << [from,from]
end
end
# append location of publication dir to from
dir = keys.dir
trans.collect!{ |from,to| [File.join(dir,from), to] }
if keys.dryrun
puts "sftp open #{keys.user}@#{keys.host}:#{keys.root}/"
keys.trans.each do |f,t|
puts "sftp put #{f} #{t}"
end
else
require 'net/sftp'
Net::SFTP.start(keys.host, keys.user, keys.pass) do |sftp|
#sftp.login( user )
sftp.chdir(keys.root)
keys.trans.each do |f,t|
puts "sftp #{f} #{t}" unless keys.quiet
sftp.put_file(f,t) #, 1024 )
end
end
end
end
#
# Use rsync to upload files.
#
def rsync( keys )
keys = upload_parameters(keys)
flags = []
flags << "-n" if keys.dryrun
flags << "-q" if keys.quiet
flags << "-v" if keys.verbose
flags << "--progress" unless keys.quiet
flags = flags.join(' ').strip
flags = ' ' + flags unless flags.empty?
manfile = 'Publish.txt'
if keys.stage
dir = stage_linkdir(keys.dir, keys.stage)
Dir.chdir(dir) do
cpy = files(keys.copy)
end
manifest = File.join(dir,manfile)
cmd = %{rsync#{flags} -L -arz --files-from='#{manifest}' #{dir} #{keys.user}@#{keys.host}:/#{keys.root}}
else
dir = keys.dir
cpy = files(dir, keys.copy)
manifest = File.join(dir,manfile)
cmd = %{rsync#{flags} -arz --files-from='#{manifest}' #{dir} #{keys.user}@#{keys.host}:/#{keys.root}}
end
#Dir.chdir(keys.dir) do
begin
File.open(manifest, 'w'){ |f| f << cpy.join("\n") }
ENV['RSYNC_PASSWORD'] = keys.pass if keys.pass
puts cmd unless keys.quiet
system cmd
ensure
ENV.delete('RSYNC_PASSWORD') if keys.pass
end
#end
end
# private (can't do b/c of module_function)
# parse publishing options.
def upload_parameters( keys )
keys = OpenObject.new(keys)
keys.copy = keys.copy || '**/*'
keys.host = keys.host || keys.domain
keys.user = keys.user || keys.username
keys.root = keys.root || '/'
#keys.pass = keys.pass || keys.password
# validate
raise ArgumentError, "missing publish parameter -- dir" unless keys.dir
raise ArgumentError, "missing publish parameter -- host" unless keys.host
raise ArgumentError, "missing publish parameter -- user" unless keys.user
#raise ArgumentError, "missing publish parameter -- copy" unless keys.copy
#raise ArgumentError, "missing publish parameter -- root" unless keys.root
keys.root = '' if keys.root.nil?
keys.root.sub!(/^\//,'')
if String===keys.copy and File.directory?(keys.copy)
copy = File.join(keys.copy, '*')
end
keys.copy = [keys.copy].flatten.compact
# trans = []
# keys.copy.each do |from|
# #from, to = *Shellwords.shellwords(c)
# #to = from if to.nil?
# #to = to[1..-1] if to[0,1] == '/'
# from.sub('*','**/*') unless from =~ /\*\*/
# files = Dir.glob(from)
# files.each do |f|
# #t = File.join(to,File.basename(f))
# #t = t[1..-1] if t[0,1] == '/'
# trans << [f,f]
# end
# end
# keys.trans = trans
return keys
end
# Put together the list of files to copy.
def files( dir, copy )
Dir.chdir(dir) do
del, add = copy.partition{ |f| /^[-]/ =~ f }
# remove - and + prefixes
del.collect!{ |f| f.sub(/^[-]/,'') }
add.collect!{ |f| f.sub(/^[+]/,'') }
#del.concat(must_exclude)
files = []
add.each{ |g| files += Dir.multiglob(g) }
del.each{ |g| files -= Dir.multiglob(g) }
files.collect!{ |f| f.sub(/^\//,'') }
return files
end
end
# Combine three part stage list into a two part from->to list.
#
# Using the stage list of three space separated fields.
#
# fromdir file todir
#
# This is used to generate a from -> to list of the form:
#
# fromdir/file todir/file
#
def stage_transfer( list )
trans = []
list.each do |line|
trans << Shellwords.shellwords(line)
end
trans.collect! do |from, base, to|
file = File.join(from,base)
to = File.join(to,base)
[from, to]
end
end
# When using stage options this will create temporary linked location.
def stage_linkdir( dir, list )
folder = File.join(Dir.tmpdir, 'ratchets', 'project', object_id.abs.to_s)
FileUtils.mkdir_p(folder)
Dir.chdir(dir) do
stage_transfer(list).each do |file, to|
link = File.join(folder,to)
FileUtils.ln_s(link,file)
end
end
return folder
end
=begin
#--
# SHELLS OUT! Need net/scp library to fix.
#++
# Use scp to upload files.
def scp( keys )
keys = upload_parameters(keys)
flags = []
flags << "-v" if keys.verbose
flags << "-q" if keys.quiet
flags = flags.join(' ').strip
flags = ' ' + flags unless flags.empty?
upload_stage(keys) do #|tmpdir|
cmd = "scp -r#{flags} * #{keys.user}@#{keys.host}:/#{keys.root}"
puts cmd unless keys.quiet
system cmd unless keys.dryrun
end
end
# Use rsync to upload files.
def rsync( keys )
keys = upload_parameters(keys)
flags = []
flags << "-n" if keys.dryrun
flags << "-v" if keys.verbose
flags << "-q" if keys.quiet
flags = flags.join(' ').strip
flags = ' ' + flags unless flags.empty?
upload_stage(keys) do #|tmpdir|
begin
ENV['RSYNC_PASSWORD'] = keys.pass if keys.pass
cmd = "rsync -R#{flags} -arz * #{keys.user}@#{keys.host} /#{keys.root}"
ensure
ENV.delete('RSYNC_PASSWORD') if keys.pass
end
end
end
# Use ftp to upload files.
def ftp( keys )
keys = upload_parameters(keys)
if keys.dryrun
puts "ftp open #{keys.user}@#{keys.host}:#{keys.root}/"
keys.trans.each do |f, t|
puts "ftp put #{f} #{t}"
end
else
require 'net/ftp'
Net::FTP.open(keys.host) do |ftp|
ftp.login(keys.user) #password?
ftp.chdir(keys.root)
keys.trans.each do |f, t|
puts "ftp #{f} #{t}" unless keys.quiet
ftp.putbinaryfile( f, t, 1024 )
end
end
end
end
# Use sftp to upload files.
def sftp( keys )
keys = upload_parameters(keys)
if keys.dryrun
puts "sftp open #{keys.user}@#{keys.host}:#{keys.root}/"
keys.trans.each do |f,t|
puts "sftp put #{f} #{t}"
end
else
require 'net/sftp'
Net::SFTP.start(keys.host, keys.user, keys.pass) do |sftp|
#sftp.login( user )
sftp.chdir(keys.root)
keys.trans.each do |f,t|
puts "sftp #{f} #{t}" unless keys.quiet
sftp.put_file(f,t) #, 1024 )
end
end
end
end
# Creates a stage from which the whole directory can be uploaded.
# This is needed for scp and rsync which have to shelled out,
# and can't conveniently copy one file at a time.
def upload_stage(keys) #:yield:
tmp = "scp_#{object_id.abs}_#{ Time.now.strftime("%Y%m%d%H%M%S")}"
tmpdir = File.join(Dir.tmpdir,tmp)
puts "mkdir -p #{tmpdir}" unless keys.quiet
FileUtils.mkdir_p(tmpdir) # go ahead and do this even if dryrun
fu = keys.dryrun ? FileUtils::DryRun : FileUtils
keys.trans.each do |f, t|
to = File.join(tmpdir, t)
fu.mv(f,to)
end
puts "cd #{tmpdir}" unless keys.quiet
Dir.chdir(tmpdir) do
yield #(tmpdir)
end
puts "rm -r #{tmpdir}" unless keys.quiet
FileUtils.rm_r(tmpdir) # now remove the temp dir
end
=end
end
|