|
| 1 | +#!/usr/bin/env bash |
| 2 | +GIT_USER=${GIT_USER:-git} |
| 3 | +APP_USER=${APP_USER:-appuser} |
| 4 | +GIT_HOME=${GIT_HOME:-/srv/git} |
| 5 | +REPO_NAME=${REPO_NAME:-carrot-cruncher} |
| 6 | +REPO_DIR="${GIT_HOME}/repositories/${REPO_NAME}.git" |
| 7 | +WORK_DIR="/opt/git" |
| 8 | +APP_DIR=${APP_DIR:-/opt/app} |
| 9 | +DEFAULT_BRANCH=${DEFAULT_BRANCH:-main} |
| 10 | +SSH_PORT=${SSH_PORT:-2332} |
| 11 | +BRANCH_NAME=${BRANCH_NAME:-release/bunnies_v1} |
| 12 | + |
| 13 | +# Install ezlog |
| 14 | +command -v git >/dev/null || { apt-get update && apt-get install -y git; } |
| 15 | +[[ -d /usr/local/share/ezlog ]] || git clone 'https://github.com/opensourcecorp/ezlog.git' /usr/local/share/ezlog |
| 16 | +# shellcheck disable=SC1091 |
| 17 | +source /usr/local/share/ezlog/src/main.sh |
| 18 | + |
| 19 | +function _setup_ssh_keys_for_git_user() { |
| 20 | + local ssh_dir="/home/${GIT_USER}/.ssh" |
| 21 | + local public_key_file="${ssh_dir}/id_rsa.pub" |
| 22 | + local private_key_file="${ssh_dir}/id_rsa" |
| 23 | + local authorized_keys_file="${ssh_dir}/authorized_keys" |
| 24 | + |
| 25 | + # Create .ssh directory if it doesn't exist |
| 26 | + mkdir -p "${ssh_dir}" |
| 27 | + chown "${GIT_USER}:${GIT_USER}" "${ssh_dir}" |
| 28 | + chmod 700 "${ssh_dir}" |
| 29 | + |
| 30 | + # Generate the SSH key pair if it doesn't exist |
| 31 | + if [[ ! -f "${public_key_file}" ]]; then |
| 32 | + su - "${GIT_USER}" -c "ssh-keygen -t rsa -f ${private_key_file} -q -N ''" |
| 33 | + fi |
| 34 | + |
| 35 | + # Add the public key to authorized_keys if it's not already there |
| 36 | + if ! grep -q "$(cat "${public_key_file}")" "${authorized_keys_file}" 2>/dev/null; then |
| 37 | + cat "${public_key_file}" >>"${authorized_keys_file}" |
| 38 | + fi |
| 39 | + chmod 600 "${authorized_keys_file}" |
| 40 | + chown "${GIT_USER}:${GIT_USER}" "${authorized_keys_file}" |
| 41 | + [[ -d /home/${APP_USER}/.ssh ]] || mkdir /home/"${APP_USER}"/.ssh |
| 42 | + cat <<EOF >/home/"${APP_USER}"/.ssh/config |
| 43 | +HOST localhost |
| 44 | + USER ${GIT_USER} |
| 45 | + PORT ${SSH_PORT} |
| 46 | +EOF |
| 47 | +} |
| 48 | + |
| 49 | +function _add_to_known_hosts() { |
| 50 | + local ssh_dir="/home/${GIT_USER}/.ssh" |
| 51 | + local known_hosts_file="${ssh_dir}/known_hosts" |
| 52 | + su - "${GIT_USER}" -c "ssh-keyscan -p ${SSH_PORT} -H localhost >> ${known_hosts_file}" |
| 53 | + chmod 644 "${known_hosts_file}" |
| 54 | +} |
| 55 | + |
| 56 | +function _setup_git_user() { |
| 57 | + if id "${GIT_USER}" &>/dev/null; then |
| 58 | + log-info "User ${GIT_USER} already exists." |
| 59 | + else |
| 60 | + log-info "setting up git user" |
| 61 | + useradd -m "${GIT_USER}" || return 1 |
| 62 | + echo "${GIT_USER}:${GIT_USER}" | chpasswd |
| 63 | + fi |
| 64 | + _setup_ssh_keys_for_git_user |
| 65 | + # _add_to_known_hosts |
| 66 | + which git-shell >>/etc/shells |
| 67 | + chsh --shell "$(command -v /bin/bash)" "${GIT_USER}" |
| 68 | +} |
| 69 | + |
| 70 | +function _init_git_repo() { |
| 71 | + log-info "Initializing remote carrot cruncher" |
| 72 | + rm -rf "${REPO_DIR}" |
| 73 | + mkdir -p "${REPO_DIR}" |
| 74 | + [[ -d "${GIT_HOME}/ssh-keys" ]] || mkdir "${GIT_HOME}/ssh-keys" |
| 75 | + chown -R "${GIT_USER}:${GIT_USER}" "${GIT_HOME}" |
| 76 | + pushd "${REPO_DIR}" >/dev/null || exit |
| 77 | + su - "${GIT_USER}" -c "git config --global init.defaultBranch ${DEFAULT_BRANCH}" |
| 78 | + su - "${GIT_USER}" -c "git config --global user.email 'bugs@bigbadbunnies.com'" |
| 79 | + su - "${GIT_USER}" -c "git config --global user.name 'Bugs Bunny'" |
| 80 | + su - "${GIT_USER}" -c "pushd ""${REPO_DIR}"" >/dev/null; git init --bare" |
| 81 | + popd >/dev/null || exit |
| 82 | +} |
| 83 | + |
| 84 | +function _setup_local_clone() { |
| 85 | + local clone_dir="${WORK_DIR}/${REPO_NAME}" |
| 86 | + log-info "cloning carrot cruncher" |
| 87 | + if [ -d "${WORK_DIR}" ]; then |
| 88 | + rm -rf "${WORK_DIR}" |
| 89 | + fi |
| 90 | + mkdir "${WORK_DIR}" |
| 91 | + chmod 777 "${WORK_DIR}" |
| 92 | + pushd "${WORK_DIR}" >/dev/null || exit |
| 93 | + su - "${GIT_USER}" -c "GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=accept-new' git clone '${GIT_USER}@localhost:${REPO_DIR}' ${clone_dir}" |
| 94 | + git config --global --add safe.directory /opt/git/carrot-cruncher |
| 95 | + pushd "${clone_dir}" >/dev/null || exit |
| 96 | + cp -r "${APP_DIR}"/* . |
| 97 | + sed -i 's/PrintLine/Println/g' main.go |
| 98 | + su - "${GIT_USER}" -c "pushd ${clone_dir}; git add .; git commit -m 'Initial commit'; git push origin" |
| 99 | + popd >/dev/null || exit |
| 100 | +} |
| 101 | + |
| 102 | +function _create_release_branch() { |
| 103 | + local clone_dir="${WORK_DIR}/${REPO_NAME}" |
| 104 | + local branch_2="v1.0.2-rc-tmp-bugfix-2.0.1" |
| 105 | + pushd "${clone_dir}" >/dev/null || exit |
| 106 | + log-info "setting up release branch" |
| 107 | + su - "${GIT_USER}" -c "pushd ${clone_dir}; git checkout -b '${BRANCH_NAME}'" |
| 108 | + sed -i -e 's/printing/picking/g' -e 's/money/carrots/g' -e 's/CHA-CHING/CRUNCH/g' main.go |
| 109 | + echo -e "Name: Bugs Bunny\nSecurity Question Answer: 'Crunchy King'\nSSN: 1234-BUNNY" >banking.txt |
| 110 | + su - "${GIT_USER}" -c "pushd ${clone_dir}; git add .; git commit -m 'Prepare release branch'" |
| 111 | + rm banking.txt |
| 112 | + su - "${GIT_USER}" -c "pushd ${clone_dir}; git add .; git commit -m 'oops did not mean to add that...'" |
| 113 | + su - "${GIT_USER}" -c "pushd ${clone_dir}; git push --set-upstream origin '${BRANCH_NAME}'" |
| 114 | + su - "${GIT_USER}" -c "pushd ${clone_dir}; git checkout '${DEFAULT_BRANCH}'" |
| 115 | + su - "${GIT_USER}" -c "pushd ${clone_dir}; git checkout -b '${branch_2}'" |
| 116 | + sed -i -e 's/printing/uh/g' -e 's/money/oh/g' -e 's/CHA-CHING/NO-NO-NOOOOO/g' main.go |
| 117 | + su - "${GIT_USER}" -c "pushd ${clone_dir}; git add .; git commit -m 'I think we might be on to something...'" |
| 118 | + su - "${GIT_USER}" -c "pushd ${clone_dir}; git push --set-upstream origin '${branch_2}'" |
| 119 | + su - "${GIT_USER}" -c "pushd ${clone_dir}; git checkout '${DEFAULT_BRANCH}'" |
| 120 | + su - "${GIT_USER}" -c "pushd ${clone_dir}; git branch -D ${BRANCH_NAME} ${branch_2}" |
| 121 | + popd >/dev/null || exit |
| 122 | +} |
| 123 | + |
| 124 | +function _polish_off() { |
| 125 | + chsh --shell "$(command -v git-shell)" "${GIT_USER}" # switch Git User to git-shell |
| 126 | + [[ -d /home/git/git-shell-commands ]] || mkdir -m 777 /home/git/git-shell-commands |
| 127 | + cat >/home/git/git-shell-commands/no-interactive-login <<\EOF |
| 128 | +#!/bin/sh |
| 129 | +printf '%s\n' "Hi! You've successfully authenticated, but we do not" |
| 130 | +printf '%s\n' "provide interactive shell access." |
| 131 | +exit 128 |
| 132 | +EOF |
| 133 | + chmod 777 /home/git/git-shell-commands/no-interactive-login |
| 134 | + chown -R "${APP_USER}":"${APP_USER}" /opt/git # git appuser ownership of git directory |
| 135 | +} |
| 136 | + |
| 137 | +function main() { |
| 138 | + _setup_git_user |
| 139 | + _init_git_repo |
| 140 | + _setup_local_clone |
| 141 | + _create_release_branch |
| 142 | + _polish_off |
| 143 | + log-info "Git server setup is complete." |
| 144 | +} |
| 145 | + |
| 146 | +main "$@" |
0 commit comments