Class: Biosphere::Deployment

Inherits:
Object
  • Object
show all
Defined in:
lib/biosphere/deployment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Deployment

Returns a new instance of Deployment.



10
11
12
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/biosphere/deployment.rb', line 10

def initialize(*args)

    @parent = nil
    @name = ""
    if args[0].kind_of?(::Biosphere::Deployment) || args[0].kind_of?(::Biosphere::Suite)
        @parent = args.shift
    end
    if args[0].kind_of?(String)
        @name = args.shift
    end

    settings = {}
    @_settings = {}
    if args[0].kind_of?(Hash)
        settings = args.shift
        @_settings = settings
    elsif args[0].kind_of?(::Biosphere::Settings)
        @_settings = args.shift
        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)
    elsif @parent
        @node = @parent.node
        @state = @parent.state
        @export = @parent.export
        @parent.register(self)
        
    else
        @node = Node.new
    end

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

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

    self.setup(settings)

end

Instance Attribute Details

#_settingsObject (readonly)

Returns the value of attribute _settings.



8
9
10
# File 'lib/biosphere/deployment.rb', line 8

def _settings
  @_settings
end

#exportObject (readonly)

Returns the value of attribute export.



8
9
10
# File 'lib/biosphere/deployment.rb', line 8

def export
  @export
end

#feature_manifestsObject (readonly)

Returns the value of attribute feature_manifests.



8
9
10
# File 'lib/biosphere/deployment.rb', line 8

def feature_manifests
  @feature_manifests
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/biosphere/deployment.rb', line 8

def name
  @name
end

#nodeObject

Returns the value of attribute node.



9
10
11
# File 'lib/biosphere/deployment.rb', line 9

def node
  @node
end

#resourcesObject (readonly)

Returns the value of attribute resources.



8
9
10
# File 'lib/biosphere/deployment.rb', line 8

def resources
  @resources
end

#stateObject

Returns the value of attribute state.



9
10
11
# File 'lib/biosphere/deployment.rb', line 9

def state
  @state
end

#target_groupsObject (readonly)

Returns the value of attribute target_groups.



8
9
10
# File 'lib/biosphere/deployment.rb', line 8

def target_groups
  @target_groups
end

Instance Method Details

#add_resource_to_target_group(resource_type, resource_name, target_group) ⇒ Object



69
70
71
72
# File 'lib/biosphere/deployment.rb', line 69

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) ⇒ Object



105
106
107
108
109
110
# File 'lib/biosphere/deployment.rb', line 105

def delayed(&block)
    delayed_call = {
        :block => block
    }
    @delayed << delayed_call
end

#evaluate_outputs(outputs) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/biosphere/deployment.rb', line 164

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_resourcesObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/biosphere/deployment.rb', line 206

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) ⇒ Object



230
231
232
# File 'lib/biosphere/deployment.rb', line 230

def id_of(type,name)
    "${#{type}.#{name}.id}"
end

#load_outputs(tfstate_filename) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/biosphere/deployment.rb', line 188

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) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/biosphere/deployment.rb', line 142

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) ⇒ Object



234
235
236
237
238
239
# File 'lib/biosphere/deployment.rb', line 234

def output_of(type, name, *values)
    if self.name
        name = self.name + "_" + name
    end
    "${#{type}.#{name}.#{values.join(".")}}"
end

#provider(name, spec = {}) ⇒ Object



101
102
103
# File 'lib/biosphere/deployment.rb', line 101

def provider(name, spec={})
    @export["provider"][name.to_s] = spec
end

#register(deployment) ⇒ Object



91
92
93
# File 'lib/biosphere/deployment.rb', line 91

def register(deployment)
    @deployments << deployment
end

#resource(type, name, target_group = nil, &block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/biosphere/deployment.rb', line 112

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

end

#setup(settings) ⇒ Object



74
75
# File 'lib/biosphere/deployment.rb', line 74

def setup(settings)
end

#to_json(pretty = false) ⇒ Object



241
242
243
244
245
246
247
# File 'lib/biosphere/deployment.rb', line 241

def to_json(pretty=false)
    if pretty
        return JSON.pretty_generate(@export)
    else
        return JSON.generate(@export)
    end
end

#variable(name, value) ⇒ Object



95
96
97
98
99
# File 'lib/biosphere/deployment.rb', line 95

def variable(name, value)
    @export["variable"][name] = {
        "default" => value
    }
end