forked from sous-chefs/docker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sous-chefs#1044 from smcavallo/image_purge
Image prune
- Loading branch information
Showing
5 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module DockerCookbook | ||
class DockerImagePrune < DockerBase | ||
resource_name :docker_image_prune | ||
# Requires docker API v1.25 | ||
# Modify the default of read_timeout from 60 to 120 | ||
property :read_timeout, default: 120, desired_state: false | ||
property :host, [String, nil], default: lazy { ENV['DOCKER_HOST'] }, desired_state: false | ||
|
||
# https://docs.docker.com/engine/api/v1.35/#operation/ImagePrune | ||
property :dangling, [TrueClass, FalseClass], default: true | ||
property :prune_until, String | ||
# https://docs.docker.com/engine/reference/builder/#label | ||
property :with_label, String | ||
property :without_label, String | ||
|
||
######### | ||
# Actions | ||
######### | ||
|
||
default_action :prune | ||
|
||
action :prune do | ||
# Have to call this method ourselves due to | ||
# https://github.com/swipely/docker-api/pull/507 | ||
json = generate_json(new_resource) | ||
# Post | ||
res = connection.post('/images/prune', json) | ||
Chef::Log.info res | ||
end | ||
|
||
def generate_json(new_resource) | ||
opts = { filters: ["dangling=#{new_resource.dangling}"] } | ||
opts[:filters].push("until=#{new_resource.prune_until}") if new_resource.property_is_set?(:prune_until) | ||
opts[:filters].push("label=#{new_resource.with_label}") if new_resource.property_is_set?(:with_label) | ||
opts[:filters].push("label!=#{new_resource.without_label}") if new_resource.property_is_set?(:without_label) | ||
opts.to_json | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'spec_helper' | ||
|
||
describe 'docker_test::image_prune' do | ||
context 'it steps over the provider' do | ||
cached(:chef_run) { ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '18.04').converge(described_recipe) } | ||
|
||
context 'testing default action, default properties' do | ||
it 'prunes docker_image[hello-world]' do | ||
expect(chef_run).to prune_docker_image_prune('hello-world').with( | ||
dangling: true | ||
) | ||
end | ||
|
||
it 'prunes docker_image[hello-world]' do | ||
expect(chef_run).to prune_docker_image_prune('prune-old-images').with( | ||
dangling: true, | ||
prune_until: '1h30m', | ||
with_label: 'com.example.vendor=ACME', | ||
without_label: 'no_prune' | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Load all the libraries | ||
require 'chef' | ||
Dir['libraries/*.rb'].each { |f| require File.expand_path(f) } | ||
|
||
describe DockerCookbook::DockerImagePrune do | ||
let(:resource) { DockerCookbook::DockerImagePrune.new('rspec') } | ||
|
||
it 'has a default action of [:prune]' do | ||
expect(resource.action).to eql([:prune]) | ||
end | ||
|
||
it 'generates filter json' do | ||
# Arrange | ||
expected = '{"filters":["dangling=true","until=1h30m","label=com.example.vendor=ACME","label!=no_prune"]}' | ||
resource.dangling = true | ||
resource.prune_until = '1h30m' | ||
resource.with_label = 'com.example.vendor=ACME' | ||
resource.without_label = 'no_prune' | ||
resource.action :prune | ||
|
||
# Act | ||
actual = resource.generate_json(resource) | ||
|
||
# Assert | ||
expect(actual).to eq(expected) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
######################### | ||
# :prune | ||
######################### | ||
|
||
docker_image_prune 'hello-world' do | ||
dangling true | ||
end | ||
|
||
docker_image_prune "prune-old-images" do | ||
dangling true | ||
prune_until '1h30m' | ||
with_label 'com.example.vendor=ACME' | ||
without_label 'no_prune' | ||
action :prune | ||
end |