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(parent, name, settings = {}) ⇒ Deployment

Returns a new instance of Deployment.



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
68
69
70
71
72
73
# 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

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

#all_resourcesObject (readonly)

Returns the value of attribute all_resources.



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

def all_resources
  @all_resources
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



75
76
77
78
# 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) ⇒ Object



111
112
113
114
115
116
# File 'lib/biosphere/deployment.rb', line 111

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

#evaluate_outputs(outputs) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# 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_resourcesObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# 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) ⇒ Object



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

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

#load_outputs(tfstate_filename) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# 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) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# 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) ⇒ Object



241
242
243
244
245
246
# 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 = {}) ⇒ Object



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

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

#register(deployment) ⇒ Object



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

def register(deployment)
    @deployments << deployment
end

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# 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) ⇒ Object



80
81
# File 'lib/biosphere/deployment.rb', line 80

def setup(settings)
end

#to_json(pretty = false) ⇒ Object



248
249
250
251
252
253
254
# 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) ⇒ Object



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

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