Skip to content

Commit

Permalink
Add volume_prune resource
Browse files Browse the repository at this point in the history
Issue #1240
  • Loading branch information
bmhughes committed Apr 20, 2023
1 parent 4ae46f7 commit 5f9a81a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Add volume_prune resource

## 10.4.6 - *2023-04-01*

## 10.4.5 - *2023-03-02*
Expand Down
3 changes: 3 additions & 0 deletions kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ suites:
- recipe[docker_test::container]
- recipe[docker_test::exec]
- recipe[docker_test::plugin]
- recipe[docker_test::image_prune]
- recipe[docker_test::volume_prune]

- name: network
provisioner:
Expand All @@ -108,6 +110,7 @@ suites:
run_list:
- recipe[docker_test::default]
- recipe[docker_test::volume]
- recipe[docker_test::volume_prune]

- name: registry
provisioner:
Expand Down
6 changes: 4 additions & 2 deletions libraries/helpers_json.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module DockerCookbook
module DockerHelpers
module Json
def generate_json(dangling, prune_until = nil, with_label = nil, without_label = nil)
def prune_generate_json(dangling:, prune_until: nil, with_label: nil, without_label: nil, all: false)
opts = { dangling: { "#{dangling}": true } }
opts['until'] = { "#{prune_until}": true } if prune_until
opts['label'] = { "#{with_label}": true } if with_label
opts['label!'] = { "#{without_label}": true } if without_label
'filters=' + URI.encode_www_form_component(opts.to_json)
opts['all'] = true if all

'filters=' + URI.encode_www_form_component(opts.to_jso)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion resources/image_prune.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
action :prune do
# Have to call this method ourselves due to
# https://github.com/swipely/docker-api/pull/507
json = generate_json(new_resource.dangling, new_resource.prune_until, new_resource.with_label, new_resource.without_label)
json = prune_generate_json(dangling: new_resource.dangling, prune_until: new_resource.prune_until, with_label: new_resource.with_label, without_label: new_resource.without_label)

res = connection.post('/images/prune', json)
Chef::Log.info res
Expand Down
24 changes: 24 additions & 0 deletions resources/volume_prune.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
unified_mode true
use 'partial/_base'

property :read_timeout, Integer, default: 120, desired_state: false
property :host, [String, nil], default: lazy { ENV['DOCKER_HOST'] }, desired_state: false

# https://docs.docker.com/engine/api/v1.42/#tag/Volume/operation/VolumePrune
property :all, [true, false], default: false
# https://docs.docker.com/engine/reference/builder/#label
property :with_label, String
property :without_label, String

action :prune do
# Have to call this method ourselves due to
# https://github.com/swipely/docker-api/pull/507
json = prune_generate_json(with_label: new_resource.with_label, without_label: new_resource.without_label, all: new_resource.all)

res = connection.post('/volumes/prune', json)
Chef::Log.info res
end

action_class do
include DockerCookbook::DockerHelpers::Json
end
7 changes: 7 additions & 0 deletions test/cookbooks/docker_test/recipes/volume_prune.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#########################
# :prune
#########################

docker_volume_prune 'all' do
all true
end

0 comments on commit 5f9a81a

Please sign in to comment.