PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/pkg/windows/build_env.ps1

https://gitlab.com/ricardo.hernandez/salt
Powershell | 302 lines | 159 code | 50 blank | 93 comment | 3 complexity | 9d47ad66c9897d3033027a9b984bacc2 MD5 | raw file
  1. #==============================================================================
  2. # You may need to change the execution policy in order to run this script
  3. # Run the following in powershell:
  4. #
  5. # Set-ExecutionPolicy RemoteSigned
  6. #
  7. #==============================================================================
  8. #
  9. # FILE: dev_env.ps1
  10. #
  11. # DESCRIPTION: Development Environment Installation for Windows
  12. #
  13. # BUGS: https://github.com/saltstack/salt-windows-bootstrap/issues
  14. #
  15. # COPYRIGHT: (c) 2012-2015 by the SaltStack Team, see AUTHORS.rst for more
  16. # details.
  17. #
  18. # LICENSE: Apache 2.0
  19. # ORGANIZATION: SaltStack (saltstack.org)
  20. # CREATED: 03/15/2015
  21. #==============================================================================
  22. # Load parameters
  23. param(
  24. [switch]$Silent
  25. )
  26. Clear-Host
  27. Write-Output "================================================================="
  28. Write-Output ""
  29. Write-Output " Development Environment Installation"
  30. Write-Output ""
  31. Write-Output " - Installs All Salt Dependencies"
  32. Write-Output " - Detects 32/64 bit Architectures"
  33. Write-Output ""
  34. Write-Output " To run silently add -Silent"
  35. Write-Output " eg: dev_env.ps1 -Silent"
  36. Write-Output ""
  37. Write-Output "================================================================="
  38. Write-Output ""
  39. #==============================================================================
  40. # Get the Directory of actual script
  41. #==============================================================================
  42. $script_path = dir "$($myInvocation.MyCommand.Definition)"
  43. $script_path = $script_path.DirectoryName
  44. #==============================================================================
  45. # Import Modules
  46. #==============================================================================
  47. Import-Module $script_path\Modules\download-module.psm1
  48. Import-Module $script_path\Modules\get-settings.psm1
  49. Import-Module $script_path\Modules\uac-module.psm1
  50. Import-Module $script_path\Modules\zip-module.psm1
  51. #==============================================================================
  52. # Check for Elevated Privileges
  53. #==============================================================================
  54. If (!(Get-IsAdministrator)) {
  55. If (Get-IsUacEnabled) {
  56. # We are not running "as Administrator" - so relaunch as administrator
  57. # Create a new process object that starts PowerShell
  58. $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
  59. # Specify the current script path and name as a parameter
  60. $newProcess.Arguments = $myInvocation.MyCommand.Definition
  61. # Specify the current working directory
  62. $newProcess.WorkingDirectory = "$script_path"
  63. # Indicate that the process should be elevated
  64. $newProcess.Verb = "runas";
  65. # Start the new process
  66. [System.Diagnostics.Process]::Start($newProcess);
  67. # Exit from the current, unelevated, process
  68. Exit
  69. } Else {
  70. Throw "You must be administrator to run this script"
  71. }
  72. }
  73. #------------------------------------------------------------------------------
  74. # Load Settings
  75. #------------------------------------------------------------------------------
  76. $ini = Get-Settings
  77. #------------------------------------------------------------------------------
  78. # Create Directories
  79. #------------------------------------------------------------------------------
  80. $p = New-Item $ini['Settings']['DownloadDir'] -ItemType Directory -Force
  81. $p = New-Item $ini['Settings']['SaltDir'] -ItemType Directory -Force
  82. #------------------------------------------------------------------------------
  83. # Determine Architecture (32 or 64 bit) and assign variables
  84. #------------------------------------------------------------------------------
  85. If ([System.IntPtr]::Size -ne 4) {
  86. Write-Output "Detected 64bit Architecture..."
  87. $bitDLLs = "64bitDLLs"
  88. $bitPaths = "64bitPaths"
  89. $bitPrograms = "64bitPrograms"
  90. $bitFolder = "64"
  91. } Else {
  92. Write-Output "Detected 32bit Architecture"
  93. $bitDLLs = "32bitDLLs"
  94. $bitPaths = "32bitPaths"
  95. $bitPrograms = "32bitPrograms"
  96. $bitFolder = "32"
  97. }
  98. #------------------------------------------------------------------------------
  99. # Check for installation of NSIS
  100. #------------------------------------------------------------------------------
  101. Write-Output " - Checking for NSIS installation . . ."
  102. If (Test-Path "$($ini[$bitPaths]['NSISDir'])\NSIS.exe") {
  103. # Found NSIS, do nothing
  104. Write-Output " - NSIS Found . . ."
  105. } Else {
  106. # NSIS not found, install
  107. Write-Output " - NSIS Not Found . . ."
  108. Write-Output " - Downloading $($ini['Prerequisites']['NSIS']) . . ."
  109. $file = "$($ini['Prerequisites']['NSIS'])"
  110. $url = "$($ini['Settings']['SaltRepo'])/$file"
  111. $file = "$($ini['Settings']['DownloadDir'])\$file"
  112. DownloadFileWithProgress $url $file
  113. # Install NSIS
  114. Write-Output " - Installing $($ini['Prerequisites']['NSIS']) . . ."
  115. $file = "$($ini['Settings']['DownloadDir'])\$($ini['Prerequisites']['NSIS'])"
  116. $p = Start-Process $file -ArgumentList '/S' -Wait -NoNewWindow -PassThru
  117. }
  118. #------------------------------------------------------------------------------
  119. # Check for installation of Microsoft Visual C++ Compiler for Python 2.7
  120. #------------------------------------------------------------------------------
  121. Write-Output " - Checking for VC Compiler for Python 2.7 installation . . ."
  122. If (Test-Path "$($ini[$bitPaths]['VCforPythonDir'])\vcvarsall.bat") {
  123. # Found Microsoft Visual C++ for Python2.7, do nothing
  124. Write-Output " - Microsoft Visual C++ for Python 2.7 Found . . ."
  125. } Else {
  126. # Microsoft Visual C++ for Python2.7 not found, install
  127. Write-Output " - Microsoft Visual C++ for Python2.7 Not Found . . ."
  128. Write-Output " - Downloading $($ini['Prerequisites']['VCforPython']) . . ."
  129. $file = "$($ini['Prerequisites']['VCforPython'])"
  130. $url = "$($ini['Settings']['SaltRepo'])/$file"
  131. $file = "$($ini['Settings']['DownloadDir'])\$file"
  132. DownloadFileWithProgress $url $file
  133. # Install Microsoft Visual C++ for Python2.7
  134. Write-Output " - Installing $($ini['Prerequisites']['VCforPython']) . . ."
  135. $file = "$($ini['Settings']['DownloadDir'])\$($ini['Prerequisites']['VCforPython'])"
  136. $p = Start-Process msiexec.exe -ArgumentList "/i $file /qb ALLUSERS=1" -Wait -NoNewWindow -PassThru
  137. }
  138. #------------------------------------------------------------------------------
  139. # Install Python
  140. #------------------------------------------------------------------------------
  141. Write-Output " - Checking for Python 2.7 installation . . ."
  142. If (Test-Path "$($ini['Settings']['PythonDir'])\python.exe") {
  143. # Found Python2.7, do nothing
  144. Write-Output " - Python 2.7 Found . . ."
  145. } Else {
  146. Write-Output " - Downloading $($ini[$bitPrograms]['Python']) . . ."
  147. $file = "$($ini[$bitPrograms]['Python'])"
  148. $url = "$($ini['Settings']['SaltRepo'])/$bitFolder/$file"
  149. $file = "$($ini['Settings']['DownloadDir'])\$file"
  150. DownloadFileWithProgress $url $file
  151. Write-Output " - Installing $($ini[$bitPrograms]['Python']) . . ."
  152. $file = "$($ini['Settings']['DownloadDir'])\$($ini[$bitPrograms]['Python'])"
  153. $p = Start-Process msiexec -ArgumentList "/i $file /qb ADDLOCAL=DefaultFeature,Extensions,pip_feature,PrependPath TARGETDIR=$($ini['Settings']['PythonDir'])" -Wait -NoNewWindow -PassThru
  154. }
  155. #------------------------------------------------------------------------------
  156. # Update Environment Variables
  157. #------------------------------------------------------------------------------
  158. Write-Output " - Updating Environment Variables . . ."
  159. $Path = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path
  160. If (!($Path.ToLower().Contains("$($ini['Settings']['ScriptsDir'])".ToLower()))) {
  161. $newPath = "$($ini['Settings']['ScriptsDir']);$Path"
  162. Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath
  163. $env:Path = $newPath
  164. }
  165. #==============================================================================
  166. # Update PIP and SetupTools
  167. #==============================================================================
  168. Write-Output " ----------------------------------------------------------------"
  169. Write-Output " - Updating PIP and SetupTools . . ."
  170. Write-Output " ----------------------------------------------------------------"
  171. $p = Start-Process "$($ini['Settings']['PythonDir'])\python.exe" -ArgumentList "-m pip --no-cache-dir install -r $($script_path)\req_pip.txt" -Wait -NoNewWindow -PassThru
  172. #==============================================================================
  173. # Install pypi resources using pip
  174. #==============================================================================
  175. Write-Output " ----------------------------------------------------------------"
  176. Write-Output " - Installing pypi resources using pip . . ."
  177. Write-Output " ----------------------------------------------------------------"
  178. $p = Start-Process "$($ini['Settings']['ScriptsDir'])\pip.exe" -ArgumentList "--no-cache-dir install -r $($script_path)\req.txt" -Wait -NoNewWindow -PassThru
  179. #==============================================================================
  180. # Install PyYAML with CLoader
  181. # This has to be a compiled binary to get the CLoader
  182. #==============================================================================
  183. Write-Output " ----------------------------------------------------------------"
  184. Write-Output " - Installing PyYAML . . ."
  185. Write-Output " ----------------------------------------------------------------"
  186. # Download
  187. $file = "$($ini[$bitPrograms]['PyYAML'])"
  188. $url = "$($ini['Settings']['SaltRepo'])/$bitFolder/$file"
  189. $file = "$($ini['Settings']['DownloadDir'])\$file"
  190. DownloadFileWithProgress $url $file
  191. # Install
  192. $file = "$($ini['Settings']['DownloadDir'])\$($ini[$bitPrograms]['PyYAML'])"
  193. $p = Start-Process "$($ini['Settings']['ScriptsDir'])\easy_install.exe" -ArgumentList "-Z $file " -Wait -NoNewWindow -PassThru
  194. #==============================================================================
  195. # Install PyCrypto from wheel file
  196. #==============================================================================
  197. Write-Output " ----------------------------------------------------------------"
  198. Write-Output " - Installing PyCrypto . . ."
  199. Write-Output " ----------------------------------------------------------------"
  200. # Download
  201. $file = "$($ini[$bitPrograms]['PyCrypto'])"
  202. $url = "$($ini['Settings']['SaltRepo'])/$bitFolder/$file"
  203. $file = "$($ini['Settings']['DownloadDir'])\$file"
  204. DownloadFileWithProgress $url $file
  205. # Install
  206. $file = "$($ini['Settings']['DownloadDir'])\$($ini[$bitPrograms]['PyCrypto'])"
  207. $p = Start-Process "$($ini['Settings']['ScriptsDir'])\pip.exe" -ArgumentList "install --no-index --find-links=$($ini['Settings']['DownloadDir']) $file " -Wait -NoNewWindow -PassThru
  208. #==============================================================================
  209. # Copy DLLs to Python Directory
  210. #==============================================================================
  211. Write-Output " ----------------------------------------------------------------"
  212. Write-Output " - Copying DLLs . . ."
  213. Write-Output " ----------------------------------------------------------------"
  214. ForEach ($key in $ini['CommonDLLs'].Keys) {
  215. If ($arrInstalled -notcontains $key) {
  216. Write-Output " - $key . . ."
  217. $file = "$($ini['CommonDLLs'][$key])"
  218. $url = "$($ini['Settings']['SaltRepo'])/$file"
  219. $file = "$($ini['Settings']['PythonDir'])\$file"
  220. DownloadFileWithProgress $url $file
  221. }
  222. }
  223. # Architecture Specific DLL's
  224. ForEach($key in $ini[$bitDLLs].Keys) {
  225. If ($arrInstalled -notcontains $key) {
  226. Write-Output " - $key . . ."
  227. $file = "$($ini[$bitDLLs][$key])"
  228. $url = "$($ini['Settings']['SaltRepo'])/$bitFolder/$file"
  229. $file = "$($ini['Settings']['PythonDir'])\$file"
  230. DownloadFileWithProgress $url $file
  231. }
  232. }
  233. #------------------------------------------------------------------------------
  234. # Script complete
  235. #------------------------------------------------------------------------------
  236. Write-Output "================================================================="
  237. Write-Output "Salt Stack Dev Environment Script Complete"
  238. Write-Output "================================================================="
  239. Write-Output ""
  240. If (-Not $Silent) {
  241. Write-Output "Press any key to continue ..."
  242. $p = $HOST.UI.RawUI.Flushinputbuffer()
  243. $p = $HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
  244. }
  245. #------------------------------------------------------------------------------
  246. # Remove the temporary download directory
  247. #------------------------------------------------------------------------------
  248. Write-Output " ----------------------------------------------------------------"
  249. Write-Output " - Cleaning up downloaded files"
  250. Write-Output " ----------------------------------------------------------------"
  251. Write-Output ""
  252. Remove-Item $($ini['Settings']['DownloadDir']) -Force -Recurse