-
Notifications
You must be signed in to change notification settings - Fork 165
/
Deploy-SonarQubeAzureAppService.ps1
112 lines (96 loc) · 4.29 KB
/
Deploy-SonarQubeAzureAppService.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
param(
[string]$ApplicationInsightsApiKey = $Env:Deployment_Telemetry_Instrumentation_Key,
[string]$Edition = $Env:SonarQubeEdition,
[string]$Version = $Env:SonarQubeVersion
)
function TrackTimedEvent {
param (
[string]$InstrumentationKey,
[string]$EventName,
[scriptblock]$ScriptBlock,
[Object[]]$ScriptBlockArguments
)
[System.Diagnostics.Stopwatch]$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $ScriptBlockArguments
$stopwatch.Stop()
if ($InstrumentationKey) {
$uniqueId = ''
if ($Env:WEBSITE_INSTANCE_ID) {
$uniqueId = $Env:WEBSITE_INSTANCE_ID.substring(5, 15)
}
$properties = @{
"Location" = $Env:REGION_NAME;
"SKU" = $Env:WEBSITE_SKU;
"Processor Count" = $Env:NUMBER_OF_PROCESSORS;
"Always On" = $Env:WEBSITE_SCM_ALWAYS_ON_ENABLED;
"UID" = $uniqueId
}
$measurements = @{
'duration (ms)' = $stopwatch.ElapsedMilliseconds
}
$body = ConvertTo-Json -Depth 5 -InputObject @{
name = "Microsoft.ApplicationInsights.Dev.$InstrumentationKey.Event";
time = [Datetime]::UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
iKey = $InstrumentationKey;
data = @{
baseType = "EventData";
baseData = @{
ver = 2;
name = $EventName;
properties = $properties;
measurements = $measurements;
}
};
}
Invoke-RestMethod -Method POST -Uri "https://dc.services.visualstudio.com/v2/track" -ContentType "application/json" -Body $body | out-null
}
}
TrackTimedEvent -InstrumentationKey $ApplicationInsightsApiKey -EventName 'Download And Extract Binaries' -ScriptBlock {
Write-Output 'Copy wwwroot folder'
xcopy wwwroot ..\wwwroot /YI
Write-Output 'Setting Security to TLS 1.2'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Write-Output 'Prevent the progress meter from trying to access the console'
$global:progressPreference = 'SilentlyContinue'
if (!$Version -or ($Version -ieq 'Latest')) {
# binaries.sonarsource.com moved to S3 and is not easily searchable anymore. Getting the latest version from GitHub releases.
$releasesFromApi = (Invoke-WebRequest -Uri 'https://api.github.com/repos/SonarSource/sonarqube/releases' -UseBasicParsing).Content
$releasesPS = $releasesFromApi | ConvertFrom-Json
$Version = $releasesPS.Name | ForEach-Object { if ($_) { [version]$_ } } | Sort-Object -Descending | Select-Object -First 1
Write-Output "Found the latest release to be $Version"
}
if (!$Edition) {
$Edition = 'Community'
}
$downloadFolder = 'Distribution/sonarqube' # Community Edition
$fileNamePrefix = 'sonarqube' # Community Edition
switch ($Edition) {
'Developer' {
$downloadFolder = 'CommercialDistribution/sonarqube-developer'
$fileNamePrefix = 'sonarqube-developer'
}
'Enterprise' {
$downloadFolder = 'CommercialDistribution/sonarqube-enterprise'
$fileNamePrefix = 'sonarqube-enterprise'
}
'Data Center' {
$downloadFolder = 'CommercialDistribution/sonarqube-datacenter'
$fileNamePrefix = 'sonarqube-datacenter'
}
}
$fileName = "$fileNamePrefix-$Version.zip"
$downloadUri = "https://binaries.sonarsource.com/$downloadFolder/$fileName"
if (!$downloadUri -or !$fileName) {
throw 'Could not get download uri or filename.'
}
Write-Output "Downloading '$downloadUri'"
$outputFile = "..\wwwroot\$fileName"
Invoke-WebRequest -Uri $downloadUri -OutFile $outputFile -UseBasicParsing
Write-Output 'Done downloading file'
TrackTimedEvent -InstrumentationKey $ApplicationInsightsApiKey -EventName 'Extract Binaries' -ScriptBlockArguments $outputFile -ScriptBlock {
param([string]$outputFile)
Write-Output 'Extracting zip'
Expand-Archive -Path $outputFile -DestinationPath '..\wwwroot' -Force
Write-Output 'Extraction complete'
}
}