-
Notifications
You must be signed in to change notification settings - Fork 107
/
github.sh
executable file
·119 lines (104 loc) · 2.74 KB
/
github.sh
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
#!/bin/bash
function usage() {
echo "USAGE: "
echo "$CMD init"
echo "$CMD push|pull repo_name"
echo ""
echo "$CMD init: Create git.private.pem and git.public.pem under $KEYDIR. Then create leaf directory under this direcotry and git-clone root from Github."
echo "NOTE: A Github repo called root should be created on github.com beforehand."
echo ""
echo "$CMD push repo_name: Make directory repo_name under leaf/ to an compressed archived file into root/ with the same name."
echo "Then add this archived file to git and push it to remote."
echo ""
echo "$CMD pull repo_name: Pull the update files from github to root. Decompress file repo_name under root/ to leaf/."
}
function info() {
echo "[INFO]$@"
}
function error() {
echo "[ERROR]$@"
}
function init() {
cd /
if [ ! -d "$LEAF_DIR" ];
then
mkdir "$LEAF_DIR"
fi
cd "$BASE"
if [ -e "$GITPRIVATE" ] || [ -e "$GITPUBLIC" ];
then
error "Pem files exits with the same name. Exit."
exit 1
fi
info "Create pem files $GITPRIVATE and $GITPUBLIC under $KEYDIR"
openssl req -x509 -nodes -days 100000 -newkey rsa:2048 -keyout "$GITPRIVATE" -out "$GITPUBLIC" -subj '/'
info "Pem files created. Please clone 'root' from your Github under $BASE."
}
function push() {
if [ ! -d "$LEAFREPO" ];
then
error "$LEAFREPO does NOT exist."
exit 1
fi
cd /
info "Push $LEAFREPO to Github"
info "Remove $REPO under $ROOT_DIR/"
rm -f "$ROOTREPO"
info "Encrypt $REPO from $LEAF_DIR to $ROOT_DIR"
tar czf "$LEAFTMP" "$LEAFREPO"
openssl smime -encrypt -aes256 -binary -outform DEM -in "$LEAFTMP" -out "$ROOTREPO" "$BASE/$GITPUBLIC"
rm -f "$LEAFTMP"
cd "$ROOT_DIR"
info "Add to Github"
git add "$REPO"
git commit -m"Push $REPO"
git push -u origin master
info "Finish push $REPO"
}
function pull() {
info "Pull from Github"
cd "$ROOT_DIR"
git pull --rebase
if [ ! -e "$REPO" ];
then
error "$REPO does NOT exist."
exit 1
fi
cd /
info "Decrypting $ROOTREPO to $REPO"
info "$TMP"
openssl smime -decrypt -binary -inform DEM -inkey "$BASE/$GITPRIVATE" -in "$ROOTREPO" -out "$LEAFTMP"
rm -rf "$LEAFREPO"
tar xzf "$LEAFTMP"
rm -r "$LEAFTMP"
info "Finish pull $REPO"
}
BASE="$(cd `dirname $0`; pwd)"
info "BASE=$BASE"
KEYDIR="~/keys"
CMD="$(basename $0)"
ACTION="$1"
ROOT_DIR="$BASE/root"
LEAF_DIR="$BASE/leaf"
REPO="$2"
TMP="$REPO.ttl1"
if ( [ "$ACTION" == "push" ] || [ "$ACTION" == "pull" ] ) && [ -z "$REPO" ];
then
error "Need a repository name."
exit 1
fi
LEAFREPO="$LEAF_DIR/$REPO"
ROOTREPO="$ROOT_DIR/$REPO"
LEAFTMP="$LEAF_DIR/$TMP"
GITPRIVATE="git.private.pem"
GITPUBLIC="git.public.pem"
case $ACTION in
"init")
init ;;
"push")
push "$REPO" ;;
"pull")
pull "$REPO" ;;
*)
usage ;;
esac