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
68
69
70
71
# File 'lib/biosphere/deployment.rb', line 10

def initialize(*args)

    @parent = nil
    @name = "unnamed"
    if args[0].kind_of?(::Biosphere::Deployment) || args[0].kind_of?(::Biosphere::Suite)
        @parent = args.shift
    elsif 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" => {}
    }

    if @parent.is_a?(::Biosphere::Suite)
        if settings[:deployment_name]
            @name = settings[:deployment_name]
        else
            puts "\nYou need to specify :deployment_name in the Deployment settings. For example:"
            puts "cluster = AdsDeliveryCluster.new(suite, MyDeliveryTestSettings.new({deployment_name: \"my-delivery-test-cluster\"})\n\n"
            raise RuntimeError.new "No :deployment_name specified in Deployment settings"
        end
        
        @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 = []

    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

#stateObject

Returns the value of attribute state.



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

def state
  @state
end

Instance Method Details

#delayed(&block) ⇒ Object



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

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

#evaluate_outputs(outputs) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/biosphere/deployment.rb', line 149

def evaluate_outputs(outputs)

    # Call first sub-deployments
    @deployments.each do |deployment|
        deployment.evaluate_outputs(outputs)
    end
    
    @outputs.each do |output|
        value = outputs[output[:name]]
        instance_exec(output[:name], value["value"], value, &output[:block])
    end
end

#evaluate_resourcesObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/biosphere/deployment.rb', line 176

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)
        proxy.instance_eval(&resource[:block])

        @export["resource"][resource[:type].to_s][resource[:name].to_s] = proxy.output
    end
end

#load_outputs(tfstate_filename) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/biosphere/deployment.rb', line 162

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"]

    evaluate_outputs(outputs)
end

#output(name, value, &block) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/biosphere/deployment.rb', line 134

def output(name, value, &block)
    @export["output"][name] = {
        "value" => value
    }

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

        @outputs << output
    end
end

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



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

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

#register(deployment) ⇒ Object



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

def register(deployment)
    @deployments << deployment
end

#resource(type, name, &block) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/biosphere/deployment.rb', line 111

def resource(type, name, &block)
    @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 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



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

def setup(settings)
end

#to_json(pretty = false) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/biosphere/deployment.rb', line 198

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

#variable(name, value) ⇒ Object



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

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