Skip to content

Commit 994dcd9

Browse files
author
Liu, Xiaopeng (133)
committed
Add ec2_os actions, enable os level burn_cpu,network,kill process function via aws ssm
Signed-off-by: Liu, Xiaopeng (133) <xiaopeng.liu@daimler.com>
1 parent a05a04c commit 994dcd9

23 files changed

+1964
-1
lines changed

MANIFEST.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ include requirements.txt
33
include requirements-dev.txt
44
include LICENSE
55
include CHANGELOG.md
6-
include pytest.ini
6+
include pytest.ini
7+
include chaosaws/ec2_os/scripts/*

chaosaws/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,5 +239,7 @@ def load_exported_activities() -> List[DiscoveredActivities]:
239239
activities.extend(discover_actions("chaosaws.rds.actions"))
240240
activities.extend(discover_probes("chaosaws.rds.probes"))
241241
activities.extend(discover_actions("chaosaws.elasticache.actions"))
242+
activities.extend(discover_actions("chaosaws.ec2_os.actions"))
243+
activities.extend(discover_probes("chaosaws.ec2_os.probes"))
242244

243245
return activities

chaosaws/ec2_os/__init__.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import os
2+
3+
from logzero import logger
4+
from chaoslib.exceptions import FailedActivity
5+
6+
from .constants import OS_WINDOWS, OS_LINUX
7+
8+
9+
def construct_script_content(action: str = None,
10+
os_type: str = None,
11+
parameters: dict = None):
12+
"""
13+
As for now, no Windows action supported except burn CPU
14+
15+
:param action:
16+
:param os_type: { OS_LINUX | OS_WINDOWS }
17+
:param parameters:
18+
:return:
19+
"""
20+
21+
cmd_param = ""
22+
if os_type == OS_LINUX:
23+
file_suffix = ".sh"
24+
p_delimiter = ""
25+
cmdline_delimiter = " && "
26+
elif os_type == OS_WINDOWS:
27+
file_suffix = ".ps1"
28+
p_delimiter = "$"
29+
cmdline_delimiter = "\n"
30+
else:
31+
raise FailedActivity(
32+
"Cannot find corresponding script for {} on OS: {}".format(
33+
action, os_type))
34+
35+
if action == "run_cmd":
36+
cmdline_param = cmdline_delimiter.join(parameters['cmd'])
37+
# parameters.pop('cmd')
38+
del parameters['cmd']
39+
else:
40+
cmdline_param = ""
41+
42+
if parameters is not None:
43+
param_list = list()
44+
for k, v in parameters.items():
45+
param_list.append('='.join([p_delimiter + k, "'" + v + "'"]))
46+
cmd_param = '\n'.join(param_list)
47+
else:
48+
logger.info("No parameter parsed, return default script content")
49+
50+
script_name = action + file_suffix
51+
52+
with open(os.path.join(os.path.dirname(__file__),
53+
"scripts", script_name)) as file:
54+
script_content = file.read()
55+
# merge duration
56+
script_content = cmd_param + "\n" + cmdline_param + "\n" + script_content
57+
return script_content

0 commit comments

Comments
 (0)