Skip to content

Commit

Permalink
moar debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
skaes committed Feb 9, 2024
1 parent ccf6e2b commit eca4042
Showing 1 changed file with 71 additions and 66 deletions.
137 changes: 71 additions & 66 deletions lib/fpm/fry/command/cook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,73 +212,11 @@ def pull_base_image!
end

def build!
body = begin
url = client.url('containers','create')
args = {
headers: {
'Content-Type' => 'application/json'
},
path: url,
expects: [201],
body: JSON.generate({"Image" => build_image})
}
args[:query] = { platform: platform } if platform
res = client.post(args)
JSON.parse(res.body)
rescue Excon::Error
logger.error "could not create #{build_image}, url: #{url}"
raise
end
container = body['Id']
container = create_build_container
begin
begin
url = client.url('containers',container,'start')
client.post(
headers: {
'Content-Type' => 'application/json'
},
path: url,
expects: [204],
body: JSON.generate({})
)
rescue Excon::Error
logger.error "could not start container #{container}, url: #{url}"
raise
end

begin
url = client.url('containers',container,'attach?stderr=1&stdout=1&stream=1&logs=1')
client.post(
path: url,
body: '',
expects: [200],
middlewares: [
StreamParser.new(out,err),
Excon::Middleware::Expects,
Excon::Middleware::Instrumentor,
Excon::Middleware::Mock
]
)
rescue Excon::Error
logger.error "could not attach to container #{container}, url: #{url}"
raise
end

begin
res = client.post(
path: client.url('containers',container,'wait'),
expects: [200],
body: ''
)
json = JSON.parse(res.body)
if json["StatusCode"] != 0
raise Fry::WithData("Build failed", json)
end
rescue Excon::Error
logger.error "could not wait successfully for container #{container}, url: #{url}"
raise
end

start_build_container(container)
attach_to_build_container_and_stream_logs(container)
wait_for_build_container_to_shut_down(container)
yield container
ensure
unless keep?
Expand All @@ -287,6 +225,73 @@ def build!
end
end

def create_build_container
url = client.url('containers','create')
args = {
headers: {
'Content-Type' => 'application/json'
},
path: url,
expects: [201],
body: JSON.generate({"Image" => build_image})
}
args[:query] = { platform: platform } if platform
res = client.post(args)
JSON.parse(res.body)['Id']
rescue Excon::Error
logger.error "could not create #{build_image}, url: #{url}"
raise
end

def start_build_container(container)
url = client.url('containers',container,'start')
client.post(
headers: {
'Content-Type' => 'application/json'
},
path: url,
expects: [204],
body: JSON.generate({})
)
rescue Excon::Error
logger.error "could not start container #{container}, url: #{url}"
raise
end

def attach_to_build_container_and_stream_logs(container)
url = client.url('containers',container,'attach?stderr=1&stdout=1&stream=1&logs=1')
res = client.post(
path: url,
body: '',
expects: [200],
middlewares: [
StreamParser.new(out,err),
Excon::Middleware::Expects,
Excon::Middleware::Instrumentor,
Excon::Middleware::Mock
]
)
puts "----> #{res.inspect} <------"
rescue Excon::Error
logger.error "could not attach to container #{container}, url: #{url}"
raise
end

def wait_for_build_container_to_shut_down(container)
res = client.post(
path: client.url('containers',container,'wait'),
expects: [200],
body: ''
)
json = JSON.parse(res.body)
if json["StatusCode"] != 0
raise Fry::WithData("Build failed", json)
end
rescue Excon::Error
logger.error "could not wait successfully for container #{container}, url: #{url}"
raise
end

def input_package(container)
input = FPM::Package::Docker.new(
logger: logger,
Expand Down

0 comments on commit eca4042

Please sign in to comment.