From 836af2b9be91cef156a6867fe4f9abc172015899 Mon Sep 17 00:00:00 2001 From: enggnr <129082112+enggnr@users.noreply.github.com> Date: Sun, 9 Jul 2023 10:14:11 +0530 Subject: [PATCH] 32 gitomatic (#61) * Initial work to support gitomatic services * Changes to create systemd service to run gitomatic * Update logic after review * Support MacOS --- home/.chezmoi.yaml.tmpl | 7 +++ .../run_onchange_after_32-gitomatic.sh.tmpl | 56 +++++++++++++++++++ .../dot_config/gitomatic/gitomatic.plist.tmpl | 13 +++++ .../gitomatic/gitomatic.service.tmpl | 12 ++++ .../bin/executable_gitomatic_service.tmpl | 17 ++++++ 5 files changed, 105 insertions(+) create mode 100644 home/.chezmoiscripts/universal/run_onchange_after_32-gitomatic.sh.tmpl create mode 100644 home/dot_config/gitomatic/gitomatic.plist.tmpl create mode 100644 home/dot_config/gitomatic/gitomatic.service.tmpl create mode 100644 home/dot_local/bin/executable_gitomatic_service.tmpl diff --git a/home/.chezmoi.yaml.tmpl b/home/.chezmoi.yaml.tmpl index ffa788c6..affa938c 100644 --- a/home/.chezmoi.yaml.tmpl +++ b/home/.chezmoi.yaml.tmpl @@ -193,6 +193,13 @@ data: email: "{{ $gcloudEmail }}" github: username: "{{ $githubUsername }}" + gitomatic: + - git: https://github.com/megabyte-labs/install.doctor + path: $HOME/sources/install.doctor + - git: https://github.com/megabyte-labs/install.doctor-docs + path: $HOME/sources/install.doctor-docs + - git: https://github.com/megabyte-labs/install.doctor-site + path: $HOME/sources/install.doctor-site gmail: username: "{{ $gmailAddress }}" gpg: diff --git a/home/.chezmoiscripts/universal/run_onchange_after_32-gitomatic.sh.tmpl b/home/.chezmoiscripts/universal/run_onchange_after_32-gitomatic.sh.tmpl new file mode 100644 index 00000000..9dedb67f --- /dev/null +++ b/home/.chezmoiscripts/universal/run_onchange_after_32-gitomatic.sh.tmpl @@ -0,0 +1,56 @@ +{{- if ne .host.distro.family "windows" -}} +#!/usr/bin/env bash +# @file git-o-matic Configuration +# @brief Starts service on Linux systems to monitor Git repositories +# @description +# git-o-matic is a tool to monitor git repositories and automatically pull/push changes. Multiple repositories can be +# monitored by running multiple instances of `gitomatic`. This script supports SSH Key based authentication only. +# +# If the `gitomatic` program is installed, this script creates and starts a Systemd service to monitor the repositories. +# The repositories are cloned if they are not available at the path. +# +# ## Notes +# * The author name and email address for commits are the same as `.user.name` and `.user.email` (configured in the `home/.chezmoi.yaml.tmpl` file) +# * `gitomatic` automatically pushes and pulls changes. The script does not change this behavior +# * `gitomatic` checks for changes every minute. This setting is not changed by this script +# * The User's default SSH Key is used for authentication +# +# ## Links +# +# * [gitomatic GitHub repository](https://github.com/muesli/gitomatic/) +# * [Systemd Unit file](https://github.com/megabyte-labs/install.doctor/blob/master/home/dot_config/gitomatic/gitomatic.service.tmpl) +# * [Helper script](https://github.com/megabyte-labs/install.doctor/blob/master/home/dot_local/bin/executable_gitomatic_service.tmpl) + +{{ includeTemplate "universal/profile" }} +{{ includeTemplate "universal/logg" }} + +### Clone the repositories +logg info 'Cloning the repositories' +{{ range .data.user.gitomatic }} +if [ ! -d {{ .path }} ] ; then + git clone {{ .git }} {{ .path }} +fi +{{ end -}} + +### Create Systemd service to run gitomatic +if command -v gitomatic > /dev/null; then + if [ -d /Applications ] && [ -d /System ]; then + ### macOS + logg info 'Copying `gitomatic` plist file to /Library/LaunchDaemons' + sudo cp -f {{ .chezmoi.homeDir }}/.config/gitomatic/gitomatic.plist /Library/LaunchDaemons/gitomatic.plist + logg info 'Running `sudo launchctl load com.gitomatic`' + sudo launchctl load com.gitomatic + logg info 'Running `sudo launchctl start com.gitomatic`' + sudo launchctl start com.gitomatic + else + ### Linux + logg info 'Copying `gitomatic` systemd unit file to /etc/systemd/system/' + sudo cp -f {{ .chezmoi.homeDir }}/.config/gitomatic/gitomatic.service /etc/systemd/system/gitomatic.service + logg info 'Reloading systemd daemon' + sudo systemctl daemon-reload + logg info 'Enabling and starting `gitomatic` service' + sudo systemctl enable --now gitomatic + fi +else + logg info 'Git-o-matic is not installed or it is not available in PATH' +fi diff --git a/home/dot_config/gitomatic/gitomatic.plist.tmpl b/home/dot_config/gitomatic/gitomatic.plist.tmpl new file mode 100644 index 00000000..fc9ed4df --- /dev/null +++ b/home/dot_config/gitomatic/gitomatic.plist.tmpl @@ -0,0 +1,13 @@ +{{- if eq .host.distro.family "darwin" -}} + + + + + Label + com.gitomatic + Program + {{ .chezmoi.homeDir }}/.local/bin/gitomatic_service + RunAtLoad + + +{{ end }} diff --git a/home/dot_config/gitomatic/gitomatic.service.tmpl b/home/dot_config/gitomatic/gitomatic.service.tmpl new file mode 100644 index 00000000..609bfa23 --- /dev/null +++ b/home/dot_config/gitomatic/gitomatic.service.tmpl @@ -0,0 +1,12 @@ +{{- if eq .host.distro.family "linux" -}} +[Unit] +Description=Service to watch git repositories + +[Service] +Type=simple +ExecStart={{ .chezmoi.homeDir }}/.local/bin/gitomatic_service +Restart=on-failure + +[Install] +WantedBy=multi-user.target +{{ end }} diff --git a/home/dot_local/bin/executable_gitomatic_service.tmpl b/home/dot_local/bin/executable_gitomatic_service.tmpl new file mode 100644 index 00000000..c1574d2e --- /dev/null +++ b/home/dot_local/bin/executable_gitomatic_service.tmpl @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +# @file .local/bin/gitomatic_service +# @brief Helper script to run gitomatic to monitor git repositories +# @description +# This script is executed by gitomatic systemd service. `gitomatic` does not support monitoring multiple +# repositories in a single process. This script starts as many gitomatic processes as there are repositories. +# +# ## Links +# +# [Systemd Unit file](https://github.com/megabyte-labs/install.doctor/blob/master/home/dot_config/gitomatic/gitomatic.service.tmpl) + +{{ includeTemplate "universal/logg" }} + +{{ range .data.user.gitomatic }} +gitomatic -author {{ $.data.user.name }} -email {{ $.data.user.email }} -privkey {{ $.chezmoi.homeDir }}/.ssh/id_rsa {{ .path }} & +{{ end -}}