2015-05-22 18:25:05 +00:00
|
|
|
#!powershell
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# WANT_JSON
|
|
|
|
# POWERSHELL_COMMON
|
|
|
|
|
2016-01-05 00:05:43 +00:00
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
|
2017-01-27 23:44:16 +00:00
|
|
|
$params = Parse-Args $args -supports_check_mode $true
|
2015-05-22 18:25:05 +00:00
|
|
|
|
2017-01-27 23:44:16 +00:00
|
|
|
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -default $false
|
2015-05-22 18:25:05 +00:00
|
|
|
|
2017-02-24 07:10:25 +00:00
|
|
|
$path = Get-AnsibleParam -obj $params -name "path" -type "path" -failifempty $true -aliases "dest","name"
|
2017-01-27 23:44:16 +00:00
|
|
|
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -validateset "absent","directory","file","touch"
|
2015-05-22 18:25:05 +00:00
|
|
|
|
2017-01-27 23:44:16 +00:00
|
|
|
$result = @{
|
|
|
|
changed = $false
|
2015-05-29 01:57:13 +00:00
|
|
|
}
|
2015-05-22 18:25:05 +00:00
|
|
|
|
2017-02-09 02:49:49 +00:00
|
|
|
# Used to delete symlinks as powershell cannot delete broken symlinks
|
|
|
|
$symlink_util = @"
|
|
|
|
using System;
|
|
|
|
using System.ComponentModel;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace Ansible.Command {
|
|
|
|
public class SymLinkHelper {
|
|
|
|
[DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
|
|
|
|
public static extern bool RemoveDirectory(string lpPathName);
|
|
|
|
|
|
|
|
public static void DeleteSymLink(string linkPathName) {
|
|
|
|
bool result = RemoveDirectory(linkPathName);
|
|
|
|
if (result == false)
|
|
|
|
throw new Exception(String.Format("Error deleting symlink: {0}", new Win32Exception(Marshal.GetLastWin32Error()).Message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"@
|
|
|
|
Add-Type -TypeDefinition $symlink_util
|
|
|
|
|
|
|
|
# Used to delete directories and files with logic on handling symbolic links
|
2017-02-24 07:10:25 +00:00
|
|
|
function Remove-File($file, $checkmode) {
|
2017-02-09 02:49:49 +00:00
|
|
|
try {
|
|
|
|
if ($file.Attributes -band [System.IO.FileAttributes]::ReparsePoint) {
|
|
|
|
# Bug with powershell, if you try and delete a symbolic link that is pointing
|
|
|
|
# to an invalid path it will fail, using Win32 API to do this instead
|
2017-02-24 07:10:25 +00:00
|
|
|
if (-Not $checkmode) {
|
|
|
|
[Ansible.Command.SymLinkHelper]::DeleteSymLink($file.FullName)
|
|
|
|
}
|
2017-02-09 02:49:49 +00:00
|
|
|
} elseif ($file.PSIsContainer) {
|
2017-02-24 07:10:25 +00:00
|
|
|
Remove-Directory -directory $file -WhatIf:$checkmode
|
2017-02-09 02:49:49 +00:00
|
|
|
} else {
|
2017-02-24 07:10:25 +00:00
|
|
|
Remove-Item -Path $file.FullName -Force -WhatIf:$checkmode
|
2017-02-09 02:49:49 +00:00
|
|
|
}
|
|
|
|
} catch [Exception] {
|
|
|
|
Fail-Json (New-Object psobject) "Failed to delete $($file.FullName): $($_.Exception.Message)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function Remove-Directory($directory) {
|
|
|
|
foreach ($file in Get-ChildItem $directory.FullName) {
|
|
|
|
Remove-File -file $file
|
|
|
|
}
|
|
|
|
Remove-Item -Path $directory.FullName -Force -Recurse
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-27 23:44:16 +00:00
|
|
|
if ($state -eq "touch") {
|
2017-02-24 07:10:25 +00:00
|
|
|
if (Test-Path -Path $path) {
|
|
|
|
(Get-ChildItem -Path $path).LastWriteTime = Get-Date
|
|
|
|
} else {
|
|
|
|
Write-Output $null | Out-File -FilePath $path -Encoding ASCII -WhatIf:$check_mode
|
|
|
|
$result.changed = $true
|
2015-05-22 18:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 23:44:16 +00:00
|
|
|
if (Test-Path $path) {
|
2017-02-24 07:10:25 +00:00
|
|
|
$fileinfo = Get-Item -Path $path
|
2017-01-27 23:44:16 +00:00
|
|
|
if ($state -eq "absent") {
|
2017-02-24 07:10:25 +00:00
|
|
|
Remove-File -File $fileinfo -CheckMode $check_mode
|
2017-01-27 23:44:16 +00:00
|
|
|
$result.changed = $true
|
|
|
|
} else {
|
|
|
|
if ($state -eq "directory" -and -not $fileinfo.PsIsContainer) {
|
|
|
|
Fail-Json $result "path $path is not a directory"
|
2015-05-22 18:25:05 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 23:44:16 +00:00
|
|
|
if ($state -eq "file" -and $fileinfo.PsIsContainer) {
|
|
|
|
Fail-Json $result "path $path is not a file"
|
2015-05-22 18:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-27 23:44:16 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
# If state is not supplied, test the $path to see if it looks like
|
|
|
|
# a file or a folder and set state to file or folder
|
|
|
|
if ($state -eq $null) {
|
2015-05-29 01:57:13 +00:00
|
|
|
$basename = Split-Path -Path $path -Leaf
|
2017-01-27 23:44:16 +00:00
|
|
|
if ($basename.length -gt 0) {
|
2015-05-29 01:57:13 +00:00
|
|
|
$state = "file"
|
2017-01-27 23:44:16 +00:00
|
|
|
} else {
|
2015-05-29 01:57:13 +00:00
|
|
|
$state = "directory"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 23:44:16 +00:00
|
|
|
if ($state -eq "directory") {
|
2017-02-24 07:10:25 +00:00
|
|
|
New-Item -Path $path -ItemType Directory -WhatIf:$check_mode | Out-Null
|
2017-01-27 23:44:16 +00:00
|
|
|
$result.changed = $true
|
|
|
|
} elseif ($state -eq "file") {
|
|
|
|
Fail-Json $result "path $path will not be created"
|
2015-05-22 18:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-29 01:57:13 +00:00
|
|
|
Exit-Json $result
|