1 Commits

Author SHA1 Message Date
e92347d035 temp! 2023-07-25 20:43:04 +01:00
25 changed files with 1491 additions and 200 deletions

1194
git.py Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -5,12 +5,11 @@
$module = [Ansible.Basic.AnsibleModule]::Create($args, @{
options = @{
dest = @{type = 'path'}
repo = @{required = $true; aliases = @('name')}
version = @{default = 'HEAD'; aliases = @('branch')}
remote = @{default = 'origin'}
recursive = @{default = $true; type = 'bool'}
executable = @{default = $null; type = 'path'}
dest = @{ type = 'path' }
repo = @{ required = $true; aliases = @( 'name' ) }
version = @{ default = 'HEAD' }
remote = @{ default = 'origin' }
executable = @{ default = $null; type = 'path' }
}
supports_check_mode = $false
})
@@ -20,12 +19,25 @@ $repo = $module.Params.repo
$version = $module.Params.version
$remote = $module.Params.remote
$git = $module.Params.executable
if (!$git) {
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 (
@@ -46,30 +58,70 @@ function Get-GitDir {
)
$git_dir = Join-Path $path '.git'
# Check if this .git is a file.
if ([System.IO.File]::Exists($git_dir)) {
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:') {
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)) {
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)) {
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 (
@@ -83,19 +135,79 @@ function Test-GitLocalChanges {
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
)
$command = "`"$git`" symbolic-ref --short refs/remotes/$remote/HEAD"
$result = Run-Command -command $command -working_directory $dest
if ($result.rc -ne 0) {
$module.FailJson("Could not determine the default HEAD branch of remote: $remote" ` +
"$result.stdout $result.stderr")
$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'
}
return $result.stdout.Trim().Replace("$remote/", '')
}
function Get-GitCurrentSha {
@@ -108,6 +220,44 @@ function Get-GitCurrentSha {
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 (
@@ -116,80 +266,59 @@ function Invoke-GitClone {
[Parameter(Mandatory = $true)] [String] $dest
)
$dest_parent = Split-Path -Path $dest -Parent
if (!(Test-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") {
if ( $version -ne "HEAD" ) {
$command += " --branch $version"
}
$result = Run-Command -command $command -working_directory $cwd
if ($result.rc -ne 0) {
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] $remote,
[Parameter(Mandatory = $true)] [String] $version
)
if ($version -eq "HEAD") {
$branch = Get-GitRemoteHeadBranch $dest $remote
} else {
$branch = $version
}
$result = Run-Command -command "`"$git`" checkout $branch" -working_directory $dest
if ($result.rc -ne 0) {
$module.FailJson("Failed to checkout version '$version'`n" + $result.stderr)
}
}
function Invoke-GitFetch {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)] [String] $dest,
[Parameter(Mandatory = $true)] [String] $remote,
[Parameter(Mandatory = $true)] [String] $version
)
$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:`n" + `
$result.stderr)
}
}
function Invoke-GitPull {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)] [String] $dest,
[Parameter(Mandatory = $true)] [String] $remote,
[Parameter(Mandatory = $true)] [String] $version
)
$result = Run-Command -command "`"$git`" pull $remote $version" -working_directory $dest
if ($result.rc -ne 0) {
$module.FailJson("Failed to pull version '$version' from remote '$origin':`n" + `
$result.stderr)
}
}
function Invoke-GitSubmoduleUpdate {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)] [String] $dest
)
$result = Run-Command -command "`"$git`" submodule update --init" -working_directory $dest
if ($result.rc -ne 0) {
$module.FailJson("Failed to initialized/update submodules:`n" + $result.stderr)
$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) {
if ( !$dest ) {
$module.FailJson('The destination directory must be specified.')
}
$dest = Get-AbsolutePath $dest
@@ -198,35 +327,18 @@ $gitconfig = Join-Path $git_dir 'config'
$module.Result.before = $null
if (($dest -and ![System.IO.File]::Exists($gitconfig))) {
Invoke-GitClone $repo $remote $dest
Invoke-GitCheckout $dest $remote $version
$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 (Test-GitLocalChanges $dest) {
if ( $local_changes ) {
$module.FailJson('Local modifications exist in repository.')
}
Invoke-GitFetch $dest $remote $version
Invoke-GitCheckout $dest $remote $version
Invoke-GitPull $dest $remote $version
$module.Result.after = Get-GitCurrentSha $dest
if ($module.Result.before -ne $module.Result.after) {
$module.Result.changed = $true
}
}
if ($recursive) {
Invoke-GitSubmoduleUpdate $dest
}
# Ensure the 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 `
# Checkout branch, if $version is HEAD get HEAD branch
# Pull
}
$module.ExitJson()

View File

@@ -2,6 +2,6 @@
- hosts: localhost
roles:
- role: 1password
- role: ferdium
- role: fonts
- role: obsidian
- role: webcatalog

View File

@@ -4,4 +4,3 @@
roles:
- role: gdb
- role: wsl
- role: system-info

View File

@@ -23,9 +23,7 @@
- role: 1password
- role: autohotkey
- role: ferdium
- role: firefox
- role: fonts
- role: obsidian
- role: powertoys
- role: windows-terminal

View File

@@ -35,11 +35,6 @@
https://downloads.1password.com/linux/debian/{{arch}} stable main
dest: /etc/apt/sources.list.d/1password.list
- name: apt update
become: true
apt:
update_cache: true
- name: install gui package
when: '"WSL" not in ansible_kernel'
become: true

View File

@@ -11,20 +11,35 @@
path: '{{app_exe}}'
register: app_stat
- name: get installed version
when: app_stat.stat.exists == True
win_command: '{{app_exe}} --version'
register: app_version
changed_when: false
- when: app_stat.stat.exists == True
set_fact:
installed_version: '{{app_version.stdout.strip()}}'
- name: download latest installer
when: not app_stat.stat.exists
win_get_url:
url: https://downloads.1password.com/win/1PasswordSetup-latest.exe
dest: '{{installer_exe}}'
- name: run installer
when: not app_stat.stat.exists
win_command: '{{installer_exe}}'
- name: get installer version
win_shell: |
(Get-ItemProperty {{installer_exe}}).VersionInfo.ProductVersion
register: installer_product_version
changed_when: false
- name: remove installer
win_file:
path: '{{installer_exe}}'
state: absent
# FIXME: The [5:] is to account for a mystery "\e[6 q" prefix, not sure if this
# is consistent across machines or some other oddity.
- set_fact:
installer_version: '{{installer_product_version.stdout.strip()[5:]}}'
- name: run installer
when: installed_version is not defined or installed_version != installer_version
win_command: '{{installer_exe}}'
- name: create start menu shortcut
win_shortcut:

View File

@@ -9,6 +9,10 @@
repo: git@code.infektor.net:config/AutoHotKey.git
dest: '{{autohotkey_repo_dir}}'
branch: master
- win_owner:
path: '{{autohotkey_repo_dir}}'
user: Benie
recurse: true
- name: create scheduled task
win_scheduled_task:

View File

@@ -1,17 +0,0 @@
---
- name: install homebrew package
when: ansible_os_family == 'Darwin'
homebrew_cask:
name: ferdium
state: latest
- when: ansible_os_family == 'Windows'
win_chocolatey:
name: ferdium
state: latest
- name: install flatpak package
when: ansible_os_family != 'Windows' and
ansible_os_family != 'Darwin'
flatpak:
name: org.ferdium.Ferdium

View File

@@ -1,6 +0,0 @@
---
- name: install apt package
become: true
apt:
name: flatpak
state: latest

View File

@@ -1,9 +0,0 @@
---
- include_tasks: '{{ansible_os_family}}.yaml'
- name: add flathub repository remote
become: true
flatpak_remote:
name: flathub
state: present
flatpakrepo_url: https://dl.flathub.org/repo/flathub.flatpakrepo

View File

@@ -1,5 +0,0 @@
---
- name: install chocolatey package
win_chocolatey:
name: nerd-fonts-CascadiaCode
state: latest

View File

@@ -1,7 +1,5 @@
---
- when: ansible_os_family == 'Darwin'
include_tasks: 'Darwin.yaml'
- when: ansible_os_family == 'Windows'
include_tasks: 'Windows.yaml'
- when: ansible_os_family != 'Darwin' and ansible_os_family != 'Windows'
include_tasks: 'Linux.yaml'

View File

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

View File

@@ -1,17 +1,5 @@
---
- name: slurp /etc/os-release
slurp:
src: /etc/os-release
register: os_release_slurp
- set_fact:
os_release: "{{ os_release_slurp.content |
b64decode | trim() | replace('=', ': ') | from_yaml }}"
- include_tasks: Ubuntu.yaml
when: "'ID_LIKE' in os_release and os_release.ID_LIKE == 'ubuntu debian'"
- name: install apt packages
when: "'ID_LIKE' not in os_release"
become: true
apt:
name:

View File

@@ -0,0 +1,2 @@
---
- include_tasks: Ubuntu.yaml

View File

@@ -0,0 +1,2 @@
---
- include_tasks: Ubuntu.yaml

View File

@@ -1,2 +1,5 @@
---
- include_tasks: '{{ansible_os_family}}.yaml'
when: ansible_os_family in ['Darwin', 'Windows']
- include_tasks: '{{ansible_distribution}}.yaml'
when: ansible_os_family not in ['Darwin', 'Windows']

View File

@@ -1,14 +1,7 @@
---
- name: slurp /etc/os-release
slurp:
src: /etc/os-release
register: os_release_slurp
- set_fact:
os_release: "{{ os_release_slurp.content |
b64decode | trim() | replace('=', ': ') | from_yaml }}"
- name: add neovim stable ppa
when: "'ID_LIKE' in os_release and os_release.ID_LIKE == 'ubuntu debian'"
when: ansible_distribution == 'Ubuntu' and
ansible_distribution_version == '20.04'
become: true
apt_repository:
repo: ppa:neovim-ppa/stable

View File

@@ -26,13 +26,6 @@
file_type: directory
register: found_plugins
- set_fact:
backslashes: '\\'
forwardslash: '/'
- set_fact:
managed_plugins: "{{managed_plugins | replace(backslashes, forwardslash)}}"
found_plugins: "{{found_plugins | replace(backslashes, forwardslash)}}"
- name: remove found plugins which are not in the managed list
win_file:
path: '{{item.path}}'

View File

@@ -8,11 +8,17 @@
- set_fact:
vim_config_dir: '{{ansible_env.LOCALAPPDATA}}\nvim'
- name: clone config repo
win_git:
repo: git@code.infektor.net:config/vim.git
dest: '{{vim_config_dir}}'
branch: master
# - name: clone config repo
# win_git:
# repo: git@code.infektor.net:config/vim.git
# dest: '{{vim_config_dir}}'
# branch: master
# # clone: false
# update: true
# - win_owner:
# path: '{{vim_config_dir}}'
# user: Benie
# recurse: true
# - TODO: neovim set repo email
# win_git_config:
@@ -41,18 +47,10 @@
src: '{{vim_config_dir}}/tasks.yaml'
dest: vim_config_tasks.yaml
flat: true
changed_when: false
- when: config_repo_tasks.stat.exists
include_tasks: vim_config_tasks.yaml
- name: remove fetched tasks
file:
state: absent
path: vim_config_tasks.yaml
changed_when: false
delegate_to: localhost
- when: ansible_os_family != "Windows" and
plugin_dir is defined and plugins is defined
include_tasks: 'Unix-plugins.yaml'

View File

@@ -12,3 +12,7 @@
repo: git@github.com:kbenzie/notes.git
dest: '{{obsidian_notes_repo}}'
branch: main
- win_owner:
path: '{{obsidian_notes_repo}}'
user: Benie
recurse: true

View File

@@ -3,7 +3,7 @@ Name=Obsidian
Exec={{ansible_env.HOME}}/.local/bin/Obsidian
Terminal=false
Type=Application
Icon={{ansible_env.HOME}}/.local/{{iconpath}}
Icon=obsidian
StartupWMClass=Obsidian
X-AppImage-Version={{latest.json.name}}
Comment=Private and flexible notetaking app that adapts to the way you think.

View File

@@ -5,9 +5,13 @@
- name: clone config repos
win_git:
repo: https://code.infektor.net/config/WindowsPowerShell.git
repo: git@code.infektor.net:config/WindowsPowerShell.git
dest: '{{powershell_config_dir}}'
branch: master
- win_owner:
path: '{{powershell_config_dir}}'
user: Benie
recurse: true
- name: install chocolatey package
win_chocolatey:

View File

@@ -1,6 +1,5 @@
---
- name: create wsl.conf
become: true
template:
src: templates/wsl.conf.j2
dest: /etc/wsl.conf
@@ -35,6 +34,28 @@
dest: external
state: directory
- name: clone ansible win_git module
git:
repo: https://github.com/tivrobo/ansible-win_git.git
dest: external/ansible-win_git
version: master
- set_fact:
modules_dir: ~/.config/local/modules
- name: create ansible modules directory
file:
dest: '{{modules_dir}}'
state: directory
- name: copy win_git files to ansible modules directory
copy:
src: '~/.config/local/external/ansible-win_git/{{item}}'
dest: '{{modules_dir}}/{{item}}'
with_items:
- win_git.ps1
- win_git.py
- name: read /etc/resolv.conf file contents
set_fact:
resolv_conf: '{{lookup("ansible.builtin.file", "/etc/resolv.conf")}}'