-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalues.rb
More file actions
executable file
·267 lines (230 loc) · 8.71 KB
/
Copy pathvalues.rb
File metadata and controls
executable file
·267 lines (230 loc) · 8.71 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
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
#!/usr/bin/env ruby
# values.rb - Generates helm/values.yaml from lib/ manifests
# Reads deployment.yaml for resource config; falls back to per-service values.yaml for image/port.
# For ERP services without explicit image, generates erpuno/{service}:{version}.
require 'yaml'
require 'fileutils'
require 'optparse'
options = { force: false, dry_run: false }
OptionParser.new do |opts|
opts.on("-f", "--force", "Overwrite without confirmation") { options[:force] = true }
opts.on("-d", "--dry-run", "Print to stdout, do not write") { options[:dry_run] = true }
opts.on("-v", "--version VERSION", String, "Set image version tag") { |v| options[:version] = v }
end.parse!
REPO_ROOT = Dir.pwd
LIB_DIR = File.join(REPO_ROOT, 'lib')
HELM_VALUES_PATH = File.join(REPO_ROOT, 'helm', 'values.yaml')
VERSION = options[:version] || ENV['VERSION'] || '2024.6.15'
GLOBAL = {
'domain' => 'erp.uno',
'environment' => 'production',
'registry' => 'docker.io',
'imagePullPolicy' => 'IfNotPresent',
'version' => VERSION
}.freeze
# Known public images that should NOT get the erpuno/ prefix
PUBLIC_IMAGES = {
'prometheus' => "prom/prometheus:v2.53.0",
'grafana' => "grafana/grafana:11.1.0",
'loki' => "grafana/loki:3.1.0",
'otel-collector' => "otel/opentelemetry-collector-contrib:0.102.0",
'docker-registry' => "registry:2.8.3",
'ns-dns' => "erpuno/ns:#{VERSION}",
'ca-pki' => "erpuno/ca:#{VERSION}",
'vpn-wireguard' => "erpuno/vpn:#{VERSION}",
'ias-auth' => "erpuno/ias:#{VERSION}",
'ldap-directory' => "erpuno/ldap:#{VERSION}",
'abac-clearance' => "erpuno/clearance:#{VERSION}",
'chat-messenger' => "erpuno/chat:#{VERSION}",
'mail-delivery' => "erpuno/mail:#{VERSION}",
'rest-bpe' => "erpuno/rest:#{VERSION}",
'bpe-engine' => "erpuno/bpe:#{VERSION}",
'kvs-database' => "erpuno/kvs:#{VERSION}",
'n2o-server' => "erpuno/n2o:#{VERSION}",
'nitro-portal' => "erpuno/nitro:#{VERSION}",
'mach-ivr' => "erpuno/mach:#{VERSION}",
'faiss-search' => "erpuno/faiss:#{VERSION}",
'ai-generation' => "erpuno/ai:#{VERSION}",
'acc-accounting' => "erpuno/acc:#{VERSION}",
'cart-registers' => "erpuno/cart:#{VERSION}",
'crm-documents' => "erpuno/crm:#{VERSION}",
'hl7-health' => "erpuno/health:#{VERSION}",
'itsm-incidents' => "erpuno/itsm:#{VERSION}",
'lms-education' => "erpuno/edu:#{VERSION}",
'olap-analytics' => "erpuno/olap:#{VERSION}",
'pm-projects' => "erpuno/pm:#{VERSION}",
'wms-warehouse' => "erpuno/warehouse:#{VERSION}",
}.freeze
# Parse image string → { registry_prefix, image_name, tag }
def parse_image(full_image)
# e.g. "prom/prometheus:v2.53.0" → image="prom/prometheus", tag="v2.53.0"
# e.g. "registry:2.8.3" → image="registry", tag="2.8.3"
# e.g. "erpuno/health:2024.6.15" → image="erpuno/health", tag="2024.6.15"
parts = full_image.split(':')
tag = parts.length > 1 ? parts.last : 'latest'
image = parts[0..-2].join(':').empty? ? parts[0] : parts[0..-2].join(':')
{ 'image' => image, 'tag' => tag }
end
def default_resources
{
'requests' => { 'cpu' => '100m', 'memory' => '256Mi' },
'limits' => { 'cpu' => '500m', 'memory' => '512Mi' }
}
end
def extract_service_config(service_dir, service_name)
config = {
'enabled' => true,
'replicas' => 1,
'resources' => default_resources,
}
# --- Step 1: Try per-service values.yaml for image/port overrides ---
svc_values_path = File.join(service_dir, 'values.yaml')
if File.exist?(svc_values_path)
begin
svc_vals = YAML.safe_load(File.read(svc_values_path)) || {}
config['image'] = svc_vals['image'] if svc_vals['image']
config['port'] = svc_vals['port'] if svc_vals['port']
rescue => e
warn "⚠️ Parse error #{svc_values_path}: #{e.message}"
end
end
# --- Step 2: Parse deployment.yaml for authoritative spec ---
deployment_path = File.join(service_dir, 'deployment.yaml')
if File.exist?(deployment_path)
begin
dep = YAML.safe_load(File.read(deployment_path)) || {}
spec = dep['spec'] || {}
template = spec.dig('template', 'spec') || {}
container = template.dig('containers', 0) || {}
config['replicas'] = spec['replicas'] if spec['replicas']
# Full image field wins over values.yaml
if container['image'] && !container['image'].empty?
parsed = parse_image(container['image'])
config['image'] = parsed['image']
config['tag'] = parsed['tag']
end
# Ports
if (ports = container['ports'])
main = ports.first
if main && main['containerPort']
config['port'] = main['containerPort']
config['protocol'] = main['protocol'] || 'TCP'
end
end
# Resources
if (resources = container['resources'])
config['resources'] = {
'requests' => resources['requests'] || default_resources['requests'],
'limits' => resources['limits'] || resources['requests'] || default_resources['limits']
}
end
# Persistence (volumeMounts pointing to PVC, or volumeClaimTemplates)
has_pvc = false
pvc_mount_path = nil
if spec['volumeClaimTemplates']
has_pvc = true
pvc_mount_path = '/data'
elsif template['volumes'] && container['volumeMounts']
pvc_vol_names = template['volumes']
.select { |v| v['persistentVolumeClaim'] }
.map { |v| v['name'] }
matching_mount = container['volumeMounts'].find { |m| pvc_vol_names.include?(m['name']) }
if matching_mount
has_pvc = true
pvc_mount_path = matching_mount['mountPath']
end
end
if has_pvc
config['persistence'] = {
'enabled' => true,
'size' => '10Gi',
'storageClassName' => 'standard',
'mountPath' => pvc_mount_path || '/data'
}
end
rescue => e
warn "⚠️ Parse error #{deployment_path}: #{e.message}"
end
end
# --- Step 3: HPA ---
if File.exist?(File.join(service_dir, 'hpa.yaml'))
config['hpa'] = {
'enabled' => true,
'minReplicas' => 2,
'maxReplicas' => 6,
'targetCPUUtilization' => 75
}
end
# --- Step 4: Fallback image from PUBLIC_IMAGES map ---
unless config['image']
if PUBLIC_IMAGES.key?(service_name)
parsed = parse_image(PUBLIC_IMAGES[service_name])
config['image'] = parsed['image']
config['tag'] = parsed['tag']
else
# Generic ERP service fallback
config['image'] = "erpuno/#{service_name}"
config['tag'] = VERSION
end
end
# Fallback port
config['port'] ||= 8080
config
end
def generate_values
values = {
'global' => GLOBAL.dup,
'namespaces' => {},
'services' => {}
}
Dir.glob(File.join(LIB_DIR, '*/')).sort.each do |ns_dir|
ns_name = File.basename(ns_dir.chomp('/'))
next unless ns_name.start_with?('erp-')
values['namespaces'][ns_name] = {
'enabled' => true,
'tier' => case ns_name
when /infra/ then 'infrastructure'
when /telemetry/ then 'observability'
when /security/ then 'security'
when /ai/ then 'ai'
else 'application'
end
}
values['services'][ns_name] = {}
Dir.glob(File.join(ns_dir, '*/')).sort.each do |svc_dir|
service_name = File.basename(svc_dir.chomp('/'))
next if service_name.empty?
config = extract_service_config(svc_dir, service_name)
values['services'][ns_name][service_name] = config
end
end
values['rbac'] = { 'create' => true }
values['networkPolicy'] = { 'enabled' => true }
values['ingress'] = {
'enabled' => true,
'className' => 'nginx',
'tls' => { 'enabled' => false },
'hosts' => [
{
'host' => GLOBAL['domain'],
'paths' => [{ 'path' => '/', 'service' => 'nitro-portal', 'namespace' => 'erp-services', 'port' => 8510 }]
}
]
}
values
end
puts "🔨 Generating helm/values.yaml (version: #{VERSION})"
values = generate_values
if options[:dry_run]
puts values.to_yaml(line_width: -1)
exit 0
end
if File.exist?(HELM_VALUES_PATH) && !options[:force]
print "Overwrite #{HELM_VALUES_PATH}? (y/N): "
exit 1 unless STDIN.gets.strip.downcase == 'y'
end
FileUtils.mkdir_p(File.dirname(HELM_VALUES_PATH))
File.write(HELM_VALUES_PATH, values.to_yaml(line_width: -1))
puts "✅ Generated successfully!"
puts " Namespaces : #{values['namespaces'].size}"
puts " Services : #{values['services'].values.sum(&:size)}"