yatfa 1.0.73 → 1.0.75
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.
- package/lib/yatfa_agent/docker.rb +52 -2
- package/package.json +1 -1
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "fileutils"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "json"
|
|
6
|
+
require "time"
|
|
7
|
+
require "uri"
|
|
4
8
|
|
|
5
9
|
module YatfaAgent
|
|
6
10
|
module Docker
|
|
11
|
+
STAMP_FILE = ".yatfa-build-stamp"
|
|
12
|
+
VERSION_URL = "https://objectstore.fra1.civo.com/yatfa/main/version.json"
|
|
13
|
+
|
|
7
14
|
def self.check_dockerfile!
|
|
8
15
|
unless File.exist?("Dockerfile.sandbox")
|
|
9
16
|
puts "❌ Error: Dockerfile.sandbox not found"
|
|
@@ -16,12 +23,50 @@ module YatfaAgent
|
|
|
16
23
|
end
|
|
17
24
|
end
|
|
18
25
|
|
|
26
|
+
def self.fetch_remote_version
|
|
27
|
+
uri = URI(VERSION_URL)
|
|
28
|
+
response = Net::HTTP.get_response(uri)
|
|
29
|
+
return nil unless response.is_a?(Net::HTTPSuccess)
|
|
30
|
+
data = JSON.parse(response.body)
|
|
31
|
+
data["deployed_at"]
|
|
32
|
+
rescue StandardError
|
|
33
|
+
nil
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.read_build_stamp
|
|
37
|
+
return nil unless File.exist?(STAMP_FILE)
|
|
38
|
+
JSON.parse(File.read(STAMP_FILE))["version"]
|
|
39
|
+
rescue StandardError
|
|
40
|
+
nil
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.write_build_stamp(version)
|
|
44
|
+
File.write(STAMP_FILE, JSON.pretty_generate({ version: version, built_at: Time.now.utc.iso8601 }))
|
|
45
|
+
end
|
|
46
|
+
|
|
19
47
|
def self.build_image(config)
|
|
20
48
|
check_dockerfile!
|
|
21
49
|
|
|
22
50
|
user_id = `id -u`.strip
|
|
23
51
|
group_id = `id -g`.strip
|
|
24
52
|
|
|
53
|
+
# Check if we need to invalidate Docker cache based on remote version
|
|
54
|
+
remote_version = fetch_remote_version
|
|
55
|
+
local_version = read_build_stamp
|
|
56
|
+
no_cache = remote_version && remote_version != local_version
|
|
57
|
+
|
|
58
|
+
if no_cache
|
|
59
|
+
if local_version
|
|
60
|
+
puts "🔄 Infrastructure updated (#{local_version[0..18]} → #{remote_version[0..18]}), rebuilding without cache..."
|
|
61
|
+
else
|
|
62
|
+
puts "🔄 First build (or stamp missing), rebuilding without cache..."
|
|
63
|
+
end
|
|
64
|
+
elsif remote_version.nil?
|
|
65
|
+
puts "⚠️ Could not fetch remote version, building with cache"
|
|
66
|
+
else
|
|
67
|
+
puts "✅ Infrastructure up to date, using cached build"
|
|
68
|
+
end
|
|
69
|
+
|
|
25
70
|
puts "🏗️ Building Docker image..."
|
|
26
71
|
|
|
27
72
|
# Handle .dockerignore.sandbox
|
|
@@ -42,14 +87,16 @@ module YatfaAgent
|
|
|
42
87
|
|
|
43
88
|
success = false
|
|
44
89
|
begin
|
|
45
|
-
|
|
90
|
+
build_args = [
|
|
46
91
|
"docker", "build",
|
|
92
|
+
*(no_cache ? ["--no-cache"] : []),
|
|
47
93
|
"--build-arg", "USER_ID=#{user_id}",
|
|
48
94
|
"--build-arg", "GROUP_ID=#{group_id}",
|
|
49
95
|
"-t", Config.image_name(config),
|
|
50
96
|
"-f", "Dockerfile.sandbox",
|
|
51
97
|
"."
|
|
52
|
-
|
|
98
|
+
]
|
|
99
|
+
success = system(*build_args)
|
|
53
100
|
ensure
|
|
54
101
|
if has_sandbox_ignore
|
|
55
102
|
# Restore original state
|
|
@@ -66,6 +113,9 @@ module YatfaAgent
|
|
|
66
113
|
exit 1
|
|
67
114
|
end
|
|
68
115
|
|
|
116
|
+
# Write stamp after successful build so we know which version we have
|
|
117
|
+
write_build_stamp(remote_version) if remote_version
|
|
118
|
+
|
|
69
119
|
puts "✅ Docker image built"
|
|
70
120
|
end
|
|
71
121
|
end
|