-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoty.py
492 lines (413 loc) · 20.4 KB
/
oty.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#!/usr/bin/env python3
import os
import sys
import yaml
import click
import logging
import subprocess
from typing import Dict, List, Any, Optional
from dataclasses import dataclass
import colorama
from colorama import Fore, Style as ColoramaStyle
from datetime import datetime
import json
import hashlib
from pathlib import Path
import select
@dataclass
class OTYConfig:
# config management
base_dir: str = os.path.expanduser('~/.oty')
template_dir: str = os.path.join(base_dir, 'templates')
workflow_dir: str = os.path.join(base_dir, 'workflow')
log_dir: str = os.path.join(base_dir, 'logs')
report_dir: str = os.path.join(base_dir, 'reports')
cache_dir: str = os.path.join(base_dir, 'cache')
state_file: str = os.path.join(base_dir, 'resumeoty.cfg')
states_dir: str = os.path.join(base_dir, 'states')
def __post_init__(self):
for directory in [
self.base_dir,
self.template_dir,
self.workflow_dir,
self.log_dir,
self.report_dir,
self.cache_dir,
self.states_dir
]:
os.makedirs(directory, exist_ok=True)
# template schema
WORKFLOW_SCHEMA = {
"type": "object",
"required": ["name", "steps"],
"properties": {
"name": {"type": "string"},
"description": {"type": "string"},
"variables": {
"type": "object",
"additionalProperties": {
"oneOf": [
{"type": "string"},
{"type": "number"},
{"type": "boolean"}
]
}
},
"steps": {
"type": "array",
"items": {
"type": "object",
"required": ["name", "command"],
"properties": {
"name": {"type": "string"},
"command": {"type": "string"},
"continue_on_error": {"type": "boolean"},
"timeout": {"type": "number"}
}
}
}
}
}
class Colors:
# colors
BLUE = Fore.BLUE
GREEN = Fore.GREEN
RED = Fore.RED
YELLOW = Fore.YELLOW
MAGENTA = Fore.MAGENTA
CYAN = Fore.CYAN
WHITE = Fore.WHITE
GREY = Fore.LIGHTBLACK_EX
DIM = ColoramaStyle.DIM
BRIGHT = ColoramaStyle.BRIGHT
# prefixes
INFO = f"[{Fore.BLUE}{ColoramaStyle.BRIGHT}INFO{ColoramaStyle.RESET_ALL}]"
SUCCESS = f"[{Fore.GREEN}{ColoramaStyle.BRIGHT}DONE{ColoramaStyle.RESET_ALL}]"
WARNING = f"[{Fore.YELLOW}{ColoramaStyle.BRIGHT}WARN{ColoramaStyle.RESET_ALL}]"
ERROR = f"[{Fore.RED}{ColoramaStyle.BRIGHT}FAIL{ColoramaStyle.RESET_ALL}]"
COMMAND = f"[{Fore.YELLOW}CMND{ColoramaStyle.RESET_ALL}]"
BRAND = f"{Fore.CYAN}[OTY]{ColoramaStyle.RESET_ALL}"
STEP = f"[{Fore.GREEN}STEP{ColoramaStyle.RESET_ALL}]"
class WorkflowExecutionEngine:
# workflow and error handling and logging
def __init__(self, config: OTYConfig):
colorama.init(autoreset=True)
self.config = config
self.logger = self._setup_logging()
self._print_banner()
self.current_state = {}
def _setup_logging(self):
try:
log_file_path = os.path.join(self.config.log_dir, 'oty.log')
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(log_file_path, mode='a'),
logging.StreamHandler(sys.stdout)
]
)
return logging.getLogger('OTYWorkflowEngine')
except Exception as e:
print(f"{Colors.ERROR} Error setting up logging: {e}")
logging.basicConfig(level=logging.INFO)
return logging.getLogger('OTYWorkflowEngine')
def _print_banner(self):
banner = """
┌─┐┌┬┐┬ ┬
│ │ │ └┬┘ {version}
└─┘ ┴ ┴ {author}
""".format(
version=f"{Colors.YELLOW}v1.0.0{Colors.RED}",
author=f"{Colors.CYAN}{Colors.BRIGHT}@1hehaq{Colors.RED}"
)
print(f"{Colors.RED}{Colors.BRIGHT}{banner}{ColoramaStyle.RESET_ALL}")
print(f"{Colors.BRAND}{Colors.INFO} Starting at: {Colors.WHITE}{Colors.DIM}{datetime.now().strftime(f'%Y-%m-%d %H:%M:%S')}{ColoramaStyle.RESET_ALL}")
print(f"{Colors.BRAND}{Colors.INFO} Log file: {Colors.WHITE}{Colors.DIM}{os.path.join(self.config.log_dir, 'oty.log')}{ColoramaStyle.RESET_ALL}")
def print_info(self, message: str):
print(f"{Colors.BRAND}{message} {ColoramaStyle.RESET_ALL}")
def print_success(self, message: str):
print(f"{Colors.BRAND}{message} {ColoramaStyle.RESET_ALL}")
def print_warning(self, message: str):
print(f"{Colors.BRAND}{message} {ColoramaStyle.RESET_ALL}")
def print_error(self, message: str):
print(f"{Colors.BRAND}{message} {ColoramaStyle.RESET_ALL}")
def print_debug(self, message: str):
print(f"{Colors.BRAND}{message} {ColoramaStyle.RESET_ALL}")
def validate_workflow_template(self, template_path: str) -> bool:
# validation
try:
if not os.path.exists(template_path):
self.print_error(f"Template file not found at {template_path}")
return False
with open(template_path, 'r') as f:
try:
workflow_data = yaml.safe_load(f)
except yaml.YAMLError as yaml_err:
self.print_error(f"{Colors.ERROR} YAML Parsing Error: {yaml_err}")
return False
try:
import jsonschema
jsonschema.validate(instance=workflow_data, schema=WORKFLOW_SCHEMA)
self.print_success(f"{Colors.SUCCESS} Template validation successful")
return True
except jsonschema.ValidationError as val_err:
self.print_error(f"{Colors.ERROR} Template Validation Error: {val_err}")
return False
self.logger.info(f"Template {template_path} validated successfully")
return True
except Exception as e:
self.print_error(f"{Colors.ERROR} Unexpected error during template validation: {e}")
self.logger.error(f"Template validation failed: {e}")
return False
def _generate_state_id(self, workflow_name: str, target: str) -> str:
unique_string = f"{workflow_name}_{target}"
return hashlib.md5(unique_string.encode()).hexdigest()[:12]
def _get_state_file_path(self, workflow_name: str, target: str) -> Path:
state_id = self._generate_state_id(workflow_name, target)
return Path(self.config.states_dir) / f"state_{state_id}.json"
def list_saved_states(self) -> List[Dict[str, Any]]:
states = []
for state_file in Path(self.config.states_dir).glob("state_*.json"):
try:
with open(state_file, 'r') as f:
state = json.load(f)
state['state_file'] = state_file.name
states.append(state)
except Exception as e:
self.print_warning(f"{Colors.WARNING} Failed to read state file {state_file}: {e}")
return states
def save_state(self, workflow_name: str, target: str, completed_steps: List[int], variables: Dict):
state = {
'workflow_name': workflow_name,
'template_path': self.current_template_path,
'target': target,
'completed_steps': completed_steps,
'variables': variables,
'timestamp': datetime.now().isoformat()
}
state_file = self._get_state_file_path(workflow_name, target)
try:
with open(state_file, 'w') as f:
json.dump(state, f, indent=4)
self.print_info(f"{Colors.INFO} Execution state saved to {state_file}")
except Exception as e:
self.print_error(f"{Colors.ERROR} Failed to save state: {e}")
def load_state(self, workflow_name: str, target: str) -> Optional[Dict]:
state_file = self._get_state_file_path(workflow_name, target)
try:
if state_file.exists():
with open(state_file, 'r') as f:
return json.load(f)
except Exception as e:
self.print_error(f"{Colors.ERROR} Failed to load state: {e}")
return None
def clear_state(self, workflow_name: str, target: str):
state_file = self._get_state_file_path(workflow_name, target)
try:
if state_file.exists():
state_file.unlink()
self.print_info(f"{Colors.INFO} Cleared state for workflow '{workflow_name}' and target '{target}'")
except Exception as e:
self.print_error(f"{Colors.ERROR} Failed to clear state: {e}")
def _validate_variables_and_commands(self, workflow: Dict, variables: Dict) -> tuple[bool, List[str], List[str], List[str]]:
undefined_vars = set()
invalid_paths = []
missing_commands = []
for step in workflow.get('steps', []):
command = step.get('command', '')
import re
vars_in_command = re.findall(r'{{(\w+)}}', command)
for var in vars_in_command:
if var not in variables:
undefined_vars.add(var)
if undefined_vars:
continue
test_command = command
for var, value in variables.items():
test_command = test_command.replace(f'{{{{{var}}}}}', str(value))
paths = re.findall(r'(?:^|\s)(/[^\s]+)', test_command)
for path in paths:
if not os.path.exists(path):
invalid_paths.append(path)
cmd_executable = test_command.split()[0]
if not cmd_executable.startswith('/'):
from shutil import which
if which(cmd_executable) is None:
missing_commands.append(cmd_executable)
return bool(undefined_vars or invalid_paths or missing_commands), list(undefined_vars), invalid_paths, missing_commands
def execute_workflow(self, template_path: str, target: str, dry_run: bool = False, resume: bool = False):
try:
self.current_template_path = template_path
completed_steps = []
with open(template_path, 'r') as f:
workflow = yaml.safe_load(f)
if resume:
state = self.load_state(workflow['name'], target)
if state and state['template_path'] == template_path:
completed_steps = state['completed_steps']
variables = state['variables']
self.print_info(f"{Colors.INFO} Resuming workflow from step {len(completed_steps) + 1}")
else:
self.print_warning(f"{Colors.WARNING} No matching state found, starting from beginning")
resume = False
self.print_info(f"{Colors.INFO} Loaded workflow: {workflow['name']}")
self.print_info(f"{Colors.INFO} Target: {target}\n")
variables = workflow.get('variables', {})
variables['TARGET'] = target
has_issues, undefined_vars, invalid_paths, missing_commands = self._validate_variables_and_commands(workflow, variables)
if has_issues:
self.print_warning(f"{Colors.WARNING} pre execution validation found issues:")
if undefined_vars:
self.print_warning(f"{Colors.WARNING} Undefined variables detected:")
for var in undefined_vars:
value = click.prompt(f"Enter value for {var}", type=str)
variables[var] = value
if invalid_paths:
self.print_error(f"{Colors.ERROR} Invalid paths detected:")
for path in invalid_paths:
self.print_error(f" - {path}")
if missing_commands:
self.print_error(f"{Colors.ERROR} missing commands detected:")
for cmd in missing_commands:
self.print_error(f" - {cmd}")
if invalid_paths or missing_commands:
if not click.confirm(f" {Colors.WARNING} continue such issues?", default=False):
self.print_error(f"{Colors.ERROR} execution aborted due to some issues")
return
has_issues, undefined_vars, invalid_paths, missing_commands = self._validate_variables_and_commands(workflow, variables)
if undefined_vars:
self.print_error(f"{Colors.ERROR} still have undefined variables after prompting. aborting.")
return
self.print_success(f"{Colors.SUCCESS} pre execution validation completed")
if dry_run:
self.print_info(f"{Colors.INFO} Dry run mode - Commands to be executed:")
for step in workflow.get('steps', []):
command = step.get('command', '')
for var, value in variables.items():
command = command.replace(f'{{{{{var}}}}}', str(value))
self.print_debug(f"{Colors.COMMAND} {command}")
return
total_steps = len(workflow.get('steps', []))
for idx, step in enumerate(workflow.get('steps', []), 1):
if resume and idx in completed_steps:
self.print_info(f"{Colors.INFO} Skipping completed step {idx}")
continue
name = step.get('name', 'Unnamed Step')
command = step.get('command', '')
self.print_info(f"{Colors.STEP} {Colors.MAGENTA}{ColoramaStyle.BRIGHT}[{idx}/{total_steps}]{ColoramaStyle.RESET_ALL} {name}")
for var, value in variables.items():
command = command.replace(f'{{{{{var}}}}}', str(value))
self.print_debug(f"{Colors.COMMAND} {Colors.WHITE}{Colors.DIM}{command}{ColoramaStyle.RESET_ALL}")
if not any(var in command for var in ['{{', '}}']):
try:
if not dry_run:
process = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1,
universal_newlines=True
)
while process.poll() is None:
try:
rlist, _, _ = select.select([sys.stdin, process.stdout], [], [], 0.1)
if process.stdout in rlist:
output = process.stdout.readline()
if output:
print(f" {output.strip()}")
if sys.stdin in rlist:
if not sys.stdin.readline():
self.print_warning(f"{Colors.WARNING}{ColoramaStyle.BRIGHT} Step skipped by user (Ctrl+D){ColoramaStyle.RESET_ALL}")
process.terminate()
try:
process.wait(timeout=5)
except subprocess.TimeoutExpired:
process.kill()
break
except (IOError, select.error):
continue
_, stderr = process.communicate()
if process.returncode == 0 or process.returncode == -15:
if process.returncode == -15:
self.print_warning(f"{Colors.WARNING} {name} skipped")
else:
self.print_success(f"{Colors.SUCCESS} {name} completed successfully")
else:
self.print_error(f"{Colors.ERROR} {name} failed: {stderr}")
if not step.get('continue_on_error', False):
raise Exception(f"Step '{name}' failed and continue_on_error is False")
except subprocess.TimeoutExpired:
self.print_warning(f"{Colors.WARNING} {name} timed out")
except Exception as e:
self.print_error(f"{Colors.ERROR} {name} execution error: {e}")
if not step.get('continue_on_error', False):
raise
else:
self.print_error(f"{Colors.ERROR} some variables were not replaced in command: {command}")
raise Exception("variable substitution incomplete")
completed_steps.append(idx)
self.save_state(workflow['name'], target, completed_steps, variables)
except KeyboardInterrupt:
print()
self.print_warning(f"{Colors.WARNING} execution paused. use --resume flag to continue later.")
self.save_state(workflow['name'], target, completed_steps, variables)
sys.exit(0)
except Exception as e:
self.print_error(f"{Colors.ERROR} Workflow Execution Error: {e}")
self.logger.error(f"Workflow execution failed: {e}")
# click cli ui
@click.group()
@click.version_option("1.0.0")
def cli():
pass
@cli.command()
@click.argument('template', type=click.Path(exists=True))
@click.argument('target')
@click.option('--dry-run', is_flag=True, help='Simulate workflow without execution')
@click.option('--verbose', is_flag=True, help='Enable verbose logging')
@click.option('--resume', is_flag=True, help='Resume previous execution if available')
def run(template, target, dry_run, verbose, resume):
config = OTYConfig()
engine = WorkflowExecutionEngine(config)
if not engine.validate_workflow_template(template):
engine.print_error(f"{Colors.ERROR} Template validation failed!")
sys.exit(1)
engine.execute_workflow(template, target, dry_run, resume)
@cli.command()
@click.argument('template', type=click.Path(exists=True))
def validate(template):
config = OTYConfig()
engine = WorkflowExecutionEngine(config)
if engine.validate_workflow_template(template):
engine.print_success(f"{Colors.SUCCESS} Template is valid!")
else:
engine.print_error(f"{Colors.ERROR} Template validation failed!")
@cli.command()
def list_states():
# list saved states
config = OTYConfig()
engine = WorkflowExecutionEngine(config)
states = engine.list_saved_states()
if not states:
engine.print_info(f"{Colors.INFO} No saved states found")
return
print("\nSaved Workflow States:")
for state in states:
print(f"\n{Colors.CYAN}Workflow:{Colors.WHITE} {state['workflow_name']}")
print(f"{Colors.CYAN}Target:{Colors.WHITE} {state['target']}")
print(f"{Colors.CYAN}Progress:{Colors.WHITE} {len(state['completed_steps'])} steps completed")
print(f"{Colors.CYAN}Last Updated:{Colors.WHITE} {state['timestamp']}")
print(f"{Colors.CYAN}State File:{Colors.WHITE} {state['state_file']}")
@cli.command()
@click.argument('workflow_name')
@click.argument('target')
def clear_state(workflow_name, target):
config = OTYConfig()
engine = WorkflowExecutionEngine(config)
engine.clear_state(workflow_name, target)
# main
if __name__ == "__main__":
cli()