-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitbridge.sh
More file actions
executable file
·237 lines (207 loc) · 7.13 KB
/
Copy pathgitbridge.sh
File metadata and controls
executable file
·237 lines (207 loc) · 7.13 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env bash
source utils.sh
SCRIPT_NAME=$0
REAL_ARGS=$@
show_general_help () {
echo -e "USAGE: ${Light_Green}${SCRIPT_NAME}${NC} ${Green}<action>${NC} ${Yellow}[options]${NC} ${Light_Red}...${NC} "
echo -e
echo -e "${Green}ACTIONS:${NC}"
echo -e " list List keys for the current user."
echo -e " add Add a new key for the current user."
echo -e " remove Remove an existing key for the current user."
echo -e " install Install GitBridge on the system."
echo -e " register Registers a public key for the current user."
echo -e
echo -e "${Yellow}OPTIONS:${NC}"
echo -e " -h --help Show this help."
echo -e " Shows the ${Green}action specific help${NC} if an action is specified."
echo -e " -n --userland,--no-root Allow the script to run in userland. ${Light_Red}Untested!${NC}"
echo -e " -f --force Run even if something fails."
echo -e " --target_home </path/to/target> Path to the gitbridge installation (/srv/gitbridge by default)"
echo -e " --target_user <username> local account that will be used for gitbridge (git by default)"
}
show_install_help() {
echo -e "USAGE: ${Light_Green}${SCRIPT_NAME}${NC} ${Green}install${NC} ${Yellow}[options]${NC}"
echo -e
echo -e "DESCRIPTION:"
echo -e "Creates the git user and installs gitbridge on the new user."
echo -e
echo -e "${Yellow}OPTIONS:${NC}"
echo -e " -h --help Show this help."
echo -e " Shows the ${Green}action specific help${NC} if an action is specified."
echo -e " -n --userland,--no-root Allow the script to run in userland. ${Light_Red}Untested!${NC}"
echo -e " -f --force Run even if something fails."
echo -e " --target_home </path/to/target> Path to the gitbridge installation (/srv/gitbridge by default)"
echo -e " --target_user <username> local account that will be used for gitbridge (git by default)"
}
show_register_help() {
echo -e "USAGE: ${Light_Green}${SCRIPT_NAME}${NC} ${Green}register${NC} ${Yellow}[options]${NC} ${Light_Cyan}[ssh publickey]${NC}"
echo -e
echo -e "DESCRIPTION:"
echo -e "Register a new public key for the current user."
echo -e
echo -e "${Yellow}OPTIONS:${NC}"
echo -e " -h --help Show this help."
echo -e " Shows the ${Green}action specific help${NC} if an action is specified."
echo -e " -n --userland,--no-root Allow the script to run in userland. ${Light_Red}Untested!${NC}"
echo -e " -f --force Run even if something fails."
echo -e " --target_home </path/to/target> Path to the gitbridge installation (/srv/gitbridge by default)"
echo -e " --target_user <username> local account that will be used for gitbridge (git by default)"
}
getopt --test
if [ $? != 4 ]; then
eerror "Your installation doesn't support enhanced getopt."
die
fi
echo -e ${Light_Blue} -- GitBridge Managing tool -- ${NC}
echo
SHORT="hnu:f"
LONG="help,user:,userland,no-root,force,target-user:,target-home:"
OPTS=$(getopt --options $SHORT --long $LONG --name "$0" -- "$@")
if [ $? != 0 ]
then eerror Failed to parse options
die
fi
eval set -- "$OPTS"
#Setting default values
ACTIONS="list add remove install register"
#GLOBAL VARS
NO_ROOT=false
SHOW_HELP=false
FORCE=false
ACTION=""
#Extracting arguments
edebug $@
while true
do
case "$1" in
-h | --help )
SHOW_HELP=true
shift
;;
-n | --userland | --no-root )
NO_ROOT=true
shift
;;
-f | --force )
FORCE=true
shift
;;
--target-home )
TARGET_HOME=$2
shift 2
;;
--target-user )
TARGET_USER=$2
shift 2
;;
-u | --user )
CURRENT_USER=$2
shift 2
;;
-- )
shift
;;
*)
if [ -z ${ACTION} ]
then ACTION=$1
if ! $(contains $ACTION $ACTIONS)
then
eerror $ACTION is not a valid action.
eerror See $SCRIPT_NAME --help for more info.
die
fi
shift
break
fi
shift
;;
esac
if [ -z "$1" ]
then
break
fi
done
if $SHOW_HELP
then
if [ "${ACTION}" == "install" ]
then
show_install_help
else
show_general_help
fi
end
fi
if [ -z ${ACTION} ]
then eerror Please specify an action.
ewarn Run ${SCRIPT_NAME} --help for more info.
die
fi
if [ -e ${TARGET_USER} ]
then
edebug Using default target user
TARGET_USER=git
fi
if [ -z ${TARGET_HOME} ]
then
edebug Using default target
TARGET_HOME=/srv/git
fi
if ! ${NO_ROOT}
then
if [ "$EUID" -ne 0 ]
then
eerror This script should run as root.
ewarn If you want to run the script anyway use the --no-root argument.
ewarn Warning : Userland GitBridge installation has not been tested yet.
die
fi
else
ewarn Warning : NO_ROOT is experimental.
fi
if [ -z ${CURRENT_USER} ]
then
CURRENT_USER=$SUDO_USER
if [ -z ${CURRENT_USER} ]
then
CURRENT_USER=$(who am i | awk '{print $1}' | tail -n 1)
fi
ewarn No user specified, assuming user is $CURRENT_USER
fi
if ${FORCE}
then
die () {
echo
eerror die called, but --force option is set.
}
fi
if [ "${ACTION}" == "install" ]
then
#User creation
id $TARGET_USER > /dev/null 2>&1
if [ $? -ne 0 ]
then
einfo Creating user $TARGET_USER with home directory $TARGET_HOME.
useradd -m -d "$TARGET_HOME" "$TARGET_USER"
else
TARGET_HOME=$( getent passwd "$TARGET_USER" | cut -d: -f6 )
ewarn User $TARGET_USER already exists. Home directory is $TARGET_HOME.
fi
WORKDIR="$TARGET_HOME/.gitbridge/"
#Setting up home directory structure.
einfo Setting up $TARGET_HOME directory structure.
mkdir -p "$WORKDIR/users/"
cp bridge.py "$WORKDIR"/bridge
fi
if [ "${ACTION}" == "register" ]
then
id $TARGET_USER > /dev/null 2>&1
if [ $? -ne 0 ]
then
eerror User $TARGET_USER does not exist. Exiting.
die
fi
TARGET_HOME=$( getent passwd "$TARGET_USER" | cut -d: -f6 )
WORKDIR="$TARGET_HOME/.gitbridge/"
mkdir -p "$WORKDIR/users/$CURRENT_USER/"
fi