feat: [CDE-332]: updated script for containers (#2875)

* feat: [CDE-332]: updated script for containers
* feat: [CDE-332]: updated script for containers
* feat: [CDE-332]: updated script for containers
* feat: [CDE-332]: updated scrpt for containers
devcontainer-setup
Ansuman Satapathy 2024-10-28 04:25:23 +00:00 committed by Harness
parent 1cb0e477f0
commit 6035f3b73e
4 changed files with 67 additions and 114 deletions

View File

@ -19,18 +19,15 @@ import (
"fmt"
"net/url"
"github.com/harness/gitness/app/gitspace/orchestrator/common"
"github.com/harness/gitness/app/gitspace/orchestrator/devcontainer"
"github.com/harness/gitness/app/gitspace/orchestrator/template"
"github.com/harness/gitness/app/gitspace/scm"
_ "embed"
)
var _ Service = (*ServiceImpl)(nil)
//go:embed script/install_git.sh
var installScript string
const templateGitInstallScript string = "install_git.sh"
const templateSetupGitCredentials = "setup_git_credentials.sh" // nolint:gosec
const templateCloneCode = "clone_code.sh"
@ -42,9 +39,16 @@ func NewGitServiceImpl() Service {
}
func (g *ServiceImpl) Install(ctx context.Context, exec *devcontainer.Exec) ([]byte, error) {
script, err := template.GenerateScriptFromTemplate(
templateGitInstallScript, &template.SetupGitInstallPayload{
OSInfoScript: common.GetOSInfoScript(),
})
if err != nil {
return nil, fmt.Errorf(
"failed to generate scipt to setup git install from template %s: %w", templateGitInstallScript, err)
}
output := "Setting up git inside container\n"
_, err := exec.ExecuteCommandInHomeDirectory(ctx, installScript, false, false)
_, err = exec.ExecuteCommandInHomeDirectory(ctx, script, false, false)
if err != nil {
return nil, fmt.Errorf("failed to setup git: %w", err)
}

View File

@ -1,107 +0,0 @@
#!/bin/sh
# Detect OS type
os() {
uname="$(uname)"
case $uname in
Linux) echo linux ;;
Darwin) echo macos ;;
FreeBSD) echo freebsd ;;
*) echo "$uname" ;;
esac
}
# Detect Linux distro type
distro() {
local os_name
os_name=$(os)
if [ "$os_name" = "macos" ] || [ "$os_name" = "freebsd" ]; then
echo "$os_name"
return
fi
if [ -f /etc/os-release ]; then
(
. /etc/os-release
if [ "${ID_LIKE-}" ]; then
for id_like in $ID_LIKE; do
case "$id_like" in debian | fedora | opensuse | arch)
echo "$id_like"
return
;;
esac
done
fi
echo "$ID"
)
return
fi
}
# Print a human-readable name for the OS/distro
distro_name() {
if [ "$(uname)" = "Darwin" ]; then
echo "macOS v$(sw_vers -productVersion)"
return
fi
if [ -f /etc/os-release ]; then
(
. /etc/os-release
echo "$PRETTY_NAME"
)
return
fi
uname -sr
}
# Install Git if not already installed
install_git() {
# Check if Git is installed
if ! command -v git >/dev/null 2>&1; then
echo "Git is not installed. Installing Git..."
case "$(distro)" in
debian | ubuntu)
apt-get update && apt-get install -y git
;;
fedora | centos | rhel)
dnf install -y git
;;
opensuse)
zypper install -y git
;;
alpine)
apk add git
;;
arch | manjaro)
pacman -Sy --noconfirm git
;;
freebsd)
pkg install -y git
;;
macos)
brew install git
;;
*)
echo "Unsupported OS for automatic Git installation."
return 1
;;
esac
fi
# Verify installation
if ! command -v git >/dev/null 2>&1; then
echo "Git is not installed. Exiting..."
exit 1
else
echo "Git is installed."
fi
}
# Run the installation function
install_git

View File

@ -44,6 +44,10 @@ type CloneCodePayload struct {
Email string
}
type SetupGitInstallPayload struct {
OSInfoScript string
}
type SetupGitCredentialsPayload struct {
CloneURLWithCreds string
}

View File

@ -0,0 +1,52 @@
#!/bin/sh
osInfoScript={{ .OSInfoScript }}
eval "$osInfoScript"
# Install Git if not already installed
install_git() {
# Check if Git is installed
if ! command -v git >/dev/null 2>&1; then
echo "Git is not installed. Installing Git..."
case "$(distro)" in
debian | ubuntu)
apt-get update && apt-get install -y git
;;
fedora | centos | rhel)
dnf install -y git
;;
opensuse)
zypper install -y git
;;
alpine)
apk add git
;;
arch | manjaro)
pacman -Sy --noconfirm git
;;
freebsd)
pkg install -y git
;;
macos)
brew install git
;;
*)
echo "Unsupported OS for automatic Git installation."
return 1
;;
esac
fi
# Verify installation
if ! command -v git >/dev/null 2>&1; then
echo "Git is not installed. Exiting..."
exit 1
else
echo "Git is installed."
fi
}
# Run the installation function
install_git