Class: Bosh::Clouds::Dummy

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

Defined Under Namespace

Classes: CommandTransport, ConfigureNetworksCommand, CreareVmCommand, NotImplemented

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Dummy

Returns a new instance of Dummy.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cloud/dummy.rb', line 13

def initialize(options)
  @options = options

  @base_dir = options['dir']
  if @base_dir.nil?
    raise ArgumentError, 'Must specify dir'
  end

  @agent_type = options['agent']['type']
  unless %w(ruby go).include?(@agent_type)
    raise ArgumentError, 'Unknown agent type provided'
  end

  @running_vms_dir = File.join(@base_dir, 'running_vms')

  @logger = Logger.new(options['log_device'] || STDOUT)

  @commands = CommandTransport.new(@base_dir, @logger)

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

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



11
12
13
# File 'lib/cloud/dummy.rb', line 11

def commands
  @commands
end

Instance Method Details

#agent_log_path(agent_id) ⇒ Object



160
161
162
# File 'lib/cloud/dummy.rb', line 160

def agent_log_path(agent_id)
  "#{@base_dir}/agent.#{agent_id}.log"
end

#attach_disk(vm_id, disk_id) ⇒ Object



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

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_id, networks) ⇒ Object

Raises:

  • (NotSupported)


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

def configure_networks(vm_id, networks)
  cmd = commands.next_configure_networks_cmd(vm_id)
  raise NotSupported, 'Dummy CPI was configured to return NotSupported' if cmd.not_supported
  raise NotImplemented, 'Dummy CPI does not implement configure_networks'
end

#create_disk(size, vm_locality = nil) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/cloud/dummy.rb', line 118

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



37
38
39
40
41
# File 'lib/cloud/dummy.rb', line 37

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cloud/dummy.rb', line 48

def create_vm(agent_id, stemcell, resource_pool, networks, disk_locality = nil, env = nil)
# rubocop:enable ParameterLists
  @logger.info('Dummy: create_vm')

  cmd = commands.next_create_vm_cmd

  write_agent_default_network(agent_id, cmd.ip_address) if cmd.ip_address

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

  agent_pid = spawn_agent_process(agent_id)

  FileUtils.mkdir_p(@running_vms_dir)
  File.write(vm_file(agent_pid), agent_id)

  agent_pid.to_s
end

#delete_disk(disk_id) ⇒ Object



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

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

#delete_snapshot(snapshot_id) ⇒ Object



138
139
140
# File 'lib/cloud/dummy.rb', line 138

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

#delete_stemcell(stemcell_cid) ⇒ Object



43
44
45
# File 'lib/cloud/dummy.rb', line 43

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

#delete_vm(vm_name) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/cloud/dummy.rb', line 74

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



109
110
111
112
113
114
115
116
# File 'lib/cloud/dummy.rb', line 109

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)


88
89
90
# File 'lib/cloud/dummy.rb', line 88

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

#kill_agentsObject



149
150
151
152
153
154
155
156
157
158
# File 'lib/cloud/dummy.rb', line 149

def kill_agents
  vm_cids.each do |agent_pid|
    begin
      Process.kill('INT', agent_pid.to_i)
    # rubocop:disable HandleExceptions
    rescue Errno::ESRCH
    # rubocop:enable HandleExceptions
    end
  end
end

#reboot_vm(vm_id) ⇒ Object

Raises:



84
85
86
# File 'lib/cloud/dummy.rb', line 84

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

#set_vm_metadata(vm, metadata) ⇒ Object



164
# File 'lib/cloud/dummy.rb', line 164

def (vm, ); end

#snapshot_disk(_, metadata) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/cloud/dummy.rb', line 130

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

#vm_cidsObject

Additional Dummy test helpers



144
145
146
147
# File 'lib/cloud/dummy.rb', line 144

def vm_cids
  # Shuffle so that no one relies on the order of VMs
  Dir.glob(File.join(@running_vms_dir, '*')).map { |vm| File.basename(vm) }.shuffle
end