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




75
76
77
# File 'lib/core/plugin/plan.rb', line 75

def action_settings
  _get(:action_settings, {})
end

#clear_tokensObject



153
154
155
# File 'lib/core/plugin/plan.rb', line 153

def clear_tokens
  @tokens.wipe
end

#config_pathObject




113
114
115
# File 'lib/core/plugin/plan.rb', line 113

def config_path
  _get(:config_path, path)
end

#create_batch(resources) ⇒ Object




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

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

#create_resource(settings) ⇒ Object




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

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

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

#create_sequence(resources) ⇒ Object


Utilities



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

def create_sequence(resources)
  CM.sequence({
    :id => Nucleon.sha1(resources),
    :resources => resources,
    :plan => myself
  }, _get(:sequence_provider, :default))
end

#execute(operation, check_only = false) ⇒ Object




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

def execute(operation, check_only = false)
  success = true

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

      if respond_to?(method) && load(check_only)
        init_tokens
        success = send(method) unless check_only
      else
        success = false
      end
    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
# File 'lib/core/plugin/plan.rb', line 41

def init_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)


62
63
64
# File 'lib/core/plugin/plan.rb', line 62

def initialized?(options = {})
  loaded_config
end

#key_directoryObject



85
86
87
# File 'lib/core/plugin/plan.rb', line 85

def key_directory
  _get(:key_directory, path)
end

#load(check_only = false) ⇒ Object


Operations



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

def load(check_only = false)
  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) unless check_only

    yield if block_given?
  end
  success
end

#loaded_configObject


Property accessors / modifiers



69
70
71
# File 'lib/core/plugin/plan.rb', line 69

def loaded_config
  @loaded_config
end

#manifestObject



99
100
101
# File 'lib/core/plugin/plan.rb', line 99

def manifest
  export
end

#manifest_configObject



103
104
105
# File 'lib/core/plugin/plan.rb', line 103

def manifest_config
  get_hash(:config)
end

#manifest_fileObject




91
92
93
# File 'lib/core/plugin/plan.rb', line 91

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

#manifest_pathObject



95
96
97
# File 'lib/core/plugin/plan.rb', line 95

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

#manifest_resourcesObject



107
108
109
# File 'lib/core/plugin/plan.rb', line 107

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




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

def operation_deploy
  sequence.forward(:deploy)
end

#operation_destroyObject




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

def operation_destroy
  sequence.reverse(:destroy)
end

#pathObject




81
82
83
# File 'lib/core/plugin/plan.rb', line 81

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

#remove_token(id, location) ⇒ Object



148
149
150
151
# File 'lib/core/plugin/plan.rb', line 148

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

#sequenceObject




119
120
121
# File 'lib/core/plugin/plan.rb', line 119

def sequence
  @sequence
end

#set_token(id, location, value) ⇒ Object



143
144
145
146
# File 'lib/core/plugin/plan.rb', line 143

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

#stepObject




261
262
263
264
# File 'lib/core/plugin/plan.rb', line 261

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

#token_directoryObject




125
126
127
# File 'lib/core/plugin/plan.rb', line 125

def token_directory
  _get(:token_directory, config_path)
end

#token_fileObject



129
130
131
# File 'lib/core/plugin/plan.rb', line 129

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

#token_pathObject



133
134
135
# File 'lib/core/plugin/plan.rb', line 133

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

#tokensObject




139
140
141
# File 'lib/core/plugin/plan.rb', line 139

def tokens
  @tokens.parse
end

#trapObject




159
160
161
# File 'lib/core/plugin/plan.rb', line 159

def trap
  _get(:trap, false)
end