Merge lp:~gz/juju-ci-tools/copy_ps_polish into lp:juju-ci-tools

Proposed by Martin Packman
Status: Merged
Merged at revision: 1015
Proposed branch: lp:~gz/juju-ci-tools/copy_ps_polish
Merge into: lp:juju-ci-tools
Diff against target: 102 lines (+46/-38)
1 file modified
remote.py (+46/-38)
To merge this branch: bzr merge lp:~gz/juju-ci-tools/copy_ps_polish
Reviewer Review Type Date Requested Status
Curtis Hovey (community) code Approve
Review via email: mp+264060@code.launchpad.net

Description of the change

Improves powershell for gathering artifacts from windows machines

Mostly adding robustness:

* Always returns a non-zero exit code if something goes seriously wrong.
* If a particular file couldn't be opened generates a file containing the error.
* A glob that expands to no files will no longer try to open the empty string.

Would be possible instead to just output single file errors to stderr, but would mean continuing to log it all the time and it contains xml junk.

To post a comment you must log in.
Revision history for this message
Curtis Hovey (sinzui) wrote :

Thank you

review: Approve (code)
lp:~gz/juju-ci-tools/copy_ps_polish updated
1015. By Martin Packman

Wrap interpolated powershell argument in array

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'remote.py'
2--- remote.py 2015-07-02 00:59:47 +0000
3+++ remote.py 2015-07-07 18:04:46 +0000
4@@ -162,42 +162,51 @@
5
6
7 _ps_copy_script = """\
8-function CopyText {
9- param([String]$file, [IO.Stream]$outstream)
10- $enc = New-Object Text.UTF8Encoding($False)
11- $us = New-Object IO.StreamWriter($outstream, $enc)
12- $us.NewLine = "\n"
13- $uin = New-Object IO.StreamReader($file)
14- while (($line = $uin.ReadLine()) -ne $Null) {
15- $us.WriteLine($line)
16- }
17- $uin.Close()
18- $us.Close()
19-}
20-
21-function CopyBinary {
22- param([String]$file, [IO.Stream]$outstream)
23- $in = New-Object IO.FileStream($file, [IO.FileMode]::Open,
24- [IO.FileAccess]::Read, [IO.FileShare]"ReadWrite,Delete")
25- $in.CopyTo($outstream)
26- $in.Close()
27-}
28-
29-ForEach ($pattern in %s) {
30- $path = [Environment]::ExpandEnvironmentVariables($pattern)
31- $files = Get-Item -path $path
32- ForEach ($file in $files) {
33- [Console]::Out.Write($file.name + "|")
34- $trans = New-Object Security.Cryptography.ToBase64Transform
35- $out = [Console]::OpenStandardOutput()
36- $bs = New-Object Security.Cryptography.CryptoStream($out, $trans,
37- [Security.Cryptography.CryptoStreamMode]::Write)
38- $zs = New-Object IO.Compression.DeflateStream($bs,
39- [IO.Compression.CompressionMode]::Compress)
40- CopyBinary -file $file -outstream $zs
41- $zs.Close()
42- [Console]::Out.Write("\n")
43- }
44+$ErrorActionPreference = "Stop"
45+
46+function OutputEncodedFile {
47+ param([String]$filename, [IO.Stream]$instream)
48+ $trans = New-Object Security.Cryptography.ToBase64Transform
49+ $out = [Console]::OpenStandardOutput()
50+ $bs = New-Object Security.Cryptography.CryptoStream($out, $trans,
51+ [Security.Cryptography.CryptoStreamMode]::Write)
52+ $zs = New-Object IO.Compression.DeflateStream($bs,
53+ [IO.Compression.CompressionMode]::Compress)
54+ [Console]::Out.Write($filename + "|")
55+ try {
56+ $instream.CopyTo($zs)
57+ } finally {
58+ $zs.close()
59+ $bs.close()
60+ [Console]::Out.Write("`n")
61+ }
62+}
63+
64+function GatherFiles {
65+ param([String[]]$patterns)
66+ ForEach ($pattern in $patterns) {
67+ $path = [Environment]::ExpandEnvironmentVariables($pattern)
68+ ForEach ($file in Get-Item -path $path) {
69+ try {
70+ $in = New-Object IO.FileStream($file, [IO.FileMode]::Open,
71+ [IO.FileAccess]::Read, [IO.FileShare]"ReadWrite,Delete")
72+ OutputEncodedFile -filename $file.name -instream $in
73+ } catch {
74+ $utf8 = New-Object Text.UTF8Encoding($False)
75+ $errstream = New-Object IO.MemoryStream(
76+ $utf8.GetBytes($_.Exception), $False)
77+ $errfilename = $file.name + ".copyerror"
78+ OutputEncodedFile -filename $errfilename -instream $errstream
79+ }
80+ }
81+ }
82+}
83+
84+try {
85+ GatherFiles -patterns @(%s)
86+} catch {
87+ Write-Error $_.Exception
88+ exit 1
89 }
90 """
91
92@@ -249,9 +258,8 @@
93 # Encode globs into script to run on remote machine and return result.
94 script = _ps_copy_script % ",".join(s.join('""') for s in source_globs)
95 result = self.run_ps(script)
96- if result.std_err:
97+ if result.status_code:
98 logging.warning("winrm copy stderr:\n%s", result.std_err)
99- if result.status_code:
100 raise subprocess.CalledProcessError(result.status_code,
101 "powershell", result)
102 self._encoded_copy_to_dir(destination_dir, result.std_out)

Subscribers

People subscribed via source and target branches