Class: OsVm::SharedDir

Inherits:
Object
  • Object
show all
Defined in:
lib/osvm/shared_dir.rb

Overview

SharedDir is used to push and pull files to and from machines

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ SharedDir

Returns a new instance of SharedDir.

Parameters:



21
22
23
24
25
26
27
28
29
30
# File 'lib/osvm/shared_dir.rb', line 21

def initialize(machine)
  @machine = machine
  @fs_name = 'vmSharedDir'
  @host_path = File.join(machine.send(:tmpdir), 'shared-dir')
  @host_push = File.join(host_path, 'push')
  @host_pull = File.join(host_path, 'pull')
  @machine_mountpoint = '/run/osvm/shared-dir'
  @machine_push = File.join(machine_mountpoint, 'push')
  @machine_pull = File.join(machine_mountpoint, 'pull')
end

Instance Attribute Details

#fs_nameString (readonly)

Returns:

  • (String)


12
13
14
# File 'lib/osvm/shared_dir.rb', line 12

def fs_name
  @fs_name
end

#host_pathString (readonly)

Returns:

  • (String)


15
16
17
# File 'lib/osvm/shared_dir.rb', line 15

def host_path
  @host_path
end

#host_pullObject (readonly, protected)

Returns the value of attribute host_pull.



72
73
74
# File 'lib/osvm/shared_dir.rb', line 72

def host_pull
  @host_pull
end

#host_pushObject (readonly, protected)

Returns the value of attribute host_push.



72
73
74
# File 'lib/osvm/shared_dir.rb', line 72

def host_push
  @host_push
end

#machineMachine (readonly)

Returns:



9
10
11
# File 'lib/osvm/shared_dir.rb', line 9

def machine
  @machine
end

#machine_mountpointString (readonly)

Returns:

  • (String)


18
19
20
# File 'lib/osvm/shared_dir.rb', line 18

def machine_mountpoint
  @machine_mountpoint
end

#machine_pullObject (readonly, protected)

Returns the value of attribute machine_pull.



72
73
74
# File 'lib/osvm/shared_dir.rb', line 72

def machine_pull
  @machine_pull
end

#machine_pushObject (readonly, protected)

Returns the value of attribute machine_push.



72
73
74
# File 'lib/osvm/shared_dir.rb', line 72

def machine_push
  @machine_push
end

Instance Method Details

#destroyObject

Destroy the shared directory on the host



48
49
50
51
# File 'lib/osvm/shared_dir.rb', line 48

def destroy
  system('rm', '-rf', host_path) \
    || (raise "unable to delete shared directory at '#{host_path}'")
end

#mountObject

Mount the shared directory within the machine



40
41
42
43
44
45
# File 'lib/osvm/shared_dir.rb', line 40

def mount
  machine.all_succeed(
    "mkdir -p \"#{machine_mountpoint}\"",
    "mount -t virtiofs #{fs_name} \"#{machine_mountpoint}\""
  )
end

#path_to_safe_name(path) ⇒ Object (protected)



74
75
76
77
78
# File 'lib/osvm/shared_dir.rb', line 74

def path_to_safe_name(path)
  pn = Pathname.new(path)
  name = pn.cleanpath.to_s.sub(%r{^/}, '').gsub('/', '--')
  "#{SecureRandom.hex(4)}-#{name}"
end

#pull_file(src, preserve: false) ⇒ Object

Pull file from the machine



62
63
64
65
66
67
68
# File 'lib/osvm/shared_dir.rb', line 62

def pull_file(src, preserve: false)
  safe_name = path_to_safe_name(src)
  opt = preserve ? '-p' : ''
  dst = File.join(machine_pull, safe_name)
  machine.succeeds("cp #{opt} \"#{src}\" \"#{dst}\"")
  File.join(host_pull, safe_name)
end

#push_file(src, dst, preserve: false) ⇒ Object

Push file to the machine



54
55
56
57
58
59
# File 'lib/osvm/shared_dir.rb', line 54

def push_file(src, dst, preserve: false)
  safe_name = path_to_safe_name(src)
  FileUtils.cp(src, File.join(host_push, safe_name), preserve:)
  machine.succeeds("mv \"#{File.join(machine_push, safe_name)}\" \"#{dst}\"")
  dst
end

#setupObject

Create the shared directory on the host



33
34
35
36
37
# File 'lib/osvm/shared_dir.rb', line 33

def setup
  FileUtils.mkpath(host_path)
  FileUtils.mkpath(host_push)
  FileUtils.mkpath(host_pull)
end