Use a script and systemd timer to keep the FreePBX tftpboot directory up to date with a git repository
-
I had previously posted this guide on how to use a systemd timer instead of cron to automate a git pull.
It works as is, but it is just pulling down changes. While that can be useful, it is not enough for most systems, because I typically will have phones push changes back to the phone system also.
So let's make things better.
Assumption: You have the
/tftpboot
already in a git repository checked out via ssh (key based)Let's get started.
- Open the update the script
sudo nano /usr/local/bin/update_git.sh
- Paste or type in this information.
Change the commit messages if they make more sense to you as something else.
#!/bin/sh # Script to commit local changes and pull down remote changes #change to the directory the git repo resides in cd /tftpboot # Pull down any changes git pull # Add any tracked or untracked -local.cfg files git add *-local.cfg git commit -m "local config file changes" # Add any tracked or untracked -contact.xml files git add *-contact.xml git commit -m "local directory file changes" # Add any other tracked files that have been updated git commit -a -m "other file changes" # Push the changes git push
- Save
ctrl+o
the file and exitctrl+x
from the editor. - Make the script executable and update the owner
sudo chmod +x /usr/local/bin/update_git.sh sudo chown asterisk:asterisk /usr/local/bin/update_git.sh
- Create the
systemd
service file
sudo nano /etc/systemd/system/gitsync.service
- Paste in this information.
[Unit] Description=update /tftpboot with the git repository [Service] Type=simple User=asterisk Group=asterisk ExecStart=/usr/local/bin/update_git.sh [Install] WantedBy=multi-user.target
- Save
ctrl+o
the file and exitctrl+x
from the editor. - create the
systemd
timer file.
sudo nano /etc/systemd/system/gitsync.timer
- Paste in this information.
[Unit] Description=Execute /usr/local/bin/update_git.sh every 10 minutes [Timer] OnCalendar=*:0/10 Unit=gitsync.service [Install] WantedBy=multi-user.target
- Save
ctrl+o
the file and exitctrl+x
from the editor. - Start the timer.
sudo systemctl enable --now gitsync.timer
- You can verify it is running with normal
systemd
commands.
[jbusch@pbx ~]# sudo systemctl list-timers --all NEXT LEFT LAST PASSED UNIT ACTIVATES Mon 2020-04-27 13:00:00 CDT 4min 8s left n/a n/a gitsync.timer gitsync.service Tue 2020-04-28 12:02:15 CDT 23h left Mon 2020-04-27 12:02:15 CDT 53min ago systemd-tmpfiles-clean.timer systemd-tmpfiles-cle n/a n/a n/a n/a systemd-readahead-done.timer systemd-readahead-do 3 timers listed.
- Once it is running, you can also monitor the
systemd
logs to see what has happened.
[jbusch@pbx ~]$ journalctl -u gitsync.timer -- Logs begin at Sun 2020-04-26 22:06:15 CDT, end at Mon 2020-04-27 20:49:17 CDT. -- Apr 27 12:55:43 pbx.domain.com systemd[1]: Started Execute /usr/local/bin/update_git.sh every 10 minutes. [jbusch@pbx ~]$ journalctl -u gitsync.service -- Logs begin at Sun 2020-04-26 22:06:15 CDT, end at Mon 2020-04-27 20:49:17 CDT. -- Apr 27 13:00:01 pbx.domain.com systemd[1]: Started update /tftpboot with the git repository. Apr 27 13:00:02 pbx.domain.com update_git.sh[30246]: Warning: Permanently added the ECDSA host key for IP address '172.65.251.78 Apr 27 13:00:13 pbx.domain.com update_git.sh[30246]: Already up-to-date. Apr 27 13:00:13 pbx.domain.com update_git.sh[30246]: [master 536e8bc] local config file changes Apr 27 13:00:13 pbx.domain.com update_git.sh[30246]: 2 files changed, 16 insertions(+), 8 deletions(-) Apr 27 13:00:13 pbx.domain.com update_git.sh[30246]: # On branch master Apr 27 13:00:13 pbx.domain.com update_git.sh[30246]: # Your branch is ahead of 'origin/master' by 1 commit. Apr 27 13:00:13 pbx.domain.com update_git.sh[30246]: # (use "git push" to publish your local commits) Apr 27 13:00:13 pbx.domain.com update_git.sh[30246]: # Apr 27 13:00:13 pbx.domain.com update_git.sh[30246]: nothing to commit, working directory clean Apr 27 13:00:13 pbx.domain.com update_git.sh[30246]: # On branch master Apr 27 13:00:13 pbx.domain.com update_git.sh[30246]: # Your branch is ahead of 'origin/master' by 1 commit. Apr 27 13:00:13 pbx.domain.com update_git.sh[30246]: # (use "git push" to publish your local commits) Apr 27 13:00:13 pbx.domain.com update_git.sh[30246]: # Apr 27 13:00:13 pbx.domain.com update_git.sh[30246]: nothing to commit, working directory clean Apr 27 13:00:24 pbx.domain.com update_git.sh[30246]: To [email protected]:BundyAssoc/Bundy/PBX/Phone-Configs.git Apr 27 13:00:24 pbx.domain.com update_git.sh[30246]: a1f81bf..536e8bc master -> master Apr 27 13:10:01 pbx.domain.com systemd[1]: Started update /tftpboot with the git repository. Apr 27 13:10:11 pbx.domain.com update_git.sh[31749]: Already up-to-date. Apr 27 13:10:11 pbx.domain.com update_git.sh[31749]: # On branch master Apr 27 13:10:11 pbx.domain.com update_git.sh[31749]: nothing to commit, working directory clean Apr 27 13:10:11 pbx.domain.com update_git.sh[31749]: # On branch master Apr 27 13:10:11 pbx.domain.com update_git.sh[31749]: nothing to commit, working directory clean Apr 27 13:10:11 pbx.domain.com update_git.sh[31749]: # On branch master Apr 27 13:10:11 pbx.domain.com update_git.sh[31749]: nothing to commit, working directory clean Apr 27 13:10:12 pbx.domain.com update_git.sh[31749]: Everything up-to-date
This is what it looks like in the GitLab interface. Github would be similar.