Module: SubutaiDisk

Defined in:
lib/vagrant-subutai/packer/subutai_disk.rb

Overview

For managing VM disks

Constant Summary collapse

DISK_NAME =
"SubutaiDisk".freeze
DISK_FORMAT =
"vdi".freeze
DISK_FORMAT_VIRTUALBOX =
"vdi".freeze
DISK_FORMAT_VMWARE =
"vmdk".freeze
DISK_FORMAT_HYPERV =
"vhdx".freeze
DISK_FORMAT_LIBVIRT =
"qcow2".freeze
PROVIDER_VMWARE =
"vmware".freeze
PROVIDER_HYPERV =
"hyper_v".freeze
PROVIDER_LIBVIRT =
"libvirt".freeze
SCRIPT_HYPERV_DISK_CREATE_PATH =
'script/create_disk_and_attach.ps1'.freeze
SCRIPT_HYPERV_DISK_REMOVE_PATH =
'script/remove_virtual_disk.ps1'.freeze

Class Method Summary collapse

Class Method Details

.file(grow_by) ⇒ Object

Gives disk file name THIS IS FOR OLD VERSION BOXES UNDER <= v3.0.5



147
148
149
150
151
152
153
154
155
156
# File 'lib/vagrant-subutai/packer/subutai_disk.rb', line 147

def self.file(grow_by)
  disk_port = port

  # get disk path from conf file
  if SubutaiConfig.get(:SUBUTAI_DISK_PATH).nil?
    "./#{DISK_NAME}-#{disk_port.to_i}-#{grow_by}.#{DISK_FORMAT}"
  else
    File.join(SubutaiConfig.get(:SUBUTAI_DISK_PATH).to_s, "#{DISK_NAME}-#{disk_port.to_i}-#{grow_by}.#{DISK_FORMAT}")
  end
end

.file_path(grow_by, provider) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/vagrant-subutai/packer/subutai_disk.rb', line 158

def self.file_path(grow_by, provider)
  disk_port = port
  disk_format = DISK_FORMAT

  case provider
  when PROVIDER_VMWARE
    disk_format = DISK_FORMAT_VMWARE
  when PROVIDER_HYPERV
    disk_format = DISK_FORMAT_HYPERV
  when PROVIDER_LIBVIRT
    disk_format = DISK_FORMAT_LIBVIRT
  end

  # get disk path from conf file
  if SubutaiConfig.get(:SUBUTAI_DISK_PATH).nil?
    File.expand_path "#{DISK_NAME}-#{disk_port.to_i}-#{grow_by}.#{disk_format}"
  else
    File.join(SubutaiConfig.get(:SUBUTAI_DISK_PATH).to_s, "#{DISK_NAME}-#{disk_port.to_i}-#{grow_by}.#{disk_format}")
  end
end

.has_growObject

Checks disk size for adding new VM disks



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vagrant-subutai/packer/subutai_disk.rb', line 23

def self.has_grow
  grow_by = SubutaiConfig.get_grow_by

  if grow_by.nil?
    [false, nil]
  elsif grow_by > 0
    [true, grow_by]
  else
    [false, nil]
  end
end

.hyperv_create_disk(grow_by, file_disk) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/vagrant-subutai/packer/subutai_disk.rb', line 85

def self.hyperv_create_disk(grow_by, file_disk)
  script = File.join(File.expand_path(File.dirname(__FILE__)), SCRIPT_HYPERV_DISK_CREATE_PATH)
  id = SubutaiConfig.machine_id(:hyper_v)

  if id.nil?
    Put.error("[FAILED] Disk Creation. Not found machine id")
    false
  else
    VagrantSubutai::Util::Powershell.execute(script, "-VmId", id, "-DiskPath", "'#{file_disk}'", "-DiskSize", "#{vmware_size(grow_by)}")
  end
end

.hyperv_remove_diskObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/vagrant-subutai/packer/subutai_disk.rb', line 109

def self.hyperv_remove_disk
  script = File.join(File.expand_path(File.dirname(__FILE__)), SCRIPT_HYPERV_DISK_REMOVE_PATH)
  id = SubutaiConfig.machine_id(:hyper_v)

  if id.nil?
    Put.error("[FAILED] Remove virtual disk. Not found machine id")
    false
  else
    VagrantSubutai::Util::Powershell.execute(script, "-VmId", id)
  end
end

.libvirt_size(grow_by) ⇒ Object



66
67
68
69
# File 'lib/vagrant-subutai/packer/subutai_disk.rb', line 66

def self.libvirt_size(grow_by)
  size = grow_by.to_i + 2   # 2 gb for overhead, unit in gb
  "#{size}G"
end

.message(grow_by) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/vagrant-subutai/packer/subutai_disk.rb', line 135

def self.message(grow_by)
  disk_size = SubutaiConfig.get(:DISK_SIZE)

  unless disk_size.nil?
    disk_size = disk_size.to_i
    "==> default: Disk size configured to #{disk_size}GB, increasing #{disk_size - grow_by}GB default by #{grow_by}GB."
  end
end

.parallels_create_disk(grow_by) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/vagrant-subutai/packer/subutai_disk.rb', line 97

def self.parallels_create_disk(grow_by)
  id = SubutaiConfig.machine_id(:parallels)

  if id.nil?
    Put.error("[FAILED] Disk Creation. Not found machine id")
    false
  else
    # prlctl set ec45bf0c-1d1e-44c0-b5b8-6d80623b8364 --device-add=hdd --size=4092 # in megabytes
    VagrantSubutai::Util::Terminal.execute_cmd("prlctl", "set", id, "--device-add=hdd", "--size=#{SubutaiDisk.size(grow_by)}")
  end
end

.portObject

Gives disk port



51
52
53
54
55
56
57
58
59
60
# File 'lib/vagrant-subutai/packer/subutai_disk.rb', line 51

def self.port
  port = SubutaiConfig.get(:_DISK_PORT)

  # Default port value is 1
  if port.nil?
    1
  else
    port.to_i + 1 # increasing by one for next vm disk attach
  end
end

.save_conf(grow_by) ⇒ Object

Save disk size and port to generated.yml



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/vagrant-subutai/packer/subutai_disk.rb', line 122

def self.save_conf(grow_by)
  SubutaiConfig.put(:_DISK_PORT, port, true)

  generated_disk = SubutaiConfig.get(:_DISK_SIZE)
  if generated_disk.nil?
    SubutaiConfig.put(:_DISK_SIZE, grow_by, true) # we set all size of virtual disks to _DISK_SIZE in unit gb
    true
  else
    SubutaiConfig.put(:_DISK_SIZE, grow_by + generated_disk.to_i, true) # we set all size of virtual disks to _DISK_SIZE in unit gb
    true
  end
end

.save_path(port, file_path) ⇒ Object

Save disk pathes. We saves for cleanup while destroying peer



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vagrant-subutai/packer/subutai_disk.rb', line 36

def self.save_path(port, file_path)
  if SubutaiConfig.get(:_DISK_PATHES).nil?
    hash = {}
    hash[port] = file_path
    SubutaiConfig.put(:_DISK_PATHES, hash, true)
    true
  else
    hash = SubutaiConfig.get(:_DISK_PATHES)
    hash[port] = file_path
    SubutaiConfig.put(:_DISK_PATHES, hash, true)
    true
  end
end

.size(grow_by) ⇒ Object



62
63
64
# File 'lib/vagrant-subutai/packer/subutai_disk.rb', line 62

def self.size(grow_by)
  grow_by.to_i * 1024 + 2 * 1024 # 2 gb for overhead, unit in megabytes
end

.vmware_create_disk(grow_by, file_disk) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/vagrant-subutai/packer/subutai_disk.rb', line 75

def self.vmware_create_disk(grow_by, file_disk)
  if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
    system("\"C:\\Program Files (x86)\\VMware\\VMware Workstation\\vmware-vdiskmanager.exe\" -c -s #{vmware_size(grow_by)}GB -a lsilogic -t 0 #{file_disk}")
  elsif RbConfig::CONFIG['host_os'] =~ /darwin/
    system("\"/Applications/VMware Fusion.app/Contents/Library/vmware-vdiskmanager\" -c -s #{vmware_size(grow_by)}GB -a lsilogic -t 0 #{file_disk}")
  elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
    system "vmware-vdiskmanager -c -s #{vmware_size(grow_by)}GB -a lsilogic -t 0 #{file_disk}"
  end
end

.vmware_size(grow_by) ⇒ Object



71
72
73
# File 'lib/vagrant-subutai/packer/subutai_disk.rb', line 71

def self.vmware_size(grow_by)
  grow_by.to_i + 2 # 2 gb for overhead, unit in gb
end