Class: Bosh::Clouds::Dummy
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
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
Instance Method Summary collapse
- #agent_dir_for_vm_cid(vm_cid) ⇒ Object
- #agent_log_path(agent_id) ⇒ Object
- #all_ips ⇒ Object
- #all_snapshots ⇒ Object
- #all_stemcells ⇒ Object
- #attach_disk(vm_cid, disk_id) ⇒ Object
- #create_disk(size, cloud_properties, vm_locality) ⇒ Object
- #create_stemcell(image_path, cloud_properties) ⇒ Object
-
#create_vm(agent_id, stemcell_id, cloud_properties, networks, disk_cids, env) ⇒ Object
rubocop:disable ParameterLists.
- #current_apply_spec_for_vm(vm_cid) ⇒ Object
- #delete_disk(disk_id) ⇒ Object
- #delete_snapshot(snapshot_id) ⇒ Object
- #delete_stemcell(stemcell_id) ⇒ Object
- #delete_vm(vm_cid) ⇒ Object
- #detach_disk(vm_cid, disk_id) ⇒ Object
- #disk_attached_to_vm?(vm_cid, disk_id) ⇒ Boolean
- #disk_cids ⇒ Object
- #has_disk?(disk_id) ⇒ Boolean
- #has_vm?(vm_cid) ⇒ Boolean
-
#initialize(options) ⇒ Dummy
constructor
A new instance of Dummy.
- #invocations ⇒ Object
- #invocations_for_method(method) ⇒ Object
- #kill_agents ⇒ Object
- #latest_stemcell ⇒ Object
-
#prepare ⇒ Object
Additional Dummy test helpers.
- #read_cloud_properties(vm_cid) ⇒ Object
- #reboot_vm(vm_cid) ⇒ Object
- #reset ⇒ Object
- #set_vm_metadata(vm_cid, metadata) ⇒ Object
- #snapshot_disk(disk_id, metadata) ⇒ Object
- #vm_cids ⇒ Object
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 = @base_dir = ['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', ['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
#commands ⇒ Object (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
306 307 308 309 |
# File 'lib/cloud/dummy.rb', line 306 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
258 259 260 |
# File 'lib/cloud/dummy.rb', line 258 def agent_log_path(agent_id) "#{@base_dir}/agent.#{agent_id}.log" end |
#all_ips ⇒ Object
300 301 302 303 304 |
# File 'lib/cloud/dummy.rb', line 300 def all_ips Dir.glob(File.join(@base_dir, 'dummy_cpi_networks', '*')) .reject { |path| File.directory?(path) } .map { |path| File.basename(path) } end |
#all_snapshots ⇒ Object
292 293 294 295 296 297 298 |
# File 'lib/cloud/dummy.rb', line 292 def all_snapshots if File.exists?(snapshot_file('')) Dir.glob(snapshot_file('*')) else [] end end |
#all_stemcells ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/cloud/dummy.rb', line 274 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
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/cloud/dummy.rb', line 158 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 = (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
190 191 192 193 194 195 196 197 |
# File 'lib/cloud/dummy.rb', line 190 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 |
# 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: '', 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
315 316 317 318 319 |
# File 'lib/cloud/dummy.rb', line 315 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
200 201 202 203 |
# File 'lib/cloud/dummy.rb', line 200 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
216 217 218 219 |
# File 'lib/cloud/dummy.rb', line 216 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
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/cloud/dummy.rb', line 124 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
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/cloud/dummy.rb', line 176 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((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
311 312 313 |
# File 'lib/cloud/dummy.rb', line 311 def disk_attached_to_vm?(vm_cid, disk_id) File.exist?((vm_cid, disk_id)) end |
#disk_cids ⇒ Object
242 243 244 245 |
# File 'lib/cloud/dummy.rb', line 242 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
152 153 154 155 |
# File 'lib/cloud/dummy.rb', line 152 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
146 147 148 149 |
# File 'lib/cloud/dummy.rb', line 146 def has_vm?(vm_cid) validate_and_record_inputs(HAS_VM_SCHEMA, __method__, vm_cid) @vm_repo.exists?(vm_cid) end |
#invocations ⇒ Object
266 267 268 |
# File 'lib/cloud/dummy.rb', line 266 def invocations @inputs_recorder.read_all end |
#invocations_for_method(method) ⇒ Object
270 271 272 |
# File 'lib/cloud/dummy.rb', line 270 def invocations_for_method(method) @inputs_recorder.read(method) end |
#kill_agents ⇒ Object
247 248 249 250 251 252 253 254 255 256 |
# File 'lib/cloud/dummy.rb', line 247 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_stemcell ⇒ Object
288 289 290 |
# File 'lib/cloud/dummy.rb', line 288 def latest_stemcell all_stemcells.last end |
#prepare ⇒ Object
Additional Dummy test helpers
228 229 230 |
# File 'lib/cloud/dummy.rb', line 228 def prepare FileUtils.mkdir_p(@base_dir) end |
#read_cloud_properties(vm_cid) ⇒ Object
262 263 264 |
# File 'lib/cloud/dummy.rb', line 262 def read_cloud_properties(vm_cid) @vm_repo.load(vm_cid).cloud_properties end |
#reboot_vm(vm_cid) ⇒ Object
140 141 142 143 |
# File 'lib/cloud/dummy.rb', line 140 def reboot_vm(vm_cid) validate_and_record_inputs(__method__, vm_cid) raise NotImplemented, 'Dummy CPI does not implement reboot_vm' end |
#reset ⇒ Object
232 233 234 235 |
# File 'lib/cloud/dummy.rb', line 232 def reset FileUtils.rm_rf(@base_dir) prepare end |
#set_vm_metadata(vm_cid, metadata) ⇒ Object
222 223 224 |
# File 'lib/cloud/dummy.rb', line 222 def (vm_cid, ) validate_and_record_inputs(SET_VM_METADATA_SCHEMA, __method__, vm_cid, ) end |
#snapshot_disk(disk_id, metadata) ⇒ Object
206 207 208 209 210 211 212 213 |
# File 'lib/cloud/dummy.rb', line 206 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_cids ⇒ Object
237 238 239 240 |
# File 'lib/cloud/dummy.rb', line 237 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 |