Class: RHC::Commands::Snapshot

Inherits:
Base show all
Includes:
SSHHelpers
Defined in:
lib/rhc/commands/snapshot.rb

Constant Summary

Constants included from Helpers

Helpers::BOUND_WARNING, Helpers::PREFIX, Helpers::ROLES

Instance Method Summary collapse

Methods included from SSHHelpers

#check_ssh_executable!, #exe?, #fingerprint_for_default_key, #fingerprint_for_local_key, #generate_ssh_key_ruby, #has_ssh?, #run_on_gears, #ssh_command_for_op, #ssh_key_triple_for, #ssh_key_triple_for_default_key, #ssh_keygen_fallback, #ssh_ruby, #ssh_send_file_ruby, #ssh_send_url_ruby, #ssh_version, #table_from_gears

Methods inherited from Base

#initialize

Methods included from RHC::ContextHelpers

#find_app, #find_app_or_domain, #find_domain, #from_local_git, included, #namespace_context, #server_context

Methods included from GitHelpers

#git_clone_application, #git_clone_deploy_hooks, #git_clone_repo, #git_config_get, #git_config_set, #git_version, #has_git?

Methods included from Helpers

#agree, #certificate_file, #client_from_options, #collect_env_vars, #color, #confirm_action, #date, #datetime_rfc3339, #debug, #debug?, #debug_error, #decode_json, #deprecated, #deprecated_command, #disable_deprecated?, #distance_of_time_in_words, #env_var_regex_pattern, #error, #exec, #host_exists?, #hosts_file_contains?, #human_size, #info, #interactive?, #jruby?, #mac?, #pluralize, #protonbox_online_server?, #protonbox_rest_endpoint, #protonbox_server, #protonbox_url, #results, #role_name, #run_with_tee, #ssh_string, #ssh_string_parts, #ssl_options, #success, #system_path, #table_heading, #to_host, #to_uri, #token_for_user, #unix?, #user_agent, #warn, #windows?, #with_tolerant_encoding

Methods included from OutputHelpers

#default_display_env_var, #display_app, #display_app_configurations, #display_authorization, #display_cart, #display_cart_storage_info, #display_cart_storage_list, #display_deployment, #display_deployment_list, #display_domain, #display_env_var_list, #display_key, #format_cart_gears, #format_cart_header, #format_gear_info, #format_key_header, #format_scaling_info, #format_usage_message

Constructor Details

This class inherits a constructor from RHC::Commands::Base

Instance Method Details

#restore(app) ⇒ Object



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
# File 'lib/rhc/commands/snapshot.rb', line 76

def restore(app)
  ssh = check_ssh_executable! options.ssh
  rest_app = find_app
  filename = options.filepath ? options.filepath : "#{rest_app.name}.tar.gz"

  if File.exists? filename

    include_git = RHC::Helpers.windows? ? true : RHC::TarGz.contains(filename, './*/git')
    ssh_uri = URI.parse(rest_app.ssh_url)

    ssh_cmd = "cat '#{filename}' | #{ssh} #{ssh_uri.user}@#{ssh_uri.host} 'restore#{include_git ? ' INCLUDE_GIT' : ''}'"

    say "Restoring from snapshot #{filename}..."
    debug ssh_cmd

    begin
      if !RHC::Helpers.windows?
        status, output = exec(ssh_cmd)
        if status != 0
          debug output
          raise RHC::SnapshotRestoreException.new "Error in trying to restore snapshot. You can try to restore manually by running:\n#{ssh_cmd}"
        end
      else
        ssh = Net::SSH.start(ssh_uri.host, ssh_uri.user)
        ssh.open_channel do |channel|
          channel.exec("restore#{include_git ? ' INCLUDE_GIT' : ''}") do |ch, success|
            channel.on_data do |ch, data|
              say data
            end
            channel.on_extended_data do |ch, type, data|
              say data
            end
            channel.on_close do |ch|
              say "Terminating..."
            end
            File.open(filename, 'rb') do |file|
              file.chunk(1024) do |chunk|
                channel.send_data chunk
              end
            end
            channel.eof!
          end
        end
        ssh.loop
      end
    rescue Timeout::Error, Errno::EADDRNOTAVAIL, Errno::EADDRINUSE, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Net::SSH::AuthenticationFailed => e
      debug e.backtrace
      raise RHC::SnapshotRestoreException.new "Error in trying to restore snapshot. You can try to restore manually by running:\n#{ssh_cmd}"
    end

  else
    raise RHC::SnapshotRestoreException.new "Archive not found: #{filename}"
  end
  results { say "Success" }
  0
end

#save(app) ⇒ Object



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
# File 'lib/rhc/commands/snapshot.rb', line 27

def save(app)
  ssh = check_ssh_executable! options.ssh
  rest_app = find_app

  raise RHC::DeploymentsNotSupportedException.new if options.deployment && !rest_app.supports?("DEPLOY")

  ssh_uri = URI.parse(rest_app.ssh_url)
  filename = options.filepath ? options.filepath : "#{rest_app.name}.tar.gz"

  snapshot_cmd = options.deployment ? 'gear archive-deployment' : 'snapshot'
  ssh_cmd = "#{ssh} #{ssh_uri.user}@#{ssh_uri.host} '#{snapshot_cmd}' > #{filename}"
  debug ssh_cmd

  say "Pulling down a snapshot to #{filename}..."

  begin
    if !RHC::Helpers.windows?
      status, output = exec(ssh_cmd)
      if status != 0
        debug output
        raise RHC::SnapshotSaveException.new "Error in trying to save snapshot. You can try to save manually by running:\n#{ssh_cmd}"
      end
    else
      Net::SSH.start(ssh_uri.host, ssh_uri.user) do |ssh|
        File.open(filename, 'wb') do |file|
          ssh.exec! "snapshot" do |channel, stream, data|
            if stream == :stdout
              file.write(data)
            else
              debug data
            end
          end
        end
      end
    end
  rescue Timeout::Error, Errno::EADDRNOTAVAIL, Errno::EADDRINUSE, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Net::SSH::AuthenticationFailed => e
    debug e.backtrace
    raise RHC::SnapshotSaveException.new "Error in trying to save snapshot. You can try to save manually by running:\n#{ssh_cmd}"
  end
  results { say "Success" }
  0
end