-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_core
executable file
·128 lines (105 loc) · 4.45 KB
/
sync_core
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
126
127
#!/bin/bash
SERVERNAME=$( hostname --fqdn ) # added a space before `)' to fix vim syntax highlighting issue
function kvdb_global_get_file_name() {
DBF="$KVDBROOT/global.kc"
if [[ ! -f "$DBF" ]]; then
kchashmgr create "$DBF" 2>/dev/null
fi
echo "$DBF"
}
function kvdb_local_get_file_name() {
DBF="$KVDBROOT/local.$DISTRO.kc"
if [[ ! -f "$DBF" ]]; then
kchashmgr create "$DBF" 2>/dev/null
fi
echo "$DBF"
}
function kvdb_global_get() {
DBF=$(kvdb_global_get_file_name)
kchashmgr get "$DBF" "$1" 2>/dev/null
}
function kvdb_global_set() {
DBF=$(kvdb_global_get_file_name)
kchashmgr set "$DBF" "$1" "$2" 2>/dev/null
}
function kvdb_global_remove() {
DBF=$(kvdb_global_get_file_name)
kchashmgr remove "$DBF" "$1" 2>/dev/null
}
function kvdb_global_clear() {
DBF=$(kvdb_global_get_file_name)
kchashmgr clear "$DBF" 2>/dev/null
}
function kvdb_local_get() {
DBF=$(kvdb_local_get_file_name)
kchashmgr get "$DBF" "$1" 2>/dev/null
}
function kvdb_local_set() {
DBF=$(kvdb_local_get_file_name)
kchashmgr set "$DBF" "$1" "$2" 2>/dev/null
}
function kvdb_local_remove() {
DBF=$(kvdb_local_get_file_name)
kchashmgr remove "$DBF" "$1" 2>/dev/null
}
function kvdb_local_clear() {
DBF=$(kvdb_local_get_file_name)
kchashmgr clear "$DBF" 2>/dev/null
}
source "$SCRIPTROOT/syncbin/$1"
GenJSON='import json;from itertools import izip;import sys;i=iter(sys.argv[1:]);print json.dumps(dict(izip(i,i)))'
SIZE=$(cat "$STATUSROOT/$1.json" | python -c 'import json;p=json.loads(raw_input());print "size" in p and p["size"] or -1')
LASTSYNC=$(cat "$STATUSROOT/$1.json" | python -c 'import json;p=json.loads(raw_input());print "lastsync" in p and p["lastsync"] or -1')
NEXTSYNC=$(cat "$STATUSROOT/$1.json" | python -c 'import json;p=json.loads(raw_input());print "nextsync" in p and p["nextsync"] or -1')
LASTSTATUS=$(cat "$STATUSROOT/$1.json" | python -c 'import json;p=json.loads(raw_input());print "status" in p and p["status"] or -1')
UPSTREAM=$(_get_upstream)
python -c "$GenJSON" upstream "$UPSTREAM" size "$SIZE" status syncing lastsync "$LASTSYNC" nextsync "$NEXTSYNC" name $1 > "$STATUSROOT/$1.json" 2>/dev/null
if [[ ! -d "$LOGROOT/$1" ]]; then
mkdir -p "$LOGROOT/$1"
fi
logrotate() {
for ((i=13; i>=0; i--)); do
j=$((i+1))
mv "$LOGROOT/$1/log.stdout.$i" "$LOGROOT/$1/log.stdout.$j" 2>/dev/null
mv "$LOGROOT/$1/log.stderr.$i" "$LOGROOT/$1/log.stderr.$j" 2>/dev/null
mv "$LOGROOT/$1/log.outerr.$i" "$LOGROOT/$1/log.outerr.$j" 2>/dev/null
done
cp "$LOGROOT/$1/log.stdout" "$LOGROOT/$1/log.stdout.0"
cp "$LOGROOT/$1/log.stderr" "$LOGROOT/$1/log.stderr.0"
cp "$LOGROOT/$1/log.outerr" "$LOGROOT/$1/log.outerr.0"
}
rm -f "$LOGROOT/$1/log.stdout"
rm -f "$LOGROOT/$1/log.stderr"
rm -f "$LOGROOT/$1/log.outerr"
date -u > "$MIRRORROOT/$1/Archive-Update-in-Progress-${SERVERNAME}";
_SAVELOG=1
_sync > >(tee -a "$LOGROOT/$1/log.outerr" > "$LOGROOT/$1/log.stdout") 2> >(tee -a "$LOGROOT/$1/log.outerr" > "$LOGROOT/$1/log.stderr")
RETVAL=$?
if [[ "$_SAVELOG" == "1" ]]; then
logrotate "$1";
fi
if [[ $RETVAL == 0 || $RETVAL == 23 ]]; then
LASTSTATUS=success
if [[ -f "$MIRRORROOT/$1/Archive-Update-in-Progress-${SERVERNAME}" ]]; then
rm "$MIRRORROOT/$1/Archive-Update-in-Progress-${SERVERNAME}"
fi
SIZE=$(_get_size)
NEXTSYNC=$(_get_next_sync_time)
LASTSYNC=$(date +%s)
python -c "$GenJSON" upstream "$UPSTREAM" size "$SIZE" status success lastsync "$LASTSYNC" nextsync "$NEXTSYNC" name $1 > "$STATUSROOT/$1.json" 2>/dev/null
_post_sync > >(tee -a "$LOGROOT/$1/log.outerr" >> "$LOGROOT/$1/log.stdout" ) 2> >(tee -a "$LOGROOT/$1/log.outerr" >> "$LOGROOT/$1/log.stderr")
elif [[ $RETVAL == 5 ]]; then
LASTSTATUS=failed
if [[ -f "$MIRRORROOT/$1/Archive-Update-in-Progress-${SERVERNAME}" ]]; then
rm "$MIRRORROOT/$1/Archive-Update-in-Progress-${SERVERNAME}"
fi
NEXTSYNC=$(_get_next_sync_time)
python -c "$GenJSON" upstream "$UPSTREAM" size "$SIZE" status success lastsync "$LASTSYNC" nextsync "$NEXTSYNC" name $1 > "$STATUSROOT/$1.json" 2>/dev/null
else
LASTSTATUS=failed
if [[ -f "$MIRRORROOT/$1/Archive-Update-in-Progress-${SERVERNAME}" ]]; then
rm "$MIRRORROOT/$1/Archive-Update-in-Progress-${SERVERNAME}"
fi
NEXTSYNC=$(_get_next_sync_time)
python -c "$GenJSON" upstream "$UPSTREAM" size "$SIZE" status failed lastsync "$LASTSYNC" nextsync "$NEXTSYNC" name $1 > "$STATUSROOT/$1.json" 2>/dev/null
fi