From c882d888a7f75de833a08cfa1ba79abf134573b0 Mon Sep 17 00:00:00 2001 From: Joe Pleso Date: Fri, 15 Dec 2017 12:39:34 -0500 Subject: [PATCH 1/2] Add docker cap to windows to detect daemon and fix whitespace --- plugins/communicators/winrm/command_filters/rm.rb | 2 +- .../docker/cap/windows/docker_daemon_running.rb | 13 +++++++++++++ plugins/provisioners/docker/client.rb | 9 ++++----- plugins/provisioners/docker/plugin.rb | 5 +++++ .../communicators/winrm/command_filter_test.rb | 6 ++---- 5 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 plugins/provisioners/docker/cap/windows/docker_daemon_running.rb diff --git a/plugins/communicators/winrm/command_filters/rm.rb b/plugins/communicators/winrm/command_filters/rm.rb index 9133febf6..efc533197 100644 --- a/plugins/communicators/winrm/command_filters/rm.rb +++ b/plugins/communicators/winrm/command_filters/rm.rb @@ -33,7 +33,7 @@ module VagrantPlugins if recurse ret_cmd = "rm \"#{dir}\" -recurse -force" else - ret_cmd = "rm \"#{dir}\" -force" + ret_cmd = "if (Test-Path #{dir}) {Remove-Item #{dir} -force}" end return ret_cmd end diff --git a/plugins/provisioners/docker/cap/windows/docker_daemon_running.rb b/plugins/provisioners/docker/cap/windows/docker_daemon_running.rb new file mode 100644 index 000000000..2ff750cc3 --- /dev/null +++ b/plugins/provisioners/docker/cap/windows/docker_daemon_running.rb @@ -0,0 +1,13 @@ +module VagrantPlugins + module DockerProvisioner + module Cap + module Windows + module DockerDaemonRunning + def self.docker_daemon_running(machine) + machine.communicate.test("tasklist | find \"`\"dockerd`\"\"") + end + end + end + end + end +end diff --git a/plugins/provisioners/docker/client.rb b/plugins/provisioners/docker/client.rb index 84129a5d4..fa13f9744 100644 --- a/plugins/provisioners/docker/client.rb +++ b/plugins/provisioners/docker/client.rb @@ -127,12 +127,11 @@ module VagrantPlugins def create_container(config) args = container_run_args(config) - @machine.communicate.sudo %[ - rm -f #{config[:cidfile]} - docker run #{args} - ] + @machine.communicate.sudo %[rm -f #{config[:cidfile]} + ] + @machine.communicate.sudo %[docker run #{args} + ] - name = container_name(config) sha = Digest::SHA1.hexdigest(args) container_data_path(config).open("w+") do |f| f.write(sha) diff --git a/plugins/provisioners/docker/plugin.rb b/plugins/provisioners/docker/plugin.rb index b39422cd0..6de03e727 100644 --- a/plugins/provisioners/docker/plugin.rb +++ b/plugins/provisioners/docker/plugin.rb @@ -54,6 +54,11 @@ module VagrantPlugins Cap::Linux::DockerDaemonRunning end + guest_capability("windows", "docker_daemon_running") do + require_relative "cap/windows/docker_daemon_running" + Cap::Windows::DockerDaemonRunning + end + provisioner(:docker) do require_relative "provisioner" Provisioner diff --git a/test/unit/plugins/communicators/winrm/command_filter_test.rb b/test/unit/plugins/communicators/winrm/command_filter_test.rb index 0bd6e7ba8..79b8808f8 100644 --- a/test/unit/plugins/communicators/winrm/command_filter_test.rb +++ b/test/unit/plugins/communicators/winrm/command_filter_test.rb @@ -65,11 +65,9 @@ describe VagrantPlugins::CommunicatorWinRM::CommandFilter, unit: true do it 'filters out rm commands' do expect(subject.filter('rm /some/dir')).to eq( - "rm \"/some/dir\" -force") + "if (Test-Path /some/dir) {Remove-Item /some/dir -force}") expect(subject.filter('rm -f /some/dir')).to eq( - "rm \"/some/dir\" -force") - expect(subject.filter('rm -f "/some/dir"')).to eq( - "rm \"/some/dir\" -force") + "if (Test-Path /some/dir) {Remove-Item /some/dir -force}") end it 'filters out mkdir commands' do From 93356d463581f2ad61c03cb7fda9974235675a23 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 30 Apr 2018 09:18:09 -0700 Subject: [PATCH 2/2] Update rm filters to test for path This commit updates the rm filter for winrm to operate like how rm works in bash. If a folder doesn't exist, the command returns 0 rather than 1. --- plugins/communicators/winrm/command_filters/rm.rb | 4 ++-- plugins/provisioners/docker/client.rb | 6 ++---- .../communicators/winrm/command_filter_test.rb | 14 ++++++++------ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/plugins/communicators/winrm/command_filters/rm.rb b/plugins/communicators/winrm/command_filters/rm.rb index efc533197..755a48896 100644 --- a/plugins/communicators/winrm/command_filters/rm.rb +++ b/plugins/communicators/winrm/command_filters/rm.rb @@ -31,9 +31,9 @@ module VagrantPlugins ret_cmd = '' if recurse - ret_cmd = "rm \"#{dir}\" -recurse -force" + ret_cmd = "if (Test-Path \"#{dir}\") {Remove-Item \"#{dir}\" -force -recurse}" else - ret_cmd = "if (Test-Path #{dir}) {Remove-Item #{dir} -force}" + ret_cmd = "if (Test-Path \"#{dir}\") {Remove-Item \"#{dir}\" -force}" end return ret_cmd end diff --git a/plugins/provisioners/docker/client.rb b/plugins/provisioners/docker/client.rb index fa13f9744..4293dffed 100644 --- a/plugins/provisioners/docker/client.rb +++ b/plugins/provisioners/docker/client.rb @@ -127,10 +127,8 @@ module VagrantPlugins def create_container(config) args = container_run_args(config) - @machine.communicate.sudo %[rm -f #{config[:cidfile]} - ] - @machine.communicate.sudo %[docker run #{args} - ] + @machine.communicate.sudo %[rm -f "#{config[:cidfile]}"] + @machine.communicate.sudo %[docker run #{args}] sha = Digest::SHA1.hexdigest(args) container_data_path(config).open("w+") do |f| diff --git a/test/unit/plugins/communicators/winrm/command_filter_test.rb b/test/unit/plugins/communicators/winrm/command_filter_test.rb index 79b8808f8..151227b35 100644 --- a/test/unit/plugins/communicators/winrm/command_filter_test.rb +++ b/test/unit/plugins/communicators/winrm/command_filter_test.rb @@ -54,20 +54,22 @@ describe VagrantPlugins::CommunicatorWinRM::CommandFilter, unit: true do it 'filters out rm recurse commands' do expect(subject.filter('rm -Rf /some/dir')).to eq( - "rm \"/some/dir\" -recurse -force") + "if (Test-Path \"/some/dir\") {Remove-Item \"/some/dir\" -force -recurse}") expect(subject.filter('rm -fr /some/dir')).to eq( - "rm \"/some/dir\" -recurse -force") + "if (Test-Path \"/some/dir\") {Remove-Item \"/some/dir\" -force -recurse}") expect(subject.filter('rm -r /some/dir')).to eq( - "rm \"/some/dir\" -recurse -force") + "if (Test-Path \"/some/dir\") {Remove-Item \"/some/dir\" -force -recurse}") expect(subject.filter('rm -r "/some/dir"')).to eq( - "rm \"/some/dir\" -recurse -force") + "if (Test-Path \"/some/dir\") {Remove-Item \"/some/dir\" -force -recurse}") end it 'filters out rm commands' do expect(subject.filter('rm /some/dir')).to eq( - "if (Test-Path /some/dir) {Remove-Item /some/dir -force}") + "if (Test-Path \"/some/dir\") {Remove-Item \"/some/dir\" -force}") expect(subject.filter('rm -f /some/dir')).to eq( - "if (Test-Path /some/dir) {Remove-Item /some/dir -force}") + "if (Test-Path \"/some/dir\") {Remove-Item \"/some/dir\" -force}") + expect(subject.filter('rm -f "/some/dir"')).to eq( + "if (Test-Path \"/some/dir\") {Remove-Item \"/some/dir\" -force}") end it 'filters out mkdir commands' do