Class: Sacback::CLI
- Inherits:
-
Thor
- Object
- Thor
- Sacback::CLI
- Defined in:
- lib/sacback/cli.rb
Constant Summary collapse
- ARCHIVE_IMAGE_NAME =
'archive.img'
- DEFAULT_LOCAL_DIR_PATH =
'/tmp'
- FTP_PORT =
21
- SACLOUD_API_VERSION =
'1.1'
Instance Method Summary collapse
- #a(disk_name, local_dir_path = DEFAULT_LOCAL_DIR_PATH) ⇒ Object
- #c(disk_name) ⇒ Object
- #g(archive_id, local_dir_path = DEFAULT_LOCAL_DIR_PATH) ⇒ Object
- #r(archive_id) ⇒ Object
- #z(dir, name_orig, name_new) ⇒ Object
Instance Method Details
#a(disk_name, local_dir_path = DEFAULT_LOCAL_DIR_PATH) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/sacback/cli.rb', line 25 def a(disk_name, local_dir_path = DEFAULT_LOCAL_DIR_PATH) log "a: Run all through: disk=#{disk_name}, local dir=#{local_dir_path}" log archive = invoke :c, [disk_name] # log "archive: #{archive.id} #{archive.name}" invoke :g, [archive.id, local_dir_path] invoke :r, [archive.id] compressed_file_name = archive.name + '.gz' invoke :z, [local_dir_path, ARCHIVE_IMAGE_NAME, compressed_file_name] end |
#c(disk_name) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/sacback/cli.rb', line 38 def c(disk_name) log "c: Create an archive: disk=#{disk_name}" @api = disk = get_disk disk_name # say "disk: #{disk.id}" archive = create_archive disk log "The archive was created: id=#{archive.id}, name=#{archive.name}" log archive end |
#g(archive_id, local_dir_path = DEFAULT_LOCAL_DIR_PATH) ⇒ Object
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 |
# File 'lib/sacback/cli.rb', line 51 def g(archive_id, local_dir_path = DEFAULT_LOCAL_DIR_PATH) log "g: Get the archive: archive id=#{archive_id}, local dir=#{local_dir_path}" @api = archive = @api.archive.get_by_id archive_id log "archive: #{archive.id} #{archive.name}" archive.open_ftp log 'ftp opened' begin ftp_info = archive.ftp_info # log "ftp: #{ftp_info.user},#{ftp_info.password},#{ftp_info.host_name}" local_file = File.join local_dir_path, ARCHIVE_IMAGE_NAME if [:curl] download_with_curl ftp_info, local_file else download_with_doublebugftps ftp_info, local_file end ensure archive.close_ftp log 'ftp closed' end log end |
#r(archive_id) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/sacback/cli.rb', line 79 def r(archive_id) log "r: Remove the archive: archive id=#{archive_id}" @api = archive = @api.archive.get_by_id archive_id result = archive.destroy if result[:Success] && result[:is_ok] log "The archive #{archive_id} was removed." else abort "Error: removing the archive #{archive_id} failed" end log end |
#z(dir, name_orig, name_new) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/sacback/cli.rb', line 94 def z(dir, name_orig, name_new) log "z: Compress the local file: local dir=#{dir}, target file=#{name_orig}, compressed file=#{name_new}" path_orig = File.join dir, name_orig path_new = File.join dir, name_new log "Start compressing #{path_orig} to #{path_new}" # Zlib::GzipWriter.open(path_new, Zlib::BEST_COMPRESSION) do |gz| # gz.mtime = File.mtime(path_orig) # gz.orig_name = name_orig # File.open(path_orig, 'rb') do |fp| # while chunk = fp.read(1024 * 1024) do # gz.puts chunk # end # end # end command = "(gzip -c #{path_orig} > #{path_new}) && rm #{path_orig}" `#{command}` log "Compression succeeded: #{path_new}" log end |