-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_create_named_repo_in_private_gitserver.sh
169 lines (139 loc) · 5.69 KB
/
03_create_named_repo_in_private_gitserver.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
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
#!/bin/bash
# #############################################
# The MIT License (MIT)
#
# Copyright © 2020 Michael Czapski
# #############################################
set -o pipefail
set -o errexit
traperr() {
echo "ERROR: ------------------------------------------------"
echo "ERROR: ${BASH_SOURCE[1]} at about ${BASH_LINENO[0]}"
echo "ERROR: ------------------------------------------------"
}
set -o errtrace
trap traperr ERR
declare -r _03_create_named_repo_in_private_gitserver="1.0.1"
# common environment variable values and utility functions
#
[[ ${libSourceMgmt} ]] || source ./libs/libSourceMgmt.sh "1.0.0"
[[ ${__env_GlobalConstants} ]] || source ./utils/__env_GlobalConstants.sh "1.0.0" || exit ${__EXECUTION_ERROR}
[[ ${__env_gitserverConstants} ]] || source ./utils/__env_gitserverConstants.sh "1.0.0" || exit ${__EXECUTION_ERROR}
[[ ${fn__GitserverGeneric} ]] || source ./utils/fn__GitserverGeneric.sh "1.0.0" || exit ${__EXECUTION_ERROR}
[[ ${fn__UtilityGeneric} ]] || source ./utils/fn__UtilityGeneric.sh "1.0.0" || exit ${__EXECUTION_ERROR}
## ==========================================================================
## internal functions
## ==========================================================================
function fn__InputIsValid() {
local -r lUsage='
Usage:
fn__InputIsValid
"${pClientGitRemoteRepoName}"
${pCanonicalClientGitRemoteRepoName}
${pGiterverRemoteRepoNameMaxLen}
${pClientIdRSAPubFilePath}
&& STS=${__DONE}
|| STS=${__FAILED}
'
[[ $# -lt 4 || "${0^^}" == "HELP" ]] && {
echo -e "____ Insufficient number of arguments $@\n${lUsage}"
return ${__FAILED}
}
local -r pClientGitRemoteRepoName=${1?"${lUsage}"}
local -r pCanonicalClientGitRemoteRepoName=${2?"${lUsage}"}
local -r pGiterverRemoteRepoNameMaxLen=${3?"${lUsage}"}
local -r pClientIdRSAPubFilePath=${4?"${lUsage}"}
[[ ${#pCanonicalClientGitRemoteRepoName} -lt 2 ]] && {
echo "____ Git repository name '${pClientGitRemoteRepoName}' translated to '${pCanonicalClientGitRemoteRepoName}'"
echo "____ Git repository name must be at least 2 characters long"
return ${__FAILED}
}
[[ ${#pCanonicalClientGitRemoteRepoName} -gt ${pGiterverRemoteRepoNameMaxLen} ]] && {
echo "____ Final Git repository name '${pCanonicalClientGitRemoteRepoName}' is longer than the maximum of ${pGiterverRemoteRepoNameMaxLen} characters"
echo "____ Git repository name must be no longer than ${pGiterverRemoteRepoNameMaxLen} characters"
return ${__FAILED}
}
[[ -e ${pClientIdRSAPubFilePath} ]] || {
echo "____ RSA Public Key '${pClientIdRSAPubFilePath}' not found"
return ${__FAILED}
}
return ${__DONE}
}
## ##########################################################################################
## ##########################################################################################
##
## ##########################################################################################
## ##########################################################################################
# see if repo name has been provided as $1 - if yes, use it, if no use the default
#
# need remote repo name and location of the id_rsa.pub public key file associated
# with the client which wants to access the gitserver
#
declare pClientGitRemoteRepoName=${1:-${__GITSERVER_REM_TEST_REPO_NAME}}
declare pClientIdRSAPubFilePath=${2:-~/.ssh/id_rsa.pub}
# validate input, if provided
#
lCanonicalClientGitRemoteRepoName=$( fn__SanitizeInputIdentifier ${pClientGitRemoteRepoName} )
fn__InputIsValid \
"${pClientGitRemoteRepoName}" \
${lCanonicalClientGitRemoteRepoName} \
${__GITSERVER_REMOTE_REPO_NAME_MAX_LEN} \
${pClientIdRSAPubFilePath} || {
echo "____ Aborting ..."
exit ${__FAILED}
}
# client id can be extracted from the id_rsa.pub which generated it if the file contains a RSA public key
#
declare lClientIdRSAPub=""
lClientIdRSAPub=$(cat ${pClientIdRSAPubFilePath})
[[ ${lClientIdRSAPub// */} == "ssh-rsa" ]] || {
echo "____ Public key in file '${pClientIdRSAPubFilePath}' does not appear to be the RSA public key - aborting"
exit ${__FAILED}
}
# confirm values
#
echo "____ Set to create remote git repository '${lCanonicalClientGitRemoteRepoName}'"
echo "____ Set to use public key for '${lClientIdRSAPub//* /}'"
fn__ConfirmYN "Proceed?" && true || {
echo "____ Chose NO - Aborting ..."
exit ${__FAILED}
}
# client's public key must be in git server's authorised_keys file for ssh commands do be able to be executed
#
fn__AddClientPublicKeyToServerAuthorisedKeysStore \
"${lClientIdRSAPub}" \
${__GITSERVER_CONTAINER_NAME} \
${__GIT_USERNAME} \
${__GITSERVER_SHELL} \
&& STS=${__DONE} \
|| STS=${__FAILED}
# if repo already exists we can't create a new one with the same name
#
fn__DoesRepoAlreadyExist \
${lCanonicalClientGitRemoteRepoName} \
${__GITSERVER_CONTAINER_NAME} \
${__GIT_USERNAME} \
${__GITSERVER_SHELL} \
&& {
echo "____ Git Repository '${lCanonicalClientGitRemoteRepoName}' already exists - aborting"
exit
} \
|| STS=$? # can be __NO (good) or __EXECUTION_ERROR (bad)
[[ ${STS} -eq ${__EXECUTION_ERROR} ]] && {
echo "____ Failed to determine whether Git Repository '${lCanonicalClientGitRemoteRepoName}' already exists - aborting"
exit
}
fn__CreateNewClientGitRepositoryOnRemote \
${lCanonicalClientGitRemoteRepoName} \
${__GITSERVER_CONTAINER_NAME} \
${__GIT_USERNAME} \
${__GITSERVER_SHELL} \
${__GITSERVER_REPOS_ROOT} \
&& {
echo "____ Created remote repository '${lCanonicalClientGitRemoteRepoName}'"
exit ${__DONE}
} \
|| {
echo "____ Failed to create remote repository '${lCanonicalClientGitRemoteRepoName}'"
exit ${__FAILED}
}