This commit is contained in:
Kenneth Benzie 2023-07-25 20:43:04 +01:00
parent da132c5fb1
commit e92347d035
7 changed files with 1625 additions and 27 deletions

1
.gitignore vendored
View File

@ -1,3 +1,2 @@
external external
modules/win_git*
playbooks/test.yaml playbooks/test.yaml

View File

@ -1,5 +1,5 @@
[defaults] [defaults]
collections_path = collections collections_path = collections
library = modules library = library
roles_path = roles roles_path = roles
stdout_callback = yaml stdout_callback = yaml

1194
git.py Normal file

File diff suppressed because it is too large Load Diff

344
library/win_git.ps1 Normal file
View File

@ -0,0 +1,344 @@
#!powershell
#AnsibleRequires -CSharpUtil Ansible.Basic
#AnsibleRequires -PowerShell Ansible.ModuleUtils.CommandUtil
$module = [Ansible.Basic.AnsibleModule]::Create($args, @{
options = @{
dest = @{ type = 'path' }
repo = @{ required = $true; aliases = @( 'name' ) }
version = @{ default = 'HEAD' }
remote = @{ default = 'origin' }
executable = @{ default = $null; type = 'path' }
}
supports_check_mode = $false
})
$dest = $module.Params.dest
$repo = $module.Params.repo
$version = $module.Params.version
$remote = $module.Params.remote
$git = $module.Params.executable
if ( !$git ) {
$git = Get-ExecutablePath 'git'
}
# ================================= Utilities ==================================
function Test-SshAcceptNewHostKey {
try {
$ssh = Get-ExecutablePath 'ssh'
} catch {
throw 'Remote host is missing ssh command, so you cannot use acceptnewhostkey option.'
}
$result = Run-Command "$ssh -o StrictHostKeyChecking=accept-new -V"
if ( $result.rc -ne 0 ) {
return $false
}
return $true
}
function Get-AbsolutePath {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)] [String] $path
)
try {
$result = Resolve-Path $path
} catch {
return $_[0].TargetObject
}
return $result
}
function Get-GitDir {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)] [String] $path
)
$git_dir = Join-Path $path '.git'
# Check if this .git is a file.
if ( [System.IO.File]::Exists($git_dir) ) {
# Extract the gitdir: path from the .git file.
$groups = Get-Content "$git_dir" | `
Select-String '(gitdir:) (.*)' | `
ForEach { $_.Matches[0].Groups[1..2] }
$ref_prefix = $groups[0]
$gitdir = $groups[1]
if ( $ref_prefix -ne 'gitdir:' ) {
$module.FailJson('The .git file has invalid gitdir reference format.')
}
# Check if the repo path is absolute.
if ( [System.IO.Path]::IsPathRooted($gitdir) ) {
$git_dir = $gitdir
} else {
# Join with the input path to construct an absolute path.
$git_dir = Join-Path $path $gitdir
}
if ( ![System.IO.Directory]::Exists($git_dir) ) {
throw "$git_dir is not a directory."
}
}
return $git_dir
}
function Get-GitSshExecutablePath {
if ( $env:GIT_SSH ) {
return $env:GIT_SSH
}
if ( $env:GIT_SSH_COMMAND ) {
return $env:GIT_SSH_COMMAND
}
return Get-ExecutablePath 'ssh'
}
function Test-GitRemoteBranch {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)] [String] $dest,
[Parameter(Mandatory = $true)] [String] $repository,
[Parameter(Mandatory = $true)] [String] $branch
)
$command = "`"$git`" ls-remote $repository -h refs/heads/$branch"
$result = Run-Command -command $command
if ( $result.stdout.Contains($version) ) {
return $true
}
return $false
}
function Test-GitRemoteTag {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)] [String] $dest,
[Parameter(Mandatory = $true)] [String] $repository,
[Parameter(Mandatory = $true)] [String] $tag
)
$command = "`"$git`" ls-remote $repository -t refs/tags/$tag"
$result = Run-Command -command $command
if ( $result.stdout.Contains($version) ) {
return $true
}
return $false
}
function Test-GitLocalChanges {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)] [String] $dest
)
$command = "`"$git`" status --porcelain"
$result = Run-Command -command $command -working_directory $dest
$changes = $result.stdout.Split([System.Environment]::NewLine, `
[System.StringSplitOptions]::RemoveEmptyEntries) | `
Where-Object { -not $_.StartsWith('??') } | Measure-Object -Line
return $changes.Lines -gt 0
}
function Get-GitRemoteHead {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)] [String] $repo,
[Parameter(Mandatory = $true)] [String] $dest,
[Parameter(Mandatory = $true)] [String] $version,
[Parameter(Mandatory = $true)] [String] $remote
)
$cloning = $false
$cwd = $null
$tag = $false
if ( $remote -eq $repo ) {
$cloning = $true
} else {
$cwd = $dest
}
if ( $version -eq 'HEAD' ) {
if ( $cloning ) {
# Cloning the repo, just get the remote's HEAD version.
$command = "`"$git`" ls-remote $remote -h HEAD"
} else {
$head_branch = Get-GitRemoteHeadBranch $module $dest $remote
$command = "`"$git`" ls-remote $remote -h refs/heads/$head_branch"
}
} elseif ( Test-GitRemoteBranch $dest $remote $version ) {
$command = "`"$git`" ls-remote $remote -h refs/head/$version"
} elseif ( Test-GitRemoteTag $dest $remote $version ) {
$tag = $true
$command = "`"$git`" ls-remote $remote -t refs/tags/$version*"
} else {
# Appears to be a sha1, return as-is since it apparently not possible
# to check for a specific sha1 on remote.
return $version
}
$result = Run-Command -command $command -working_directory $cwd
if ( $result.rc -ne 0 -or $result.stdout.Length -lt 1 ) {
throw "Could not determine remote ref for $vesion"
}
$ref = $result.stdout
if ( $tag ) {
# Find the dereferenced tag if this is an annotated tag.
ForEach ( $tag in $ref.Split([System.Environment]::NewLine) ) {
if ( $tag.EndsWith("$version ^{}") ) {
$ref = $tag
} elseif ( $tag.EndsWith($version) ) {
$ref = $tag
}
}
}
return $ref.Split()[0]
}
function Test-GitDetachedHead {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)] [String] $dest
)
}
function Get-GitRemoteHeadBranch {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)] [String] $dest,
[Parameter(Mandatory = $true)] [String] $version,
[Parameter(Mandatory = $true)] [String] $remote
)
$git_dir = Join-Path $dest '.git'
# TODO: Check if the .git is a file. If it is a file, it means that we are
# in a submodule structure.
$head_file = Join-Path $git_dir 'HEAD'
if ( Test-GitDetachedHead $dest ) {
$head_file = Join-Path $git_dir 'refs' 'remotes' $remote 'HEAD'
}
}
function Get-GitCurrentSha {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)] [String] $dest
)
$command = "`"$git`" rev-parse HEAD"
$result = Run-Command -command $command -working_directory $dest
return $result.stdout.Trim()
}
function Get-GitRemoteUrl {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)] [String] $dest,
[Parameter(Mandatory = $true)] [String] $remote
)
$command = "`"$git`" ls-remote --get-url $remote"
$result = Run-Command -command $command -working_directory $dest
if ( $result.rc -ne 0 ) {
# There was an issue getting the remote URL, most likely command is not
# available in this version of Git.
return $null
}
return $result.stdout.Trim()
}
function Set-GitRemoteUrl {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)] [String] $dest,
[Parameter(Mandatory = $true)] [String] $remote,
[Parameter(Mandatory = $true)] [String] $url
)
# Return if remote URL isn't changing.
$remote_url = Get-GitRemoteUrl $dest $remote
if ( $remote_url -eq $repo ) {
return $false
}
$command = "`"$git`" remote set-url $remote $url"
$result = Run-Command -command $command -working_directory $dest
if ( $result.rc -ne 0 ) {
$module.FailJson("Failed to set a new url $url for $remote`: $result.stderr")
}
# Return false if remote_url is null to maintain previous behavior for Git
# versions prior to 1.7.5 that lack required functionality.
return $remote_url -ne $null
}
function Invoke-GitClone {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)] [String] $repo,
[Parameter(Mandatory = $true)] [String] $remote,
[Parameter(Mandatory = $true)] [String] $dest
)
$dest_parent = Split-Path -Path $dest -Parent
if ( !(Test-Path $dest_parent) ) {
New-Item -Path $dest_parent -ItemType Directory
}
$command = "`"$git`" clone --recursive --origin $remote $repo $dest"
if ( $version -ne "HEAD" ) {
$command += " --branch $version"
}
$result = Run-Command -command $command -working_directory $cwd
if ( $result.rc -ne 0 ) {
$module.FailJson($result.stderr)
}
# Ensure the newly cloned repository has the correct owner
$userName = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$idRef = [System.Security.Principal.NTAccount]::new($userName)
Get-Item $dest | foreach { `
$_ ; $_ | Get-ChildItem -Force -Recurse `
} | foreach { `
$acl = $_ | Get-Acl; $acl.SetOwner($idRef); $_ | Set-Acl -AclObject $acl `
}
}
function Invoke-GitFetch {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)] [String] $repo,
[Parameter(Mandatory = $true)] [String] $dest,
[Parameter(Mandatory = $true)] [String] $version,
[Parameter(Mandatory = $true)] [String] $remote
)
$command = "`"$git`" fetch --tags $remote"
$result = Run-Command -command $command -working_directory $dest
if ( $result.rc -ne 0 ) {
$module.FailJson("Failed to download remote objects and refs:" + `
"$result.stdout $result.stderr")
}
}
function Invoke-GitCheckout {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)] [String] $dest,
[Parameter(Mandatory = $true)] [String] $version
)
$result = Run-Command -command "`"$git`" checkout $version" -working_directory $dest
if ( $result.rc -ne 0 ) {
$module.FailJson("Failed to checkout version '$version': " + `
"$result.stdout $result.stderr")
}
}
# ================================ Start logic =================================
if ( !$dest ) {
$module.FailJson('The destination directory must be specified.')
}
$dest = Get-AbsolutePath $dest
$git_dir = Get-GitDir $dest
$gitconfig = Join-Path $git_dir 'config'
$module.Result.before = $null
$local_changes = $false
if ( ($dest -and ![System.IO.File]::Exists($gitconfig)) -or (!$dest -and !$clone) ) {
Invoke-GitClone $repo $remote $dest $version
$module.Result.changed = $true
} else {
$local_changes = Test-GitLocalChanges $dest
$module.Result.before = Get-GitCurrentSha $dest
if ( $local_changes ) {
$module.FailJson('Local modifications exist in repository.')
}
# Checkout branch, if $version is HEAD get HEAD branch
# Pull
}
$module.ExitJson()

64
library/win_git.py Normal file
View File

@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-
# TODO: copyright
DOCUMENTATION = '''
---
module: win_git
author:
- "Kenneth Benzie (Benie)"
short_description: Deploy software (or files) from git checkouts on Windows
description:
- Manage git checkouts of repositories to deploy files or software on Windows.
options:
data:
description:
- Alternate data to return instead of 'pong'.
- If this parameter is set to C(crash), the module will cause an
exception.
type: str
default: pong
seealso:
- module: ansible.builtin.git
'''
# DOCUMENTATION = r'''
# ---
# module: win_ping
# short_description: A windows version of the classic ping module
# description:
# - Checks management connectivity of a windows host.
# - This is NOT ICMP ping, this is just a trivial test module.
# - For non-Windows targets, use the M(ansible.builtin.ping) module instead.
# options:
# data:
# description:
# - Alternate data to return instead of 'pong'.
# - If this parameter is set to C(crash), the module will cause an exception.
# type: str
# default: pong
# seealso:
# - module: ansible.builtin.ping
# author:
# - Chris Church (@cchurch)
# '''
EXAMPLES = r'''
# Test connectivity to a windows host
# ansible winserver -m ansible.windows.win_ping
- name: Example from an Ansible Playbook
ansible.windows.win_ping:
- name: Induce an exception to see what happens
ansible.windows.win_ping:
data: crash
'''
RETURN = r'''
ping:
description: Value provided with the data parameter.
returned: success
type: str
sample: pong
'''

View File

@ -31,17 +31,17 @@
- include_tasks: Windows-installer.yaml - include_tasks: Windows-installer.yaml
when: git_run_installer when: git_run_installer
- name: clone config repos # - name: clone config repos
win_git: # win_git:
repo: '{{item.repo}}' # repo: '{{item.repo}}'
dest: '{{ansible_env.USERPROFILE}}/.config/{{item.name}}' # dest: '{{ansible_env.USERPROFILE}}\.config\{{item.name}}'
version: master # version: master
with_items: '{{git_config_repos}}' # with_items: '{{git_config_repos}}'
- win_owner: # - win_owner:
path: '{{ansible_env.USERPROFILE}}/.config/{{item.name}}' # path: '{{ansible_env.USERPROFILE}}\.config\{{item.name}}'
user: Benie # user: Benie
recurse: true # recurse: true
with_items: '{{git_config_repos}}' # with_items: '{{git_config_repos}}'
# - TODO: install pip packages # - TODO: install pip packages
# win_pip: # win_pip:

View File

@ -8,20 +8,17 @@
- set_fact: - set_fact:
vim_config_dir: '{{ansible_env.LOCALAPPDATA}}\nvim' vim_config_dir: '{{ansible_env.LOCALAPPDATA}}\nvim'
- name: clone config repo # - name: clone config repo
win_git: # win_git:
repo: git@code.infektor.net:config/vim.git # repo: git@code.infektor.net:config/vim.git
dest: '{{vim_config_dir}}' # dest: '{{vim_config_dir}}'
branch: master # branch: master
# clone: false # # clone: false
update: true # update: true
- win_owner: # - win_owner:
path: '{{vim_config_dir}}' # path: '{{vim_config_dir}}'
user: Benie # user: Benie
recurse: true # recurse: true
- assert:
that: False
# - TODO: neovim set repo email # - TODO: neovim set repo email
# win_git_config: # win_git_config: