-
Notifications
You must be signed in to change notification settings - Fork 0
/
inotif
executable file
·125 lines (111 loc) · 3.58 KB
/
inotif
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/sh
PATH="/sbin:/bin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/bin:/usr/bin"
#set -x
# Define username and branch name for git config
user=$(whoami)
home=$(getent passwd $user | cut -d: -f6)
# Store proccess id.
echo $$ > $home/.inotif.pid
exit1() {
echo "$1"
exit 1;
}
# function for get or generate verified interval in range 5-10 minutes.
verify_interval() {
local v=$(echo $1 | awk '( $1 >= 300) && ($1 <= 600) { print $1 }')
[ ! -z $v ] || v=300
echo $v
}
# Handling error.
check_requirements() {
# checking required variables
if [ -z $interval ] || [ -z $consul ] || [ -z $repo ] || [ -z $dir ] || [ -z $branch ]; then
exit1 "can't load variables completely."
fi
# checking dependency in remote ssh host
hash jq > /dev/null 2>&1 || exit1 "Errpr: jq not installed."
hash git > /dev/null 2>&1 || exit1 "Errpr: git not installed."
hash curl > /dev/null 2>&1 || exit1 "Errpr: curl not installed."
hash rsync > /dev/null 2>&1 || exit1 "Error: rsync not installed."
}
# Load and get inotif configuration
load_environments() {
if [ -f /etc/inotif.conf ]; then
CONF="/etc/inotif.conf"
elif [ -f conf/inotif.conf ]; then
CONF="conf/inotif.conf"
else
exit1 "can't found inotif.conf"
fi
. $CONF
# Get Primary IP for branch name.
branch="$(ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p' | head -n1)" > /dev/null 2>&1
# Clear variable to avoid cache.
repo_tmp='' && dir_list=''
# Get additional directory to dir_list.
dir_list="$dir $(curl -s $consul/v1/kv/inotif/$branch?raw | jq .dir[] | sed "s/\"/ /g")"
# Get repo from consul to override repo value.
repo_tmp="$(curl -s $consul/v1/kv/inotif/config?raw | jq .repo | sed "s/\"/ /g")"
if [ ! -z "$repo_tmp" ]; then
repo="$repo_tmp"
fi
}
# Setup git init.
setup_repo(){
git checkout -B $branch > /dev/null 2>&1
git remote add origin $repo > /dev/null 2>&1 || git remote set-url origin $repo > /dev/null 2>&1
for dir_name in $dir_list; do
if [ -d "$home/.inotif"$dir_name"" ] || mkdir -p "$home/.inotif"$dir_name""; then
# Rsync required folder that max size 1MB, and don't forget to exclude git config
# file contain error permission denied will be skiped.
rsync -aq --delete --max-size=1.0m --exclude=.git* --exclude=.git "$dir_name"/ $home/.inotif"$dir_name"/ > /dev/null 2>&1
fi
done
git config user.name $user
git config user.email $user@$branch
if [ -n "$(git status --porcelain)" ]; then
git add -A > /dev/null 2>&1
git commit -m "inotif-commit" > /dev/null 2>&1
git push origin $branch > /dev/null
echo "Pushed directory: $dir_list"
else
echo "Nothing to commit";
fi
}
inotif_start() {
# Create inotif directory if doesn't exist.
# This directory will be used to store the monitored file or directory.
[ -d $home/.inotif ] || mkdir -p $home/.inotif
# Change to inotif directory.
cd $home/.inotif
# Just updating git repository if git already installed.
# or reinit git when git not installed.
if [ -d "$home/.inotif/.git" ]; then
# Only pull specific branch from repository.
git pull origin $branch > /dev/null 2>&1
git stash > /dev/null 2>&1
# Doing setup repo
setup_repo
else
rm -rf $home/.inotif/*
# Clone if remote repository already stored.
git clone -b $branch $repo ./ > /dev/null 2>&1
if [ $? -eq 0 ]; then
# Doing setup repo
setup_repo
else
# Init git if on repository doesn't exists.
git init > /dev/null 2>&1
# Doing setup repo
setup_repo
fi
fi
}
# Doing job every $interval seconds.
while [ true ]; do
load_environments
check_requirements
inotif_start
interval=$(verify_interval $interval)
sleep $interval
done