utils/FileUtils.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
<#
.Synopsis
    Utility function to fetch cluster's hadoop configs based on the cluster type
.DESCRIPTION
   This function takes.
    
.EXAMPLE
    ## To be added.
     
.LINK
    https://github.com/mabushaireh/HDICloner
#>



function Create-FolderIfNotExist($folderName, $path) {

    Show-Debug "Passed value for folderName is $folderName"
    Show-Debug "Passed value for path is $path"

    Show-Info "Creating Folder $path\$folderName"
    if (-Not (Test-Path $path)) {
        Show-Error "Folder $path not found!"
        throw "Folder $path not found!"
    }


    if (Test-Path "$path\$folderName") {
        Show-Warning "Folder $path\$folderName already exits!"
        return
    }
    
    $null = New-Item -Path "$path\$folderName" -ItemType Directory
    Show-Info "Folder $path\$folderName is created"
}

function Get-LastConfigurationFolder($path)
{
    Show-Debug "Get Last Configuration Folder on this path '$path'"
    $MaxDate = Get-Date("1/1/1900")
    Show-Debug "MaxDate is set to: $MaxDate"

    Get-ChildItem -Path $path -Directory `
    | ForEach-Object {
        Show-Debug ("Try to parse Folder name: " + $_.Name + " to DateTime")
        [datetime]::parseexact($_.Name, "yyyyMMdd_hhmmmss", $null )
    } `
    | ForEach-Object { 
        Show-Debug "Evaluating if $_ is greated that $MaxDate"
        if ($_ -gt $MaxDate) { 
            $MaxDate = $_
            Show-Debug "$MaxDate is set to: $_"
        }
    }    

    $lastConfigFolderName = $MaxDate | Get-Date -Format "yyyyMMdd_hhmmmss"

    Show-Info "Last Configuration Folder on this path $path\$lastConfigFolderName"
    return "$path\$lastConfigFolderName"
}


function Get-PathFor {
    [CmdletBinding()]
    param 
    (
        [Parameter(Mandatory = $true)] [string] $SubscriptionId,
        [Parameter(Mandatory = $true)] [string] $ClusterDnsName,
        [Parameter(Mandatory = $true)] [string] 
        [ValidateSet("Base", "ARM", "HDP", "HDP-CONFIG", "HDP-ENV", "HDP-Log4j", "Nodes-HN", "Nodes-WN", "Nodes-ZK")]
        $ConfigArea
    )

    $documentsPath = [Environment]::GetFolderPath("MyDocuments")
    $productBaseFolderName = "HDICloner";

    $clsuterPath = "$documentsPath\$productBaseFolderName\$SubscriptionId\$ClusterDnsName"

    switch -Exact ($ConfigArea) {
        'Base' {
            return "$documentsPath\$productBaseFolderName"
        }
        'ARM' {
            $lastConfigFolder = Get-LastConfigurationFolder $clsuterPath

            return "$lastConfigFolder\ARM"
        }
        'HDP' {
            $lastConfigFolder = Get-LastConfigurationFolder $clsuterPath

            return "$lastConfigFolder\HDP"
        }
        'HDP-CONFIG' {
            $lastConfigFolder = Get-LastConfigurationFolder $clsuterPath

            return "$lastConfigFolder\HDP\CONFIG"
        }
        'HDP-ENV' {
            $lastConfigFolder = Get-LastConfigurationFolder $clsuterPath

            return "$lastConfigFolder\HDP\ENV"
        }
        'HDP-Log4j' {
            $lastConfigFolder = Get-LastConfigurationFolder $clsuterPath

            return "$lastConfigFolder\HDP\Log4j"
        }
        'Nodes-HN' {
            $lastConfigFolder = Get-LastConfigurationFolder $clsuterPath

            return "$lastConfigFolder\Nodes\HN"
        }
        'Nodes-WN' {
            $lastConfigFolder = Get-LastConfigurationFolder $clsuterPath

            return "$lastConfigFolder\Nodes\WN"
        }
        'Nodes-ZK' {
            $lastConfigFolder = Get-LastConfigurationFolder $clsuterPath

            return "$lastConfigFolder\Nodes\ZK"
        }
    }

}