-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAZCOPYDSCDir.psm1
135 lines (114 loc) · 4.44 KB
/
AZCOPYDSCDir.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
# Docs https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-configure
# Docs https://docs.microsoft.com/en-us/azure/storage/common/storage-ref-azcopy?toc=/azure/storage/blobs/toc.json
# Docs https://docs.microsoft.com/en-us/azure/storage/common/storage-ref-azcopy-list
# Docs
# Defines the values for the resource's Ensure property.
enum Ensure
{
# The resource must be absent.
Absent
# The resource must be present.
Present
}
# [DscResource()] indicates the class is a DSC resource.
[DscResource()]
class AZCOPYDSCDir
{
# Can be local File or remote blob
# Can be local directory or remote blob container
[DscProperty(Key)]
[string]$SourcePath
# can be remote blob or local file
# can be remote blob container or local file directory
[DscProperty(Key)]
[string]$DestinationPath
# Should have 'Storage Blob Data Contributor' or 'Storage Blob Data Reader'
[DscProperty(Mandatory)]
[string]$ManagedIdentityClientID
[DscProperty(Mandatory)]
[string]$LogDir
# Mandatory indicates the property is required and DSC will guarantee it is set.
[DscProperty()]
[Ensure]$Ensure = [Ensure]::Present
# Tests if the resource is in the desired state.
# maybe able to enhance the performance if azcopy sync had a log only option
# https://github.com/Azure/azure-storage-azcopy/issues/1354
[bool] Test()
{
# echo azcopy path
$azcopy = "$PSScriptRoot\utilities\azcopy_windows_amd64_10.9.0\azcopy.exe"
$env:AZCOPY_LOG_LOCATION = $this.LogDir
# # need to write logs in order to track changes, will run as system, so cannot use the default path.
if (! (Test-Path -Path $this.LogDir))
{
try
{
mkdir $this.LogDir -Verbose -Force -ErrorAction stop
}
catch
{
$_
}
}
if (! (Test-Path -Path $this.DestinationPath))
{
try
{
mkdir $this.DestinationPath -Verbose -Force -ErrorAction stop
}
catch
{
$_
}
}
# Read source information via list command
& $azcopy login --identity --identity-client-id $this.ManagedIdentityClientID
$Files = & $azcopy list $this.SourcePath --machine-readable
$Source = $Files | ForEach-Object {
$null = $_ -match '(?<pre>; Content Length:) (?<length>.+)'
if ($matches)
{
$matches.length
$matches.Clear()
}
} | Measure-Object -Sum
[long]$SourceFilesBytes = $Source.Sum
[long]$SourceFilesCount = $Source.Count
# # Read destination information
$DestinationFiles = Get-ChildItem -Path $this.DestinationPath -Recurse -File | Measure-Object -Property length -Sum
[long]$DestinationFilesCount = $DestinationFiles | ForEach-Object Count
[long]$DestinationFilesBytes = $DestinationFiles | ForEach-Object Sum
Write-Verbose -Message "Source has ------> [$($SourceFilesBytes)] Bytes files"
Write-Verbose -Message "Destination has -> [$($DestinationFilesBytes)] Bytes files"
Write-Verbose -Message "Source has ------> [$($SourceFilesCount)] files"
Write-Verbose -Message "Destination has -> [$($DestinationFilesCount)] files"
return ($SourceFilesCount -le $DestinationFilesCount -and ($SourceFilesBytes) -le ($DestinationFilesBytes))
}
# Sets the desired state of the resource.
[void] Set()
{
# echo azcopy path
$azcopy = "$PSScriptRoot\utilities\azcopy_windows_amd64_10.9.0\azcopy.exe"
$env:AZCOPY_LOG_LOCATION = $this.LogDir
# need to write logs in order to track changes, will run as system, so cannot use the default path.
if (! (Test-Path -Path $this.LogDir))
{
try
{
mkdir $this.LogDir -Verbose -Force -ErrorAction stop
}
catch
{
$_
}
}
& $azcopy login --identity --identity-client-id $this.ManagedIdentityClientID
& $azcopy sync $this.SourcePath $this.DestinationPath --recursive=true
}
# Gets the resource's current state.
[AZCOPYDSCDir] Get()
{
# Return this instance or construct a new instance.
return $this
}
}