Module: Uricp::Strategy::Common

Constant Summary collapse

PIPE_URI =
URI('pipe:/')
DRY_SNAP =
'uricp_snap'.freeze

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.



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

def proposed_options
  @proposed_options
end

Instance Method Details

#all_local_files?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/uricp/strategy/common.rb', line 21

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

#always_write_sparse?Boolean

Returns:

  • (Boolean)


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

def always_write_sparse?
  options['force']
end

#compression_required?Boolean

Returns:

  • (Boolean)


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

def compression_required?
  options['compress']
end

#conversion_required?Boolean

Returns:

  • (Boolean)


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

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

#dry_run?Boolean

Returns:

  • (Boolean)


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

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

#encoding(io) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/uricp/strategy/common.rb', line 49

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)


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

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

#format_change?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/uricp/strategy/common.rb', line 68

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

#get_temp_filename(base_dir) ⇒ Object



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

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

#in_rbd_cache(target) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/uricp/strategy/common.rb', line 181

def in_rbd_cache(target)
  result = false
  if dry_run?
    result = options['dry-cache'] == :rbd && options['cache_name'] !~ /srv-...../
  else
    sh "rbd snap ls --id #{rbd_id} --format json #{target} 2>/dev/null" do |stdout|
      result = JSON.parse(stdout).any? { |x| x['name'] == rbd_base_name }
    end
  end
  result && rbd_clone_snapshot(target)
end

#in_rbd_cache?Boolean

Returns:

  • (Boolean)


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

def in_rbd_cache?
  options['in_rbd_cache']
end

#initialize(options) ⇒ Object



7
8
9
10
# File 'lib/uricp/strategy/common.rb', line 7

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

#lz4?(magic) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#lz4_source?Boolean

Returns:

  • (Boolean)


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

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

#not_in_rbd_cache?Boolean

Returns:

  • (Boolean)


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

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

#proposed_pathObject



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

def proposed_path
  proposed_options['from_uri'].path
end

#qcow2?(magic) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#raw_target?Boolean

Returns:

  • (Boolean)


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

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

#rbd_base_nameObject



97
98
99
# File 'lib/uricp/strategy/common.rb', line 97

def rbd_base_name
  'base'.freeze
end

#rbd_cache_image_exists?(target) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/uricp/strategy/common.rb', line 166

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'] !~ /srv-...../
      command = "exit 0"
    else
      command = "exit 2"
    end
  end
  sh!(command)
  true
rescue Methadone::FailedCommandError
  false
end

#rbd_cache_image_spec(uri) ⇒ Object



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

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

#rbd_cache_nameObject



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

def rbd_cache_name
  options['rbd_cache_name']
end

#rbd_cache_upload_available?Boolean

Returns:

  • (Boolean)


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

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

#rbd_clone_snapshot(cache = rbd_cache_name) ⇒ Object



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

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

#rbd_idObject



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

def rbd_id
  'libvirt'
end

#rbd_image_spec(uri) ⇒ Object



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

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

#rbd_sequence_complete?Boolean

Returns:

  • (Boolean)


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

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

#rbd_snapshot_nameObject



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

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

#rbd_snapshot_spec?(uri) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#rbd_uri(image_spec) ⇒ Object



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

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

#segmented?Boolean

Returns:

  • (Boolean)


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

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

#sequence_complete?Boolean

Returns:

  • (Boolean)


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

def sequence_complete?
  from == to
end

#supported_source?Boolean

Returns:

  • (Boolean)


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

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

#temp_uriObject



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

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

#unsupported_transferObject Also known as: command



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

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