class DTK::Client::Operation::Service::Exec

Constants

RegexTypes

Public Class Methods

execute(args = Args.new) click to toggle source
# File lib/client/operation/service/exec.rb, line 21
def self.execute(args = Args.new)
  wrap_operation(args) do |args|
    service_instance = args.required(:service_instance)
    action           = args.required(:action)
    action_params    = args[:action_params]
    directory_path   = args[:directory_path]
    breakpoint       = args[:breakpoint]
    attempts         = args[:attempts] || ""
    sleep            = args[:sleep] || ""    

    task_params = {}
    process_action_params(action_params, task_params) unless action_params.eql? nil

    # this is temporary fix to handle new node as component format ec2::node[node_name]/action
    # will transform ec2::node[node_name]/action to node_name/action
    action_node, action_name = (action||"").split('/')
    if action_node && action_name
      if action_node_match = action_node.match(/^ec2::node\[(.*)\]/) || action_node.match(/^ec2::node_group\[(.*)\]/)
        matched_node = $1
        action = "#{matched_node}/#{action_name}"
      end
    end

    # match if sent node/component
    if task_action_match = action.match(/(^[\w\-\:]*)\/(.*)/)
      node, action = $1, $2
      task_params.merge!("node" => node)
    end

    modified_args = Args.new(
      :dir => directory_path,
      :error_msg => "To allow #{args[:command]} to go through, invoke 'dtk push' to push the changes to server before invoking #{args[:command]} again",
      :command => 'exec'
    )
    ClientModuleDir::ServiceInstance.modified_service_instance_or_nested_modules?(modified_args)
  
    post_body = PostBody.new(
      :task_params? => task_params
    )
    post_body.merge!(:breakpoint => breakpoint) if breakpoint
    post_body.merge!(:attempts => attempts, :sleep => sleep) if attempts || sleep
    encoded_action =  URI.encode_www_form_component("#{action}")
    response = rest_post("#{BaseRoute}/#{service_instance}/#{encoded_action}", post_body)

    if confirmation_message = response.data(:confirmation_message)
      unless Console.prompt_yes_no("Service instance has been stopped, do you want to start it?", :add_options => true)
        return Response::Ok.new(:empty_workflow => true) 
      end

      response = rest_post("#{BaseRoute}/#{service_instance}/#{encoded_action}", post_body.merge!(:start_assembly => true, :skip_violations => true))
    end

    if response.data(:empty_workflow)
      OsUtil.print_warning("There are no steps in the workflow to execute")
      return Response::Ok.new('empty_workflow' => true) 
    end

    response
  end
end

Private Class Methods

extract_action_value(ac_params) click to toggle source
# File lib/client/operation/service/exec.rb, line 92
def self.extract_action_value(ac_params)
  if ac_params.start_with?('[')
    match_and_return(ac_params, :array)
  elsif ac_params.start_with?('{')
    match_and_return(ac_params, :hash)
  else
    if match = ac_params.match(/([^,]+),(.*)/)
      return [match[1], match[2]]
    else
      return [ac_params, ""]
    end
  end
end
match_and_return(ac_params, type) click to toggle source
# File lib/client/operation/service/exec.rb, line 106
def self.match_and_return(ac_params, type)
  match = ac_params.match(RegexTypes[type])
  return [match[1], prettify(match[2])]
end
prettify(remaining) click to toggle source
# File lib/client/operation/service/exec.rb, line 116
def self.prettify(remaining)
  remaining.sub!(",", "") unless remaining.empty?
  remaining
end
process_action_params(action_params, params) click to toggle source
# File lib/client/operation/service/exec.rb, line 84
def self.process_action_params(action_params, params)
  if match = action_params.match(/([^=]+)=(.+)/)
    extracted_value, new_params = extract_action_value(match[2])
    params.merge!(match[1] => extracted_value)
    process_action_params(new_params, params) unless new_params.empty?
  end
end