Module: Uricp::Strategy::Common

Constant Summary collapse

VOL_REGEX =
/^(srv|vol)-.....$/
PIPE_URI =
URI('pipe:/')
DRY_SNAP =
'uricp_snap'

Instance Attribute Summary collapse

Attributes included from CurlPrimitives

#options

Instance Method Summary collapse

Methods included from CurlPrimitives

#authentication, #curl_command, #curl_download_to_pipe, #curl_manifest, #curl_upload_from, #from, #from=, #http_authentication?, #to, #to=

Instance Attribute Details

#proposed_optionsObject (readonly)

Returns the value of attribute proposed_options.



16
17
18
# File 'lib/uricp/strategy/common.rb', line 16

def proposed_options
  @proposed_options
end

Instance Method Details

#all_local_files?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/uricp/strategy/common.rb', line 25

def all_local_files?
  !sequence_complete? && file_source? && to.scheme == 'file'
end

#always_write_sparse?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/uricp/strategy/common.rb', line 41

def always_write_sparse?
  options['force']
end

#compression_required?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/uricp/strategy/common.rb', line 29

def compression_required?
  options['compress']
end

#conversion_required?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/uricp/strategy/common.rb', line 37

def conversion_required?
  options['target-format']
end

#dry_run?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/uricp/strategy/common.rb', line 78

def dry_run?
  options['dry-run']
end

#encoding(io) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/uricp/strategy/common.rb', line 53

def encoding(io)
  magic = io.read(4).to_s
  if lz4?(magic)
    :lz4
  elsif qcow2?(magic)
    version = io.read(4)
    case version.unpack('N')
    when [2]
      :qcow2
    when [3]
      :qcow3
    else
      :qcow2un
    end
  else
    :raw
  end
end

#file_source?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/uricp/strategy/common.rb', line 90

def file_source?
  from.scheme == 'file'
end

#format_change?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/uricp/strategy/common.rb', line 72

def format_change?
  conversion_required? && (
    options['source-format'] != options['target-format']
  )
end

#get_temp_filename(base_dir) ⇒ Object



109
110
111
112
# File 'lib/uricp/strategy/common.rb', line 109

def get_temp_filename(base_dir)
  t = Time.now.strftime('%Y%m%d')
  File.join(base_dir, "uricp-#{t}-#{$PROCESS_ID}-#{rand(0x100000000).to_s(36)}")
end

#in_rbd_cache(target) ⇒ Object



195
196
197
198
199
200
201
202
203
# File 'lib/uricp/strategy/common.rb', line 195

def in_rbd_cache(target)
  result =
    if dry_run?
      options['dry-cache'] == :rbd && options['cache_name'] !~ VOL_REGEX
    else
      snap_check(target, rbd_base_name)
    end
  result && rbd_clone_snapshot(target)
end

#in_rbd_cache?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/uricp/strategy/common.rb', line 154

def in_rbd_cache?
  options['in_rbd_cache']
end

#initialize(options) ⇒ Object



11
12
13
14
# File 'lib/uricp/strategy/common.rb', line 11

def initialize(options)
  @options = options
  debug "#{self.class.name}: options are #{options.inspect}"
end

#lz4?(magic) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/uricp/strategy/common.rb', line 45

def lz4?(magic)
  magic.unpack('V') == [0x184D2204]
end

#lz4_source?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/uricp/strategy/common.rb', line 82

def lz4_source?
  options['source-format'] == :lz4
end

#not_in_rbd_cache?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/uricp/strategy/common.rb', line 158

def not_in_rbd_cache?
  options['in_rbd_cache'] == false
end

#proposed_pathObject



114
115
116
# File 'lib/uricp/strategy/common.rb', line 114

def proposed_path
  proposed_options['from_uri'].path
end

#qcow2?(magic) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/uricp/strategy/common.rb', line 49

def qcow2?(magic)
  magic.unpack('a3C') == ['QFI', 0xfb]
end

#raw_target?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/uricp/strategy/common.rb', line 86

def raw_target?
  options['target-format'] == :raw
end

#rbd_base_nameObject



101
102
103
# File 'lib/uricp/strategy/common.rb', line 101

def rbd_base_name
  'base'
end

#rbd_cache_image_exists?(target) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/uricp/strategy/common.rb', line 170

def rbd_cache_image_exists?(target)
  command = "rbd status --id #{rbd_id} --format json #{target} 2>/dev/null"
  if dry_run?
    if options['dry-cache'] == :partial_rbd && options['cache_name'] !~ VOL_REGEX
      command = 'exit 0'
    else
      command = 'exit 2'
    end
  end
  sh!(command)
  true
rescue Methadone::FailedCommandError
  false
end

#rbd_cache_image_spec(uri) ⇒ Object



130
131
132
# File 'lib/uricp/strategy/common.rb', line 130

def rbd_cache_image_spec(uri)
  File.join(File.dirname(uri.path)[1..-1], options['cache_name'])
end

#rbd_cache_nameObject



134
135
136
# File 'lib/uricp/strategy/common.rb', line 134

def rbd_cache_name
  options['rbd_cache_name']
end

#rbd_cache_upload_available?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/uricp/strategy/common.rb', line 166

def rbd_cache_upload_available?
  rbd_cache_name && !rbd_cache_image_exists?(rbd_cache_name)
end

#rbd_clone_snapshot(cache = rbd_cache_name) ⇒ Object



138
139
140
# File 'lib/uricp/strategy/common.rb', line 138

def rbd_clone_snapshot(cache = rbd_cache_name)
  "#{cache}@#{rbd_base_name}"
end

#rbd_idObject



150
151
152
# File 'lib/uricp/strategy/common.rb', line 150

def rbd_id
  'libvirt'
end

#rbd_image_spec(uri) ⇒ Object



126
127
128
# File 'lib/uricp/strategy/common.rb', line 126

def rbd_image_spec(uri)
  uri.path[1..-1]
end

#rbd_sequence_complete?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/uricp/strategy/common.rb', line 146

def rbd_sequence_complete?
  from.path.include?(to.path)
end

#rbd_snapshot_nameObject



105
106
107
# File 'lib/uricp/strategy/common.rb', line 105

def rbd_snapshot_name
  @rbd_snapshot_name ||= dry_run? ? DRY_SNAP : SecureRandom.uuid
end

#rbd_snapshot_spec?(uri) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/uricp/strategy/common.rb', line 162

def rbd_snapshot_spec?(uri)
  uri.scheme == 'rbd' && uri.path.include?('@')
end

#rbd_uri(image_spec) ⇒ Object



142
143
144
# File 'lib/uricp/strategy/common.rb', line 142

def rbd_uri(image_spec)
  URI.join('rbd:///', image_spec)
end

#segmented?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/uricp/strategy/common.rb', line 33

def segmented?
  options['segment-size']
end

#sequence_complete?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/uricp/strategy/common.rb', line 94

def sequence_complete?
  from == to
end

#snap_check(target, name) ⇒ Object



185
186
187
188
189
190
191
192
193
# File 'lib/uricp/strategy/common.rb', line 185

def snap_check(target, name)
  result = false
  sh "rbd snap ls --id #{rbd_id} --format json #{target} 2>/dev/null" do |stdout|
    result = JSON.parse(stdout).any? { |x| x['name'] == name }
  end
  result
rescue Methadone::FailedCommandError
  false
end

#supported_source?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/uricp/strategy/common.rb', line 122

def supported_source?
  options['source-format'] && !lz4_source?
end

#temp_uriObject



118
119
120
# File 'lib/uricp/strategy/common.rb', line 118

def temp_uri
  URI.join('file:///', get_temp_filename(options['temp']))
end

#unsupported_transferObject Also known as: command



18
19
20
21
# File 'lib/uricp/strategy/common.rb', line 18

def unsupported_transfer
  raise Uricp::UnsupportedURLtype,
        "Unsupported transfer from #{from} to #{to}"
end