1
0

build_env.ps1 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 NSIS NxS Unzip Plug-in
  130. #------------------------------------------------------------------------------
  131. Write-Output " - Checking for NSIS NxS Unzip (ansi) Plug-in installation . . ."
  132. If (Test-Path "$( $ini[$bitPaths]['NSISPluginsDirA'] )\nsisunz.dll") {
  133. # Found NSIS NxS Unzip Plug-in, do nothing
  134. Write-Output " - NSIS NxS Unzip Plugin (ansi) Found . . ."
  135. } Else
  136. {
  137. # NSIS NxS Unzip Plug-in (ansi) not found, install
  138. Write-Output " - NSIS NxS Unzip Plugin (ansi) Not Found . . ."
  139. # Ansi Plugin
  140. Write-Output " - Downloading $( $ini['Prerequisites']['NSISPluginUnzipA'] ) . . ."
  141. $file = "$( $ini['Prerequisites']['NSISPluginUnzipA'] )"
  142. $url = "$( $ini['Settings']['SaltRepo'] )/$file"
  143. $file = "$( $ini['Settings']['DownloadDir'] )\$file"
  144. DownloadFileWithProgress $url $file
  145. # Extract Ansi Zip file
  146. Write-Output " - Extracting . . ."
  147. Expand-ZipFile $file $ini['Settings']['DownloadDir']
  148. # Copy dll to plugins directory
  149. Write-Output " - Copying dll to plugins directory . . ."
  150. Move-Item "$( $ini['Settings']['DownloadDir'] )\nsisunz\Release\nsisunz.dll" "$( $ini[$bitPaths]['NSISPluginsDirA'] )\nsisunz.dll" -Force
  151. # Remove temp files
  152. Remove-Item "$( $ini['Settings']['DownloadDir'] )\nsisunz" -Force -Recurse
  153. Remove-Item "$file" -Force
  154. }
  155. Write-Output " - Checking for NSIS NxS Unzip (unicode) Plug-in installation . . ."
  156. If (Test-Path "$( $ini[$bitPaths]['NSISPluginsDirU'] )\nsisunz.dll") {
  157. # Found NSIS NxS Unzip Plug-in (unicode), do nothing
  158. Write-Output " - NSIS NxS Unzip Plugin (unicode) Found . . ."
  159. } Else {
  160. # Unicode Plugin
  161. Write-Output " - Downloading $( $ini['Prerequisites']['NSISPluginUnzipU'] ) . . ."
  162. $file = "$( $ini['Prerequisites']['NSISPluginUnzipU'] )"
  163. $url = "$( $ini['Settings']['SaltRepo'] )/$file"
  164. $file = "$( $ini['Settings']['DownloadDir'] )\$file"
  165. DownloadFileWithProgress $url $file
  166. # Extract Unicode Zip file
  167. Write-Output " - Extracting . . ."
  168. Expand-ZipFile $file $ini['Settings']['DownloadDir']
  169. # Copy dll to plugins directory
  170. Write-Output " - Copying dll to plugins directory . . ."
  171. Move-Item "$( $ini['Settings']['DownloadDir'] )\NSISunzU\Plugin unicode\nsisunz.dll" "$( $ini[$bitPaths]['NSISPluginsDirU'] )\nsisunz.dll" -Force
  172. # Remove temp files
  173. Remove-Item "$( $ini['Settings']['DownloadDir'] )\NSISunzU" -Force -Recurse
  174. Remove-Item "$file" -Force
  175. }
  176. #------------------------------------------------------------------------------
  177. # Check for installation of EnVar Plugin for NSIS
  178. #------------------------------------------------------------------------------
  179. Write-Output " - Checking for EnVar Plugin of NSIS installation . . ."
  180. If ( (Test-Path "$($ini[$bitPaths]['NSISPluginsDirA'])\EnVar.dll") -and (Test-Path "$($ini[$bitPaths]['NSISPluginsDirU'])\EnVar.dll") ) {
  181. # Found EnVar Plugin for NSIS, do nothing
  182. Write-Output " - EnVar Plugin for NSIS Found . . ."
  183. } Else {
  184. # EnVar Plugin for NSIS not found, install
  185. Write-Output " - EnVar Plugin for NSIS Not Found . . ."
  186. Write-Output " - Downloading $($ini['Prerequisites']['NSISPluginEnVar']) . . ."
  187. $file = "$($ini['Prerequisites']['NSISPluginEnVar'])"
  188. $url = "$($ini['Settings']['SaltRepo'])/$file"
  189. $file = "$($ini['Settings']['DownloadDir'])\$file"
  190. DownloadFileWithProgress $url $file
  191. # Extract Zip File
  192. Write-Output " - Extracting . . ."
  193. Expand-ZipFile $file "$($ini['Settings']['DownloadDir'])\nsisenvar"
  194. # Copy dlls to plugins directory (both ANSI and Unicode)
  195. Write-Output " - Copying dlls to plugins directory . . ."
  196. Move-Item "$( $ini['Settings']['DownloadDir'] )\nsisenvar\Plugins\x86-ansi\EnVar.dll" "$( $ini[$bitPaths]['NSISPluginsDirA'] )\EnVar.dll" -Force
  197. Move-Item "$( $ini['Settings']['DownloadDir'] )\nsisenvar\Plugins\x86-unicode\EnVar.dll" "$( $ini[$bitPaths]['NSISPluginsDirU'] )\EnVar.dll" -Force
  198. # Remove temp files
  199. Remove-Item "$( $ini['Settings']['DownloadDir'] )\nsisenvar" -Force -Recurse
  200. Remove-Item "$file" -Force
  201. }
  202. #------------------------------------------------------------------------------
  203. # Check for installation of Microsoft Visual C++ Build Tools
  204. #------------------------------------------------------------------------------
  205. Write-Output " - Checking for Microsoft Visual C++ Build Tools installation . . ."
  206. If (Test-Path "$($ini[$bitPaths]['VCppBuildToolsDir'])\vcbuildtools.bat") {
  207. # Found Microsoft Visual C++ Build Tools, do nothing
  208. Write-Output " - Microsoft Visual C++ Build Tools Found . . ."
  209. } Else {
  210. # Microsoft Visual C++ Build Tools not found, install
  211. Write-Output " - Microsoft Visual C++ Build Tools Not Found . . ."
  212. Write-Output " - Downloading $($ini['Prerequisites']['VCppBuildTools']) . . ."
  213. $file = "$($ini['Prerequisites']['VCppBuildTools'])"
  214. $url = "$($ini['Settings']['SaltRepo'])/$file"
  215. $file = "$($ini['Settings']['DownloadDir'])\$file"
  216. DownloadFileWithProgress $url $file
  217. # Install Microsoft Visual C++ Build Tools
  218. Write-Output " - Installing $($ini['Prerequisites']['VCppBuildTools']) . . ."
  219. $file = "$($ini['Settings']['DownloadDir'])\$($ini['Prerequisites']['VCppBuildTools'])"
  220. $p = Start-Process $file -ArgumentList '/Quiet' -Wait -NoNewWindow -PassThru
  221. }
  222. #------------------------------------------------------------------------------
  223. # Install Python
  224. #------------------------------------------------------------------------------
  225. Write-Output " - Checking for Python 3 installation . . ."
  226. If (Test-Path "$($ini['Settings']['Python3Dir'])\python.exe") {
  227. # Found Python 3, do nothing
  228. Write-Output " - Python 3 Found . . ."
  229. } Else {
  230. Write-Output " - Downloading $($ini[$bitPrograms]['Python3']) . . ."
  231. $file = "$($ini[$bitPrograms]['Python3'])"
  232. $url = "$($ini['Settings']['SaltRepo'])/$bitFolder/$file"
  233. $file = "$($ini['Settings']['DownloadDir'])\$bitFolder\$file"
  234. DownloadFileWithProgress $url $file
  235. Write-Output " - $script_name :: Installing $($ini[$bitPrograms]['Python3']) . . ."
  236. $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
  237. }
  238. #------------------------------------------------------------------------------
  239. # Install VCRedist
  240. #------------------------------------------------------------------------------
  241. If (Test-Path "$($ini[$bitPrograms]['VCRedistReg'])") {
  242. # Found VCRedist 2013, do nothing
  243. Write-Output " - VCRedist 2013 Found . . ."
  244. } Else {
  245. Write-Output " - Downloading $($ini[$bitPrograms]['VCRedist']) . . ."
  246. $file = "$($ini[$bitPrograms]['VCRedist'])"
  247. $url = "$($ini['Settings']['SaltRepo'])/$bitFolder/$file"
  248. $file = "$($ini['Settings']['DownloadDir'])\$bitFolder\$file"
  249. DownloadFileWithProgress $url $file
  250. Write-Output " - $script_name :: Installing $($ini[$bitPrograms]['VCRedist']) . . ."
  251. $p = Start-Process $file -ArgumentList "/install /quiet /norestart" -Wait -NoNewWindow -PassThru
  252. }
  253. #------------------------------------------------------------------------------
  254. # Update Environment Variables
  255. #------------------------------------------------------------------------------
  256. Write-Output " - Updating Environment Variables . . ."
  257. $Path = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path
  258. If (!($Path.ToLower().Contains("$($ini['Settings']['Scripts3Dir'])".ToLower()))) {
  259. $newPath = "$($ini['Settings']['Scripts3Dir']);$Path"
  260. Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath
  261. $env:Path = $newPath
  262. }
  263. #==============================================================================
  264. # Update PIP and SetupTools
  265. #==============================================================================
  266. Write-Output " ----------------------------------------------------------------"
  267. Write-Output " - $script_name :: Updating PIP and SetupTools . . ."
  268. Write-Output " ----------------------------------------------------------------"
  269. Start_Process_and_test_exitcode "cmd" "/c $($ini['Settings']['Python3Dir'])\python.exe -m pip --disable-pip-version-check --no-cache-dir install -U pip `"setuptools<50.0.0`"" "python pip"
  270. #==============================================================================
  271. # Install pypi resources using pip
  272. #==============================================================================
  273. If ($NoPipDependencies -eq $false) {
  274. Write-Output " ----------------------------------------------------------------"
  275. Write-Output " - $script_name :: Installing pypi resources using pip . . ."
  276. Write-Output " ----------------------------------------------------------------"
  277. Start_Process_and_test_exitcode "cmd" "/c $($ini['Settings']['Python3Dir'])\python.exe -m pip --disable-pip-version-check --no-cache-dir install -r $($ini['Settings']['SrcDir'])\requirements\static\pkg\py$($ini['Settings']['PyVerMajor']).$($ini['Settings']['PyVerMinor'])\windows.txt" "pip install"
  278. }
  279. If (Test-Path "$($ini['Settings']['SitePkgs3Dir'])\pywin32_system32" -PathType Container )
  280. {
  281. #==============================================================================
  282. # Cleaning Up PyWin32
  283. #==============================================================================
  284. Write-Output " ----------------------------------------------------------------"
  285. Write-Output " - $script_name :: Cleaning Up PyWin32 . . ."
  286. Write-Output " ----------------------------------------------------------------"
  287. # Move DLL's to Python Root
  288. # The dlls have to be in Python directory and the site-packages\win32 directory
  289. Write-Output " - $script_name :: Moving PyWin32 DLLs . . ."
  290. Copy-Item "$( $ini['Settings']['SitePkgs3Dir'] )\pywin32_system32\*.dll" "$( $ini['Settings']['Python3Dir'] )" -Force
  291. Move-Item "$( $ini['Settings']['SitePkgs3Dir'] )\pywin32_system32\*.dll" "$( $ini['Settings']['SitePkgs3Dir'] )\win32" -Force
  292. # Create gen_py directory
  293. Write-Output " - $script_name :: Creating gen_py Directory . . ."
  294. New-Item -Path "$( $ini['Settings']['SitePkgs3Dir'] )\win32com\gen_py" -ItemType Directory -Force | Out-Null
  295. # Remove pywin32_system32 directory
  296. Write-Output " - $script_name :: Removing pywin32_system32 Directory . . ."
  297. Remove-Item "$( $ini['Settings']['SitePkgs3Dir'] )\pywin32_system32"
  298. # Remove PyWin32 PostInstall and testall Scripts
  299. Write-Output " - $script_name :: Removing PyWin32 scripts . . ."
  300. Remove-Item "$( $ini['Settings']['Scripts3Dir'] )\pywin32_*" -Force -Recurse
  301. }
  302. #==============================================================================
  303. # Copy DLLs to Python Directory
  304. #==============================================================================
  305. Write-Output " ----------------------------------------------------------------"
  306. Write-Output " - $script_name :: Copying DLLs . . ."
  307. Write-Output " ----------------------------------------------------------------"
  308. # Architecture Specific DLL's
  309. ForEach($key in $ini[$bitDLLs].Keys) {
  310. Write-Output " - $key . . ."
  311. $file = "$($ini[$bitDLLs][$key])"
  312. $url = "$($ini['Settings']['SaltRepo'])/$bitFolder/$file"
  313. $file = "$($ini['Settings']['DownloadDir'])\$bitFolder\$file"
  314. DownloadFileWithProgress $url $file
  315. Copy-Item $file -destination $($ini['Settings']['Python3Dir'])
  316. }
  317. #------------------------------------------------------------------------------
  318. # Script complete
  319. #------------------------------------------------------------------------------
  320. Write-Output "================================================================="
  321. Write-Output " $script_name :: Salt Stack Dev Environment Script Complete"
  322. Write-Output "================================================================="
  323. Write-Output ""
  324. If (-Not $Silent) {
  325. Write-Output "Press any key to continue ..."
  326. $p = $HOST.UI.RawUI.Flushinputbuffer()
  327. $p = $HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
  328. }
  329. #------------------------------------------------------------------------------
  330. # Remove the temporary download directory
  331. #------------------------------------------------------------------------------
  332. Write-Output " ----------------------------------------------------------------"
  333. Write-Output " - $script_name :: Cleaning up downloaded files"
  334. Write-Output " ----------------------------------------------------------------"
  335. Write-Output ""
  336. Remove-Item $($ini['Settings']['DownloadDir']) -Force -Recurse