boring_services 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/boring_services/service_locator.rb +34 -31
- data/lib/boring_services/version.rb +1 -1
- data/lib/boring_services.rb +29 -14
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1cf51b8c8239ca7c000dc2fabe043321c3a490070e762741eaa44307fa521047
|
|
4
|
+
data.tar.gz: cfa55da92f539c5d60e444f1f7f0d0a95af80a96194112c6754303fda8b7cf43
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 668cc2b9f6e65930f5d7e6916dbe33ca42522e0e4139aef025c4918f328c7d73345bd449cdea3bc5bf0368fdd512268a959f92d2a87f2012d5c46d4d7ceaf782
|
|
7
|
+
data.tar.gz: d5b0158106e76199da437fa08fb6375a103614e68b884c451730b417c0879109ad0ff278ba0b27d635ef45843e7ff9006f6092c1d03740fa949d056f115e0900
|
|
@@ -11,40 +11,31 @@ module BoringServices
|
|
|
11
11
|
# Get all hosts for a service
|
|
12
12
|
# Returns array of host hashes with :host, :private_ip, :label keys
|
|
13
13
|
def hosts_for(service_name)
|
|
14
|
-
|
|
15
|
-
return []
|
|
14
|
+
services = config.services.select { |s| s['name'] == service_name.to_s }
|
|
15
|
+
return [] if services.empty?
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
services.flat_map { |service| normalize_service_hosts(service) }
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
-
# Get
|
|
21
|
-
|
|
22
|
-
# Label matching: "redis-eu" matches region "eu", "redis-us" matches "us"
|
|
23
|
-
def host_for_region(service_name, region)
|
|
24
|
-
return nil if region.to_s.strip.empty?
|
|
25
|
-
|
|
20
|
+
# Get host by exact label match
|
|
21
|
+
def host_by_label(service_name, label)
|
|
26
22
|
hosts = hosts_for(service_name)
|
|
27
|
-
host_entry = hosts.find
|
|
28
|
-
label = h[:label].to_s.downcase
|
|
29
|
-
region_str = region.to_s.downcase
|
|
30
|
-
label == region_str || label.end_with?("-#{region_str}") || label.start_with?("#{region_str}-")
|
|
31
|
-
end
|
|
32
|
-
|
|
23
|
+
host_entry = hosts.find { |h| h[:label] == label.to_s }
|
|
33
24
|
return nil unless host_entry
|
|
34
25
|
|
|
35
26
|
connection_ip(host_entry)
|
|
36
27
|
end
|
|
37
28
|
|
|
38
|
-
# Get
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
29
|
+
# Get all hosts as a hash keyed by label
|
|
30
|
+
# { "redis-eu-gcp" => "10.8.0.10", "redis-us-aws" => "10.8.0.60" }
|
|
31
|
+
def hosts_by_label(service_name)
|
|
32
|
+
hosts_for(service_name).each_with_object({}) do |h, hash|
|
|
33
|
+
hash[h[:label]] = connection_ip(h) if h[:label]
|
|
34
|
+
end
|
|
44
35
|
end
|
|
45
36
|
|
|
46
37
|
# Get all connection IPs for a service (prefers private_ip for each)
|
|
47
|
-
def
|
|
38
|
+
def all_ips(service_name)
|
|
48
39
|
hosts_for(service_name).map { |h| connection_ip(h) }
|
|
49
40
|
end
|
|
50
41
|
|
|
@@ -54,9 +45,9 @@ module BoringServices
|
|
|
54
45
|
service&.dig('port')
|
|
55
46
|
end
|
|
56
47
|
|
|
57
|
-
# Build a Redis URL for a
|
|
58
|
-
def redis_url(
|
|
59
|
-
host =
|
|
48
|
+
# Build a Redis URL for a specific label
|
|
49
|
+
def redis_url(label: nil, password: nil, db: 0)
|
|
50
|
+
host = label ? host_by_label('redis', label) : all_ips('redis').first
|
|
60
51
|
return nil unless host
|
|
61
52
|
|
|
62
53
|
port = port_for('redis') || 6379
|
|
@@ -65,12 +56,12 @@ module BoringServices
|
|
|
65
56
|
end
|
|
66
57
|
|
|
67
58
|
# Build memcached connection string (host:port,host:port format)
|
|
68
|
-
def memcached_servers(
|
|
69
|
-
hosts = if
|
|
70
|
-
host =
|
|
59
|
+
def memcached_servers(label: nil)
|
|
60
|
+
hosts = if label
|
|
61
|
+
host = host_by_label('memcached', label)
|
|
71
62
|
host ? [host] : []
|
|
72
63
|
else
|
|
73
|
-
|
|
64
|
+
all_ips('memcached')
|
|
74
65
|
end
|
|
75
66
|
|
|
76
67
|
return nil if hosts.empty?
|
|
@@ -81,8 +72,20 @@ module BoringServices
|
|
|
81
72
|
|
|
82
73
|
private
|
|
83
74
|
|
|
84
|
-
# Normalize hosts
|
|
85
|
-
|
|
75
|
+
# Normalize hosts from a service config
|
|
76
|
+
# Handles: single host:, array hosts:, or hosts: as array of hashes
|
|
77
|
+
def normalize_service_hosts(service)
|
|
78
|
+
# Single host entry (production style)
|
|
79
|
+
if service['host']
|
|
80
|
+
return [{
|
|
81
|
+
host: service['host'],
|
|
82
|
+
private_ip: service['private_ip'],
|
|
83
|
+
label: service['label']
|
|
84
|
+
}]
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Array of hosts
|
|
88
|
+
hosts = service['hosts'] || []
|
|
86
89
|
hosts.map do |h|
|
|
87
90
|
if h.is_a?(Hash)
|
|
88
91
|
{
|
data/lib/boring_services.rb
CHANGED
|
@@ -39,9 +39,14 @@ module BoringServices
|
|
|
39
39
|
|
|
40
40
|
# Convenience methods - delegate to locator
|
|
41
41
|
|
|
42
|
-
# Get Redis
|
|
43
|
-
def
|
|
44
|
-
|
|
42
|
+
# Get all Redis hosts as hash { label => private_ip }
|
|
43
|
+
def redis_hosts
|
|
44
|
+
locator.hosts_by_label('redis')
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Get Redis host by exact label
|
|
48
|
+
def redis_host(label)
|
|
49
|
+
locator.host_by_label('redis', label)
|
|
45
50
|
end
|
|
46
51
|
|
|
47
52
|
# Get Redis port
|
|
@@ -49,14 +54,19 @@ module BoringServices
|
|
|
49
54
|
locator.port_for('redis') || 6379
|
|
50
55
|
end
|
|
51
56
|
|
|
52
|
-
# Build Redis URL
|
|
53
|
-
def redis_url(
|
|
54
|
-
locator.redis_url(
|
|
57
|
+
# Build Redis URL for a label
|
|
58
|
+
def redis_url(label: nil, password: nil, db: 0)
|
|
59
|
+
locator.redis_url(label: label, password: password, db: db)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Get all Memcached hosts as hash { label => private_ip }
|
|
63
|
+
def memcached_hosts
|
|
64
|
+
locator.hosts_by_label('memcached')
|
|
55
65
|
end
|
|
56
66
|
|
|
57
|
-
# Get Memcached host
|
|
58
|
-
def memcached_host(
|
|
59
|
-
|
|
67
|
+
# Get Memcached host by exact label
|
|
68
|
+
def memcached_host(label)
|
|
69
|
+
locator.host_by_label('memcached', label)
|
|
60
70
|
end
|
|
61
71
|
|
|
62
72
|
# Get Memcached port
|
|
@@ -65,13 +75,18 @@ module BoringServices
|
|
|
65
75
|
end
|
|
66
76
|
|
|
67
77
|
# Get Memcached servers string (host:port,host:port)
|
|
68
|
-
def memcached_servers(
|
|
69
|
-
locator.memcached_servers(
|
|
78
|
+
def memcached_servers(label: nil)
|
|
79
|
+
locator.memcached_servers(label: label)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Generic: get all hosts for any service as hash { label => ip }
|
|
83
|
+
def hosts_for(service)
|
|
84
|
+
locator.hosts_by_label(service)
|
|
70
85
|
end
|
|
71
86
|
|
|
72
|
-
# Generic: get host for any service
|
|
73
|
-
def host_for(service,
|
|
74
|
-
|
|
87
|
+
# Generic: get host by exact label for any service
|
|
88
|
+
def host_for(service, label)
|
|
89
|
+
locator.host_by_label(service, label)
|
|
75
90
|
end
|
|
76
91
|
|
|
77
92
|
# Generic: get port for any service
|