class Biosphere::Deployment

Attributes

_settings[R]
all_resources[R]
export[R]
feature_manifests[R]
name[R]
node[RW]
resources[R]
state[RW]
target_groups[R]

Public Class Methods

new(parent, name, settings={}) click to toggle source
# File lib/biosphere/deployment.rb, line 11
def initialize(parent, name, settings={})
    @parent = nil
    @name = ""
    if parent.kind_of?(::Biosphere::Deployment) || parent.kind_of?(::Biosphere::Suite)
        @parent = parent
    else
        raise ArgumentError, "Deployment takes a parent Biosphere::Deployment or Biosphere::Suite as it's first parameter"
    end

    if name.kind_of?(String) && name.length > 0
        @name = name
    else
        raise ArgumentError, "Deployment takes the deployment name as the first parameter, or as the second parameter if no parent is defined. Name can't be empty."
    end

    @_settings = {}
    if settings.kind_of?(Hash)
        @_settings = settings
    elsif settings.kind_of?(::Biosphere::Settings)
        @_settings = settings
        settings = @_settings.settings
        @feature_manifests = @_settings.feature_manifests
    end

    @export = {
        "provider" => {},
        "resource" => {},
        "variable" => {},
        "output" => {}
    }

    settings[:deployment_name] = @name

    if @parent.is_a?(::Biosphere::Suite)
        @parent.register(self)
        @target_groups = {}
        @all_resources = []
    elsif @parent
        @node = @parent.node
        @state = @parent.state
        @export = @parent.export
        @all_resources = @parent.all_resources
        @target_groups = @parent.target_groups

        @parent.register(self)
        
    else
        @node = Node.new
    end

    @delayed = []
    @resources = []
    @actions = {}
    @deployments = []
    @outputs = []

    if @feature_manifests
        node[:feature_manifests] = @feature_manifests
    end

    self.setup(settings)

end

Public Instance Methods

add_resource_to_target_group(resource_type, resource_name, target_group) click to toggle source
# File lib/biosphere/deployment.rb, line 75
def add_resource_to_target_group(resource_type, resource_name, target_group)
    name = resource_type + "." + resource_name
    (@target_groups[target_group] ||= []) << name
end
delayed(&block) click to toggle source
# File lib/biosphere/deployment.rb, line 111
def delayed(&block)
    delayed_call = {
        :block => block
    }
    @delayed << delayed_call
end
evaluate_outputs(outputs) click to toggle source
# File lib/biosphere/deployment.rb, line 171
def evaluate_outputs(outputs)

    # Call first sub-deployments
    @deployments.each do |deployment|
        deployment.evaluate_outputs(outputs)
    end
    
    @outputs.each do |output|
        begin
            value = outputs[output[:resource_name]]
            instance_exec(self.name, output[:name], value["value"], value, &output[:block])
        rescue NoMethodError => e
            STDERR.puts "Error evaluating output #{output}. error: #{e}"
            puts "output:"
            pp output
            puts "value:"
            pp value
            puts "outputs:"
            pp outputs
            STDERR.puts "This is an internal error. You should be able to run biosphere commit again to try to fix this."
        end
    end
end
evaluate_resources() click to toggle source
# File lib/biosphere/deployment.rb, line 213
def evaluate_resources()

    # Call first sub-deployments
    @deployments.each do |deployment|
        deployment.evaluate_resources()
    end

    # Then all delayed calls
    @delayed.each do |delayed_call|
        proxy = ResourceProxy.new(self)
        proxy.instance_eval(&delayed_call[:block])
    end

    # And finish with our own resources
    @resources.each do |resource|
        proxy = ResourceProxy.new(self)
        if resource[:block]
            proxy.instance_eval(&resource[:block])
        end

        @export["resource"][resource[:type].to_s][resource[:name].to_s] = proxy.output
    end
end
id_of(type,name) click to toggle source
# File lib/biosphere/deployment.rb, line 237
def id_of(type,name)
    "${#{type}.#{name}.id}"
end
load_outputs(tfstate_filename) click to toggle source
# File lib/biosphere/deployment.rb, line 195
def load_outputs(tfstate_filename)

    begin
        tf_state = JSON.parse(File.read(tfstate_filename))
    rescue SystemCallError
        puts "Couldn't read Terraform statefile, can't continue"
        exit
    end

    outputs = tf_state["modules"].first["outputs"]
    if outputs.length == 0
        STDERR.puts "WARNING: No outputs found from the terraform state file #{tfstate_filename}. This might be a bug in terraform."
        STDERR.puts "Try to run \"biosphere commit\" again."
    else
        evaluate_outputs(outputs)
    end
end
output(name, value, &block) click to toggle source
# File lib/biosphere/deployment.rb, line 149
def output(name, value, &block)
    if self.name
        resource_name = self.name + "_" + name
    else
        resource_name = name
    end

    @export["output"][resource_name] = {
        "value" => value
    }

    if block_given?
        output = {
            :name => name,
            :resource_name => resource_name,
            :block => block
        }

        @outputs << output
    end
end
output_of(type, name, *values) click to toggle source
# File lib/biosphere/deployment.rb, line 241
def output_of(type, name, *values)
    if self.name
        name = self.name + "_" + name
    end
    "${#{type}.#{name}.#{values.join(".")}}"
end
provider(name, spec={}) click to toggle source
# File lib/biosphere/deployment.rb, line 107
def provider(name, spec={})
    @export["provider"][name.to_s] = spec
end
register(deployment) click to toggle source
# File lib/biosphere/deployment.rb, line 97
def register(deployment)
    @deployments << deployment
end
resource(type, name, target_group = nil, &block) click to toggle source
# File lib/biosphere/deployment.rb, line 118
def resource(type, name, target_group = nil, &block)
    if self.name
        name = self.name + "_" + name
    end
    @export["resource"][type.to_s] ||= {}
    if @export["resource"][type.to_s][name.to_s]
        throw "Tried to create a resource of type #{type} called '#{name}' when one already exists"
    end

    spec = {}
    resource = {
        :name => name,
        :type => type,
        :location => caller[0] + "a"
    }

    if target_group
        add_resource_to_target_group(type, name, target_group)
    end

    if block_given?
        resource[:block] = block
    else
        #STDERR.puts("WARNING: No block set for resource call '#{type}', '#{name}' at #{caller[0]}")
    end

    @resources << resource
    @all_resources << resource

end
setup(settings) click to toggle source
# File lib/biosphere/deployment.rb, line 80
def setup(settings)
end
to_json(pretty=false) click to toggle source
# File lib/biosphere/deployment.rb, line 248
def to_json(pretty=false)
    if pretty
        return JSON.pretty_generate(@export)
    else
        return JSON.generate(@export)
    end
end
variable(name, value) click to toggle source
# File lib/biosphere/deployment.rb, line 101
def variable(name, value)
    @export["variable"][name] = {
        "default" => value
    }
end