loft-harmony 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/bin/harmony +95 -3
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a787fc1aa34315a42462695d17d3a0acb1a52fe1
4
- data.tar.gz: 41a7edff4216a52c3818a61df9d34b15bad31698
3
+ metadata.gz: da2f7c4085dda85b1769c604bc1e17777a7dbca8
4
+ data.tar.gz: da5c724aea1fd7e80ecdb5e1ce750d22995636c1
5
5
  SHA512:
6
- metadata.gz: c7274be884f2c6f0f87a525300560dd8869796e7527ce4366d2d03b17537346692f77a892d2d2c4b3f96c1f12ff4e6c7359ba2a6ec795c2c00f8144a4bbac6c8
7
- data.tar.gz: 3f804e9eab4c49689d7a5c9a5a0c0b19e56f176c88dcc5ed7262175d04c26ce75057beff2c72f69d53164f8ce427287befae60c6d7bccc62734833c7682c829e
6
+ metadata.gz: 4346d7331a6f37f6eaddd0e6ef6d48ed25cb2c203d928accdfd151824020393de86e41551cb749a119fdc53262bbb722464e32637d73ad72d9c8e5b03cd7af0d
7
+ data.tar.gz: 4dfefa76e263737a95901c8e7f8ae0891f0025a3704a22b18519f40e9ff000b9c0ad78d0b6a611e2243dacc1890a04f3b87d03b8efd38c41a2f96357bd1bbe34
@@ -1,8 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'net/ftp'
3
+ require 'net/sftp'
3
4
  require 'terrun'
4
5
  require 'timeout'
5
6
  require 'fileutils'
7
+ require 'byebug'
6
8
 
7
9
  class Harmony < TerminalRunner
8
10
  name "Harmony"
@@ -21,6 +23,7 @@ class Harmony < TerminalRunner
21
23
  option "--passive", 0, "", "Use FTP in passive mode."
22
24
  option "--vanilla_ftp", 0, "", "Use Vanilla ruby Net::FTP. Disables automatica directory creation."
23
25
  option "--local", 0, "", "Use a local copy instead of an FTP transfer."
26
+ option "--sftp", 1, "port", "Use SFTP instead of standard FTP"
24
27
 
25
28
  help ""
26
29
 
@@ -42,6 +45,8 @@ class Harmony < TerminalRunner
42
45
  @compile_coffeescript = @@options.include?("--coffee") ? @@options["--coffee"][0] : nil
43
46
  @passive_mode = @@options.include?("--passive")
44
47
  @use_vanilla_ftp = @@options.include?("--vanilla_ftp")
48
+ @use_sftp = @@options.include?("--sftp")
49
+ @sftp_port = @@options["--sftp"][0]
45
50
  @use_local = @@options.include?("--local")
46
51
  @ignored = []
47
52
 
@@ -92,8 +97,9 @@ class Harmony < TerminalRunner
92
97
  break if self.get_command
93
98
  end
94
99
  rescue => e
95
- puts e.to_s.red
96
100
  puts " ## FATAL ERROR OCCURRED. SHUTTING DOWN.".red
101
+ puts "#{e.class.name}: #{e.to_s} (#{e.message})".red
102
+ puts e.backtrace
97
103
  end
98
104
  @thread.kill if @thread
99
105
  self.close_connection
@@ -181,7 +187,7 @@ class Harmony < TerminalRunner
181
187
  rescue Timeout::Error
182
188
  failed = true
183
189
  puts " ## [ FAIL ] #{file} timed out while syncing".red
184
- rescue Net::FTPError => e
190
+ rescue Net::FTPError, Net::SFTP::Exception, Net::SSH::Exception => e
185
191
  failed = true
186
192
  puts " ## [ FAIL ] #{file} failed to sync".red
187
193
  puts e.message
@@ -234,7 +240,7 @@ class Harmony < TerminalRunner
234
240
  if @ftp
235
241
  begin
236
242
  @ftp.list
237
- rescue Net::FTPError
243
+ rescue Net::FTPError, Net::SFTP::Exception, Net::SSH::Exception
238
244
  puts " ## Connection was closed by server".pink
239
245
  @ftp.close
240
246
  end
@@ -248,7 +254,11 @@ class Harmony < TerminalRunner
248
254
  elsif @use_local
249
255
  puts "(Using Local non-FTP)".pink
250
256
  @ftp = LocalCopyFTP.new
257
+ elsif @use_sftp
258
+ puts "(Using SFTP)".pink
259
+ @ftp = SFTP.new(@server, @user, @password, @sftp_port)
251
260
  else
261
+ puts "(Using FTP)".pink
252
262
  @ftp = BetterFTP.new(@server, @user, @password)
253
263
  end
254
264
  @ftp.passive = @passive_mode
@@ -299,6 +309,88 @@ class String
299
309
  end
300
310
  end
301
311
 
312
+ # Wrapper interface for SFTP to make it confirm to the same API
313
+ class SFTP
314
+ @dir = '/'
315
+
316
+ def initialize(server, user, password, port)
317
+ @server = server
318
+ @user = user
319
+ @password = password
320
+ @port = port
321
+ @netsftp = Net::SFTP.start(@server, @user, password: @password, port: @port)
322
+ @closed = false
323
+ @created_paths_cache = []
324
+ end
325
+
326
+ def chdir(path)
327
+ @dir = path
328
+ end
329
+
330
+ def puttextfile(file)
331
+ @netsftp.upload!(file, @dir + '/' + file.split('/').last)
332
+ end
333
+
334
+ def putbinaryfile(file)
335
+ puttextfile(file)
336
+ end
337
+
338
+ def list
339
+ l = []
340
+ @netsftp.dir.foreach(@dir) { |e| l << e }
341
+ l
342
+ end
343
+
344
+ def closed?
345
+ @netsftp.closed? || @closed
346
+ end
347
+
348
+ def close
349
+ @netsftp.close_channel
350
+ rescue Net::SSH::Exception
351
+ ensure
352
+ @closed = true
353
+ end
354
+
355
+ def mkdir_p(dir)
356
+ made_path = false
357
+
358
+ parts = dir.split("/")
359
+ if parts.first == "~"
360
+ growing_path = ""
361
+ else
362
+ growing_path = "/"
363
+ end
364
+ for part in parts
365
+ next if part == ""
366
+ if growing_path == ""
367
+ growing_path = part
368
+ else
369
+ growing_path = File.join(growing_path, part)
370
+ end
371
+ unless @created_paths_cache.include?(growing_path)
372
+ begin
373
+ mkdir(growing_path)
374
+ chdir(growing_path)
375
+ made_path = true
376
+ rescue Net::SFTP::StatusException
377
+ end
378
+ @created_paths_cache << growing_path
379
+ else
380
+ end
381
+ end
382
+
383
+ made_path
384
+ end
385
+
386
+ def mkdir(path)
387
+ @netsftp.mkdir!(path)
388
+ end
389
+
390
+ def passive=(_)
391
+ end
392
+ end
393
+
302
394
  # Only performs a local copy, instead of using an FTP connection
303
395
  class LocalCopyFTP
304
396
  @dir = '/'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loft-harmony
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caleb Simpson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-10 00:00:00.000000000 Z
11
+ date: 2016-02-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Watches a directory for changes to upload to a server via FTP
14
14
  email: caleb@simpson.center