/
02_GitClone.ps1
235 lines (217 loc) · 9.26 KB
/
02_GitClone.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#Requires -Version 2.0
<#
Copyright (c) Alya Consulting, 2019-2024
This file is part of the Alya Base Configuration.
https://alyaconsulting.ch/Loesungen/BasisKonfiguration
The Alya Base Configuration is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
Alya Base Configuration is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details: https://www.gnu.org/licenses/gpl-3.0.txt
Diese Datei ist Teil der Alya Basis Konfiguration.
https://alyaconsulting.ch/Loesungen/BasisKonfiguration
Die Alya Basis Konfiguration ist eine Freie Software: Sie können sie unter den
Bedingungen der GNU General Public License, wie von der Free Software
Foundation, Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
veröffentlichten Version, weiter verteilen und/oder modifizieren.
Die Alya Basis Konfiguration wird in der Hoffnung, dass sie nützlich sein wird,
aber OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FUER EINEN BESTIMMTEN ZWECK.
Siehe die GNU General Public License fuer weitere Details:
https://www.gnu.org/licenses/gpl-3.0.txt
History:
Date Author Description
---------- -------------------- ----------------------------
06.11.2019 Konrad Brunner Initial Version
#>
[CmdletBinding()]
Param(
)
# Loading configuration
. $PSScriptRoot\01_ConfigureEnv.ps1
# Starting Transscript
Start-Transcript -Path "$($AlyaLogs)\GitClone-$($AlyaTimeString).log" -IncludeInvocationHeader -Force
Write-Host "Checking git installation" -ForegroundColor $CommandInfo
if (-Not (Test-Path "$AlyaGitRoot"))
{
Write-Host "Downloading git"
$req = Invoke-WebRequestIndep -Uri $AlyaGitDownload -UseBasicParsing -Method Get
[regex]$regex = "[^`"]*windows[^`"]*portable[^`"]*64[^`"]*.exe"
$url = [regex]::Match($req.Content, $regex, [Text.RegularExpressions.RegexOptions]'IgnoreCase, CultureInvariant').Value
$req = Invoke-WebRequestIndep -Uri $url -Method Get -OutFile ".\PortableGit64bit.exe"
Write-Host "Installing git"
cmd /c ".\PortableGit64bit.exe" -o"$AlyaGitRoot" -y
do
{
Start-Sleep -Seconds 5
} while (Get-Process -Name "PortableGit64bit" -ErrorAction SilentlyContinue)
Remove-Item -Path ".\PortableGit64bit.exe" -Force
}
Write-Host "Checking user email" -ForegroundColor $CommandInfo
if ([string]::IsNullOrEmpty($AlyaLocalConfig.user.email))
{
$email = Read-Host -Prompt 'Please specify your email address'
$decision = $Host.UI.PromptForChoice("Confirm your email", "Is '$($email)' correct?", @("&Yes", "&No"), 0)
if ($decision -eq 0) {
$AlyaLocalConfig.user.email = $email
Save-LocalConfig
} else {
Write-Host 'Cancelled'
exit
}
}
Write-Host "Checking ssh keys" -ForegroundColor $CommandInfo
if ([string]::IsNullOrEmpty($AlyaLocalConfig.user.ssh))
{
Write-Host "Generating new ssh key pair"
if (-Not (Test-Path "$AlyaRoot\_local\ssh"))
{
$null = New-Item -Path "$AlyaRoot\_local\ssh" -ItemType Directory -Force
$sshPath = [System.Environment]::ExpandEnvironmentVariables("%USERPROFILE%\.ssh")
if (Test-Path $sshPath)
{
$sshPath += "\id_rsa"
if (Test-Path $sshPath)
{
$decision = $Host.UI.PromptForChoice("SSH Keys", "Use existing SSH keys from your userprofile?", @("&Yes", "&No"), 0)
if ($decision -eq 0)
{
Copy-Item -Path $sshPath -Destination "$($AlyaRoot)\_local\ssh\id_rsa"
Copy-Item -Path "$($sshPath).pub" -Destination "$($AlyaRoot)\_local\ssh\id_rsa.pub"
}
}
}
}
if (-Not (Test-Path "$($AlyaRoot)\_local\ssh\id_rsa"))
{
cmd /c "$AlyaGitRoot\usr\bin\ssh-keygen.exe" -q -t rsa -b 1024 -f "$($AlyaRoot)\_local\ssh\id_rsa" -N "" -C "$($AlyaLocalConfig.user.email)"
if (-Not (Test-Path "$($AlyaRoot)\_local\ssh\id_rsa"))
{
Write-Error "Error generating id_rsa" -ErrorAction Continue
exit 1
}
}
$AlyaLocalConfig.user.ssh = "$AlyaRoot\_local\ssh\id_rsa"
Save-LocalConfig
Write-Host "Please upload the following ssh key to DevOps and hit enter:" -ForegroundColor Red
Get-Content -Path "$($AlyaLocalConfig.user.ssh).pub"
$null = Read-Host -Prompt 'Please press enter'
}
Write-Host "Checking devops url" -ForegroundColor $CommandInfo
if ([string]::IsNullOrEmpty($AlyaGlobalConfig.source.devops))
{
$devopsssh = Read-Host -Prompt 'Please specify devops ssh uri'
$decision = $Host.UI.PromptForChoice("Confirm devops ssh uri", "Is '$($devopsssh)' correct?", @("&Yes", "&No"), 0)
if ($decision -eq 0) {
$AlyaGlobalConfig.source.devops = $devopsssh
Save-GlobalConfig
} else {
Write-Host 'Cancelled'
exit
}
}
Write-Host "Checking .ssh content" -ForegroundColor $CommandInfo
$sshPath = [System.Environment]::ExpandEnvironmentVariables("%USERPROFILE%\.ssh")
if (-Not (Test-Path $sshPath))
{
$null = New-Item -Path $sshPath -ItemType Directory -Force
}
#Checking kex in config
$sshConfigPath = $sshPath + "\config"
$kexMissing = $true
$devopsHost = ([System.Uri]$AlyaGlobalConfig.source.devops).Authority
if (-Not $devopsHost)
{
$devopsHost = $AlyaGlobalConfig.source.devops.Substring($AlyaGlobalConfig.source.devops.IndexOf("@")+1,$AlyaGlobalConfig.source.devops.IndexOf(":")-$AlyaGlobalConfig.source.devops.IndexOf("@")-1)
}
$devopsPort = 22
if ($AlyaGlobalConfig.source.devops.StartsWith("http://")) { $devopsPort = 80 }
if ($AlyaGlobalConfig.source.devops.StartsWith("https://")) { $devopsPort = 443 }
if ((Test-Path $sshConfigPath))
{
$kexCont = Get-Content -Path $sshConfigPath -Raw
if ($kexCont.IndexOf($devopsHost) -gt -1)
{
$kexMissing = $false
}
}
if ($kexMissing)
{
Write-Host "Fixing DevOps Kex issue" -ForegroundColor $CommandInfo
"Host $($devopsHost)`nKexAlgorithms +diffie-hellman-group1-sha1" | Add-Content -Path $sshConfigPath
}
#check keys
$sshPath += "\id_rsa"
if (-Not (Test-Path $sshPath))
{
Copy-Item -Path "$($AlyaLocalConfig.user.ssh)" -Destination $sshPath
Copy-Item -Path "$($AlyaLocalConfig.user.ssh).pub" -Destination "$($sshPath).pub"
}
else
{
if ((Get-FileHash "$($AlyaLocalConfig.user.ssh)").Hash -ne (Get-FileHash $sshPath).Hash)
{
Write-Host "Old key pair found in $sshPath"
$decision = $Host.UI.PromptForChoice("Overwrite?", "Do you want to overwrite the existing file?", @("&Yes", "&No"), 0)
if ($decision -eq 0) {
Copy-Item -Path "$($AlyaLocalConfig.user.ssh)" -Destination $sshPath -Force
Copy-Item -Path "$($AlyaLocalConfig.user.ssh).pub" -Destination "$($sshPath).pub" -Force
} else {
Write-Host "Please backup existing files in $($sshPath) and rerun this script"
exit
}
}
}
Write-Host "Checking connection" -ForegroundColor $CommandInfo #Adding host to known_hosts
$networkTest = Test-NetConnection -ComputerName $devopsHost -Port $devopsPort
if (-Not $networkTest.TcpTestSucceeded)
{
throw "No network connection to $($devopsHost):$($devopsPort)"
}
$proc = New-Object System.Diagnostics.Process
$proc.StartInfo.FileName = "$AlyaGitRoot\usr\bin\ssh.exe"
$proc.StartInfo.Arguments = "-T $devopsHost -o `"StrictHostKeyChecking no`"".Split(" ")
$proc.StartInfo.UseShellExecute = $false
$proc.StartInfo.CreateNoWindow = $true
$proc.Start()
Write-Host "Fetching repository " -ForegroundColor $CommandInfo
Push-Location
$errAct = $ErrorActionPreference
$ErrorActionPreference = 'SilentlyContinue'
try {
Set-Location "$($AlyaRoot)"
Write-Host "connecting actual directory to git repository"
$check = (cmd /c "$AlyaGitRoot\cmd\git.exe" status)
if ($check -like "*On branch *") {
cmd /c "$AlyaGitRoot\cmd\git.exe" status
Write-Host "Repository is already connected!" -ForegroundColor $CommandSuccess
Write-Host "Fetching changes"
cmd /c "$AlyaGitRoot\cmd\git.exe" fetch
Wait-UntilProcessEnds -processName "git"
}
else {
Write-Host "Repository is not yet connected. Connecting..."
cmd /c "$AlyaGitRoot\cmd\git.exe" init
Wait-UntilProcessEnds -processName "git"
Write-Host "To: $($AlyaGlobalConfig.source.devops)"
cmd /c "$AlyaGitRoot\cmd\git.exe" "remote add origin $($AlyaGlobalConfig.source.devops)".Split(" ")
Wait-UntilProcessEnds -processName "git"
Write-Host "Fetching changes"
cmd /c "$AlyaGitRoot\cmd\git.exe" fetch
Wait-UntilProcessEnds -processName "git"
Write-Host "Checking out"
cmd /c "$AlyaGitRoot\cmd\git.exe" "checkout -t origin/master --force".Split(" ")
Wait-UntilProcessEnds -processName "git"
cmd /c "$AlyaGitRoot\cmd\git.exe" status
Write-Host "Repository now connected!" -ForegroundColor $CommandSuccess
}
}
finally {
Pop-Location
$ErrorActionPreference = $errAct
}
# Stopping Transscript
Stop-Transcript