mixlibrary-core 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/mixlibrary/core/shell/scripts/powershell.rb +133 -133
- data/lib/mixlibrary/core/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29b7daf4dfe4ce619b65889d56f2c8051cb8f0f6
|
4
|
+
data.tar.gz: fc25b1fc92bbec848ac840718b0fd86fb29fe15b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e29c2c882bd14cf58717e32eebf3b4949961d3c051e61323661dff38a4ddab0ae49319d8a1b93cd895f8af8ca90db233df7b75bf01b36bb5eb6d66297555f0ed
|
7
|
+
data.tar.gz: 438a865f30cf68cc405bcdefbf526aa52b897a1cf9b407cfe58ce664d598559b421571a513336bd9a81c560f1312beb3356e4a8d10ecc793f9fc286a0c7d0061
|
@@ -1,134 +1,134 @@
|
|
1
|
-
#Runs powershell scripts with the least amount of intrusion possible. We do some syntax checking and validation that the script did not end badly, but other than that, we
|
2
|
-
#try to leave the executing script in the hands of the developer.
|
3
|
-
require "chef"
|
4
|
-
require "mixlibrary/core/shell/scripts/windows_script"
|
5
|
-
|
6
|
-
module Mixlibrary
|
7
|
-
module Core
|
8
|
-
module Shell
|
9
|
-
class Scripts
|
10
|
-
class Powershell < WindowsScript
|
11
|
-
@originalScript =nil
|
12
|
-
@validate =nil
|
13
|
-
@options =nil
|
14
|
-
@flagoverrides =nil
|
15
|
-
|
16
|
-
def initialize(script, validate, options, flags, architecture = nil)
|
17
|
-
super(architecture)
|
18
|
-
@originalScript=script
|
19
|
-
@options = options
|
20
|
-
@validate=validate
|
21
|
-
@flagoverrides=flags
|
22
|
-
end
|
23
|
-
|
24
|
-
def orchestrate
|
25
|
-
#Powershell kind of sucks with syntax checking.
|
26
|
-
#If any part of the script syntactically fails it will exit with a clean exit code of 0
|
27
|
-
#So this forced our hand to call powershell twice for every script call.
|
28
|
-
#1 - Put passed in script in a function to see if any standard error is generated -
|
29
|
-
#only will happen if syntax is incorrect since function is never called
|
30
|
-
#2 - Run the user script with exception wraps to guarantee some exit status
|
31
|
-
syntax_check()
|
32
|
-
|
33
|
-
myscriptstring = finalscript()
|
34
|
-
Chef::Log::debug("Script Contents:")
|
35
|
-
Chef::Log::debug("-----------------------------------------------------")
|
36
|
-
Chef::Log::debug(myscriptstring)
|
37
|
-
Chef::Log::debug("-----------------------------------------------------")
|
38
|
-
return run_command(shell,flags,filename,file_extension, myscriptstring, @options, @validate)
|
39
|
-
end
|
40
|
-
|
41
|
-
private
|
42
|
-
|
43
|
-
EXIT_STATUS_EXCEPTION_HANDLER= <<-EOF
|
44
|
-
trap [Exception]{
|
45
|
-
write-error -exception ($_.Exception.Message) -erroraction continue;
|
46
|
-
exit 1
|
47
|
-
}
|
48
|
-
EOF
|
49
|
-
|
50
|
-
EXIT_STATUS_RESET_SCRIPT= <<-EOF
|
51
|
-
$LASTEXITCODE=0
|
52
|
-
EOF
|
53
|
-
|
54
|
-
#Make this set-variable call a read only. Technically can be overridden but most of PS scripts that set this setting,
|
55
|
-
#usually try to set this by doing things like $ERRORACTIONPREFERENCE="Stop"
|
56
|
-
#Generally speaking this is the best way to handle this situation. We will need to determine if there are many dependent scripts
|
57
|
-
#that depend on being able to set this setting.
|
58
|
-
EXIT_STATUS_INTIALIZATION= <<-EOF
|
59
|
-
Set-Variable -Name ERRORACTIONPREFERENCE -Value "STOP" -Scope Script -Option "ReadOnly" -force
|
60
|
-
\#$ErrorActionPreference="STOP"
|
61
|
-
try{
|
62
|
-
EOF
|
63
|
-
|
64
|
-
EXIT_STATUS_POST= <<-EOF
|
65
|
-
exit $LASTEXITCODE
|
66
|
-
}
|
67
|
-
catch{
|
68
|
-
write-error -exception ($_.Exception) -erroraction continue;
|
69
|
-
exit 1
|
70
|
-
}
|
71
|
-
EOF
|
72
|
-
|
73
|
-
#Validate valid powershell was passed in.
|
74
|
-
def syntax_check
|
75
|
-
local_script_contents=("function testme(){ \n #{@originalScript.to_s} \n} \n")
|
76
|
-
|
77
|
-
result = run_command(shell,flags,filename,file_extension, local_script_contents, @options, false)
|
78
|
-
#puts result.inspect
|
79
|
-
if(result.stderr.length >= 1 || result.exitstatus !=0)
|
80
|
-
#puts "Standard Output: \n #{result.stdout}"
|
81
|
-
#puts "Standard Error: \n #{result.stderr}"
|
82
|
-
raise RuntimeError,"Did not syntactically pass the powershell check. \n Standard out: #{result.stdout} \n Standard Error:#{result.stderr}"
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def finalscript
|
87
|
-
changed_script =
|
88
|
-
EXIT_STATUS_EXCEPTION_HANDLER +
|
89
|
-
EXIT_STATUS_RESET_SCRIPT +
|
90
|
-
EXIT_STATUS_INTIALIZATION +
|
91
|
-
@originalScript +
|
92
|
-
EXIT_STATUS_POST
|
93
|
-
|
94
|
-
return changed_script
|
95
|
-
end
|
96
|
-
|
97
|
-
def flags
|
98
|
-
flags = [
|
99
|
-
# Hides the copyright banner at startup.
|
100
|
-
"-NoLogo",
|
101
|
-
# Does not present an interactive prompt to the user.
|
102
|
-
"-NonInteractive",
|
103
|
-
# Does not load the Windows PowerShell profile.
|
104
|
-
"-NoProfile",
|
105
|
-
# always set the ExecutionPolicy flag
|
106
|
-
# see http://technet.microsoft.com/en-us/library/ee176961.aspx
|
107
|
-
"-ExecutionPolicy RemoteSigned",
|
108
|
-
# Powershell will hang if STDIN is redirected
|
109
|
-
# http://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected
|
110
|
-
"-InputFormat None",
|
111
|
-
|
112
|
-
"-File"
|
113
|
-
]
|
114
|
-
return @flagoverrides if @flagoverrides != nil
|
115
|
-
return flags.join(' ')
|
116
|
-
end
|
117
|
-
|
118
|
-
def shell
|
119
|
-
return "powershell.exe"
|
120
|
-
end
|
121
|
-
|
122
|
-
def filename
|
123
|
-
return "tempPSchef"
|
124
|
-
end
|
125
|
-
|
126
|
-
def file_extension
|
127
|
-
return ".ps1"
|
128
|
-
end
|
129
|
-
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
1
|
+
#Runs powershell scripts with the least amount of intrusion possible. We do some syntax checking and validation that the script did not end badly, but other than that, we
|
2
|
+
#try to leave the executing script in the hands of the developer.
|
3
|
+
require "chef"
|
4
|
+
require "mixlibrary/core/shell/scripts/windows_script"
|
5
|
+
|
6
|
+
module Mixlibrary
|
7
|
+
module Core
|
8
|
+
module Shell
|
9
|
+
class Scripts
|
10
|
+
class Powershell < WindowsScript
|
11
|
+
@originalScript =nil
|
12
|
+
@validate =nil
|
13
|
+
@options =nil
|
14
|
+
@flagoverrides =nil
|
15
|
+
|
16
|
+
def initialize(script, validate, options, flags, architecture = nil)
|
17
|
+
super(architecture)
|
18
|
+
@originalScript=script
|
19
|
+
@options = options
|
20
|
+
@validate=validate
|
21
|
+
@flagoverrides=flags
|
22
|
+
end
|
23
|
+
|
24
|
+
def orchestrate
|
25
|
+
#Powershell kind of sucks with syntax checking.
|
26
|
+
#If any part of the script syntactically fails it will exit with a clean exit code of 0
|
27
|
+
#So this forced our hand to call powershell twice for every script call.
|
28
|
+
#1 - Put passed in script in a function to see if any standard error is generated -
|
29
|
+
#only will happen if syntax is incorrect since function is never called
|
30
|
+
#2 - Run the user script with exception wraps to guarantee some exit status
|
31
|
+
syntax_check()
|
32
|
+
|
33
|
+
myscriptstring = finalscript()
|
34
|
+
Chef::Log::debug("Script Contents:")
|
35
|
+
Chef::Log::debug("-----------------------------------------------------")
|
36
|
+
Chef::Log::debug(myscriptstring)
|
37
|
+
Chef::Log::debug("-----------------------------------------------------")
|
38
|
+
return run_command(shell,flags,filename,file_extension, myscriptstring, @options, @validate)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
EXIT_STATUS_EXCEPTION_HANDLER= <<-EOF
|
44
|
+
trap [Exception]{
|
45
|
+
write-error -exception ($_.Exception.Message) -erroraction continue;
|
46
|
+
exit 1
|
47
|
+
}
|
48
|
+
EOF
|
49
|
+
|
50
|
+
EXIT_STATUS_RESET_SCRIPT= <<-EOF
|
51
|
+
$LASTEXITCODE=0
|
52
|
+
EOF
|
53
|
+
|
54
|
+
#Make this set-variable call a read only. Technically can be overridden but most of PS scripts that set this setting,
|
55
|
+
#usually try to set this by doing things like $ERRORACTIONPREFERENCE="Stop"
|
56
|
+
#Generally speaking this is the best way to handle this situation. We will need to determine if there are many dependent scripts
|
57
|
+
#that depend on being able to set this setting.
|
58
|
+
EXIT_STATUS_INTIALIZATION= <<-EOF
|
59
|
+
Set-Variable -Name ERRORACTIONPREFERENCE -Value "STOP" -Scope Script -Option "ReadOnly" -force
|
60
|
+
\#$ErrorActionPreference="STOP"
|
61
|
+
try{
|
62
|
+
EOF
|
63
|
+
|
64
|
+
EXIT_STATUS_POST= <<-EOF
|
65
|
+
exit $LASTEXITCODE
|
66
|
+
}
|
67
|
+
catch{
|
68
|
+
write-error -exception ($_.Exception) -erroraction continue;
|
69
|
+
exit 1
|
70
|
+
}
|
71
|
+
EOF
|
72
|
+
|
73
|
+
#Validate valid powershell was passed in.
|
74
|
+
def syntax_check
|
75
|
+
local_script_contents=("function testme(){ \n #{@originalScript.to_s} \n} \n")
|
76
|
+
|
77
|
+
result = run_command(shell,flags,filename,file_extension, local_script_contents, @options, false)
|
78
|
+
#puts result.inspect
|
79
|
+
if(result.stderr.length >= 1 || result.exitstatus !=0)
|
80
|
+
#puts "Standard Output: \n #{result.stdout}"
|
81
|
+
#puts "Standard Error: \n #{result.stderr}"
|
82
|
+
raise RuntimeError,"Did not syntactically pass the powershell check. \n Standard out: #{result.stdout} \n Standard Error:#{result.stderr}"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def finalscript
|
87
|
+
changed_script =
|
88
|
+
EXIT_STATUS_EXCEPTION_HANDLER +
|
89
|
+
EXIT_STATUS_RESET_SCRIPT +
|
90
|
+
EXIT_STATUS_INTIALIZATION +
|
91
|
+
@originalScript +
|
92
|
+
EXIT_STATUS_POST
|
93
|
+
|
94
|
+
return changed_script
|
95
|
+
end
|
96
|
+
|
97
|
+
def flags
|
98
|
+
flags = [
|
99
|
+
# Hides the copyright banner at startup.
|
100
|
+
"-NoLogo",
|
101
|
+
# Does not present an interactive prompt to the user.
|
102
|
+
"-NonInteractive",
|
103
|
+
# Does not load the Windows PowerShell profile.
|
104
|
+
"-NoProfile",
|
105
|
+
# always set the ExecutionPolicy flag
|
106
|
+
# see http://technet.microsoft.com/en-us/library/ee176961.aspx
|
107
|
+
"-ExecutionPolicy RemoteSigned",
|
108
|
+
# Powershell will hang if STDIN is redirected
|
109
|
+
# http://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected
|
110
|
+
"-InputFormat None",
|
111
|
+
|
112
|
+
"-File"
|
113
|
+
]
|
114
|
+
return @flagoverrides if @flagoverrides != nil
|
115
|
+
return flags.join(' ')
|
116
|
+
end
|
117
|
+
|
118
|
+
def shell
|
119
|
+
return "powershell.exe"
|
120
|
+
end
|
121
|
+
|
122
|
+
def filename
|
123
|
+
return "tempPSchef"
|
124
|
+
end
|
125
|
+
|
126
|
+
def file_extension
|
127
|
+
return ".ps1"
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
134
|
end
|