Skip to content

Commit

Permalink
support --pull flag on cook to automatically pull the base image
Browse files Browse the repository at this point in the history
  • Loading branch information
skaes committed Feb 9, 2024
1 parent 3a3371b commit 68ad68d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
19 changes: 18 additions & 1 deletion lib/fpm/fry/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,24 @@ def changes(name)
end

def pull(image)
agent.post(path: url('images','create'), query: {'fromImage' => image})
last_status = ""
streamer = lambda do |chunk, remaining_bytes, total_bytes|
chunk.each_line do |line|
begin
msg = JSON.parse(line)
status, progress, id = *msg.values_at("status", "progress", "id")
id += ": " if id
status += " " if progress
move_up_one_line = $stdout.tty? && status =~ /Downloading|Extracting/ && last_status =~ /Downloading|Extracting/
last_status = status
cursor_move = move_up_one_line ? "\e[1A" : ""
puts [cursor_move, id, status, progress].join("")
rescue JSON::ParserError => e
$stderr.puts "Could not parse JSON response from docker: #{e}"
end
end
end
agent.post(path: url('images','create'), query: {'fromImage' => image}, :response_block => streamer)
end

def create(image)
Expand Down
12 changes: 11 additions & 1 deletion lib/fpm/fry/command/cook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ class Command::Cook < Command

option '--keep', :flag, 'Keep the container after build'
option '--overwrite', :flag, 'Overwrite package', default: true
option '--verbose', :fag, 'Verbose output', default: false
option '--verbose', :flag, 'Verbose output', default: false
option '--platform', 'PLATFORM', default: nil
option '--pull', :flag, 'Pull base image', default: false

UPDATE_VALUES = ['auto','never','always']
option '--update',"<#{UPDATE_VALUES.join('|')}>", 'Update image before installing packages ( only apt currently )', attribute_name: 'update', default: 'auto' do |value|
Expand Down Expand Up @@ -203,6 +204,13 @@ def update?
end
end

def pull_base_image!
client.pull(image)
rescue Excon::Error
logger.error "could not pull base image #{image}"
raise
end

def build!
body = begin
url = client.url('containers','create')
Expand Down Expand Up @@ -407,6 +415,8 @@ def adjust_config_files( output )
public

def execute
pull_base_image! if pull?

# force some eager loading
lint_recipe_file!
builder
Expand Down
11 changes: 11 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
to_return(:status => 200, :body =>'{"ApiVersion":"1.9"}', :headers => {})
end

describe '#pull' do
it 'pulls an existing image' do
client = real_docker
res = nil
expect{
res = client.pull("ubuntu:jammy")
}.to output(/Status: Downloaded newer image for ubuntu:jammy/).to_stdout
expect(res.status).to eq(200)
end
end

describe '#read' do
context 'existing file' do
let(:body){
Expand Down

0 comments on commit 68ad68d

Please sign in to comment.