Skip to content

Commit

Permalink
Add ec2_os actions, enable os level burn_cpu,network,kill process fun…
Browse files Browse the repository at this point in the history
…ction via aws ssm

Signed-off-by: Liu, Xiaopeng (133) <[email protected]>
  • Loading branch information
Liu, Xiaopeng (133) committed May 25, 2020
1 parent a05a04c commit 544f687
Show file tree
Hide file tree
Showing 23 changed files with 1,964 additions and 1 deletion.
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ include requirements.txt
include requirements-dev.txt
include LICENSE
include CHANGELOG.md
include pytest.ini
include pytest.ini
include chaosaws/ec2_os/scripts/*
2 changes: 2 additions & 0 deletions chaosaws/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,7 @@ def load_exported_activities() -> List[DiscoveredActivities]:
activities.extend(discover_actions("chaosaws.rds.actions"))
activities.extend(discover_probes("chaosaws.rds.probes"))
activities.extend(discover_actions("chaosaws.elasticache.actions"))
activities.extend(discover_actions("chaosaws.ec2_os.actions"))
activities.extend(discover_probes("chaosaws.ec2_os.probes"))

return activities
57 changes: 57 additions & 0 deletions chaosaws/ec2_os/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os

from logzero import logger
from chaoslib.exceptions import FailedActivity

from .constants import OS_WINDOWS, OS_LINUX


def construct_script_content(action: str = None,
os_type: str = None,
parameters: dict = None):
"""
As for now, no Windows action supported except burn CPU
:param action:
:param os_type: { OS_LINUX | OS_WINDOWS }
:param parameters:
:return:
"""

cmd_param = ""
if os_type == OS_LINUX:
file_suffix = ".sh"
p_delimiter = ""
cmdline_delimiter = " && "
elif os_type == OS_WINDOWS:
file_suffix = ".ps1"
p_delimiter = "$"
cmdline_delimiter = "\n"
else:
raise FailedActivity(
"Cannot find corresponding script for {} on OS: {}".format(
action, os_type))

if action == "run_cmd":
cmdline_param = cmdline_delimiter.join(parameters['cmd'])
# parameters.pop('cmd')
del parameters['cmd']
else:
cmdline_param = ""

if parameters is not None:
param_list = list()
for k, v in parameters.items():
param_list.append('='.join([p_delimiter + k, "'" + v + "'"]))
cmd_param = '\n'.join(param_list)
else:
logger.info("No parameter parsed, return default script content")

script_name = action + file_suffix

with open(os.path.join(os.path.dirname(__file__),
"scripts", script_name)) as file:
script_content = file.read()
# merge duration
script_content = cmd_param + "\n" + cmdline_param + "\n" + script_content
return script_content
Loading

0 comments on commit 544f687

Please sign in to comment.