#!/usr/bin/env python

import os, sys, time, json, argparse
from st2client import models
from st2client.client import Client

END_STATES= ['succeeded','failed']
ST2HOST = 'localhost'

parser = argparse.ArgumentParser()

parser.add_argument('--name', action="store", dest="name", required=True)
parser.add_argument('--action', action="store", dest="action", required=True)
parser.add_argument('--params', action="store", dest="params")

args = parser.parse_args()
runner = None

def runAction(action_ref,params):
    st2_endpoints = {
      'action': "http://%s:9101" % ST2HOST,
      'reactor': "http://%s:9102" % ST2HOST,
      'datastore': "http://%s:9103" % ST2HOST
    }

    client = Client(st2_endpoints)
    action_exec_mgr = client.managers['LiveAction']
    runner_mgr = client.managers['RunnerType']

    execution = models.LiveAction()
    execution.action = action_ref
    execution.parameters = param_parser(params)
    actionexec = action_exec_mgr.create(execution)

    while actionexec.status not in END_STATES:
      time.sleep(2)
      actionexec = action_exec_mgr.get_by_id(actionexec.id)

    return actionexec

def param_parser(params):
    parameters = {}
    if params is not None:
        param_list = params.split(' ')
        for p in param_list:
            if '=' in p:
                k, v = p.split('=',1)
                if ',' in v:
                    v = filter(None,v.split(','))
                    
            else:
                k = 'cmd'
                v = p
            parameters[k] = v
    return parameters

actionexec = runAction(action_ref=args.action,params=args.params)
output = {args.name: actionexec.result}

print json.dumps(output)
if actionexec.status != 'succeeded':
    sys.exit(2)

@@ -27,28 +27,42 @@
 ort 
-os, sys, time, json,
+sys%0Aimport time%0Aimport json%0Aimport
  arg
@@ -143,16 +143,17 @@
 D_STATES
+ 
 = %5B'succ
@@ -159,16 +159,17 @@
 ceeded',
+ 
 'failed'
@@ -487,16 +487,17 @@
 = None%0A%0A
+%0A
 def runA
@@ -513,16 +513,17 @@
 ion_ref,
+ 
 params):
@@ -545,16 +545,18 @@
 nts = %7B%0A
+  
       'a
@@ -597,16 +597,18 @@
 ,%0A      
+  
 'reactor
@@ -634,24 +634,26 @@
  %25 ST2HOST,%0A
+  
       'datas
@@ -783,55 +783,8 @@
 on'%5D
-%0A    runner_mgr = client.managers%5B'RunnerType'%5D
 %0A%0A  
@@ -1004,16 +1004,18 @@
 :%0A      
+  
 time.sle
@@ -1020,16 +1020,18 @@
 leep(2)%0A
+  
       ac
@@ -1106,16 +1106,17 @@
 onexec%0A%0A
+%0A
 def para
@@ -1308,16 +1308,17 @@
 lit('=',
+ 
 1)%0A     
@@ -1377,16 +1377,17 @@
 er(None,
+ 
 v.split(
@@ -1396,29 +1396,8 @@
 '))%0A
-                    %0A
     
@@ -1557,16 +1557,17 @@
 .action,
+ 
 params=a

make the linter happy