-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path99-git-commit
executable file
·96 lines (76 loc) · 3.12 KB
/
99-git-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/vbash
# shellcheck shell=bash
# file: 99-git-commit
# description: Saves config commands & config json to files, commits to local
# git repo. Repo stored in /config/user-data/$CONFIG_REPO. Completes
# process with a push to the remote repo.
#
# This script is intended to be used as a post-commit hook, allowing
# for automated config backup in a git repo.
#
# Install in /config/scripts/commit/post-hooks.d/ to run automatically
# after each VyOS commit.
#
# Repository path. You shouldn't need to modify this. This location is persistent
# across reboots and upgrades in VyOS.
REPO_PATH=/config/user-data
# Set your CONFIG_REPO directory name. This should be an git existing repository.
CONFIG_REPO=vyos-config
# Timestamp format (YYYY-MM-DDTHH:mm:ss Z)
TIMESTAMP="$(date '+%Y-%m-%dT%H:%M:%S %Z')"
# Below you have two options:
# OPTION 1 (DEFAULT BEHAVIOR): Set a commit message before committing VyOS changes by
# setting the environment variable $M. Otherwise we'll use a generic commit message if
# $M is empty.
#
# OPTION 2: Prompt for a git commit message every time this script is run (i.e., after
# every VyOS commit). To use OPTION 2, commend out lines 37-41 and uncomment lines
# 44 & 45.
#
# Use generic commit message unless variable "$M" is set (e.g., M="message here" commit;save)
if [ -z "$M" ]; then
MSG="Auto-triggered by $USER@$HOSTNAME config commit: $TIMESTAMP"
else
MSG="$M"
fi
# Prompt user for commit message
#echo "Enter git commit message:"
#read MSG
##
## Do the Work
##
# Source the VyOS script-template to allow VyOS config commands to be exported
source /opt/vyatta/etc/functions/script-template
# Save user's current working directory
USERPATH=$PWD
# Change to repo directory & start processing
cd $REPO_PATH/$CONFIG_REPO
echo -e "\e[1;32m> [\e[1;37m $TIMESTAMP\e[1;32m ]\e[1;36m Beginning git commit & push...\e[0m"
# Git: Perform a 'pull' to ensure our local repo is up to date
echo -e "\e[1;32m> [\e[1;37m $TIMESTAMP\e[1;32m ]\e[1;36m Ensuring our local repo is up-to-date...\e[0m"
/usr/bin/git pull --ff-only
# VyOS: Save config commands & json to files
echo -e "\e[1;32m> [\e[1;37m $TIMESTAMP\e[1;32m ]\e[1;36m Saving configuration files...\e[0m"
run show configuration commands > $REPO_PATH/$CONFIG_REPO/$HOSTNAME.commands.conf
run show configuration > $REPO_PATH/$CONFIG_REPO/$HOSTNAME.config.show.txt
cp /config/config.boot $REPO_PATH/$CONFIG_REPO/$HOSTNAME.config.boot
# Git: Stage changes
echo -e "\e[1;32m> [\e[1;37m $TIMESTAMP\e[1;32m ]\e[1;36m Staging changes...\e[0m"
/usr/bin/git add -A
# Git: Commit changes
echo -e "\e[1;32m> [\e[1;37m $TIMESTAMP\e[1;32m ]\e[1;36m Committing changes...\e[0m"
/usr/bin/git commit -m "$MSG"
# Git: Push changes to remote
echo -e "\e[1;32m> [\e[1;37m $TIMESTAMP\e[1;32m ]\e[1;36m Pushing changes to remote repository...\e[0m"
/usr/bin/git push
# Process complete
echo -e "\e[1;32m> [\e[1;37m $TIMESTAMP\e[1;32m ]\e[1;36m Git commit & push completed.\e[0m"
# Change back to user's last $PWD
cd $USERPATH
# Clean up variables
CONFIG_REPO=""
REPO_PATH=""
TIMESTAMP=""
USERPATH=""
MSG=""
M=""