Class: Bosh::Clouds::Dummy

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

Defined Under Namespace

Classes: CommandTransport, ConfigureNetworksCommand, CpiInvocation, CreateVmCommand, InputsRecorder, NotImplemented, VM, VMRepo

Constant Summary collapse

CREATE_STEMCELL_SCHEMA =
Membrane::SchemaParser.parse { {image_path: String, cloud_properties: Hash} }
DELETE_STEMCELL_SCHEMA =
Membrane::SchemaParser.parse { {stemcell_id: String} }
CREATE_VM_SCHEMA =
Membrane::SchemaParser.parse do
  {
    agent_id: String,
    stemcell_id: String,
    cloud_properties: Hash,
    networks: Hash,
    disk_cids: [String],
    env: Hash,
  }
end
DELETE_VM_SCHEMA =
Membrane::SchemaParser.parse { {vm_cid: String} }
REBOOT_VM_SCHEMA =
Membrane::SchemaParser.parse { {vm_cid: String} }
HAS_VM_SCHEMA =
Membrane::SchemaParser.parse { {vm_cid: String} }
HAS_DISK_SCHEMA =
Membrane::SchemaParser.parse { {disk_id: String} }
ATTACH_DISK_SCHEMA =
Membrane::SchemaParser.parse { {vm_cid: String, disk_id: String} }
DETACH_DISK_SCHEMA =
Membrane::SchemaParser.parse { {vm_cid: String, disk_id: String} }
CREATE_DISK_SCHEMA =
Membrane::SchemaParser.parse { {size: Integer, cloud_properties: Hash, vm_locality: String} }
DELTE_DISK_SCHEMA =
Membrane::SchemaParser.parse { {disk_id: String} }
SNAPSHOT_DISK_SCHEMA =
Membrane::SchemaParser.parse { {disk_id: String, metadata: Hash} }
DELETE_SNAPSHOT_SCHEMA =
Membrane::SchemaParser.parse { {snapshot_id: String} }
SET_VM_METADATA_SCHEMA =
Membrane::SchemaParser.parse { {vm_cid: String, metadata: Hash} }

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
36
37
38
# 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

  @running_vms_dir = File.join(@base_dir, 'running_vms')
  @vm_repo = VMRepo.new(@running_vms_dir)
  @tmp_dir = File.join(@base_dir, 'tmp')
  FileUtils.mkdir_p(@tmp_dir)

  @logger = Logging::Logger.new('DummyCPI')
  @logger.add_appenders(Logging.appenders.io(
      'DummyCPIIO',
      options['log_buffer'] || STDOUT
    ))

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

  prepare
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_dir_for_vm_cid(vm_cid) ⇒ Object



307
308
309
310
# File 'lib/cloud/dummy.rb', line 307

def agent_dir_for_vm_cid(vm_cid)
  agent_id = agent_id_for_vm_id(vm_cid)
  agent_base_dir(agent_id)
end

#agent_log_path(agent_id) ⇒ Object



259
260
261
# File 'lib/cloud/dummy.rb', line 259

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

#all_ipsObject



301
302
303
304
305
# File 'lib/cloud/dummy.rb', line 301

def all_ips
  Dir.glob(File.join(@base_dir, 'dummy_cpi_networks', '*'))
    .reject { |path| File.directory?(path) }
    .map { |path| File.basename(path) }
end

#all_snapshotsObject



293
294
295
296
297
298
299
# File 'lib/cloud/dummy.rb', line 293

def all_snapshots
  if File.exists?(snapshot_file(''))
    Dir.glob(snapshot_file('*'))
  else
    []
  end
end

#all_stemcellsObject



275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/cloud/dummy.rb', line 275

def all_stemcells
  files = Dir.entries(@base_dir).select { |file| file.match /stemcell_./ }

  Dir.chdir(@base_dir) do
    [].tap do |results|
      files.each do |file|
        # data --> [{ 'name' => 'ubuntu-stemcell', 'version': '1', 'image' => <image path> }]
        data = YAML.load(File.read(file))
        results << { 'id' => file.sub(/^stemcell_/, '') }.merge(data)
      end
    end.sort { |a, b| a[:version].to_i <=> b[:version].to_i }
  end
end

#attach_disk(vm_cid, disk_id) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/cloud/dummy.rb', line 159

def attach_disk(vm_cid, disk_id)
  validate_and_record_inputs(ATTACH_DISK_SCHEMA, __method__, vm_cid, disk_id)
  if disk_attached?(disk_id)
    raise "#{disk_id} is already attached to an instance"
  end
  file = attachment_file(vm_cid, disk_id)
  FileUtils.mkdir_p(File.dirname(file))
  FileUtils.touch(file)

  @logger.info("Attached disk: '#{disk_id}' to vm: '#{vm_cid}' at attachment file: #{file}")

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

#create_disk(size, cloud_properties, vm_locality) ⇒ Object



191
192
193
194
195
196
197
198
# File 'lib/cloud/dummy.rb', line 191

def create_disk(size, cloud_properties, vm_locality)
  validate_and_record_inputs(CREATE_DISK_SCHEMA, __method__, size, cloud_properties, vm_locality)
  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_path, cloud_properties) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cloud/dummy.rb', line 41

def create_stemcell(image_path, cloud_properties)
  validate_and_record_inputs(CREATE_STEMCELL_SCHEMA, __method__, image_path, cloud_properties)

  content = File.read(image_path)
  data = YAML.load(content)
  data.merge!('image' => image_path)
  stemcell_id = Digest::SHA1.hexdigest(content)

  File.write(stemcell_file(stemcell_id), YAML.dump(data))
  stemcell_id
end

#create_vm(agent_id, stemcell_id, cloud_properties, networks, disk_cids, env) ⇒ Object

rubocop:disable ParameterLists



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cloud/dummy.rb', line 71

def create_vm(agent_id, stemcell_id, cloud_properties, networks, disk_cids, env)
  # rubocop:enable ParameterLists
  @logger.info('Dummy: create_vm')
  validate_and_record_inputs(CREATE_VM_SCHEMA, __method__, agent_id, stemcell_id, cloud_properties, networks, disk_cids, env)

  ips = []
  cmd = commands.next_create_vm_cmd

  if cmd.failed?
    raise Bosh::Clouds::CloudError.new("Creating vm failed")
  end

  networks.each do |network_name, network|
    if network['type'] != 'dynamic'
      ips << { 'network' => network_name, 'ip' => network.fetch('ip') }
    else
      if cmd.ip_address
        ip_address = cmd.ip_address
      elsif cloud_properties['az_name']
        ip_address = cmd.ip_address_for_az(cloud_properties['az_name'])
      else
        ip_address =  NetAddr::CIDRv4.new(rand(0..4294967295)).ip #collisions?
      end

      if ip_address
        ips << { 'network' => network_name, 'ip' => ip_address }
        write_agent_default_network(agent_id, ip_address)
      end
    end
  end

  allocate_ips(ips)

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

  agent_pid = spawn_agent_process(agent_id)
  vm = VM.new(agent_pid.to_s, agent_id, cloud_properties, ips)

  @vm_repo.save(vm)

  vm.id
end

#current_apply_spec_for_vm(vm_cid) ⇒ Object



316
317
318
319
320
# File 'lib/cloud/dummy.rb', line 316

def current_apply_spec_for_vm(vm_cid)
  agent_base_dir = agent_dir_for_vm_cid(vm_cid)
  spec_file = File.join(agent_base_dir, 'bosh', 'spec.json')
  JSON.parse(File.read(spec_file))
end

#delete_disk(disk_id) ⇒ Object



201
202
203
204
# File 'lib/cloud/dummy.rb', line 201

def delete_disk(disk_id)
  validate_and_record_inputs(DELTE_DISK_SCHEMA, __method__, disk_id)
  FileUtils.rm(disk_file(disk_id))
end

#delete_snapshot(snapshot_id) ⇒ Object



217
218
219
220
# File 'lib/cloud/dummy.rb', line 217

def delete_snapshot(snapshot_id)
  validate_and_record_inputs(DELETE_SNAPSHOT_SCHEMA, __method__, snapshot_id)
  FileUtils.rm(snapshot_file(snapshot_id))
end

#delete_stemcell(stemcell_id) ⇒ Object



54
55
56
57
# File 'lib/cloud/dummy.rb', line 54

def delete_stemcell(stemcell_id)
  validate_and_record_inputs(DELETE_STEMCELL_SCHEMA, __method__, stemcell_id)
  FileUtils.rm(stemcell_file(stemcell_id))
end

#delete_vm(vm_cid) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cloud/dummy.rb', line 125

def delete_vm(vm_cid)
  validate_and_record_inputs(DELETE_VM_SCHEMA, __method__, vm_cid)
  commands.wait_for_unpause_delete_vms
  detach_disks_attached_to_vm(vm_cid)
  agent_pid = vm_cid.to_i
  Process.kill('KILL', agent_pid)

    # rubocop:disable HandleExceptions
rescue Errno::ESRCH
  # rubocop:enable HandleExceptions
ensure
  free_ips(vm_cid)
  FileUtils.rm_rf(File.join(@base_dir, 'running_vms', vm_cid))
end

#detach_disk(vm_cid, disk_id) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/cloud/dummy.rb', line 177

def detach_disk(vm_cid, disk_id)
  validate_and_record_inputs(DETACH_DISK_SCHEMA, __method__, vm_cid, disk_id)
  unless disk_attached_to_vm?(vm_cid, disk_id)
    raise Bosh::Clouds::DiskNotAttached, "#{disk_id} is not attached to instance #{vm_cid}"
  end
  FileUtils.rm(attachment_file(vm_cid, disk_id))

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

#disk_attached_to_vm?(vm_cid, disk_id) ⇒ Boolean

Returns:

  • (Boolean)


312
313
314
# File 'lib/cloud/dummy.rb', line 312

def disk_attached_to_vm?(vm_cid, disk_id)
  File.exist?(attachment_file(vm_cid, disk_id))
end

#disk_cidsObject



243
244
245
246
# File 'lib/cloud/dummy.rb', line 243

def disk_cids
  # Shuffle so that no one relies on the order of disks
  Dir.glob(disk_file('*')).map { |disk| File.basename(disk) }.shuffle
end

#has_disk?(disk_id) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
156
# File 'lib/cloud/dummy.rb', line 153

def has_disk?(disk_id)
  validate_and_record_inputs(HAS_DISK_SCHEMA, __method__, disk_id)
  File.exists?(disk_file(disk_id))
end

#has_vm?(vm_cid) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
# File 'lib/cloud/dummy.rb', line 147

def has_vm?(vm_cid)
  validate_and_record_inputs(HAS_VM_SCHEMA, __method__, vm_cid)
  @vm_repo.exists?(vm_cid)
end

#invocationsObject



267
268
269
# File 'lib/cloud/dummy.rb', line 267

def invocations
  @inputs_recorder.read_all
end

#invocations_for_method(method) ⇒ Object



271
272
273
# File 'lib/cloud/dummy.rb', line 271

def invocations_for_method(method)
  @inputs_recorder.read(method)
end

#kill_agentsObject



248
249
250
251
252
253
254
255
256
257
# File 'lib/cloud/dummy.rb', line 248

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

#latest_stemcellObject



289
290
291
# File 'lib/cloud/dummy.rb', line 289

def latest_stemcell
  all_stemcells.last
end

#prepareObject

Additional Dummy test helpers



229
230
231
# File 'lib/cloud/dummy.rb', line 229

def prepare
  FileUtils.mkdir_p(@base_dir)
end

#read_cloud_properties(vm_cid) ⇒ Object



263
264
265
# File 'lib/cloud/dummy.rb', line 263

def read_cloud_properties(vm_cid)
  @vm_repo.load(vm_cid).cloud_properties
end

#reboot_vm(vm_cid) ⇒ Object

Raises:



141
142
143
144
# File 'lib/cloud/dummy.rb', line 141

def reboot_vm(vm_cid)
  validate_and_record_inputs(__method__, vm_cid)
  raise NotImplemented, 'Dummy CPI does not implement reboot_vm'
end

#resetObject



233
234
235
236
# File 'lib/cloud/dummy.rb', line 233

def reset
  FileUtils.rm_rf(@base_dir)
  prepare
end

#set_vm_metadata(vm_cid, metadata) ⇒ Object



223
224
225
# File 'lib/cloud/dummy.rb', line 223

def (vm_cid, )
  validate_and_record_inputs(SET_VM_METADATA_SCHEMA, __method__, vm_cid, )
end

#snapshot_disk(disk_id, metadata) ⇒ Object



207
208
209
210
211
212
213
214
# File 'lib/cloud/dummy.rb', line 207

def snapshot_disk(disk_id, )
  validate_and_record_inputs(SNAPSHOT_DISK_SCHEMA, __method__, disk_id, )
  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



238
239
240
241
# File 'lib/cloud/dummy.rb', line 238

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