winrm-fs 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,119 +0,0 @@
1
- # encoding: UTF-8
2
- #
3
- # Copyright 2015 Shawn Neal <sneal@sneal.net>
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
- require_relative 'temp_zip_file'
18
- require_relative 'file_uploader'
19
- require_relative 'command_executor'
20
- require_relative '../scripts/scripts'
21
-
22
- module WinRM
23
- module FS
24
- module Core
25
- # Orchestrates the upload of a file or directory
26
- class UploadOrchestrator
27
- def initialize(service)
28
- @service = service
29
- @logger = Logging.logger[self]
30
- end
31
-
32
- def upload_file(local_path, remote_path, &block)
33
- # If the src has a file extension and the destination does not
34
- # we can assume the caller specified the dest as a directory
35
- if File.extname(local_path) != '' && File.extname(remote_path) == ''
36
- remote_path = File.join(remote_path, File.basename(local_path))
37
- end
38
- temp_path = temp_file_path(local_path)
39
- with_command_executor do |cmd_executor|
40
- return 0 unless out_of_date?(cmd_executor, local_path, remote_path)
41
- do_file_upload(cmd_executor, local_path, temp_path, remote_path, &block)
42
- end
43
- end
44
-
45
- def upload_directory(local_path, remote_path, &block)
46
- with_local_zip(local_path) do |local_zip|
47
- temp_path = temp_file_path(local_zip.path)
48
- with_command_executor do |cmd_executor|
49
- return 0 unless out_of_date?(cmd_executor, local_zip.path, temp_path)
50
- do_file_upload(cmd_executor, local_zip.path, temp_path, remote_path, &block)
51
- end
52
- end
53
- end
54
-
55
- private
56
-
57
- def do_file_upload(cmd_executor, local_path, temp_path, remote_path)
58
- file_uploader = WinRM::FS::Core::FileUploader.new(cmd_executor)
59
- bytes = file_uploader.upload(local_path, temp_path) do |bytes_copied, total_bytes|
60
- yield bytes_copied, total_bytes, local_path, remote_path if block_given?
61
- end
62
-
63
- cmd_executor.run_powershell(
64
- WinRM::FS::Scripts.render('decode_file', src: temp_path, dest: remote_path))
65
-
66
- bytes
67
- end
68
-
69
- def with_command_executor
70
- cmd_executor = WinRM::FS::Core::CommandExecutor.new(@service)
71
- cmd_executor.open
72
- yield cmd_executor
73
- ensure
74
- cmd_executor.close
75
- end
76
-
77
- def with_local_zip(local_path)
78
- local_zip = create_temp_zip_file(local_path)
79
- yield local_zip
80
- ensure
81
- local_zip.delete if local_zip
82
- end
83
-
84
- def out_of_date?(cmd_executor, local_path, remote_path)
85
- local_checksum = local_checksum(local_path)
86
- remote_checksum = remote_checksum(cmd_executor, remote_path)
87
-
88
- if remote_checksum == local_checksum
89
- @logger.debug("#{remote_path} is up to date")
90
- return false
91
- end
92
- true
93
- end
94
-
95
- def remote_checksum(cmd_executor, remote_path)
96
- script = WinRM::FS::Scripts.render('checksum', path: remote_path)
97
- cmd_executor.run_powershell(script).chomp
98
- end
99
-
100
- def local_checksum(local_path)
101
- Digest::MD5.file(local_path).hexdigest
102
- end
103
-
104
- def temp_file_path(local_path)
105
- ext = '.tmp'
106
- ext = '.zip' if File.extname(local_path) == '.zip'
107
- "$env:TEMP/winrm-upload/#{local_checksum(local_path)}#{ext}"
108
- end
109
-
110
- def create_temp_zip_file(local_path)
111
- zip = WinRM::FS::Core::TempZipFile.new(local_path, recurse_paths: true)
112
- zip.add(local_path)
113
- zip.build
114
- zip
115
- end
116
- end
117
- end
118
- end
119
- end
@@ -1,36 +0,0 @@
1
- $path = $ExecutionContext.SessionState.Path
2
- $tempFile = $path.GetUnresolvedProviderPathFromPSPath("<%= src %>")
3
- $dest = $path.GetUnresolvedProviderPathFromPSPath("<%= dest %>")
4
-
5
- function Decode-File($encodedFile, $decodedFile) {
6
- if (Test-Path $encodedFile) {
7
- $base64Content = Get-Content $encodedFile
8
- }
9
- if ($base64Content -eq $null) {
10
- New-Item -ItemType file -Force $decodedFile | Out-Null
11
- }
12
- else {
13
- $bytes = [System.Convert]::FromBase64String($base64Content)
14
- [System.IO.File]::WriteAllBytes($decodedFile, $bytes) | Out-Null
15
- }
16
- }
17
-
18
- function Ensure-Dir-Exists($path) {
19
- # ensure the destination directory exists
20
- if (!(Test-Path $path)) {
21
- New-Item -ItemType Directory -Force -Path $path | Out-Null
22
- }
23
- }
24
-
25
- if ([System.IO.Path]::GetExtension($tempFile) -eq '.zip') {
26
- Ensure-Dir-Exists $dest
27
- Decode-File $tempFile $tempFile
28
- $shellApplication = New-Object -com shell.application
29
- $zipPackage = $shellApplication.NameSpace($tempFile)
30
- $destinationFolder = $shellApplication.NameSpace($dest)
31
- $destinationFolder.CopyHere($zipPackage.Items(), 0x10) | Out-Null
32
- }
33
- else {
34
- Ensure-Dir-Exists ([System.IO.Path]::GetDirectoryName($dest))
35
- Decode-File $tempFile $dest
36
- }
@@ -1,55 +0,0 @@
1
- # encoding: UTF-8
2
- require_relative '../lib/winrm-fs/core/temp_zip_file'
3
-
4
- describe WinRM::FS::Core::TempZipFile, integration: true do
5
- let(:winrm_fs_dir) { File.expand_path('../lib/winrm-fs', File.dirname(__FILE__)) }
6
- let(:temp_zip_file_spec) { __FILE__ }
7
- let(:spec_helper) { File.expand_path('spec_helper.rb', File.dirname(__FILE__)) }
8
-
9
- subject { WinRM::FS::Core::TempZipFile.new }
10
-
11
- context 'temp file creation' do
12
- it 'should create a temp file on disk' do
13
- expect(File.exist?(subject.path)).to be true
14
- subject.delete
15
- expect(File.exist?(subject.path)).to be false
16
- end
17
- end
18
-
19
- context 'create zip' do
20
- it 'should raise error when file doesn not exist' do
21
- expect { subject.add('/etc/foo/does/not/exist') }.to raise_error
22
- end
23
-
24
- it 'should add a file to the zip' do
25
- subject.add(temp_zip_file_spec)
26
- subject.build
27
- expect(subject).to contain_zip_entries('spec/temp_zip_file_spec.rb')
28
- end
29
-
30
- it 'should add multiple files to the zip' do
31
- subject.add(temp_zip_file_spec)
32
- subject.add(spec_helper)
33
- subject.build
34
- expect(subject).to contain_zip_entries([
35
- 'spec/temp_zip_file_spec.rb',
36
- 'spec/spec_helper.rb'])
37
- end
38
-
39
- it 'should add all files in directory' do
40
- subject.add(winrm_fs_dir)
41
- subject.build
42
- expect(subject).to contain_zip_entries('lib/winrm-fs/exceptions.rb')
43
- end
44
-
45
- it 'should add all files in directory to the zip recursively' do
46
- subject = WinRM::FS::Core::TempZipFile.new(Dir.pwd, recurse_paths: true)
47
- subject.add(winrm_fs_dir)
48
- subject.build
49
- expect(subject).to contain_zip_entries([
50
- 'lib/winrm-fs/exceptions.rb',
51
- 'lib/winrm-fs/core/temp_zip_file.rb',
52
- 'lib/winrm-fs/scripts/checksum.ps1.erb'])
53
- end
54
- end
55
- end