From ad12a5d6f892c69156f0d78bc974161865c0582a Mon Sep 17 00:00:00 2001 From: Vikyath Harekal Date: Sun, 27 Oct 2024 01:43:58 +0000 Subject: [PATCH] feat: [CDE-332]: Update the gitspace setup scripts to support more OSes (#2873) * feat: [CDE-332]: fix lint * feat: [CDE-332]: Update the gitspace setup scripts to support more OSes --- .../orchestrator/git/script/install_git.sh | 60 ++++++++++++++++++- app/gitspace/orchestrator/ide/vscode.go | 10 +++- .../orchestrator/template/template.go | 14 +++-- .../template/templates/manage_user.sh | 6 +- .../template/templates/setup_ssh_server.sh | 5 +- app/gitspace/orchestrator/user/user_impl.go | 14 +++-- 6 files changed, 90 insertions(+), 19 deletions(-) diff --git a/app/gitspace/orchestrator/git/script/install_git.sh b/app/gitspace/orchestrator/git/script/install_git.sh index 1a36d2246..200f35e33 100644 --- a/app/gitspace/orchestrator/git/script/install_git.sh +++ b/app/gitspace/orchestrator/git/script/install_git.sh @@ -1,7 +1,63 @@ #!/bin/sh -# Source the common OS info script -. ../../common/script/os_info.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() { diff --git a/app/gitspace/orchestrator/ide/vscode.go b/app/gitspace/orchestrator/ide/vscode.go index 5d80e928c..2154d2de1 100644 --- a/app/gitspace/orchestrator/ide/vscode.go +++ b/app/gitspace/orchestrator/ide/vscode.go @@ -17,6 +17,7 @@ package ide import ( "context" "fmt" + "os" "strconv" "github.com/harness/gitness/app/gitspace/orchestrator/devcontainer" @@ -47,10 +48,15 @@ func (v *VSCode) Setup( ctx context.Context, exec *devcontainer.Exec, ) ([]byte, error) { + osInfoScript, err := os.ReadFile("app/gitspace/orchestrator/common/script/os_info.sh") + if err != nil { + return nil, fmt.Errorf("failed to read os_info.sh: %w", err) + } sshServerScript, err := template.GenerateScriptFromTemplate( templateSetupSSHServer, &template.SetupSSHServerPayload{ - Username: exec.UserIdentifier, - AccessType: exec.AccessType, + Username: exec.UserIdentifier, + AccessType: exec.AccessType, + OSInfoScript: string(osInfoScript), }) if err != nil { return nil, fmt.Errorf( diff --git a/app/gitspace/orchestrator/template/template.go b/app/gitspace/orchestrator/template/template.go index 67e006331..44f02f0f1 100644 --- a/app/gitspace/orchestrator/template/template.go +++ b/app/gitspace/orchestrator/template/template.go @@ -53,15 +53,17 @@ type RunVSCodeWebPayload struct { } type SetupUserPayload struct { - Username string - AccessKey string - AccessType enum.GitspaceAccessType - HomeDir string + Username string + AccessKey string + AccessType enum.GitspaceAccessType + HomeDir string + OSInfoScript string } type SetupSSHServerPayload struct { - Username string - AccessType enum.GitspaceAccessType + Username string + AccessType enum.GitspaceAccessType + OSInfoScript string } type RunSSHServerPayload struct { diff --git a/app/gitspace/orchestrator/template/templates/manage_user.sh b/app/gitspace/orchestrator/template/templates/manage_user.sh index 3e5e02403..5ae118d88 100644 --- a/app/gitspace/orchestrator/template/templates/manage_user.sh +++ b/app/gitspace/orchestrator/template/templates/manage_user.sh @@ -1,12 +1,12 @@ #!/bin/sh -# Source the common OS info script -. ../../common/script/os_info.sh - username={{ .Username }} accessKey="{{ .AccessKey }}" homeDir={{ .HomeDir }} accessType={{ .AccessType }} +osInfoScript={{ .OSInfoScript }} + +eval "$osInfoScript" # Check if the user already exists if id "$username" >/dev/null 2>&1; then diff --git a/app/gitspace/orchestrator/template/templates/setup_ssh_server.sh b/app/gitspace/orchestrator/template/templates/setup_ssh_server.sh index 9d876188c..a9838370c 100644 --- a/app/gitspace/orchestrator/template/templates/setup_ssh_server.sh +++ b/app/gitspace/orchestrator/template/templates/setup_ssh_server.sh @@ -1,7 +1,8 @@ #!/bin/sh -# Source the common OS info script -. ../../common/script/os_info.sh +osInfoScript={{ .OSInfoScript }} + +eval "$osInfoScript" # Install SSH if it's not already installed if ! command -v sshd >/dev/null 2>&1; then diff --git a/app/gitspace/orchestrator/user/user_impl.go b/app/gitspace/orchestrator/user/user_impl.go index 1370551c5..f5438e560 100644 --- a/app/gitspace/orchestrator/user/user_impl.go +++ b/app/gitspace/orchestrator/user/user_impl.go @@ -17,6 +17,7 @@ package user import ( "context" "fmt" + "os" "github.com/harness/gitness/app/gitspace/orchestrator/devcontainer" "github.com/harness/gitness/app/gitspace/orchestrator/template" @@ -34,12 +35,17 @@ func NewUserServiceImpl() Service { } func (u *ServiceImpl) Manage(ctx context.Context, exec *devcontainer.Exec) ([]byte, error) { + osInfoScript, err := os.ReadFile("app/gitspace/orchestrator/common/script/os_info.sh") + if err != nil { + return nil, fmt.Errorf("failed to read os_info.sh: %w", err) + } script, err := template.GenerateScriptFromTemplate( templateManagerUser, &template.SetupUserPayload{ - Username: exec.UserIdentifier, - AccessKey: exec.AccessKey, - AccessType: exec.AccessType, - HomeDir: exec.HomeDir, + Username: exec.UserIdentifier, + AccessKey: exec.AccessKey, + AccessType: exec.AccessType, + HomeDir: exec.HomeDir, + OSInfoScript: string(osInfoScript), }) if err != nil { return nil, fmt.Errorf(