Class: Skytap::Commands::Copier

Inherits:
Thread
  • Object
show all
Defined in:
lib/skytap/plugins/vm_copy_to_region.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, username, api_token, vm_id, region, root_dir = nil) ⇒ Copier

Returns a new instance of Copier.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/skytap/plugins/vm_copy_to_region.rb', line 83

def initialize(logger, username, api_token, vm_id, region, root_dir = nil)
  @logger = logger
  @username = username
  @api_token = api_token
  @vm_id = vm_id
  @region = region
  @root_dir = File.expand_path(root_dir || '.')

  warn_if_manual_network

  super do
    begin
      run
    rescue Exception => ex
      @result = Response.build(ex)
    end
  end
end

Instance Attribute Details

#api_tokenObject (readonly)

Returns the value of attribute api_token.



81
82
83
# File 'lib/skytap/plugins/vm_copy_to_region.rb', line 81

def api_token
  @api_token
end

#loggerObject (readonly)

Returns the value of attribute logger.



81
82
83
# File 'lib/skytap/plugins/vm_copy_to_region.rb', line 81

def logger
  @logger
end

#regionObject (readonly)

Returns the value of attribute region.



81
82
83
# File 'lib/skytap/plugins/vm_copy_to_region.rb', line 81

def region
  @region
end

#resultObject (readonly)

Returns the value of attribute result.



81
82
83
# File 'lib/skytap/plugins/vm_copy_to_region.rb', line 81

def result
  @result
end

#root_dirObject (readonly)

Returns the value of attribute root_dir.



81
82
83
# File 'lib/skytap/plugins/vm_copy_to_region.rb', line 81

def root_dir
  @root_dir
end

#usernameObject (readonly)

Returns the value of attribute username.



81
82
83
# File 'lib/skytap/plugins/vm_copy_to_region.rb', line 81

def username
  @username
end

#vm_idObject (readonly)

Returns the value of attribute vm_id.



81
82
83
# File 'lib/skytap/plugins/vm_copy_to_region.rb', line 81

def vm_id
  @vm_id
end

Instance Method Details

#finished?Boolean

Returns:

  • (Boolean)


149
150
151
152
153
# File 'lib/skytap/plugins/vm_copy_to_region.rb', line 149

def finished?
  @finished.tap do
    @finished = true if @result
  end
end

#manual_network?Boolean

Returns:

  • (Boolean)


182
183
184
185
186
187
# File 'lib/skytap/plugins/vm_copy_to_region.rb', line 182

def manual_network?
  @_manual_network ||= begin
                        vm = Skytap.invoke!(username, api_token, "vm show #{vm_id}")
                        (iface = vm['interfaces'].try(:first)) && iface['network_type'] == 'manual'
                      end
end

#runObject



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
# File 'lib/skytap/plugins/vm_copy_to_region.rb', line 102

def run
  FileUtils.mkdir_p(root_dir)

  #TODO:NLA Set vmdir = File.join(tmpdir, "tmp_vm_#{vm_id}"). Then on success, remove vmdir.

  downloads = Skytap.invoke!(username, api_token, "vm download #{vm_id}", :dir => root_dir) do |downloader|
    @no_slots_msg = if seconds = downloader.seconds_until_retry
                      m = Integer(seconds / 60)
                      "VM #{vm_id}: No export capacity is currently available on Skytap. Will retry ".tap do |msg|
                        if m < 1
                          msg << 'soon.'
                        else
                          msg << "in #{m} minutes or when more capacity is detected."
                        end
                      end.color(:yellow)
                    end
    @status_line = downloader.status_lines
  end

  @no_slots_msg = nil

  vm_dir = File.join(root_dir, "vm_#{vm_id}")
  unless downloads.include?(vm_dir)
    raise Skytap::Error.new("Response dir unexpected (was: #{downloads}; expected to contain #{vm_dir})")
  end

  # Invoke with an array to treat vm_dir as one token, even if it contains spaces.
  uploads = Skytap.invoke!(username, api_token, ['vm', 'upload', vm_dir], {}, :param => {'region' => region}) do |uploader|
    @no_slots_msg = if seconds = uploader.seconds_until_retry
                      m = Integer(seconds / 60)
                      "VM #{vm_id}: No import capacity is currently available on Skytap. Will retry ".tap do |msg|
                        if m < 1
                          msg << 'soon.'
                        else
                          msg << "in #{m} minutes or when more capacity is detected."
                        end
                        msg
                      end.color(:yellow)
                    end
    @status_line = uploader.status_lines
  end

  FileUtils.rm_r(vm_dir)

  @result = Response.build(uploads)
end

#status_lineObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/skytap/plugins/vm_copy_to_region.rb', line 159

def status_line
  payload = result.try(:payload) and return payload

  if @no_slots_msg
    unless @skip_print
      @skip_print = true
      return @no_slots_msg
    end
  else
    @skip_print = false
  end

  @status_line
end

#success?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/skytap/plugins/vm_copy_to_region.rb', line 155

def success?
  result && !result.error?
end

#summaryObject



174
175
176
177
178
179
180
# File 'lib/skytap/plugins/vm_copy_to_region.rb', line 174

def summary
  status_line.tap do |msg|
    if manual_network?
      msg << " This template has an automatic network, but the template from which it was copied has a manual network. You may want to change the network settings of the new template by creating a configuration from it, editing the network, and finally creating another template from that configuration."
    end
  end
end

#warn_if_manual_networkObject



189
190
191
192
193
194
# File 'lib/skytap/plugins/vm_copy_to_region.rb', line 189

def warn_if_manual_network
  if manual_network?
    msg = 'This VM is attached to a manual network, but the new template will instead contain an automatic network. You may want to change the network settings of the new template by creating a configuration from it, editing the network, and finally creating another template from that configuration.'
    logger.info "VM #{vm_id}: #{msg.color(:yellow)}\n---"
  end
end