Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions lib/harp-runtime/cloud/cloud_mutator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def initialize(options)
@cloud_type = options[:cloud_type]
@mock = (options.include? :mock) ? true : false
@service_connectors = {}
@harp_script = options[:harp_script]
@resources = {}
end

def service_for_set(resource_set)
Expand Down Expand Up @@ -74,6 +76,8 @@ def create(resource_name, resource_def)
created = resource.create(service)
pr = persist_resource(resource_name, resource, created, "create")
pr.state = Harp::Resources::AvailableResource::CREATED
remember(pr)
pr.save
return pr
end

Expand All @@ -85,7 +89,7 @@ def destroy(resource_name, resource_def)
end

resource.populate(resource_def)
persisted = HarpResource.entries.select{|res| res.name == resource_name}.first
persisted = get_harp_resource(resource_name)

if ! persisted.nil?
resource.populate(persisted.attributes)
Expand All @@ -95,11 +99,19 @@ def destroy(resource_name, resource_def)
service = establish_connect(resource)

destroyed = resource.destroy(service)
pr = persist_resource(resource_name, resource, resource, "destroy")
pr.state = Harp::Resources::AvailableResource::DESTROYED
return pr
#pr = persist_resource(resource_name, resource, resource, "destroy")
#persisted.live_resource = resource
persisted.state = Harp::Resources::AvailableResource::DESTROYED
remember(persisted)
persisted.save
return persisted
end

def get_harp_resource(resource_name)
#@harp_script.harp_resources.select{|res| res.name == resource_name}.first
@resources[resource_name]
end

def get_output(resource, persisted)
resource = Harp::Resources::AvailableResource.from_name resource['type']
if resource.nil?
Expand All @@ -110,6 +122,10 @@ def get_output(resource, persisted)
output = resource.get_output(service, persisted)
end

def remember(resource)
@resources[resource.name] = resource
end

def get_state(resource_name,resource_def)
resource = Harp::Resources::AvailableResource.from_name resource_def['type']
if resource.nil?
Expand All @@ -118,20 +134,22 @@ def get_state(resource_name,resource_def)
end
resource.populate(resource_def)

persisted = HarpResource.entries.select{|res| res.name == resource_name}.first
persisted = get_harp_resource(resource_name)
if ! persisted.nil?
resource.populate(persisted.attributes)
end

resource.name = resource_name
service = establish_connect(resource)

return resource.get_state(service)
end
private
def persist_resource(resource_name, resource, live_resource, action)
if live_resource
persistable = resource.make_persistable(live_resource)
persistable.name = resource_name
persistable.harp_script_id = @harp_script.id
@@logger.debug "Perform: #{action} resource #{persistable.inspect}"
persistable
end
Expand Down
5 changes: 2 additions & 3 deletions lib/harp-runtime/resources/compute/elastic_ip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ def create(service)
end

def destroy(service)
destroy_attribs = self.attribs
if @id
address = service.addresses.destroy(destroy_attribs)
if id
address = service.addresses.destroy(id)
else
puts "No ID set, cannot delete."
end
Expand Down
2 changes: 2 additions & 0 deletions lib/harp-runtime/resources/compute/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ class ComputeInstance < AvailableResource
attribute :user_data
attribute :virtualization_type, :aliases => 'virtualizationType'
attribute :vpc_id, :aliases => 'vpcId'

attribute :description
attribute :type
attribute :live_resource

register_resource :compute_instance, RESOURCES_COMPUTE

Expand Down
7 changes: 3 additions & 4 deletions lib/harp-runtime/resources/compute/security_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ def create(service)
end

def destroy(service)
destroy_attribs = self.attribs
if @id
security_group = service.security_groups.destroy(destroy_attribs)
if id
security_groups = service.security_groups.destroy(id)
else
puts "No ID set, cannot delete."
end
return security_group
return security_groups
end

end
Expand Down
5 changes: 2 additions & 3 deletions lib/harp-runtime/resources/compute/volume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ def create(service)
end

def destroy(service)
destroy_attribs = self.attribs
if @id
volume = service.volumes.destroy(destroy_attribs)
if id
volume = service.volumes.destroy(id)
else
puts "No ID set, cannot delete."
end
Expand Down
39 changes: 28 additions & 11 deletions spec/compute_cloud_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@

it "creates a cloud instance" do
result = mutator.create("test_inst1", instance_resource)

expect(result.class).to eq(ComputeInstance)
expect(result.name).to eq("test_inst1")
expect(result.state).to eq(Harp::Resources::AvailableResource::CREATED)
state = mutator.get_state("test_inst1", instance_resource)
expect(state).to eq("running")
end

it "creates a security group" do
result = mutator.create("test_sg1", security_group_resource)
expect(result.class).to eq(SecurityGroup)
expect(result.name).to eq("test_sg1")

expect(result.description).to eq("A web security group")
end

Expand All @@ -59,19 +59,36 @@
end

describe Harp::Cloud::CloudMutator, "#destroy" do
include_context "when have mutator"

it "destroys a cloud instance" do
context = {}
context[:cloud_type] = :aws # for the moment, assume AWS cloud
context[:mock] = true
context[:debug] = true
context[:access] = "test"
context[:secret] = "test"
mutator = Harp::Cloud::CloudMutator.new(context)

created = mutator.create("test_inst1", instance_resource)
result = mutator.destroy("test_inst1", instance_resource)

expect(result.class).to eq(ComputeInstance)
expect(result.name).to eq("test_inst1")
expect(result.state).to eq(Harp::Resources::AvailableResource::DESTROYED)
state = mutator.get_state("test_inst1", instance_resource)
expect(state).to eq("shutting-down")
end
# it "destroys an elastic ip" do
# result = mutator.destroy("test_eip1", elastic_ip_resource)
#
# expect(result.class).to eq(ElasticIP)
# expect(result.name).to eq("test_eip1")
# expect(result.state).to eq(Harp::Resources::AvailableResource::DESTROYED)
# end
# it "destroys a security group" do
# result = mutator.destroy("test_sg1", security_group_resource)
#
# expect(result.class).to eq(SecurityGroup)
# expect(result.name).to eq("test_sg1")
# expect(result.state).to eq(Harp::Resources::AvailableResource::DESTROYED)
# end
# it "destroys a volume" do
# result = mutator.destroy("test_vol1", volume_resource)
#
# expect(result.class).to eq(Volume)
# expect(result.name).to eq("test_vol1")
# expect(result.state).to eq(Harp::Resources::AvailableResource::DESTROYED)
# end
end
30 changes: 22 additions & 8 deletions spec/interpreter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
c[:harp_contents] = VALID_SCRIPT
c
end
let(:interpreter_context_created) do
c = create_interpreter_context()
c[:harp_contents] = VALID_SCRIPT
c[:harp_id] = '123123'
c
end
let(:interpreter) {
Harp::HarpInterpreter.new(interpreter_context())
}
Expand All @@ -30,36 +36,44 @@ def find_break_event(results)
it "instruments for debug" do
results = interpreter.play("create", interpreter_context)
break_event = find_break_event(results)
break_event.should match ".*32$" # Should have broken at line 32
break_event.should match ".*37$" # Should have broken at line 37
end

context 'when in debug mode with breakpoint' do

let(:breakpoint_context) do
c = interpreter_context()
# 38 happens to be a reasonable breakpoint in VALID_SCRIPT
c[:break] = 38
# 42 happens to be a reasonable breakpoint in VALID_SCRIPT
c[:break] = 42
c
end
let(:interpreter) {
Harp::HarpInterpreter.new(breakpoint_context())
}

it "instruments for debug and accepts breakpoint" do
results = interpreter.play("destroy", breakpoint_context)
harp_script = FactoryGirl.create(:harp_script)
breakpoint_context_created = breakpoint_context.clone
breakpoint_context_created[:harp_id] = harp_script.id
results = interpreter.play("destroy", breakpoint_context_created)
#created = interpreter.play("create", breakpoint_context)
#results = interpreter.play("destroy", breakpoint_context)
break_event = find_break_event(results)
break_event.should match ".*38$" # Should have broken at line 38
break_event.should match ".*42$" # Should have broken at line 42
end
end

it "invokes custom lifecycles" do
results = interpreter.play("custom", interpreter_context)
harp_script = FactoryGirl.create(:harp_script)
interpreter_context_created = interpreter_context.clone
interpreter_context_created[:harp_id] = harp_script.id
results = interpreter.play("custom", interpreter_context_created)
expect(results).not_to be_empty
destroyed = nil
results.each do |result|
destroyed = result[:destroy] if result.include? (:destroy)
destroyed = result[:create] if result.include? (:create)
end
destroyed.should match "computeInstance2" # Custom lifecycle should have destroyed this
destroyed.should match "computeInstance4" # Custom lifecycle should have destroyed this
end
end
end
29 changes: 15 additions & 14 deletions spec/rds_cloud_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
}

describe Harp::Cloud::CloudMutator, "#create" do
include_context "when have mutator"
it "creates a db security group" do
context = {}
context[:cloud_type] = :aws # for the moment, assume AWS cloud
context[:mock] = true
context[:debug] = true
context[:access] = "test"
context[:secret] = "test"
mutator = Harp::Cloud::CloudMutator.new(context)
# context = {}
# context[:cloud_type] = :aws # for the moment, assume AWS cloud
# context[:mock] = true
# context[:debug] = true
# context[:access] = "test"
# context[:secret] = "test"
#mutator = Harp::Cloud::CloudMutator.new(context)

result = mutator.create("test_db_sg1", db_security_group_resource)
expect(result.class).to eq(DBSecurityGroup)
Expand All @@ -33,13 +34,13 @@
expect(result.description).to eq("A web db security group")
end
it "creates a db instance" do
context = {}
context[:cloud_type] = :aws # for the moment, assume AWS cloud
context[:mock] = true
context[:debug] = true
context[:access] = "test"
context[:secret] = "test"
mutator = Harp::Cloud::CloudMutator.new(context)
# context = {}
# context[:cloud_type] = :aws # for the moment, assume AWS cloud
# context[:mock] = true
# context[:debug] = true
# context[:access] = "test"
# context[:secret] = "test"
#mutator = Harp::Cloud::CloudMutator.new(context)

result = mutator.create("test_db_db1", db_instance_resource)
expect(result.class).to eq(DBInstance)
Expand Down
13 changes: 11 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
require 'factory_girl'

# For debugging.
require 'logging'
Logging.logger.root.add_appenders(Logging.appenders.stdout)
Logging.logger.root.level = :debug
#require 'logging'
#Logging.logger.root.add_appenders(Logging.appenders.stdout)
#Logging.logger.root.level = :debug
Expand Down Expand Up @@ -57,6 +60,11 @@
"type": "Std::ComputeInstance",
"imageId": "ami-d0f89fb9",
"instanceType": "t1.micro"
},
"computeInstance4": {
"type": "Std::ComputeInstance",
"imageId": "ami-d0f89fb9",
"instanceType": "t1.micro"
}
}
}
Expand All @@ -67,7 +75,7 @@
def create()
engine.create("computeInstance1")
engine.create("computeInstance2")
engine.break # should be line 32, from top of script
engine.break # should be line 37, from top of script
engine.create("computeInstance3")
end

Expand All @@ -77,7 +85,7 @@ def destroy()
end

def custom()
engine.destroy("computeInstance2")
engine.create("computeInstance4")
end

OUTER
Expand All @@ -90,6 +98,7 @@ def create_interpreter_context
interpreter_context[:debug] = true
interpreter_context[:access] = "test"
interpreter_context[:secret] = "test"
interpreter_context[:harp_script] = FactoryGirl.create(:harp_script)
interpreter_context
end
end
Expand Down