File: file_sync.ps1

package info (click to toggle)
vagrant 2.2.14%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,800 kB
  • sloc: ruby: 97,301; sh: 375; makefile: 16; lisp: 1
file content (123 lines) | stat: -rw-r--r-- 4,185 bytes parent folder | download | duplicates (5)
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
#Requires -Modules VagrantMessages
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Open Technologies, Inc.
# All Rights Reserved. Licensed under the MIT License.
#--------------------------------------------------------------------------

param (
    [parameter (Mandatory=$true)]
    [string]$vm_id,
    [parameter (Mandatory=$true)]
    [string]$guest_ip,
    [parameter (Mandatory=$true)]
    [string]$username,
    [parameter (Mandatory=$true)]
    [string]$password,
    [parameter (Mandatory=$true)]
    [string]$host_path,
    [parameter (Mandatory=$true)]
    [string]$guest_path
)

function Get-file-hash($source_path, $delimiter) {
    $source_files = @()
    (Get-ChildItem $source_path -rec | ForEach-Object -Process {
      Get-FileHash -Path $_.FullName -Algorithm MD5 } ) |
        ForEach-Object -Process {
          $source_files += $_.Path.Replace($source_path, "") + $delimiter + $_.Hash
        }
    $source_files
}

function Get-Remote-Session($guest_ip, $username, $password) {
    $secstr = convertto-securestring -AsPlainText -Force -String $password
    $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
    New-PSSession -ComputerName $guest_ip -Credential $cred
}

function Get-remote-file-hash($source_path, $delimiter, $session) {
    Invoke-Command -Session $session -ScriptBlock ${function:Get-file-hash} -ArgumentList $source_path, $delimiter
    # TODO:
    # Check if remote PS Scripting errors
}

function Sync-Remote-Machine($machine, $remove_files, $copy_files, $host_path, $guest_path) {
    ForEach ($item in $copy_files) {
      $from = $host_path + $item
      $to = $guest_path + $item
      # Copy VM can also take a VM object
      Hyper-V\Copy-VMFile  -VM $machine -SourcePath $from -DestinationPath $to -CreateFullPath -FileSource Host -Force
    }
}

function Create-Remote-Folders($empty_source_folders, $guest_path) {
    ForEach ($item in $empty_source_folders) {
        $new_name =  $guest_path + $item
        New-Item "$new_name" -type directory -Force
    }
}

function Get-Empty-folders-From-Source($host_path) {
  Get-ChildItem $host_path -recurse |
        Where-Object {$_.PSIsContainer -eq $True} |
            Where-Object {$_.GetFiles().Count -eq 0} |
                Select-Object FullName | ForEach-Object -Process {
                    $empty_source_folders += ($_.FullName.Replace($host_path, ""))
                }
}

$delimiter = " || "

$machine = Hyper-V\Get-VM -Id $vm_id

# FIXME: PowerShell guys please fix this.
# The below script checks for all VMIntegrationService which are not enabled
# and will enable this.
# When when all the services are enabled this throws an error.
# Enable VMIntegrationService to true
try {
  Hyper-V\Get-VM -Id $vm_id | Hyper-V\Get-VMIntegrationService -Name "Guest Service Interface" | Hyper-V\Enable-VMIntegrationService -Passthru
  }
  catch { }

$session = Get-Remote-Session $guest_ip $username $password

$source_files = Get-file-hash $host_path $delimiter
$destination_files = Get-remote-file-hash $guest_path $delimiter $session

if (!$destination_files) {
  $destination_files = @()
}
if (!$source_files) {
  $source_files = @()
}

# Compare source and destination files
$remove_files = @()
$copy_files = @()


Compare-Object -ReferenceObject $source_files -DifferenceObject $destination_files | ForEach-Object {
  if ($_.SideIndicator -eq '=>') {
      $remove_files += $_.InputObject.Split($delimiter)[0]
  } else {
      $copy_files += $_.InputObject.Split($delimiter)[0]
  }
}

# Update the files to remote machine
Sync-Remote-Machine $machine $remove_files $copy_files $host_path $guest_path

# Create any empty folders which missed to sync to remote machine
$empty_source_folders = @()
$directories = Get-Empty-folders-From-Source $host_path

$result = Invoke-Command -Session $session -ScriptBlock ${function:Create-Remote-Folders} -ArgumentList $empty_source_folders, $guest_path
# Always remove the connection after Use
Remove-PSSession -Id $session.Id

$resultHash = @{
  message = "OK"
}
$result = ConvertTo-Json $resultHash
Write-OutputMessage $result