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 | # integration-framework.sh - Reusable integration patterns
#!/bin/bash
set -euo pipefail
# IaC tool abstraction
class IaCTool {
private tool_name: string;
private tool_version: string;
constructor(tool_name: string) {
this.tool_name = tool_name;
this.tool_version = this.get_tool_version();
}
private get_tool_version(): string {
try {
const result = child_process.execSync(`${this.tool_name} --version`, {
encoding: 'utf8',
timeout: 10000
});
return result.trim();
} catch (error) {
throw new Error(`Failed to get ${this.tool_name} version: ${error}`);
}
}
async execute_command(subcommand: string, args: string[] = []): Promise<{ stdout: string; stderr: string }> {
const full_command = `${this.tool_name} ${subcommand} ${args.join(' ')}`;
return new Promise((resolve, reject) => {
const child = child_process.exec(full_command, { timeout: 300000 }, (error, stdout, stderr) => {
if (error) {
reject(new Error(`Command failed: ${stderr || error.message}`));
} else {
resolve({ stdout: stdout.trim(), stderr: stderr.trim() });
}
});
});
}
async validate_prerequisites(): Promise<boolean> {
// Check if tool is installed
try {
await this.execute_command('--version');
return true;
} catch (error) {
console.error(`Prerequisite check failed for ${this.tool_name}: ${error}`);
return false;
}
}
}
// Terraform integration
class TerraformIntegration extends IaCTool {
constructor() {
super('terraform');
}
async initialize_backend(config_path: string): Promise<void> {
await this.execute_command('init', ['-backend-config', config_path]);
}
async plan(variables: Record<string, string>): Promise<void> {
const var_args = Object.entries(variables).map(([key, value]) => `-var=${key}=${value}`);
await this.execute_command('plan', [...var_args, '-out=plan.out']);
}
async apply(auto_approve: boolean = false): Promise<void> {
const args = auto_approve ? ['-auto-approve'] : [];
await this.execute_command('apply', [...args, 'plan.out']);
}
}
// Ansible integration
class AnsibleIntegration extends IaCTool {
constructor() {
super('ansible-playbook');
}
async run_playbook(playbook_path: string, inventory: string, extra_vars: Record<string, string> = {}): Promise<void> {
const var_args = Object.entries(extra_vars).map(([key, value]) => `-e ${key}=${value}`);
const args = [
'-i', inventory,
...var_args,
playbook_path
];
await this.execute_command('', args);
}
}
// Kubernetes integration
class KubernetesIntegration extends IaCTool {
constructor() {
super('kubectl');
}
async apply_manifest(manifest_path: string, namespace?: string): Promise<void> {
const args = namespace ? ['-n', namespace, 'apply', '-f', manifest_path] : ['apply', '-f', manifest_path];
await this.execute_command('', args);
}
async wait_for_ready(resource_type: string, resource_name: string, namespace?: string): Promise<void> {
const args = namespace ?
['-n', namespace, 'wait', '--for=condition=ready', resource_type, resource_name, '--timeout=300s'] :
['wait', '--for=condition=ready', resource_type, resource_name, '--timeout=300s'];
await this.execute_command('', args);
}
}
|