7 Commits

Author SHA1 Message Date
50f53d3a35 Fix REMOTE HOST in new clone for host in ssh config check 2026-01-09 14:07:30 +09:00
4fc2c7f009 Update new clone.sh usage and argument parsing
Use proper arguments for new clone
2026-01-09 11:30:44 +09:00
08e9ff9e67 Add help and fix problem with Host name set for new clone
The host name ise set as ssh config entry with ssh key, so we use the short name set for the remote host name
2025-11-17 11:12:54 +09:00
329f3abb6b in create SSH command with escaped empty string for passphrase
Make sure there is "" visible in the ouput
2025-11-11 13:03:21 +09:00
495879207c Fix SSH PEM key setup script
Remove ":" from host name
Prepare proper host name without user
Set user from hostname part
Add command for vim to edit ssh config
2025-11-11 12:58:57 +09:00
6048c11791 Readme update 2025-09-11 18:00:48 +09:00
4d0a684ac6 Base setup update with ssh config create script 2025-09-11 17:57:51 +09:00
6 changed files with 133 additions and 24 deletions

View File

@@ -37,20 +37,25 @@ Planned to get auto fixes for wrong ACL, etc or missing config settings
A SSH Key has to be generated for each git respository that will be synced and the configuration has to be added to the ssh config file
```sh
create_ssh_config.sh [SSH Key name] [repo url full] ([jump proxy])
create_ssh_config.sh [repo url full] ([jump proxy])
```
> [!notice]
> This will currently output the command to create the SSH key and the host config to add to the ssh config file
### new_clone.sh
Create a new clone
```sh
new_clone.sh [repository] [branch] ([host]) ([remote name])
new_clone.sh -r <Repository URL> -b <Branch Name> [-H <Host name>] [-f <Folder name>] [-n <Remote name>]
```
The [host] is the SSH Host name entry, this will replace any "[host]:" in the [repository]. If the [host] is not set the host set in the [repository] will be used. If nothing found the script will abort
The [host (-H)] is the SSH Host name entry, this will replace any "[host]:" in the [repository]. If the [host (-H)] is not set the host set in the [repository] will be used. If nothing found the script will abort. Note that if [host (-H)] is not set the host name will be the Repository name which has to match the SSH config setting.
A [branch] name must be set all the time.
A [branch (-B)] name must be set all the time.
An override folder naem can be set with `-f`
An optional [remote name] can be set, if not set "origin" will be used.

View File

@@ -90,6 +90,7 @@ if [ -d "${GIT_WEBHOOK_BASE_FOLDER}" ]; then
"${BASE_FOLDER}init.sh" \
"${BASE_FOLDER}git_sync.sh" \
"${BASE_FOLDER}switch_branch.sh" \
"${BASE_FOLDER}create_ssh_config.sh" \
"${GIT_WEBHOOK_BASE_FOLDER}${CLONE_SCRIPTS_FOLDER}";
cp "${CONFIG_BASE}/webhook.default.cfg" \
"${GIT_WEBHOOK_BASE_FOLDER}${CONFIG_FOLDER}";
@@ -99,6 +100,7 @@ if [ -d "${GIT_WEBHOOK_BASE_FOLDER}" ]; then
"${BASE_FOLDER}init.sh" \
"${BASE_FOLDER}git_sync.sh" \
"${BASE_FOLDER}switch_branch.sh" \
"${BASE_FOLDER}create_ssh_config.sh" \
"${CONFIG_BASE}/webhook.default.cfg";
# check config entries missing
exit;
@@ -180,6 +182,7 @@ EOF
"${BASE_FOLDER}init.sh" \
"${BASE_FOLDER}git_sync.sh" \
"${BASE_FOLDER}switch_branch.sh" \
"${BASE_FOLDER}create_ssh_config.sh" \
"${GIT_WEBHOOK_BASE_FOLDER}${CLONE_SCRIPTS_FOLDER}";
cp \
"${CONFIG_BASE}/webhook.cfg" \
@@ -191,6 +194,7 @@ EOF
"${BASE_FOLDER}init.sh" \
"${BASE_FOLDER}git_sync.sh" \
"${BASE_FOLDER}switch_branch.sh" \
"${BASE_FOLDER}create_ssh_config.sh" \
"${CONFIG_BASE}/webhook.cfg" \
"${CONFIG_BASE}/webhook.default.cfg";
fi;

View File

@@ -13,6 +13,10 @@
REPOSITORY="$1";
JUMP_PROXY="$2";
if [ "${REPOSITORY}" == "--help" ]; then
echo "$0 <Repo.git> [<Jump Proxy>]";
exit;
fi;
# below are only to skip error
BRANCH="-"
@@ -28,7 +32,9 @@ if [ ! -f "${SSH_CONFIG_BASE}config" ]; then
fi;
if [[ "${REPOSITORY}" == *":"* ]]; then
REMOTE_HOST=$(echo "${REPOSITORY}" | cut -d ":" -f 1);
REMOTE_USER_HOST=$(echo "${REPOSITORY}" | cut -d ":" -f 1);
REMOTE_HOST=$(echo "${REMOTE_USER_HOST}" | cut -d "@" -f 2);
REMOTE_USER=$(echo "${REMOTE_USER_HOST}" | cut -d "@" -f 1);
else
echo "[!] Must set a full repository path with remote host for the repository";
error=1;
@@ -59,7 +65,8 @@ fi;
# SUDO_COMMAND= as base
# ssh-keygen -t ed25519 -N "" -C "${GIT_REPOSITORY_NAME}" -f "${SSH_CONFIG_BASE}${SSH_KEY_NAME}"
# must add ".pem" if key name does not end in .pem
SSH_COMMAND=("${SUDO_COMMAND[@]}" "ssh-keygen" "-t" "ed25519" "-N" "" "-C" "${GIT_REPOSITORY_NAME}" "-f" "${SSH_CONFIG_BASE}${GIT_REPOSITORY_NAME}.pem")
SSH_COMMAND=("${SUDO_COMMAND[@]}" "ssh-keygen" "-t" "ed25519" "-N" "\"\"" "-C" "${GIT_REPOSITORY_NAME}" "-f" "${SSH_CONFIG_BASE}${GIT_REPOSITORY_NAME}.pem")
SSH_CONFIG_COMMAND=("${SUDO_COMMAND[@]}" "vim" "${SSH_CONFIG_BASE}config")
# debug output for now
echo "";
@@ -69,9 +76,11 @@ echo "${SSH_COMMAND[*]}";
echo "";
echo "* ADD TO: ${SSH_CONFIG_BASE}config";
echo "";
echo "Host ${GIT_REPOSITORY_NAME}":
echo "${SSH_CONFIG_COMMAND[*]}";
echo "";
echo "Host ${GIT_REPOSITORY_NAME}"
echo " Hostname ${REMOTE_HOST}";
echo " User git";
echo " User ${REMOTE_USER}";
echo " PreferredAuthentications publickey";
echo " IdentityFile ~/.ssh/${GIT_REPOSITORY_NAME}.pem";
if [ -n "${JUMP_PROXY}" ]; then

View File

@@ -7,6 +7,10 @@
REPOSITORY="$1";
BRANCH="$2";
REMOTE_NAME="$3";
if [ "${REPOSITORY}" == "--help" ]; then
echo "$0 <Repo.git> <branch> [<remote name, defaults to origin>]";
exit;
fi;
if [ -z "${REMOTE_NAME}" ]; then
REMOTE_NAME="origin"
fi;

View File

@@ -6,11 +6,99 @@
# COMMAND: new_clone.sh <Repo.git> <branch> [<host>] [<Repo Target Name>] [<remote name>]
REPOSITORY="$1";
BRANCH="$2";
REMOTE_HOST="$3";
REPOSITORY_FOLDER="$4"
REMOTE_NAME="$5";
function error() {
if [ -t 1 ]; then echo "[MAK] ERROR: $*" >&2; fi; exit 0;
}
usage() {
cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h | --help] -r | --repository <Repository URL> -b | --branch <Branch Name> [-H | --host <Host name>] [-f | --folder <Folder name>] [-n | --remote <Remote name>]
New clone a git repository via ssh into the clone folder.
Available options:
-h, --help Print this help and exit
-r, --repository <Repository URL> Repository path (e.g. user/repo.git)
-b, --branch <Branch Name> Branch to clone (e.g. main)
-H, --host <Host name> Override SSH host from ssh config (e.g. my-ssh-host)
-f, --folder <Folder name> Target folder name for the repository (e.g. repo-name)
-n, --remote <Remote name> Remote name (defaults to origin)
EOF
exit
}
# REPOSITORY="$1";
# BRANCH="$2";
# REMOTE_HOST="$3";
# REPOSITORY_FOLDER="$4"
# REMOTE_NAME="$5";
# if [ "${REPOSITORY}" == "--help" ]; then
# echo "$0 <Repo.git> <branch> [<override host>] [<target folder>] [<remote name, defaults to origin>]";
# exit;
# fi;
REPOSITORY="";
BRANCH="";
REMOTE_HOST="";
REPOSITORY_FOLDER="";
REMOTE_NAME="origin";
while [ -n "${1-}" ]; do
case "${1}" in
-r | --repository)
REPOSITORY="${2-}";
shift
;;
-b | --branch)
BRANCH="${2-}";
shift
;;
-H | --host)
REMOTE_HOST="${2-}";
shift
;;
-f | --folder)
REPOSITORY_FOLDER="${2-}";
shift
;;
-n | --remote)
REMOTE_NAME="${2-}";
shift
;;
-h | --help)
usage
;;
# invalid option
-?*)
error "[!] Unknown option: '$1'."
;;
esac
shift;
done;
# if no repository or banch name given, show error
error=0
if [ -z "${REPOSITORY}" ]; then
echo "[!] Must set a repository full url, ssh only";
error=1;
fi;
if [ -z "${BRANCH}" ]; then
echo "[!] Must set a branch name";
error=1;
fi;
# further checks that repository folder if set can only by alphanumeric, -, _ or .
if [ -n "${REPOSITORY_FOLDER}" ]; then
if ! [[ "${REPOSITORY_FOLDER}" =~ ^[a-zA-Z0-9._-]+$ ]]; then
echo "[!] Repository folder name can only contain alphanumeric characters, dots, dashes or underscores";
error=1;
fi;
fi;
if [ $error -eq 1 ]; then
exit;
fi;
if [ -z "${REMOTE_NAME}" ]; then
REMOTE_NAME="origin"
fi;
@@ -22,15 +110,6 @@ if [ -z "${REPOSITORY}" ]; then
echo "[!] Must set a repository path";
error=1;
fi;
# if remote host is empty try to set from repository
if [ -z "${REMOTE_HOST}" ]; then
if [[ "${REPOSITORY}" == *":"* ]]; then
REMOTE_HOST=$(echo "${REPOSITORY}" | cut -d ":" -f 1);
else
echo "[!] Must set a repository path with remote host for the repository";
error=1;
fi;
fi;
# if we have an ":" in the repository, split by it and replace it with the remote host
if [[ "${REPOSITORY}" == *":"* ]]; then
REPOSITORY=$(echo "${REPOSITORY}" | cut -d ":" -f 2);
@@ -41,14 +120,18 @@ if [ -z "${REPOSITORY_FOLDER}" ]; then
else
GIT_REPOSITORY_NAME="${REPOSITORY_FOLDER}";
fi;
# set remote host if not set to the ssh config name
if [ -z "${REMOTE_HOST}" ]; then
REMOTE_HOST="$(basename "${REPOSITORY}" .git)";
fi;
if [ $error -eq 1 ]; then
exit;
fi;
error=0
echo "* Validate SSH PEM Key exist and SSH config";
if ! grep "Host ${GIT_REPOSITORY_NAME}" "${GIT_WEBHOOK_BASE_FOLDER}"/.ssh/config; then
echo "[!] ssh config entry for Host ${GIT_REPOSITORY_NAME} is missing";
if ! grep "Host ${REMOTE_HOST}" "${GIT_WEBHOOK_BASE_FOLDER}"/.ssh/config; then
echo "[!] ssh config entry for Host ${REMOTE_HOST} is missing in ${GIT_WEBHOOK_BASE_FOLDER}/.ssh/config";
error=1;
else
# make sure the identiy file is there

View File

@@ -8,6 +8,10 @@
REPOSITORY="$1";
BRANCH="$2";
REMOTE_NAME="$3";
if [ "${REPOSITORY}" == "--help" ]; then
echo "$0 <Repo.git> <branch> [<remote name, defaults to origin>]";
exit;
fi;
if [ -z "${REMOTE_NAME}" ]; then
REMOTE_NAME="origin"
fi;