Class: Bosh::Clouds::Dummy

Inherits:
Object show all
Defined in:
lib/cloud/dummy.rb

Defined Under Namespace

Classes: NotImplemented

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Dummy

Returns a new instance of Dummy.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cloud/dummy.rb', line 10

def initialize(options)
  if options['dir'].nil?
    raise ArgumentError, 'please provide base directory for dummy cloud'
  end

  @options = options
  @base_dir = options['dir']

  FileUtils.mkdir_p(@base_dir)
rescue Errno::EACCES
  raise ArgumentError, "cannot create dummy cloud base directory #{@base_dir}"
end

Instance Method Details

#attach_disk(vm_id, disk_id) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/cloud/dummy.rb', line 81

def attach_disk(vm_id, disk_id)
  file = attachment_file(vm_id, disk_id)
  FileUtils.mkdir_p(File.dirname(file))
  FileUtils.touch(file)

  agent_id = agent_id_for_vm_id(vm_id)
  settings = read_agent_settings(agent_id)
  settings['disks']['persistent'][disk_id] = 'attached'
  write_agent_settings(agent_id, settings)
end

#configure_networks(vm, networks) ⇒ Object

Raises:



77
78
79
# File 'lib/cloud/dummy.rb', line 77

def configure_networks(vm, networks)
  raise NotImplemented, 'Dummy CPI does not implement configure_networks'
end

#create_disk(size, vm_locality = nil) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/cloud/dummy.rb', line 101

def create_disk(size, vm_locality = nil)
  disk_id = SecureRandom.hex
  file = disk_file(disk_id)
  FileUtils.mkdir_p(File.dirname(file))
  File.write(file, size.to_s)
  disk_id
end

#create_stemcell(image, _) ⇒ Object



23
24
25
26
27
# File 'lib/cloud/dummy.rb', line 23

def create_stemcell(image, _)
  stemcell_id = Digest::SHA1.hexdigest(File.read(image))
  File.write(stemcell_file(stemcell_id), image)
  stemcell_id
end

#create_vm(agent_id, stemcell, resource_pool, networks, disk_locality = nil, env = nil) ⇒ Object

rubocop:disable ParameterLists



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cloud/dummy.rb', line 34

def create_vm(agent_id, stemcell, resource_pool, networks, disk_locality = nil, env = nil)
# rubocop:enable ParameterLists
  root_dir = File.join(agent_base_dir(agent_id), 'root_dir')
  FileUtils.mkdir_p(File.join(root_dir, 'etc', 'logrotate.d'))

  write_agent_settings(agent_id, {
    agent_id: agent_id,
    blobstore: @options['agent']['blobstore'],
    ntp: [],
    disks: { persistent: {} },
    vm: { name: "vm-#{agent_id}" },
    mbus: @options['nats'],
  })

  agent_cmd = agent_cmd(agent_id, root_dir, :ruby)
  agent_log = "#{@options['dir']}/agent.#{agent_id}.log"
  agent_pid = Process.spawn(*agent_cmd, chdir: agent_base_dir(agent_id), out: agent_log, err: agent_log)
  Process.detach(agent_pid)

  FileUtils.mkdir_p(File.join(@base_dir, 'running_vms'))
  File.write(vm_file(agent_pid), agent_id)

  agent_pid.to_s
end

#delete_disk(disk_id) ⇒ Object



109
110
111
# File 'lib/cloud/dummy.rb', line 109

def delete_disk(disk_id)
  FileUtils.rm(disk_file(disk_id))
end

#delete_snapshot(snapshot_id) ⇒ Object



121
122
123
# File 'lib/cloud/dummy.rb', line 121

def delete_snapshot(snapshot_id)
  FileUtils.rm(snapshot_file(snapshot_id))
end

#delete_stemcell(stemcell_cid) ⇒ Object



29
30
31
# File 'lib/cloud/dummy.rb', line 29

def delete_stemcell(stemcell_cid)
  FileUtils.rm(stemcell_file(stemcell_cid))
end

#delete_vm(vm_name) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/cloud/dummy.rb', line 59

def delete_vm(vm_name)
  agent_pid = vm_name.to_i
  Process.kill('INT', agent_pid)
# rubocop:disable HandleExceptions
rescue Errno::ESRCH
# rubocop:enable HandleExceptions
ensure
  FileUtils.rm_rf(File.join(@base_dir, 'running_vms', vm_name))
end

#detach_disk(vm_id, disk_id) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/cloud/dummy.rb', line 92

def detach_disk(vm_id, disk_id)
  FileUtils.rm(attachment_file(vm_id, disk_id))

  agent_id = agent_id_for_vm_id(vm_id)
  settings = read_agent_settings(agent_id)
  settings['disks']['persistent'].delete(disk_id)
  write_agent_settings(agent_id, settings)
end

#has_vm?(vm_id) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/cloud/dummy.rb', line 73

def has_vm?(vm_id)
  File.exists?(vm_file(vm_id))
end

#reboot_vm(vm) ⇒ Object

Raises:



69
70
71
# File 'lib/cloud/dummy.rb', line 69

def reboot_vm(vm)
  raise NotImplemented, 'Dummy CPI does not implement reboot_vm'
end

#snapshot_disk(_, metadata) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/cloud/dummy.rb', line 113

def snapshot_disk(_, )
  snapshot_id = SecureRandom.hex
  file = snapshot_file(snapshot_id)
  FileUtils.mkdir_p(File.dirname(file))
  File.write(file, .to_json)
  snapshot_id
end

#validate_deployment(old_manifest, new_manifest) ⇒ Object

Raises:



125
126
127
# File 'lib/cloud/dummy.rb', line 125

def validate_deployment(old_manifest, new_manifest)
  raise NotImplemented, 'Dummy CPI does not implement validate_deployment'
end