-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish.py
More file actions
157 lines (127 loc) · 5.13 KB
/
publish.py
File metadata and controls
157 lines (127 loc) · 5.13 KB
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
import os
import requests
import json
from ruamel.yaml import YAML
from pathlib import Path
from io import StringIO
def yaml() -> YAML:
yml = YAML()
yml.indent(mapping=2, sequence=4, offset=2)
yml.allow_unicode = True
yml.default_flow_style = False
yml.preserve_quotes = True
return yml
def update_addon_file_values_schema(ADDON_FILE, VALUES_SCHEMA_JSON_FILE):
# Load the VALUES SCHEMA JSON file content
with open(VALUES_SCHEMA_JSON_FILE, 'r') as f:
json_schema = json.load(f)
properties = json_schema.get('properties', {})
# Load the ADDON manifest
with open(ADDON_FILE, 'r') as f:
manifest = safe_load(f.read())
# Update the valuesSchema field
if not manifest or not isinstance(manifest, dict) or not manifest.get('spec'):
print('❌ ADDON file "spec" field is missing or empty!')
exit(1)
manifest['spec']['valuesSchema'] = properties
# Write back to the ADDON file
with open(ADDON_FILE, 'w') as f:
yml = yaml()
yml.dump(manifest, f)
print(f"Updated valuesSchema in {ADDON_FILE}")
if VERBOSE:
print("🕵️ ADDON FILE CONTENT WITH VALUE SCHEMA:", manifest)
def safe_load(content: str) -> dict:
yml = yaml()
return yml.load(StringIO(content))
def get_environment_urls(CLIENT_REALM, ORGANIZATION_ID):
return {
"auth": f"https://idm.stackspot.com/{CLIENT_REALM}/oidc/oauth/token",
"publish": f"https://cloud-platform-horizon.stackspot.com/v1/organizations/{ORGANIZATION_ID}/addons"
}
def authentication(CLIENT_REALM, CLIENT_ID, CLIENT_KEY, ORGANIZATION_ID):
urls = get_environment_urls(CLIENT_REALM, ORGANIZATION_ID)
iam_url = urls["auth"]
iam_headers = {'Content-Type': 'application/x-www-form-urlencoded'}
iam_data = {
"client_id": CLIENT_ID,
"grant_type": "client_credentials",
"client_secret": CLIENT_KEY
}
print(f"⚙️ Authenticating in {CLIENT_REALM}...")
response = requests.post(url=iam_url, headers=iam_headers, data=iam_data)
if response.status_code == 200:
print("✅ Authentication successful")
return response.json().get("access_token")
print("❌ Authentication error")
print(f"Status: {response.status_code}, Error: {response.text}")
exit(1)
def publish(addon_name, addon_version, publish_headers, yaml_file_path, CLIENT_REALM, VERBOSE, ORGANIZATION_ID):
urls = get_environment_urls(CLIENT_REALM, ORGANIZATION_ID)
publish_url = urls["publish"]
print(f'⚙️ Publishing "{addon_name}", version "{addon_version}" to {CLIENT_REALM} organization')
if VERBOSE:
print(f'⚙️ PUBLICATION URL: {publish_url}')
with open(yaml_file_path, 'r') as file:
yaml_content = file.read()
headers = {
"Authorization": publish_headers["Authorization"],
"Content-Type": "application/yaml",
"Accept": "application/json"
}
if VERBOSE:
print(f'⚙️ YAML: {yaml_content}')
response = requests.post(
url=publish_url,
headers=headers,
data=yaml_content
)
if response.status_code == 200:
print(f'✅ Addon "{addon_name}" published successfully (ID: {response.json().get('metadata').get('id')})')
if VERBOSE:
print("🕵️ ADDON RESPONSE DATA:", response.json())
else:
print(f'❌ Addon "{addon_name}" publication failed!')
print(f"Status: {response.status_code}, Error: {response.reason}")
if VERBOSE:
print("🕵️ ERROR RESPONSE DATA:", response.text)
exit(1)
# Environment variables
CLIENT_ID = os.getenv("CLIENT_ID")
CLIENT_KEY = os.getenv("CLIENT_KEY")
CLIENT_REALM = os.getenv("CLIENT_REALM")
VERBOSE = os.getenv("VERBOSE")
ADDON_FILE = os.getenv("ADDON_FILE")
VALUES_SCHEMA_JSON_FILE = os.getenv("VALUES_SCHEMA_JSON_FILE")
if VALUES_SCHEMA_JSON_FILE is not None:
update_addon_file_values_schema(ADDON_FILE, VALUES_SCHEMA_JSON_FILE)
if not all([CLIENT_ID, CLIENT_KEY, CLIENT_REALM, ADDON_FILE]):
print("❌ Missing required environment variables!")
exit(1)
# Load the YAML file to extract metadata for logging purposes
with open(Path(ADDON_FILE), 'r') as file:
yaml_data = safe_load(file.read())
# Extract data from YAML
metadata = yaml_data.get('metadata', {})
spec = yaml_data.get('spec', {})
addon_name = metadata.get('name')
addon_version = metadata.get('version')
helm = spec.get('helm')
organization_id = spec.get('organizationId')
required_fields = {
"organization_id": organization_id,
"addon_name": addon_name,
"addon_version": addon_version,
"helm": helm,
}
missing_fields = [field for field, value in required_fields.items() if not value]
if missing_fields:
print("❌ Missing addon required fields in the YAML file:")
for field in missing_fields:
print(f"- {field}")
exit(1)
# Authenticate
access_token = authentication(CLIENT_REALM, CLIENT_ID, CLIENT_KEY, organization_id)
publish_headers = {"Authorization": f"Bearer {access_token}"}
# Start publishing addon by sending the YAML file directly
publish(addon_name, addon_version, publish_headers, ADDON_FILE, CLIENT_REALM, VERBOSE, organization_id)