Skip to content

Commit 79bdb1f

Browse files
author
Liu, Xiaopeng (133)
committed
0.1.3 fix #4 #5
Signed-off-by: Liu, Xiaopeng (133) <xiaopeng.liu@daimler.com>
1 parent fc45b9e commit 79bdb1f

File tree

9 files changed

+131
-36
lines changed

9 files changed

+131
-36
lines changed

CHANGELOG.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
# Changelog
22

3-
## [0.1.1][]
3+
## [0.1.3]
44

55
### Changed
66

7-
- Bugfix: kill process now passes correct parameter
8-
- Refactory: abstract core workflow into one common function to reduce duplicates
7+
- Refactory: [Issue #4](https://github.com/chaostoolkit-incubator/chaostoolkit-saltstack/issues/4) Salt API accepts eauth method as parameter in configuration
98

109
### Added
1110

12-
- Network experiments now accepts device name to adapt different os
13-
- Add experiment action of kill processes by name
14-
- Add experiment action of kill process by PID
15-
- Add experiment probe to check process PID by name
11+
- Add experiment action: run customized command line with run_cmd method
12+
13+
## [0.1.1]
1614

1715
### Changed
1816

19-
- Refactory: abstract core workflow into one common function to reduce duplicates
17+
- Bugfix: kill process now passes correct parameter
18+
- Refactory: abstract core workflow into one common function to reduce duplicates
2019

2120
### Added
2221

23-
- Add experiment action of kill processes by name
24-
- Add experiment action of kill process by PID
25-
- Add experiment probe to check process PID by name
22+
- Network experiments now accepts device name to adapt different os
23+
- Add experiment action of kill processes by name
24+
- Add experiment action of kill process by PID
25+
- Add experiment probe to check process PID by name
2626

2727
## [0.1.0][]
2828

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,15 @@ There are two ways of doing this:
120120
```
121121

122122
* or you inject the secrets explicitly to the experiment definition:
123+
SALTMASTER_EAUTH is optional and default to pam
123124

124125
```json
125126
{
126127
"saltstack": {
127128
"SALTMASTER_HOST": "https://172.10.20.666",
128129
"SALTMASTER_USER": "username",
129130
"SALTMASTER_PASSWORD": "password"
131+
"SALTMASTER_EAUTH": "sharedsecret"
130132
}
131133
}
132134
```

chaossaltstack/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ def __init__(self, configuration):
3333
elif 'username' in configuration:
3434
self.username = configuration['username']
3535
self.password = configuration['password']
36+
self.eauth = configuration['eauth']
3637
# Default settings for Salt Master
3738
self.headers = {"Content-type": "application/json"}
3839
self.params = {'client': 'local', 'fun': '', 'tgt': ''}
3940
# Use User/Pass
4041
self.login_url = self.url + "/login"
4142
self.login_params = {
4243
'username': self.username, 'password': self.password,
43-
'eauth': 'pam'
44+
'eauth': self.eauth
4445
}
4546

4647
def run_cmd(self, tgt, method: str, arg=None):
@@ -164,9 +165,10 @@ def saltstack_api_client(secrets: Secrets = None) -> salt_api_client:
164165
165166
* SALTMASTER_HOST: Salt Master API address
166167
167-
You can authenticate with user / password via:
168+
You can authenticate with user / password / eauth via:
168169
* SALTMASTER_USER: the user name
169170
* SALTMASTER_PASSWORD: the password
171+
* SALTMASTER_EAUTH: the auth method, default to pam
170172
171173
Or via a token:
172174
* SALTMASTER_TOKEN
@@ -197,6 +199,7 @@ def lookup(k: str, d: str = None) -> str:
197199
if "SALTMASTER_USER" in env or "SALTMASTER_USER" in secrets:
198200
configuration['username'] = lookup("SALTMASTER_USER", "")
199201
configuration['password'] = lookup("SALTMASTER_PASSWORD", "")
202+
configuration['eauth'] = lookup("SALTMASTER_EAUTH", "pam")
200203
elif "SALTMASTER_TOKEN" in env or "SALTMASTER_TOKEN" in secrets:
201204
configuration['token'] = lookup("SALTMASTER_TOKEN")
202205
else:

chaossaltstack/machine/actions.py

Lines changed: 87 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
from ..types import SaltStackResponse
1313
from .constants import OS_LINUX, OS_WINDOWS
1414
from .constants import BURN_CPU, FILL_DISK, NETWORK_UTIL, \
15-
BURN_IO, KILLALL_PROCESSES, KILL_PROCESS
15+
BURN_IO, KILLALL_PROCESSES, KILL_PROCESS, RUN_CMD
1616

1717

1818
__all__ = ["burn_cpu", "fill_disk", "network_latency", "burn_io",
1919
"network_loss", "network_corruption", "network_advanced",
20-
"killall_processes", "kill_process"]
20+
"killall_processes", "kill_process", "run_cmd"]
2121

2222

2323
def burn_cpu(instance_ids: List[str] = None,
@@ -209,7 +209,7 @@ def network_loss(instance_ids: List[str] = None,
209209
param["duration"] = execution_duration
210210
param["param"] = "loss " + loss_ratio
211211
param["device"] = device
212-
212+
213213
return __default_salt_experiment__(instance_ids=instance_ids,
214214
execution_duration=execution_duration,
215215
param=param,
@@ -253,7 +253,6 @@ def network_corruption(instance_ids: List[str] = None,
253253
param["param"] = "corrupt " + corruption_ratio
254254
param["device"] = device
255255

256-
257256
return __default_salt_experiment__(instance_ids=instance_ids,
258257
execution_duration=execution_duration,
259258
param=param,
@@ -341,7 +340,7 @@ def killall_processes(instance_ids: List[str] = None,
341340
Chaostoolkit Secrets
342341
"""
343342
logger.debug(
344-
"Start network_latency: configuration='{}', instance_ids='{}'".format(
343+
"Start killall_process: configuration='{}', instance_ids='{}'".format(
345344
configuration, instance_ids))
346345

347346
param = dict()
@@ -389,7 +388,7 @@ def kill_process(instance_ids: List[str] = None,
389388
Chaostoolkit Secrets
390389
"""
391390
logger.debug(
392-
"Start network_latency: configuration='{}', instance_ids='{}'".format(
391+
"Start kill_process: configuration='{}', instance_ids='{}'".format(
393392
configuration, instance_ids))
394393

395394
param = dict()
@@ -406,6 +405,50 @@ def kill_process(instance_ids: List[str] = None,
406405
)
407406

408407

408+
def run_cmd(instance_ids: List[str] = None,
409+
execution_duration: str = "60",
410+
cmd: List[str] = None,
411+
configuration: Configuration = None,
412+
secrets: Secrets = None) -> SaltStackResponse:
413+
"""
414+
run cmd
415+
Linus -> Shell
416+
Windows -> PowerShell
417+
418+
Parameters
419+
----------
420+
instance_ids : List[str]
421+
Filter the virtual machines. If the filter is omitted all machines in
422+
the subscription will be selected as potential chaos candidates.
423+
execution_duration : str, optional default to 1 second
424+
This is not technically not useful as the process usually is killed
425+
without and delay, however you can set more seconds here to let the
426+
thread wait for more time to extend your experiment execution in case
427+
you need to watch more on the observation metrics.
428+
cmd : List[str]
429+
Lines of your commands
430+
configuration : Configuration
431+
Chaostoolkit Configuration
432+
secrets : Secrets
433+
Chaostoolkit Secrets
434+
"""
435+
logger.debug(
436+
"Start run_cmd: configuration='{}', instance_ids='{}'".format(
437+
configuration, instance_ids))
438+
439+
param = dict()
440+
param["duration"] = execution_duration
441+
param["cmd"] = cmd
442+
443+
return __default_salt_experiment__(instance_ids=instance_ids,
444+
execution_duration=execution_duration,
445+
param=param,
446+
experiment_type=RUN_CMD,
447+
configuration=configuration,
448+
secrets=secrets
449+
)
450+
451+
409452
###############################################################################
410453
# Private helper functions
411454
###############################################################################
@@ -459,25 +502,52 @@ def __default_salt_experiment__(instance_ids: List[str] = None,
459502
)
460503

461504

462-
def __construct_script_content__(action, os_type, parameters):
505+
def __construct_script_content__(action: str = None,
506+
os_type: str = None,
507+
parameters: dict = None):
508+
"""
509+
As for now, no Windows action supported except burn CPU
510+
511+
:param action:
512+
:param os_type: { OS_LINUX | OS_WINDOWS }
513+
:param parameters:
514+
:return:
515+
"""
463516

464-
if os_type == OS_WINDOWS:
465-
script_name = action+".ps1"
466-
# TODO in ps1
467-
cmd_param = '\n'.join(
468-
['='.join([k, "'"+v+"'"]) for k, v in parameters.items()])
469-
elif os_type == OS_LINUX:
470-
script_name = action+".sh"
471-
cmd_param = '\n'.join(
472-
['='.join([k, "'"+v+"'"]) for k, v in parameters.items()])
517+
cmd_param = ""
518+
if os_type == OS_LINUX:
519+
file_suffix = ".sh"
520+
p_delimiter = ""
521+
cmdline_delimiter = " && "
522+
elif os_type == OS_WINDOWS:
523+
file_suffix = ".ps1"
524+
p_delimiter = "$"
525+
cmdline_delimiter = "\n"
473526
else:
474527
raise FailedActivity(
475528
"Cannot find corresponding script for {} on OS: {}".format(
476529
action, os_type))
477530

531+
if action == "run_cmd":
532+
cmdline_param = cmdline_delimiter.join(parameters['cmd'])
533+
# parameters.pop('cmd')
534+
del parameters['cmd']
535+
else:
536+
cmdline_param = ""
537+
538+
if parameters is not None:
539+
param_list = list()
540+
for k, v in parameters.items():
541+
param_list.append('='.join([p_delimiter + k, "'" + v + "'"]))
542+
cmd_param = '\n'.join(param_list)
543+
else:
544+
logger.info("No parameter parsed, return default script content")
545+
546+
script_name = action + file_suffix
547+
478548
with open(os.path.join(os.path.dirname(__file__),
479549
"scripts", script_name)) as file:
480550
script_content = file.read()
481551
# merge duration
482-
script_content = cmd_param + "\n" + script_content
552+
script_content = cmd_param + "\n" + cmdline_param + "\n" + script_content
483553
return script_content

chaossaltstack/machine/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
NETWORK_UTIL = "network_advanced"
1010
KILLALL_PROCESSES = "killall_processes"
1111
KILL_PROCESS = "kill_process"
12+
RUN_CMD = "run_cmd"

chaossaltstack/machine/scripts/burn_io.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ done
88
EOF
99

1010
chmod +x /tmp/loop.sh
11-
timeout --preserve-status $duration /tmp/loop.sh
11+
timeout $duration /tmp/loop.sh
1212

1313
# while true;
1414
# do
1515
# dd if=/dev/urandom of=/experiment_burnio bs=1M count=1024 iflag=fullblock status=none
1616
# done &
1717

1818
ret=$?
19-
if [ $ret -eq 0 ]; then
19+
if [ $ret -eq 0 || $ret -eq 124 ]; then
2020
echo "experiment burnio -> <$instance_id>: success"
2121
else
2222
echo "experiment brunio -> <$instance_id>: fail"

chaossaltstack/machine/scripts/network_advanced.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ fi
1717
experiment=$(tc qdisc add dev $device root netem $param)
1818
ret=$?
1919
if [ $ret -eq 0 ]; then
20-
echo "experiment network_latency -> <$instance_id>: success"
20+
echo "experiment network:[$param] -> <$instance_id>: success"
2121
else
22-
echo "experiment network_latency -> <$instance_id>: fail"
22+
echo "experiment network:[$param] -> <$instance_id>: fail"
2323
fi
2424

2525
#Sleep $duration
@@ -38,7 +38,7 @@ fi
3838
tc -s qdisc show dev $device |grep pfifo_fast 2>&1 >/dev/null
3939
ret2=$?
4040
if [ $ret2 -eq 0 ]; then
41-
echo "recover network_latency -> <$instance_id>: success"
41+
echo "recover network:[$param] -> <$instance_id>: success"
4242
else
43-
echo "recover network_latency -> <$instance_id>: fail"
43+
echo "recover network:[$param] -> <$instance_id>: fail"
4444
fi
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Try
2+
{
3+
4+
}
5+
Catch
6+
{
7+
8+
}
9+
Finally
10+
{
11+
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
ret=$?
3+
if [ $ret -eq 0 ]; then
4+
echo "experiment run_cmd -> <$instance_id>: success"
5+
else
6+
echo "experiment run_cmd -> <$instance_id>: fail"
7+
fi

0 commit comments

Comments
 (0)