-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into feature/workbench-session
- Loading branch information
Showing
10 changed files
with
393 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module entrypoint | ||
|
||
go 1.22.2 | ||
|
||
require ( | ||
github.com/otiai10/copy v1.14.0 // indirect | ||
golang.org/x/sync v0.3.0 // indirect | ||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU= | ||
github.com/otiai10/copy v1.14.0/go.mod h1:ECfuL02W+/FkTWZWgQqXPWZgW9oeKCSQ5qVfSc4qc4w= | ||
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= | ||
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= | ||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= | ||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
cp "github.com/otiai10/copy" | ||
) | ||
|
||
const ( | ||
sourceDir = "/opt/session-components" | ||
targetDir = "/mnt/init" | ||
) | ||
|
||
var ( | ||
// Read the PWB_SESSION_TYPE environment variable | ||
sessionType = os.Getenv("PWB_SESSION_TYPE") | ||
|
||
// Set the copy options. | ||
// Preserve permissions, times, and owner. | ||
opt = cp.Options{ | ||
PermissionControl: cp.PerservePermission, | ||
PreserveTimes: true, | ||
PreserveOwner: true, | ||
NumOfWorkers: 20, | ||
} | ||
|
||
// List of dependencies common to all session types | ||
commonDeps = []string{ | ||
"bin/git-credential-pwb", | ||
"bin/focal", | ||
"bin/jammy", | ||
"bin/noble", | ||
"bin/opensuse15", | ||
"bin/postback", | ||
"bin/pwb-supervisor", | ||
"bin/quarto", | ||
"bin/r-ldpath", | ||
"bin/rhel8", | ||
"bin/rhel9", | ||
"bin/shared-run", | ||
"R", | ||
"resources", | ||
"www", | ||
"www-symbolmaps", | ||
} | ||
|
||
// Map of session-specific dependencies | ||
sessionDeps = map[string][]string{ | ||
"jupyter": { | ||
"bin/jupyter-session-run", | ||
"bin/node", | ||
"extras", | ||
}, | ||
"positron": { | ||
"bin/positron-server", | ||
"bin/positron-session-run", | ||
"extras", | ||
}, | ||
"rstudio": { | ||
"bin/node", | ||
"bin/rsession-run", | ||
}, | ||
"vscode": { | ||
"bin/pwb-code-server", | ||
"bin/vscode-session-run", | ||
"extras", | ||
}, | ||
} | ||
) | ||
|
||
func main() { | ||
if sessionType == "" { | ||
fmt.Println("PWB_SESSION_TYPE environment variable is not set") | ||
os.Exit(1) | ||
} | ||
|
||
programStart := time.Now() | ||
defer func() { | ||
elapsed := time.Since(programStart) | ||
fmt.Printf("Program took %s\n", elapsed) | ||
}() | ||
|
||
filesToCopy, err := getFilesToCopy(sessionType) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
err = validateTargetDir(targetDir) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
err = copyFiles(sourceDir, targetDir, filesToCopy) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
fmt.Println("Copy operation completed.") | ||
} | ||
|
||
// getFilesToCopy returns the list of files to copy based on the session type. | ||
func getFilesToCopy(sessionType string) ([]string, error) { | ||
files := commonDeps | ||
if deps, ok := sessionDeps[sessionType]; ok { | ||
files = append(files, deps...) | ||
} else { | ||
return nil, fmt.Errorf("unknown session type: %s", sessionType) | ||
} | ||
return files, nil | ||
} | ||
|
||
// validateTargetDir checks if the target directory exists and is empty. | ||
func validateTargetDir(targetDir string) error { | ||
if _, err := os.Stat(targetDir); os.IsNotExist(err) { | ||
return fmt.Errorf("cannot find the copy target %s", targetDir) | ||
} | ||
|
||
isEmpty, err := isDirEmpty(targetDir) | ||
if err != nil { | ||
return fmt.Errorf("error checking if target directory is empty: %v", err) | ||
} | ||
if !isEmpty { | ||
return fmt.Errorf("target directory %s is not empty", targetDir) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// isDirEmpty checks if a directory is empty. | ||
func isDirEmpty(dir string) (bool, error) { | ||
f, err := os.Open(dir) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer f.Close() | ||
|
||
_, err = f.ReadDir(1) | ||
if err == io.EOF { | ||
return true, nil | ||
} | ||
return false, err | ||
} | ||
|
||
// copyFiles copies the files from the source directory to the target directory. | ||
// It uses the otiai10/copy package to copy files, with options to preserve | ||
// permissions, times, and owner. | ||
func copyFiles(src, dst string, filesToCopy []string) error { | ||
fmt.Printf("Copying files from %s to %s\n", src, dst) | ||
start := time.Now() | ||
|
||
for _, file := range filesToCopy { | ||
srcPath := filepath.Join(src, file) | ||
dstPath := filepath.Join(dst, file) | ||
err := cp.Copy(srcPath, dstPath, opt) | ||
if err != nil { | ||
return fmt.Errorf("error copying %s: %v", srcPath, err) | ||
} | ||
} | ||
|
||
elapsed := time.Since(start) | ||
fmt.Printf("Copy operation took %s\n", elapsed) | ||
|
||
return nil | ||
} |
Oops, something went wrong.