Skip to content

Commit 7821b40

Browse files
authored
Merge pull request #1 from argodevops/feature/run-sim-example
Feature/run sim example
2 parents c7b5f2c + d76f2af commit 7821b40

File tree

9 files changed

+599
-20
lines changed

9 files changed

+599
-20
lines changed

.pre-commit-config.yaml

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
repos:
2-
- repo: https://github.com/psf/black
3-
rev: 22.8.0
4-
hooks:
2+
- repo: https://github.com/psf/black
3+
rev: 22.8.0
4+
hooks:
55
- id: black
6-
- repo: local
7-
hooks:
8-
- id: pylint
9-
name: pylint
10-
entry: pylint
11-
language: system
12-
types: [python]
13-
args:
14-
[
15-
"-rn", # Only display messages
16-
"--rcfile=.pylintrc", # Link to your config file
17-
]
6+
- repo: https://github.com/astral-sh/ruff-pre-commit
7+
# Ruff version.
8+
rev: v0.1.7
9+
hooks:
10+
# Run the linter.
11+
- id: ruff
12+
# Run the formatter.
13+
- id: ruff-format

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
The example project installs `pre-commit`, `black` and `pylint` to demonstrate how to apply formatting and linting to a `git` commit.
44

5+
Contains example Python scripts which can be adapted and may be useful.
6+
7+
* run_simulation.py - runs a simulation command based on an environment and type of simulation command, orginanlly written for running Gatling load simulations
8+
59
## Pre-requisites
610

711
Run ` $ pip install -r requirements.txt`
@@ -22,10 +26,10 @@ Run `black *.py`
2226

2327
https://pypi.org/project/black/
2428

25-
## pylint
29+
## ruff
2630

2731
Pylint is a static code analyser for Python.
2832

29-
Run `pylint *.py`
33+
Run `ruff *.py`
3034

31-
https://pypi.org/project/pylint/
35+
https://github.com/astral-sh/ruff

config/env.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
dc1:
2+
properties:
3+
domain: "https://example.com/dc1"
4+
admin_user: "auto.admin"
5+
admin_password: "$ADMIN_PASSWORD"
6+
pt1:
7+
properties:
8+
domain: "https://example.com/pt1"

config/sim.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
informer:
2+
command: java -version -Dtest.properties
3+
properties:
4+
personSearch: person.csv
5+
vehicleSearch: vehicle.csv
6+
locationSearch: location.csv
7+
relater:
8+
command: which java
9+
properties:
10+
objectSearch: object.csv

main.py renamed to example.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import requests
2-
import time
32

43
newHeaders = {"Content-type": "application/json", "Accept": "text/plain"}
54

output.log

Lines changed: 410 additions & 0 deletions
Large diffs are not rendered by default.

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
requests
22
pre-commit
33
black
4-
pylint
4+
ruff

run_simulation.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
"""
2+
Script Name: run_simulation.py
3+
Description: Runs a simulation command based on provided arguments and support config.
4+
5+
Usage: python script.py <env> <sim>
6+
7+
<env> - The environment argument.
8+
<sim> - The simulation argument.
9+
10+
The script relies on two config files which it consults to build the simulation command.
11+
* env.txt
12+
* sim.txt
13+
14+
"""
15+
import argparse
16+
import logging
17+
import os
18+
import subprocess
19+
import sys
20+
import yaml
21+
22+
23+
def print_usage(parser):
24+
parser.print_help()
25+
logging.error("Please provide the 'env' and 'sim' arguments.")
26+
27+
28+
def check_file_exists(file_path):
29+
"""check file actually exists"""
30+
if not os.path.isfile(file_path):
31+
logging.error(f"File not found: {file_path}")
32+
return False
33+
return True
34+
35+
36+
def read_env_properties(env_value):
37+
"""read and return the env properties"""
38+
logging.info(f"Environment: {env_value}")
39+
env_file = os.path.join("config", "env.yml")
40+
if not check_file_exists(env_file):
41+
return False
42+
43+
with open(env_file, "r") as file:
44+
env_data = yaml.safe_load(file)
45+
for env in env_data:
46+
if env == env_value:
47+
return env_data[env_value]
48+
logging.info(f"Unknown environment: {env_value}")
49+
50+
51+
def read_sim_properties(sim_value):
52+
"""read and return simulation properties"""
53+
logging.info(f"Simulaton: {sim_value}")
54+
sim_file = os.path.join("config", "sim.yml")
55+
if not check_file_exists(sim_file):
56+
return False
57+
58+
with open(sim_file, "r") as file:
59+
sim_data = yaml.safe_load(file)
60+
for sim in sim_data:
61+
if sim == sim_value:
62+
return sim_data[sim_value]
63+
logging.info(f"Unknown simulation: {sim_value}")
64+
65+
66+
def write_properties_file(
67+
env: str, sim: str, env_props: dict, sim_props: dict, file_path: str
68+
):
69+
"""write properties file"""
70+
with open(file_path, "w") as file:
71+
file.write(f"\n# TEST PROPERTIES. ENV {env}, SIM {sim}\n")
72+
for name, value in env_props["properties"].items():
73+
if "$" in value:
74+
logging.info(f"Substituting in environment variable for {value}")
75+
value = os.environ.get(value[1:])
76+
if value is None:
77+
logging.warning(f"Environment variable {value} is None")
78+
file.write(f"{name}={value}\n")
79+
for name, value in sim_props["properties"].items():
80+
file.write(f"{name}={value}\n")
81+
with open(file_path, "r") as file:
82+
contents = file.read()
83+
logging.info(contents)
84+
85+
86+
### MAIN ###
87+
88+
# Configure logging
89+
log_file = "output.log"
90+
logging.basicConfig(
91+
filename=log_file,
92+
level=logging.INFO,
93+
format="%(asctime)s - %(levelname)s - %(message)s",
94+
)
95+
console = logging.StreamHandler(sys.stdout)
96+
console.setLevel(logging.INFO)
97+
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
98+
console.setFormatter(formatter)
99+
logging.getLogger().addHandler(console)
100+
101+
# Create the argument parser
102+
parser = argparse.ArgumentParser(
103+
description="Runs a simulation command based on the provided arguments "
104+
+ "and support config"
105+
)
106+
107+
# Add the mandatory arguments
108+
parser.add_argument("env", type=str, help="Environment argument")
109+
parser.add_argument("sim", type=str, help="Simulation argument")
110+
111+
# Parse the command-line arguments
112+
args = parser.parse_args()
113+
114+
# Check if the arguments are empty
115+
if not args.env or not args.sim:
116+
print_usage(parser)
117+
exit(1)
118+
119+
# Check if the env.yml file exists and if the provided env argument exists within it
120+
env_props = read_env_properties(args.env)
121+
logging.info(f"Environment properties: {env_props}")
122+
123+
# Check if the sim.yml file exists and if the provided sim argument exists within it
124+
sim_props = read_sim_properties(args.sim)
125+
logging.info("Simulation properties: {sim_props}")
126+
127+
if env_props is None or sim_props is None:
128+
logging.error("Environment or Simulation is not defined")
129+
exit(2)
130+
131+
# Building test.properties file
132+
logging.info("Building test.properties file")
133+
file_path = "test.properties"
134+
write_properties_file(args.env, args.sim, env_props, sim_props, file_path)
135+
136+
try:
137+
# Execute command
138+
command = sim_props["command"]
139+
logging.info(f"Executing simulation command: {command}")
140+
output = subprocess.check_output(command, shell=True, universal_newlines=True)
141+
logging.info(f"Output: ${output}")
142+
except subprocess.CalledProcessError as e:
143+
logging.error(f"Command failed with return code {e.returncode}. Error output:")
144+
logging.error(e.output)

test.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
# TEST PROPERTIES. ENV dc1, SIM informer
3+
domain=https://example.com/dc1
4+
admin_user=auto.admin
5+
admin_password=None
6+
personSearch=person.csv
7+
vehicleSearch=vehicle.csv
8+
locationSearch=location.csv

0 commit comments

Comments
 (0)