-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-wiki.py
149 lines (124 loc) · 5.14 KB
/
gen-wiki.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
import os
import yaml
import json
import boto3
import argparse
import subprocess
TEMP_DIR = 'temp_wiki'
def load_config(config_file):
with open(config_file, 'r') as f:
config = yaml.safe_load(f)
return config
def read_json_file(file_path):
try:
with open(file_path, 'r') as file:
data = json.load(file)
return data
except Exception as e:
print(f"Error reading file: {e}")
return None
def extract_attributes(data, resource_type, attributes):
extracted_data = []
for resource in data.get('resources', []):
if resource.get('type') == resource_type:
for instance in resource.get('instances', []):
extracted_item = {}
if not attributes:
extracted_item['id'] = instance.get('attributes', {}).get('id')
for attr in attributes:
if attr:
extracted_item[attr] = instance.get('attributes', {}).get(attr)
extracted_data.append(extracted_item)
return extracted_data
def generate_markdown_table(header, data):
if not data:
return ""
headers = list(data[0].keys())
table = f"### {header}\n\n"
table += "| " + " | ".join(headers) + " |\n"
table += "| " + " | ".join(['---'] * len(headers)) + " |\n"
for item in data:
row = "| " + " | ".join(f"`{str(item.get(h, ''))}`" for h in headers) + " |\n"
table += row
table += "\n"
return table
def extract_repo_name(file_path):
with open(file_path, 'r') as file:
for line in file:
if "customer_name" in line:
customer_name = line.split('=', 1)[1].strip().strip('"')
return customer_name
def download_bucket(directory):
s3 = boto3.client('s3')
print(f"directory = ", directory)
bucket_name = "terraform-remote-state-" + extract_repo_name("terragrunt.hcl")
try:
s3.download_file(bucket_name, directory, "tmp_file.json")
return "tmp_file.json"
except Exception as e:
print(f"Error: {e}")
return None
def process_directory(directory, config):
json_content = download_bucket(directory)
if not json_content:
return None
data = read_json_file(json_content)
markdown_content = ""
for resource_type, settings in config.get('resources', {}).items():
header = settings.get('header', resource_type)
attributes = settings.get('attributes', [])
extracted_data = extract_attributes(data, resource_type, attributes)
table = generate_markdown_table(header, extracted_data)
if table:
markdown_content += table
if markdown_content:
directory = directory.split('/', 1)[1]
markdown_content = f"## {directory.capitalize()}\n\n" + markdown_content
return markdown_content
def process_environment(environment, config, output_dir):
markdown_content = ""
for root, dirs, files in os.walk(environment):
if '.terragrunt-cache' in root:
continue
content = process_directory(root, config)
if content:
markdown_content += content
for file in files:
if file.endswith('.md'):
with open(os.path.join(root, file), 'r') as f:
content = f.read()
if content.strip():
markdown_content += f"### Docs\n\n"
markdown_content += content
if markdown_content.strip() != f"# {environment.capitalize()} Environment\n\n":
output_file = os.path.join(output_dir, f"{environment.capitalize()}-environment.md")
os.makedirs(output_dir, exist_ok=True)
with open(output_file, 'w') as f:
f.write(markdown_content)
print(f"Markdown file generated for {environment}: {output_file}")
def list_md_files(directory):
"""List all .md files in a given directory."""
md_files = []
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.md'):
md_files.append(os.path.join(root, file))
return md_files
def copy_wiki(md_files):
for md_file in md_files:
subprocess.run(['cp', "terraform/live/" + md_file, 'temp_wiki'], check=True)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Extract secrets using terragrunt state pull based on a YAML configuration.')
parser.add_argument('config_file', help='Path to the YAML configuration file')
parser.add_argument('output_dir', help='Path to the output directory')
parser.add_argument('--repo_name', default=os.getenv("REPO_NAME"), help='Name of the repo you want to create a doc for')
parser.add_argument('--shared_dir', default='shared', help='Directory for shared resources')
args = parser.parse_args()
config = load_config(args.config_file)
os.chdir("terraform/live")
directories = [d for d in os.listdir(os.getcwd()) if os.path.isdir(os.path.join(os.getcwd(), d))]
for directory in directories:
process_environment(directory, config, args.output_dir)
md_files = list_md_files(args.output_dir)
os.chdir("../..")
copy_wiki(md_files)