|
1
|
|
|
# pylint: disable=no-member |
|
2
|
|
|
|
|
3
|
|
|
from st2actions.runners.pythonrunner import Action |
|
4
|
|
|
from requests import Request |
|
5
|
|
|
from utils import sanitize_payload |
|
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class SaltPackage(object): |
|
9
|
|
|
_expression_forms = [ |
|
10
|
|
|
'glob', |
|
11
|
|
|
'grain', |
|
12
|
|
|
'pillar', |
|
13
|
|
|
'nodegroup', |
|
14
|
|
|
'list', |
|
15
|
|
|
'compound' |
|
16
|
|
|
] |
|
17
|
|
|
_data = {"eauth": "", |
|
18
|
|
|
"username": "", |
|
19
|
|
|
"password": "", |
|
20
|
|
|
"client": "", |
|
21
|
|
|
"fun": ""} |
|
22
|
|
|
|
|
23
|
|
|
def __init__(self, client='local'): |
|
24
|
|
|
self._data['client'] = client |
|
25
|
|
|
|
|
26
|
|
|
@property |
|
27
|
|
|
def data(self): |
|
28
|
|
|
return self._data |
|
29
|
|
|
|
|
30
|
|
|
@data.setter |
|
31
|
|
|
def data(self, key_value=[]): |
|
|
|
|
|
|
32
|
|
|
key, value = key_value |
|
|
|
|
|
|
33
|
|
|
self._data[key] = value |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
class SaltAction(Action): |
|
37
|
|
|
|
|
38
|
|
|
def __init__(self, config): |
|
39
|
|
|
super(SaltAction, self).__init__(config=config) |
|
40
|
|
|
self.url = self.config.get('api_url', None) |
|
41
|
|
|
self.eauth = self.config.get('eauth', None) |
|
42
|
|
|
self.username = self.config.get('username', None) |
|
43
|
|
|
self.password = self.config.get('password', None) |
|
44
|
|
|
|
|
45
|
|
|
def generate_package(self, client='local', cmd=None, args=None, |
|
46
|
|
|
**kwargs): |
|
47
|
|
|
self.data = SaltPackage(client).data |
|
|
|
|
|
|
48
|
|
|
self.data['eauth'] = self.eauth |
|
49
|
|
|
self.data['username'] = self.username |
|
50
|
|
|
self.data['password'] = self.password |
|
51
|
|
|
if cmd: |
|
52
|
|
|
self.data['fun'] = cmd |
|
53
|
|
|
if client is 'local': |
|
54
|
|
|
self.data['tgt'] = kwargs.get('target', '*') |
|
55
|
|
|
self.data['expr_form'] = kwargs.get('expr_form', 'glob') |
|
56
|
|
|
if kwargs.get('kwargs', None) is not None: |
|
57
|
|
|
self.data['kwarg'] = kwargs['kwargs']['kwargs'] |
|
58
|
|
|
if args is not None: |
|
59
|
|
|
self.data['arg'] = args |
|
60
|
|
|
clean_payload = sanitize_payload(('username', 'password'), self.data) |
|
61
|
|
|
self.logger.info("[salt] Payload to be sent: {0}".format(clean_payload)) |
|
62
|
|
|
|
|
63
|
|
|
def generate_request(self): |
|
64
|
|
|
req = Request('POST', |
|
65
|
|
|
"{0}/run".format(self.url), |
|
66
|
|
|
headers={'content-type': 'application/json', |
|
67
|
|
|
'charset': 'utf-8'}) |
|
68
|
|
|
return req.prepare() |
|
69
|
|
|
|