Class: Nucleon::Plugin::CloudAction

Inherits:
Object
  • Object
show all
Defined in:
lib/core/plugin/cloud_action.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.namespaceObject


Property accessor / modifiers



46
47
48
# File 'lib/core/plugin/cloud_action.rb', line 46

def self.namespace
  :corl
end

Instance Method Details

#configureObject




62
63
64
65
66
67
# File 'lib/core/plugin/cloud_action.rb', line 62

def configure
  super do
    yield if block_given?
    node_config
  end
end

#ensure_network(&block) ⇒ Object




179
180
181
182
183
184
185
186
187
# File 'lib/core/plugin/cloud_action.rb', line 179

def ensure_network(&block)
  codes :network_failure

  if network
    block.call
  else
    myself.status = code.network_failure
  end
end

#ensure_node(node, &block) ⇒ Object



189
190
191
192
193
194
195
196
197
# File 'lib/core/plugin/cloud_action.rb', line 189

def ensure_node(node, &block)
  codes :node_failure

  if node
    block.call
  else
    myself.status = code.node_failure
  end
end

#execute(use_network = true, &code) ⇒ Object




97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/core/plugin/cloud_action.rb', line 97

def execute(use_network = true, &code)
  if use_network
    super(true, true) do
      node_exec do |node|
        hook_config = { :node => node, :network => network }

        code.call(node) if code && extension_check(:exec_init, hook_config)
        myself.status = extension_set(:exec_exit, status, hook_config)
      end
    end
  else
    super(false, false, &code)
  end
end

#execute_remote(node, op, data) ⇒ Object




172
173
174
175
# File 'lib/core/plugin/cloud_action.rb', line 172

def execute_remote(node, op, data)
  # Implement in sub classes if needed
  data
end

#init_network(provider = nil, path = nil) ⇒ Object




150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/core/plugin/cloud_action.rb', line 150

def init_network(provider = nil, path = nil)
  provider = extension_set(:network_provider, :corl) unless provider

  # Get network configuration path
  if CORL.admin?
    network_path = lookup(:corl_network)
    Dir.mkdir(network_path) unless File.directory?(network_path)
  else
    network_path = ( path.nil? ? Dir.pwd : File.expand_path(path) )
  end

  if File.exists?(File.join(network_path, '.nucleon'))
    # Load network if it exists
    network_config = extended_config(:network, { :directory => network_path, :new => true })
    @network       = CORL.network(network_path, network_config, provider)
  else
    @network = nil
  end
end

#networkObject



56
57
58
# File 'lib/core/plugin/cloud_action.rb', line 56

def network
  @network
end

#network=(network) ⇒ Object




52
53
54
# File 'lib/core/plugin/cloud_action.rb', line 52

def network=network
  @network = network
end

#node_configObject


Settings



72
73
74
75
76
77
78
79
80
# File 'lib/core/plugin/cloud_action.rb', line 72

def node_config
  register_str :net_remote, :edit, 'corl.core.action.options.net_remote'
  register_network_provider :net_provider, :corl, [ 'corl.core.action.options.net_provider', 'corl.core.action.errors.network_provider' ]

  register_node_provider :node_provider, :local, [ 'corl.core.action.options.node_provider', 'corl.core.action.errors.node_provider' ]
  register_nodes :nodes, [], [ 'corl.core.action.options.nodes', 'corl.core.action.errors.nodes' ]

  register_bool :parallel, true, 'corl.core.action.options.parallel'
end

#node_execObject




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
141
142
143
144
145
146
# File 'lib/core/plugin/cloud_action.rb', line 114

def node_exec
  init_network(settings[:net_provider]) unless network && settings[:net_provider].to_sym == network.plugin_provider

  #
  # A fork in the road...
  #
  if network && network.has_nodes? && ! settings[:nodes].empty?
    # Execute action on remote nodes
    success = network.batch(settings[:nodes], settings[:node_provider], settings[:parallel]) do |node|
      exec_config = Config.new(settings, {}, true, false)
      exec_config.delete(:nodes)

      result = node.action(plugin_provider, exec_config) do |op, data|
        execute_remote(node, op, data)
      end
      result.status == code.success
    end
    myself.status = code.batch_error unless success
  else
    # Execute statement locally
    node = nil
    node = network.local_node if network

    settings[:net_remote] = sanitize_remote(settings[:net_remote]) if settings.has_key?(:net_remote)

    if validate(node, network)
      yield(node) if block_given?
    else
      puts "\n" + I18n.t('nucleon.core.exec.help.usage') + ': ' + help + "\n" unless quiet?
      myself.status = code.validation_failed
    end
  end
end

#node_ignoreObject




84
85
86
# File 'lib/core/plugin/cloud_action.rb', line 84

def node_ignore
  [ :parallel, :node_provider, :nodes ]
end

#normalize(reload) ⇒ Object


Constuctor / Destructor



37
38
39
40
41
# File 'lib/core/plugin/cloud_action.rb', line 37

def normalize(reload)
  super do
    init_network
  end
end

#parse_property_name(property) ⇒ Object




214
215
216
217
218
219
220
221
222
# File 'lib/core/plugin/cloud_action.rb', line 214

def parse_property_name(property)
  property = property.clone

  if property.size > 1
    property.shift.to_s + '[' + property.join('][') + ']'
  else
    property.shift.to_s
  end
end

#remote_message(remote) ⇒ Object




208
209
210
# File 'lib/core/plugin/cloud_action.rb', line 208

def remote_message(remote)
  remote ? "#{remote}" : "LOCAL ONLY"
end

#sanitize_remote(remote) ⇒ Object


Utilities



202
203
204
# File 'lib/core/plugin/cloud_action.rb', line 202

def sanitize_remote(remote)
  remote && ( ! network || network.remote(remote) ) ? remote : nil
end

#validate(node = nil, network = nil) ⇒ Object


Operations



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

def validate(node = nil, network = nil)
  super(node, network)
end