cEPRSDeployAzureWebsite.psm1

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
enum Ensure
{
    Absent
    Present
}
[DscResource()]
Class cEPRSCreateAzureWebsite
{
    [DscProperty(Key)][String]$SubscriptionName

    [DscProperty()][String]$WebsiteName

    [DscProperty()][String][ValidateSet("East US","West US","Central US","South Central US","East US 2","North Europe","West Europe","Southeast Asia","East Asia")]
    $Location

    [DscProperty()][String]$HostName

    [DscProperty()][String]$PublishingUsername

    [DscProperty()][String][ValidateSet('Staging','Production')]$Slot

    [DscProperty()][Ensure]$Ensure

    [DscProperty()][String]$PublishSettingsFile

    [cEPRSCreateAzureWebsite] Get()
    {
        Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)"

        Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"
        Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"

        $GetWebsite = Get-AzureWebsite -Name ($this.WebsiteName) -Slot $($this.Slot) -ErrorAction SilentlyContinue

        $this.WebsiteName = $GetWebsite.Name
        
        return $this
    }
    [bool] Test()
    {
        Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)"

        Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"
        Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"

        $TestWebsite = Get-AzureWebsite -Name ($this.WebsiteName) -Slot $($this.Slot) -ErrorAction SilentlyContinue

        if($this.Ensure -eq [Ensure]::Present)
        {
            if($TestWebsite -eq $null)
            {
                return $false
            }
            else
            {
                return $true
            }
        }
        else
        {
            if($TestWebsite -eq $null)
            {
                return $true
            }
            else
            {
                return $false
            }
        }
    }
    Set()
    {
        Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)"

        Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"
        Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"

        if($this.Ensure -eq [Ensure]::Present)
        {
            Write-Verbose "Creating New Azure Website $($this.WebsiteName) in slot $($this.Slot) with host name $($this.HostName)"
            New-AzureWebsite -Location $($this.Location) -Hostname $($this.HostName) -PublishingUsername $($this.PublishingUsername) -Name $($this.WebsiteName) -Slot $this.Slot
        }
        else
        {
            Write-Verbose "Ensure Set to Absent... Removing the website $($this.WebsiteName)"
            Remove-AzureWebsite -Force -Name $($this.WebsiteName) -Slot $this.Slot
        }
    }
}

[DscResource()]
Class cEPRSDeployAzureWebsite
{
    [DscProperty()][String]
    $PackageFile

    [DscProperty()][String]
    $PublishSettingsFile

    [DscProperty()][String]
    $WebsiteName

    [DscProperty(Key)][String]
    $SubscriptionName

    [DscProperty()][String]
    $Slot

    [cEPRSDeployAzureWebsite] Get()
    {
        Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)"

        Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"
        Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"

        $GetDeployment = Get-AzureWebsiteDeployment -Name $this.WebsiteName -Slot $this.Slot -ErrorAction SilentlyContinue

        return $this
    }
    [bool] Test()
    {
        Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)"

        Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"
        Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"

        $GetDeployment = Get-AzureWebsiteDeployment -Name $this.WebsiteName -Slot $this.Slot -ErrorAction SilentlyContinue

        return $false
    }
    Set()
    {
        Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)"

        Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"
        Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"

        Get-AzureWebsite $this.WebsiteName;
        Publish-AzureWebsiteProject -Name wwtkwebint -Package "$($this.PackageFile)"
    }
}