Framework/Core/FixControl/Services/AppServiceFix.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
Set-StrictMode -Version Latest 

class AppServiceFix: FixServicesBase
{       
    [PSObject] $ResourceObject = $null;
    AppServiceFix([string] $subscriptionId, [ResourceConfig] $resourceConfig, [string] $resourceGroupName): 
        Base($subscriptionId, $resourceConfig, $resourceGroupName) 
    { }

    [PSObject] FetchAppServiceObject()
    {
        if(-not $this.ResourceObject)
        {
            $this.ResourceObject = Get-AzWebApp -Name $this.ResourceName -ResourceGroupName $this.ResourceGroupName
        }

        return $this.ResourceObject;
    }

    [MessageData[]] DisableWebSocket([PSObject] $parameters)
    {
        [MessageData[]] $detailedLogs = @();
        
        $detailedLogs += [MessageData]::new("Disabling web socket for app service [$($this.ResourceName)]...");
        $result = Set-AzWebApp `
                    -ResourceGroupName $this.ResourceGroupName `
                    -Name $this.ResourceName `
                    -WebSocketsEnabled $false `
                    -ErrorAction Stop

        $detailedLogs += [MessageData]::new("Web socket has been disabled for app service [$($this.ResourceName)]", $result);
        
        return $detailedLogs;
    }

    [MessageData[]] SetLatestDotNetVersion([PSObject] $parameters)
    {
        [MessageData[]] $detailedLogs = @();
        
        $detailedLogs += [MessageData]::new("Setting up .Net version for app service [$($this.ResourceName)]...");
        $result = Set-AzWebApp `
                    -ResourceGroupName $this.ResourceGroupName `
                    -Name $this.ResourceName `
                    -NetFrameworkVersion $this.ControlSettings.AppService.LatestDotNetFrameworkVersionNumber `
                    -ErrorAction Stop

        $detailedLogs += [MessageData]::new(".Net version has been set to latest version for app service [$($this.ResourceName)]", $result);
        
        return $detailedLogs;
    }

    [MessageData[]] Set64bitPlatform([PSObject] $parameters)
    {
        [MessageData[]] $detailedLogs = @();
        
        $detailedLogs += [MessageData]::new("Setting up platform architecture to 64 bit for app service [$($this.ResourceName)]...");
        $result = Set-AzWebApp `
                    -ResourceGroupName $this.ResourceGroupName `
                    -Name $this.ResourceName `
                    -Use32BitWorkerProcess $false `
                    -ErrorAction Stop

        $detailedLogs += [MessageData]::new("Platform architecture has been set to 64 bit for app service [$($this.ResourceName)]", $result);
        
        return $detailedLogs;
    }

    [MessageData[]] EnableLogging([PSObject] $parameters)
    {
        [MessageData[]] $detailedLogs = @();
        
        $detailedLogs += [MessageData]::new("Setting up logging and request tracing for app service [$($this.ResourceName)]...");
        $result = Set-AzWebApp `
                    -ResourceGroupName $this.ResourceGroupName `
                    -Name $this.ResourceName `
                    -DetailedErrorLoggingEnabled $true `
                    -HttpLoggingEnabled $true `
                    -RequestTracingEnabled $true `
                    -ErrorAction Stop

        $detailedLogs += [MessageData]::new("Logging and request tracing has been enabled for app service [$($this.ResourceName)]", $result);
        
        return $detailedLogs;
    }

    [MessageData[]] SetMultipleInstances([PSObject] $parameters)
    {
        [MessageData[]] $detailedLogs = @();
        $instanceCount = $this.ControlSettings.AppService.Minimum_Instance_Count;
        $detailedLogs += [MessageData]::new("Setting up minimum $instanceCount instances for app service [$($this.ResourceName)]...");

        $appServiceObject = $this.FetchAppServiceObject();
        if($appServiceObject)
        {
            $serverFarm = Get-AzResource -ResourceId $appServiceObject.ServerFarmId

            if($serverFarm)
            {
                if($serverFarm.Sku.Capacity -ge $this.ControlSettings.AppService.Minimum_Instance_Count)
                {
                    $detailedLogs += [MessageData]::new("No action is required as minimum $instanceCount instances have already been set up for app service [$($this.ResourceName)].");
                }
                else
                {
                    $result = Set-AzAppServicePlan `
                                    -Name $serverFarm.Name `
                                    -ResourceGroupName $serverFarm.ResourceGroupName `
                                    -NumberofWorkers $instanceCount `
                                    -ErrorAction Stop
                    $detailedLogs += [MessageData]::new("Minimum instances have been set up for app service [$($this.ResourceName)]", $result);
                }
            }
            else
            {
                $detailedLogs += [MessageData]::new("Not able to fetch server farm with id [$($appServiceObject.ServerFarmId)]");
            }
        }
        else
        {
            $detailedLogs += [MessageData]::new("Unable to fetch app service [$($this.ResourceName)]");
        }
        
        return $detailedLogs;
    }

    [MessageData[]] EnableHttpsFlag([PSObject] $parameters)
    {
        [MessageData[]] $detailedLogs = @();
        
        $detailedLogs += [MessageData]::new("Enabling HTTPS only flag for app service [$($this.ResourceName)]...");
        $result = Set-AzResource -ResourceName $this.ResourceName `
                        -ResourceGroupName $this.ResourceGroupName `
                        -ResourceType 'Microsoft.Web/sites' `
                        -Properties @{httpsOnly='true'} `
                        -Force `
                        -ErrorAction Stop

        $detailedLogs += [MessageData]::new("HTTPS only flag is enabled for app service [$($this.ResourceName)]", $result);
        
        return $detailedLogs;
    }

     
}