Class: CM::Plugin::Plan

Inherits:
Object
  • Object
show all
Includes:
Nucleon::Parallel
Defined in:
lib/core/plugin/plan.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.register_idsObject




14
15
16
# File 'lib/core/plugin/plan.rb', line 14

def self.register_ids
  [ :directory, :revision ]
end

Instance Method Details

#action_settingsObject




77
78
79
# File 'lib/core/plugin/plan.rb', line 77

def action_settings
  _get(:action_settings, {})
end

#clear_tokensObject



155
156
157
# File 'lib/core/plugin/plan.rb', line 155

def clear_tokens
  @tokens.wipe
end

#config_pathObject




115
116
117
# File 'lib/core/plugin/plan.rb', line 115

def config_path
  _get(:config_path, path)
end

#create_batch(resources) ⇒ Object




240
241
242
243
244
245
246
# File 'lib/core/plugin/plan.rb', line 240

def create_batch(resources)
  CM.batch({
    :plan => myself,
    :resources => resources,
    :new => true
  }, _get(:batch_provider, :celluloid))
end

#create_resource(settings) ⇒ Object




250
251
252
253
254
255
256
257
258
259
# File 'lib/core/plugin/plan.rb', line 250

def create_resource(settings)
  settings = Nucleon::Config.ensure(settings)
  settings[:type] ||= _get(:default_resource_provider, :variables)

  CM.resource({
    :plan => myself,
    :settings => settings.export,
    :id => settings[:name]
  }, settings[:type])
end

#create_sequence(resources) ⇒ Object


Utilities



229
230
231
232
233
234
235
236
# File 'lib/core/plugin/plan.rb', line 229

def create_sequence(resources)
  CM.sequence({
    :plan => myself,
    :settings => manifest_config,
    :resources => resources,
    :new => true,
  }, _get(:sequence_provider, :default))
end

#execute(operation) ⇒ Object




192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/core/plugin/plan.rb', line 192

def execute(operation)
  success = true

  if initialized?
    if ::File.exist?(manifest_path)
      method = "operation_#{operation}"

      if respond_to?(method) && load
        init_tokens
        success = send(method)
      end
      success
    else
      error('manifest_file', { :file => manifest_path })
      success = false
    end
  else
    success = false
  end
  success
end

#init_tokensObject




41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/core/plugin/plan.rb', line 41

def init_tokens
  clear_tokens

  collect_tokens = lambda do |local_settings, token|
    local_settings.each do |name, value|
      setting_token = [ array(token), name ].flatten

      if value.is_a?(Hash)
        collect_tokens.call(value, setting_token)
      else
        token_base = setting_token.shift
        set_token(token_base, setting_token, value)
      end
    end
  end

  # Generate config tokens
  collect_tokens.call(manifest_config, 'config')
end

#initialized?(options = {}) ⇒ Boolean


Checks

Returns:

  • (Boolean)


64
65
66
# File 'lib/core/plugin/plan.rb', line 64

def initialized?(options = {})
  loaded_config
end

#key_directoryObject



87
88
89
# File 'lib/core/plugin/plan.rb', line 87

def key_directory
  _get(:key_directory, path)
end

#loadObject


Operations



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/core/plugin/plan.rb', line 168

def load
  success = true

  if initialized?
    # Initialize plan manifest (default config and resources)
    wipe
    import(CM.configuration(extended_config(:manifest_data, {
      :provider => _get(:manifest_provider, :file),
      :path => manifest_path
    })).export)

    # Merge in configuration overlay (private config)
    override(loaded_config.get_hash(:config), :config)

    # Initializeresource sequence
    @sequence = create_sequence(manifest_resources)

    yield if block_given?
  end
  success
end

#loaded_configObject


Property accessors / modifiers



71
72
73
# File 'lib/core/plugin/plan.rb', line 71

def loaded_config
  @loaded_config
end

#manifestObject



101
102
103
# File 'lib/core/plugin/plan.rb', line 101

def manifest
  export
end

#manifest_configObject



105
106
107
# File 'lib/core/plugin/plan.rb', line 105

def manifest_config
  get_hash(:config)
end

#manifest_fileObject




93
94
95
# File 'lib/core/plugin/plan.rb', line 93

def manifest_file
  _get(:manifest_file, 'plan.yml')
end

#manifest_pathObject



97
98
99
# File 'lib/core/plugin/plan.rb', line 97

def manifest_path
  ::File.join(path, manifest_file)
end

#manifest_resourcesObject



109
110
111
# File 'lib/core/plugin/plan.rb', line 109

def manifest_resources
  get_array(:resources)
end

#normalize(reload) ⇒ Object


Plugin interface



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/core/plugin/plan.rb', line 21

def normalize(reload)
  super

  if !reload
    @loaded_config = CM.configuration(extended_config(:config_data, {
      :provider => _get(:config_provider, :directory),
      :path => config_path
    }))

    @tokens = CM.configuration(extended_config(:token_data, {
      :provider => _get(:token_provider, :file),
      :path => token_path
    }))

    yield if block_given?
  end
end

#operation_deployObject




216
217
218
# File 'lib/core/plugin/plan.rb', line 216

def operation_deploy
  sequence.forward(:deploy)
end

#operation_destroyObject




222
223
224
# File 'lib/core/plugin/plan.rb', line 222

def operation_destroy
  sequence.reverse(:destroy)
end

#pathObject




83
84
85
# File 'lib/core/plugin/plan.rb', line 83

def path
  _get(:path, Dir.pwd)
end

#remove_token(id, location) ⇒ Object



150
151
152
153
# File 'lib/core/plugin/plan.rb', line 150

def remove_token(id, location)
  @tokens.delete("#{id}:#{array(location).join('.')}")
  @tokens.save
end

#sequenceObject




121
122
123
# File 'lib/core/plugin/plan.rb', line 121

def sequence
  @sequence
end

#set_token(id, location, value) ⇒ Object



145
146
147
148
# File 'lib/core/plugin/plan.rb', line 145

def set_token(id, location, value)
  @tokens["#{id}:#{array(location).join('.')}"] = value
  @tokens.save
end

#stepObject




263
264
265
266
# File 'lib/core/plugin/plan.rb', line 263

def step
  answer = ask('Continue? (yes|no): ', { :i18n => false })
  answer.match(/^[Yy][Ee][Ss]$/) ? false : true
end

#token_directoryObject




127
128
129
# File 'lib/core/plugin/plan.rb', line 127

def token_directory
  _get(:token_directory, config_path)
end

#token_fileObject



131
132
133
# File 'lib/core/plugin/plan.rb', line 131

def token_file
  _get(:token_file, 'tokens.json')
end

#token_pathObject



135
136
137
# File 'lib/core/plugin/plan.rb', line 135

def token_path
  ::File.join(token_directory, token_file)
end

#tokensObject




141
142
143
# File 'lib/core/plugin/plan.rb', line 141

def tokens
  @tokens.parse
end

#trapObject




161
162
163
# File 'lib/core/plugin/plan.rb', line 161

def trap
  _get(:trap, false)
end