From: Michal Fojtik <[email protected]>
Signed-off-by: Michal fojtik <[email protected]> --- .../lib/deltacloud/drivers/ec2/ec2_mock_driver.rb | 186 -------------------- server/lib/deltacloud/method_serializer.rb | 83 --------- 2 files changed, 269 deletions(-) delete mode 100644 server/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb delete mode 100644 server/lib/deltacloud/method_serializer.rb diff --git a/server/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb b/server/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb deleted file mode 100644 index 03db2c1..0000000 --- a/server/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb +++ /dev/null @@ -1,186 +0,0 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. The -# ASF licenses this file to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance with the -# License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -module RightAws - class MockEc2 - - def initialize(opts={}) - end - - def describe_images(id) - load_fixtures_for(:images).select { |i| i[:aws_id].eql?(id) } - end - - def describe_images_by_owner(id) - load_fixtures_for(:images).select { |i| i[:aws_owner].eql?(id) } - end - - def describe_images(opts={}) - load_fixtures_for(:images) - end - - def describe_availability_zones(opts={}) - load_fixtures_for(:realms) - end - - def describe_instances(opts={}) - instances = load_fixtures_for(:instances) - instances.each_with_index do |instance, i| - instances[i] = update_delayed_state(instance) - return [instance] if opts and opts[:id] and instance[:aws_instance_id].eql?(opts[:id]) - end - update_fixtures_for(:instances, instances) - instances - end - - def run_instances(image_id, min_count, max_count, group_ids, key_name, user_data='', addressing_type = nil, instance_type = nil, kernel_id = nil, ramdisk_id = nil, availability_zone = nil, block_device_mappings = nil) - - instances = load_fixtures_for(:instances) - image = load_fixtures_for(:images).select { |img| img[:aws_id].eql?(image_id) }.first - - if availability_zone - realm = load_fixtures_for(:realms).select { |realm| realm[:zone_name].eql?(availability_zone) }.first - else - realm = load_fixtures_for(:realms).first - end - - raise Exception unless image - raise Exception unless realm - - instance = { } - instance[:aws_image_id] = image[:aws_id] - instance[:aws_availability_zone] = realm[:zone_name] - instance[:aws_instance_type] = instance_type - instance[:aws_owner] = user_data - instance[:aws_state] = 'pending' - instance[:aws_reason] = '' - instance[:dns_name] = "#{random_dns}-01-C9.usma2.compute.amazonaws.com" - instance[:private_dns_name] = "#{random_dns}-02-P9.usma2.compute.amazonaws.com" - instance[:aws_state_code] = "0" - instance[:aws_key_name] = "staging" - instance[:aws_kernel_id] = "aki-be3adfd7" - instance[:aws_ramdisk_id] = "ari-ce34gad7" - instance[:aws_groups] = ["default"] - instance[:aws_instance_id] = random_instance_id - instance[:aws_reservation_id] = "r-aabbccdd" - instance[:aws_launch_time] = instance_time_format - - instances << instance - - update_fixtures_for(:instances, instances) - - return [instance] - end - - - def terminate_instances(id) - update_instance_state(id, 'stopping', '80') - end - - def reboot_instances(id) - update_instance_state(id, 'pending', '0') - end - - alias :destroy_instance :terminate_instances - - def describe_snapshots(opts={}) - load_fixtures_for(:storage_snapshot) - end - - def describe_volumes(opts={}) - load_fixtures_for(:storage_volume) - end - - private - - def driver_dir - File::expand_path(File::join(File::dirname(__FILE__), '../../../../features/support/ec2')) - end - - def fixtures_path - File::expand_path(File::join(driver_dir, 'fixtures')) - end - - def load_fixtures_for(collection) - YAML.load_file(File::join(fixtures_path, "#{collection}.yaml")) - end - - def update_fixtures_for(collection, new_data) - File.open(File::join(fixtures_path, "#{collection}.yaml"), 'w' ) do |out| - YAML.dump(new_data, out) - end - return new_data - end - - def instance_time_format - DateTime.now.to_s.gsub(/\+(.+)$/, '.000Z') - end - - def random_instance_id - id_1 = ("%.4s" % Time.now.to_i.to_s.reverse).reverse - id_2 = ("%.3s" % Time.now.to_i.to_s.reverse) - "i-#{id_1}f#{id_2}" - end - - def random_dns - "domU-#{rand(90)+10}-#{rand(90)+10}-#{rand(90)+10}-#{rand(90)+10}" - end - - def update_delayed_state(instance) - time = DateTime.now - DateTime.parse(instance[:aws_launch_time]) - hours, minutes, seconds, frac = Date.day_fraction_to_time(time) - - if (minutes>(rand(2)+1) or hours>0) and instance[:aws_state].eql?('pending') - instance[:aws_state], instance[:aws_state_code] = 'running', '16' - end - - if (minutes>(rand(1)+1) or hours>0) and instance[:aws_state].eql?('stopping') - instance[:aws_state], instance[:aws_state_code] = 'stopped', '80' - end - - return instance - end - - def update_instance_state(id, state, state_code) - instance = describe_instances(:id => id).first - if instance - instance[:aws_state], instance[:aws_state_code] = state, state_code - instance[:aws_launch_time] = instance_time_format - instances = load_fixtures_for(:instances) - instances.each_with_index do |inst, i| - instances[i] = instance if inst[:aws_instance_id].eql?(id) - end - update_fixtures_for(:instances, instances) - return instance - else - raise Exception - end - end - - end -end - -Deltacloud::Drivers::EC2::EC2Driver.class_eval do - alias_method :original_new_client, :new_client - - def new_client(credentials, opts={}) - if credentials.user != 'mockuser' and credentials.password != 'mockpassword' - raise "AuthFailure" - end - RightAws::MockEc2.new - end - -end diff --git a/server/lib/deltacloud/method_serializer.rb b/server/lib/deltacloud/method_serializer.rb deleted file mode 100644 index eec9abe..0000000 --- a/server/lib/deltacloud/method_serializer.rb +++ /dev/null @@ -1,83 +0,0 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. The -# ASF licenses this file to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance with the -# License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -require 'base64' -require 'digest' - -module MethodSerializer - - module Cache - - def cache_dir - storage_dir = $methods_cache_dir || File.join(File.dirname(__FILE__), 'cache') - class_dir = self.class.name.split('::').last - class_dir ||= self.class.name - File.join(storage_dir, class_dir.downcase) - end - - def serialize_data(method_name, args, data) - File.open(cache_file_name(method_name, args), 'w') do |f| - f.puts(Base64.encode64(Marshal.dump(data))) - end - return data - end - - def deserialize_data(method_name, args) - begin - data = File.readlines(cache_file_name(method_name, args)).join - Marshal.load(Base64.decode64(data)) - rescue Errno::ENOENT - return false - end - end - - def args_hash(args) - if args.class == Hash - args = args.to_a.collect {|i| [i[0].to_s, i[1]]}.sort - end - Digest::SHA1.hexdigest(args.to_s) - end - - def cache_file_name(method_name, args) - FileUtils.mkdir_p(cache_dir) unless File.directory?(cache_dir) - method_name = $scenario_prefix ? "#{$scenario_prefix}_#{method_name}" : method_name - File.join(cache_dir, "#{method_name}.#{args_hash(args)}") - end - - def self.wrap_methods(c, opts={}) - $methods_cache_dir = opts[:cache_dir] - $scenario_prefix = nil - c.class_eval do - cached_methods.each do |m| - next if c.instance_methods(false).include?("original_#{m}") - alias_method "original_#{m}".to_sym, m.to_sym - define_method m.to_sym do |*args| - args = args.first if args.size.eql?(1) and not args.first.class.eql?(Array) - output = deserialize_data(m, args) - unless output - output = method("original_#{m}".to_sym).to_proc[args] - return serialize_data(m, args, output) - else - return output - end - end - end - end - end - - end - -end -- 1.7.10
