build_env_3.ps1 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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-2017 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/10/2017
  21. #==============================================================================
  22. # Load parameters
  23. param(
  24. [switch]$Silent,
  25. [switch]$NoPipDependencies
  26. )
  27. #==============================================================================
  28. # Get the Directory of actual script
  29. #==============================================================================
  30. $script_path = dir "$($myInvocation.MyCommand.Definition)"
  31. $script_path = $script_path.DirectoryName
  32. #==============================================================================
  33. # Get the name of actual script
  34. #==============================================================================
  35. $script_name = $MyInvocation.MyCommand.Name
  36. Write-Output "================================================================="
  37. Write-Output ""
  38. Write-Output " Development Environment Installation"
  39. Write-Output ""
  40. Write-Output " - Installs All Salt Dependencies"
  41. Write-Output " - Detects 32/64 bit Architectures"
  42. Write-Output ""
  43. Write-Output " To run silently add -Silent"
  44. Write-Output " eg: ${script_name} -Silent"
  45. Write-Output ""
  46. Write-Output " To run skip installing pip dependencies add -NoPipDependencies"
  47. Write-Output " eg: ${script_name} -NoPipDependencies"
  48. Write-Output ""
  49. Write-Output "================================================================="
  50. Write-Output ""
  51. #==============================================================================
  52. # Import Modules
  53. #==============================================================================
  54. Import-Module $script_path\Modules\download-module.psm1
  55. Import-Module $script_path\Modules\get-settings.psm1
  56. Import-Module $script_path\Modules\uac-module.psm1
  57. Import-Module $script_path\Modules\zip-module.psm1
  58. Import-Module $script_path\Modules\start-process-and-test-exitcode.psm1
  59. #==============================================================================
  60. # Check for Elevated Privileges
  61. #==============================================================================
  62. If (!(Get-IsAdministrator)) {
  63. If (Get-IsUacEnabled) {
  64. # We are not running "as Administrator" - so relaunch as administrator
  65. # Create a new process object that starts PowerShell
  66. $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
  67. # Specify the current script path and name as a parameter
  68. $newProcess.Arguments = $myInvocation.MyCommand.Definition
  69. # Specify the current working directory
  70. $newProcess.WorkingDirectory = "$script_path"
  71. # Indicate that the process should be elevated
  72. $newProcess.Verb = "runas";
  73. # Start the new process
  74. [System.Diagnostics.Process]::Start($newProcess);
  75. # Exit from the current, unelevated, process
  76. Exit
  77. } Else {
  78. Throw "You must be administrator to run this script"
  79. }
  80. }
  81. #------------------------------------------------------------------------------
  82. # Load Settings
  83. #------------------------------------------------------------------------------
  84. $ini = Get-Settings
  85. #------------------------------------------------------------------------------
  86. # Create Directories
  87. #------------------------------------------------------------------------------
  88. $p = New-Item $ini['Settings']['DownloadDir'] -ItemType Directory -Force
  89. $p = New-Item "$($ini['Settings']['DownloadDir'])\64" -ItemType Directory -Force
  90. $p = New-Item "$($ini['Settings']['DownloadDir'])\32" -ItemType Directory -Force
  91. $p = New-Item $ini['Settings']['SaltDir'] -ItemType Directory -Force
  92. #------------------------------------------------------------------------------
  93. # Determine Architecture (32 or 64 bit) and assign variables
  94. #------------------------------------------------------------------------------
  95. If ([System.IntPtr]::Size -ne 4) {
  96. Write-Output "Detected 64bit Architecture..."
  97. $bitDLLs = "64bitDLLs"
  98. $bitPaths = "64bitPaths"
  99. $bitPrograms = "64bitPrograms"
  100. $bitFolder = "64"
  101. } Else {
  102. Write-Output "Detected 32bit Architecture"
  103. $bitDLLs = "32bitDLLs"
  104. $bitPaths = "32bitPaths"
  105. $bitPrograms = "32bitPrograms"
  106. $bitFolder = "32"
  107. }
  108. #------------------------------------------------------------------------------
  109. # Check for installation of NSIS
  110. #------------------------------------------------------------------------------
  111. Write-Output " - Checking for NSIS installation . . ."
  112. If (Test-Path "$($ini[$bitPaths]['NSISDir'])\NSIS.exe") {
  113. # Found NSIS, do nothing
  114. Write-Output " - NSIS Found . . ."
  115. } Else {
  116. # NSIS not found, install
  117. Write-Output " - NSIS Not Found . . ."
  118. Write-Output " - Downloading $($ini['Prerequisites']['NSIS']) . . ."
  119. $file = "$($ini['Prerequisites']['NSIS'])"
  120. $url = "$($ini['Settings']['SaltRepo'])/$file"
  121. $file = "$($ini['Settings']['DownloadDir'])\$file"
  122. DownloadFileWithProgress $url $file
  123. # Install NSIS
  124. Write-Output " - Installing $($ini['Prerequisites']['NSIS']) . . ."
  125. $file = "$($ini['Settings']['DownloadDir'])\$($ini['Prerequisites']['NSIS'])"
  126. $p = Start-Process $file -ArgumentList '/S' -Wait -NoNewWindow -PassThru
  127. }
  128. #------------------------------------------------------------------------------
  129. # Check for installation of Microsoft Visual C++ Build Tools
  130. #------------------------------------------------------------------------------
  131. Write-Output " - Checking for Microsoft Visual C++ Build Tools installation . . ."
  132. If (Test-Path "$($ini[$bitPaths]['VCppBuildToolsDir'])\vcbuildtools.bat") {
  133. # Found Microsoft Visual C++ Build Tools, do nothing
  134. Write-Output " - Microsoft Visual C++ Build Tools Found . . ."
  135. } Else {
  136. # Microsoft Visual C++ Build Tools not found, install
  137. Write-Output " - Microsoft Visual C++ Build Tools Not Found . . ."
  138. Write-Output " - Downloading $($ini['Prerequisites']['VCppBuildTools']) . . ."
  139. $file = "$($ini['Prerequisites']['VCppBuildTools'])"
  140. $url = "$($ini['Settings']['SaltRepo'])/$file"
  141. $file = "$($ini['Settings']['DownloadDir'])\$file"
  142. DownloadFileWithProgress $url $file
  143. # Install Microsoft Visual C++ Build Tools
  144. Write-Output " - Installing $($ini['Prerequisites']['VCppBuildTools']) . . ."
  145. $file = "$($ini['Settings']['DownloadDir'])\$($ini['Prerequisites']['VCppBuildTools'])"
  146. $p = Start-Process $file -ArgumentList '/Quiet' -Wait -NoNewWindow -PassThru
  147. }
  148. #------------------------------------------------------------------------------
  149. # Install Python
  150. #------------------------------------------------------------------------------
  151. Write-Output " - Checking for Python 3.5 installation . . ."
  152. If (Test-Path "$($ini['Settings']['Python3Dir'])\python.exe") {
  153. # Found Python 3.5, do nothing
  154. Write-Output " - Python 3.5 Found . . ."
  155. } Else {
  156. Write-Output " - Downloading $($ini[$bitPrograms]['Python3']) . . ."
  157. $file = "$($ini[$bitPrograms]['Python3'])"
  158. $url = "$($ini['Settings']['SaltRepo'])/$bitFolder/$file"
  159. $file = "$($ini['Settings']['DownloadDir'])\$bitFolder\$file"
  160. DownloadFileWithProgress $url $file
  161. Write-Output " - $script_name :: Installing $($ini[$bitPrograms]['Python3']) . . ."
  162. $p = Start-Process $file -ArgumentList "/Quiet InstallAllUsers=1 TargetDir=`"$($ini['Settings']['Python3Dir'])`" Include_doc=0 Include_tcltk=0 Include_test=0 Include_launcher=1 PrependPath=1 Shortcuts=0" -Wait -NoNewWindow -PassThru
  163. }
  164. #------------------------------------------------------------------------------
  165. # Update Environment Variables
  166. #------------------------------------------------------------------------------
  167. Write-Output " - Updating Environment Variables . . ."
  168. $Path = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path
  169. If (!($Path.ToLower().Contains("$($ini['Settings']['Scripts3Dir'])".ToLower()))) {
  170. $newPath = "$($ini['Settings']['Scripts3Dir']);$Path"
  171. Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath
  172. $env:Path = $newPath
  173. }
  174. #==============================================================================
  175. # Update PIP and SetupTools
  176. #==============================================================================
  177. Write-Output " ----------------------------------------------------------------"
  178. Write-Output " - $script_name :: Updating PIP and SetupTools . . ."
  179. Write-Output " ----------------------------------------------------------------"
  180. Start_Process_and_test_exitcode "cmd" "/c $($ini['Settings']['Python3Dir'])\python.exe -m pip --disable-pip-version-check --no-cache-dir install -r $($script_path)\req_pip.txt" "python pip"
  181. #==============================================================================
  182. # Install windows specific pypi resources using pip
  183. #==============================================================================
  184. Write-Output " ----------------------------------------------------------------"
  185. Write-Output " - $script_name :: Installing windows specific pypi resources using pip . . ."
  186. Write-Output " ----------------------------------------------------------------"
  187. Start_Process_and_test_exitcode "cmd" "/c $($ini['Settings']['Python3Dir'])\python.exe -m pip --disable-pip-version-check --no-cache-dir install -r $($script_path)\req_win.txt" "pip install"
  188. #==============================================================================
  189. # Install pypi resources using pip
  190. #==============================================================================
  191. If ($NoPipDependencies -eq $false) {
  192. Write-Output " ----------------------------------------------------------------"
  193. Write-Output " - $script_name :: Installing pypi resources using pip . . ."
  194. Write-Output " ----------------------------------------------------------------"
  195. Start_Process_and_test_exitcode "cmd" "/c $($ini['Settings']['Python3Dir'])\python.exe -m pip --disable-pip-version-check --no-cache-dir install -r $($script_path)\req.txt" "pip install"
  196. }
  197. #==============================================================================
  198. # Cleaning Up PyWin32
  199. #==============================================================================
  200. Write-Output " ----------------------------------------------------------------"
  201. Write-Output " - $script_name :: Cleaning Up PyWin32 . . ."
  202. Write-Output " ----------------------------------------------------------------"
  203. # Move DLL's to Python Root
  204. # The dlls have to be in Python directory and the site-packages\win32 directory
  205. Write-Output " - $script_name :: Moving PyWin32 DLLs . . ."
  206. Copy-Item "$($ini['Settings']['SitePkgs3Dir'])\pywin32_system32\*.dll" "$($ini['Settings']['Python3Dir'])" -Force
  207. Move-Item "$($ini['Settings']['SitePkgs3Dir'])\pywin32_system32\*.dll" "$($ini['Settings']['SitePkgs3Dir'])\win32" -Force
  208. # Create gen_py directory
  209. Write-Output " - $script_name :: Creating gen_py Directory . . ."
  210. New-Item -Path "$($ini['Settings']['SitePkgs3Dir'])\win32com\gen_py" -ItemType Directory -Force | Out-Null
  211. # Remove pywin32_system32 directory
  212. Write-Output " - $script_name :: Removing pywin32_system32 Directory . . ."
  213. Remove-Item "$($ini['Settings']['SitePkgs3Dir'])\pywin32_system32"
  214. # Remove pythonwin directory
  215. Write-Output " - $script_name :: Removing pythonwin Directory . . ."
  216. Remove-Item "$($ini['Settings']['SitePkgs3Dir'])\pythonwin" -Force -Recurse
  217. # Remove PyWin32 PostInstall and testall Scripts
  218. Write-Output " - $script_name :: Removing PyWin32 scripts . . ."
  219. Remove-Item "$($ini['Settings']['Scripts3Dir'])\pywin32_*" -Force -Recurse
  220. #==============================================================================
  221. # Fix PyCrypto
  222. #==============================================================================
  223. If ($NoPipDependencies -eq $false) {
  224. Write-Output " ----------------------------------------------------------------"
  225. Write-Output " - $script_name :: Fixing PyCrypto . . ."
  226. Write-Output " ----------------------------------------------------------------"
  227. $nt_file = "$($ini['Settings']['Python3Dir'])\Lib\site-packages\Crypto\Random\OSRNG\nt.py"
  228. (Get-Content $nt_file) | Foreach-Object {$_ -replace '^import winrandom$', 'from Crypto.Random.OSRNG import winrandom'} | Set-Content $nt_file
  229. }
  230. #==============================================================================
  231. # Copy DLLs to Python Directory
  232. #==============================================================================
  233. Write-Output " ----------------------------------------------------------------"
  234. Write-Output " - $script_name :: Copying DLLs . . ."
  235. Write-Output " ----------------------------------------------------------------"
  236. # Architecture Specific DLL's
  237. ForEach($key in $ini[$bitDLLs].Keys) {
  238. Write-Output " - $key . . ."
  239. $file = "$($ini[$bitDLLs][$key])"
  240. $url = "$($ini['Settings']['SaltRepo'])/$bitFolder/$file"
  241. $file = "$($ini['Settings']['DownloadDir'])\$bitFolder\$file"
  242. DownloadFileWithProgress $url $file
  243. Copy-Item $file -destination $($ini['Settings']['Python3Dir'])
  244. }
  245. #------------------------------------------------------------------------------
  246. # Script complete
  247. #------------------------------------------------------------------------------
  248. Write-Output "================================================================="
  249. Write-Output " $script_name :: Salt Stack Dev Environment Script Complete"
  250. Write-Output "================================================================="
  251. Write-Output ""
  252. If (-Not $Silent) {
  253. Write-Output "Press any key to continue ..."
  254. $p = $HOST.UI.RawUI.Flushinputbuffer()
  255. $p = $HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
  256. }
  257. #------------------------------------------------------------------------------
  258. # Remove the temporary download directory
  259. #------------------------------------------------------------------------------
  260. Write-Output " ----------------------------------------------------------------"
  261. Write-Output " - $script_name :: Cleaning up downloaded files"
  262. Write-Output " ----------------------------------------------------------------"
  263. Write-Output ""
  264. Remove-Item $($ini['Settings']['DownloadDir']) -Force -Recurse