PageRenderTime 1258ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/chef/resource/powershell_script.rb

https://github.com/cread/chef
Ruby | 95 lines | 51 code | 9 blank | 35 comment | 4 complexity | ce851c8a9e1754fe7e3539e40fb2b585 MD5 | raw file
  1. #
  2. # Author:: Adam Edwards (<adamed@chef.io>)
  3. # Copyright:: Copyright 2013-2016, Chef Software Inc.
  4. # License:: Apache License, Version 2.0
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. require "chef/resource/windows_script"
  19. class Chef
  20. class Resource
  21. class PowershellScript < Chef::Resource::WindowsScript
  22. provides :powershell_script, os: "windows"
  23. property :flags, String,
  24. description: "A string that is passed to the Windows PowerShell command",
  25. default: lazy { default_flags },
  26. coerce: proc { |input|
  27. if input == default_flags
  28. # Means there was no input provided,
  29. # and should use defaults in this case
  30. input
  31. else
  32. # The last occurance of a flag would override its
  33. # previous one at the time of command execution.
  34. [default_flags, input].join(" ")
  35. end
  36. }
  37. description "Use the powershell_script resource to execute a script using the Windows PowerShell"\
  38. " interpreter, much like how the script and script-based resources—bash, csh, perl, python,"\
  39. " and ruby—are used. The powershell_script is specific to the Microsoft Windows platform"\
  40. " and the Windows PowerShell interpreter.\n\n The powershell_script resource creates and"\
  41. " executes a temporary file (similar to how the script resource behaves), rather than running"\
  42. " the command inline. Commands that are executed with this resource are (by their nature) not"\
  43. " idempotent, as they are typically unique to the environment in which they are run. Use not_if"\
  44. " and only_if to guard this resource for idempotence."
  45. def initialize(name, run_context = nil)
  46. super(name, run_context, :powershell_script, "powershell.exe")
  47. @convert_boolean_return = false
  48. end
  49. def convert_boolean_return(arg = nil)
  50. set_or_return(
  51. :convert_boolean_return,
  52. arg,
  53. kind_of: [ FalseClass, TrueClass ]
  54. )
  55. end
  56. # Allow callers evaluating guards to request default
  57. # attribute values. This is needed to allow
  58. # convert_boolean_return to be true in guard context by default,
  59. # and false by default otherwise. When this mode becomes the
  60. # default for this resource, this method can be removed since
  61. # guard context and recipe resource context will have the
  62. # same behavior.
  63. def self.get_default_attributes(opts)
  64. { convert_boolean_return: true }
  65. end
  66. # Options that will be passed to Windows PowerShell command
  67. def default_flags
  68. return "" if Chef::Platform.windows_nano_server?
  69. # Execution policy 'Bypass' is preferable since it doesn't require
  70. # user input confirmation for files such as PowerShell modules
  71. # downloaded from the Internet. However, 'Bypass' is not supported
  72. # prior to PowerShell 3.0, so the fallback is 'Unrestricted'
  73. execution_policy = Chef::Platform.supports_powershell_execution_bypass?(run_context.node) ? "Bypass" : "Unrestricted"
  74. [
  75. "-NoLogo",
  76. "-NonInteractive",
  77. "-NoProfile",
  78. "-ExecutionPolicy #{execution_policy}",
  79. # PowerShell will hang if STDIN is redirected
  80. # http://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected
  81. "-InputFormat None",
  82. ].join(" ")
  83. end
  84. end
  85. end
  86. end