-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: lunchpail "needs" support for installing dependencies
Signed-off-by: aavarghese <[email protected]>
- Loading branch information
1 parent
8331951
commit 590ca66
Showing
25 changed files
with
404 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package subcommands | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"lunchpail.io/cmd/subcommands/needs" | ||
) | ||
|
||
func init() { | ||
var cmd = &cobra.Command{ | ||
Use: "needs", | ||
GroupID: internalGroup.ID, | ||
Short: "Commands for installing dependencies to run the application", | ||
Long: "Commands for installing dependencies to run the application", | ||
} | ||
|
||
rootCmd.AddCommand(cmd) | ||
cmd.AddCommand(needs.Minio()) | ||
cmd.AddCommand(needs.Python()) | ||
} |
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,32 @@ | ||
package needs | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"lunchpail.io/cmd/options" | ||
"lunchpail.io/pkg/runtime/needs" | ||
) | ||
|
||
func Minio() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "minio <version>", | ||
Short: "Install minio", | ||
Long: "Install minio", | ||
Args: cobra.MatchAll(cobra.MaximumNArgs(1), cobra.OnlyValidArgs), | ||
} | ||
|
||
logOpts := options.AddLogOptions(cmd) | ||
|
||
cmd.RunE = func(cmd *cobra.Command, args []string) error { | ||
version := "latest" | ||
if len(args) > 0 { | ||
version = args[0] | ||
} | ||
|
||
return needs.InstallMinio(context.Background(), version, needs.Options{LogOptions: *logOpts}) | ||
} | ||
|
||
return cmd | ||
} |
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,36 @@ | ||
package needs | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"lunchpail.io/cmd/options" | ||
"lunchpail.io/pkg/runtime/needs" | ||
) | ||
|
||
func Python() *cobra.Command { | ||
var requirementsPath string | ||
var virtualEnvPath string | ||
cmd := &cobra.Command{ | ||
Use: "python <version> [-r /path/to/requirements.txt] [-v /path/to/.venv]", | ||
Short: "Install python environment", | ||
Long: "Install python environment", | ||
Args: cobra.MatchAll(cobra.MaximumNArgs(1), cobra.OnlyValidArgs), | ||
} | ||
|
||
logOpts := options.AddLogOptions(cmd) | ||
cmd.Flags().StringVarP(&requirementsPath, "requirements", "r", requirementsPath, "Install from the given requirements file") | ||
cmd.Flags().StringVarP(&virtualEnvPath, "venv", "d", virtualEnvPath, "Path to virtual environment dir") | ||
|
||
cmd.RunE = func(cmd *cobra.Command, args []string) error { | ||
version := "latest" | ||
if len(args) >= 1 { | ||
version = args[0] | ||
} | ||
|
||
return needs.InstallPython(context.Background(), version, virtualEnvPath, requirementsPath, needs.Options{LogOptions: *logOpts}) | ||
} | ||
|
||
return cmd | ||
} |
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
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,81 @@ | ||
package needs | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"os/user" | ||
"path/filepath" | ||
) | ||
|
||
func homedir() (string, error) { | ||
currentUser, err := user.Current() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return currentUser.HomeDir, nil | ||
} | ||
|
||
func installMinio(ctx context.Context, version string, verbose bool) error { | ||
if err := setenv(); err != nil { //$HOME must be set for brew | ||
return err | ||
} | ||
|
||
return brewInstall(ctx, "minio/stable/minio", version, verbose) //Todo: versions other than latest | ||
} | ||
|
||
func installPython(ctx context.Context, version string, verbose bool) error { | ||
if err := setenv(); err != nil { //$HOME must be set for brew | ||
return err | ||
} | ||
|
||
return brewInstall(ctx, "python3", version, verbose) //Todo: versions other than latest | ||
} | ||
|
||
func brewInstall(ctx context.Context, pkg string, version string, verbose bool) error { | ||
var cmd *exec.Cmd | ||
if verbose { | ||
fmt.Fprintf(os.Stdout, "Installing %s release of %s \n", version, pkg) | ||
cmd = exec.CommandContext(ctx, "brew", "install", "--verbose", "--debug", pkg) | ||
cmd.Stdout = os.Stdout | ||
} else { | ||
cmd = exec.Command("brew", "install", pkg) | ||
} | ||
cmd.Stderr = os.Stderr | ||
return cmd.Run() | ||
} | ||
|
||
func requirementsInstall(ctx context.Context, venvPath string, requirementsPath string, verbose bool) error { | ||
var cmd *exec.Cmd | ||
var verboseFlag string | ||
dir := filepath.Dir(venvPath) | ||
|
||
if verbose { | ||
verboseFlag = "--verbose" | ||
} | ||
|
||
venvRequirementsPath := filepath.Join(venvPath, filepath.Base(requirementsPath)) | ||
cmds := fmt.Sprintf(`python3 -m venv %s | ||
cp %s %s | ||
source %s/bin/activate | ||
python3 -m pip install --upgrade pip %s | ||
pip3 install -r %s %s 1>&2`, venvPath, requirementsPath, venvPath, venvPath, verboseFlag, venvRequirementsPath, verboseFlag) | ||
|
||
cmd = exec.CommandContext(ctx, "/bin/bash", "-c", cmds) | ||
cmd.Dir = dir | ||
if verbose { | ||
cmd.Stdout = os.Stdout | ||
} | ||
cmd.Stderr = os.Stderr | ||
return cmd.Run() | ||
} | ||
|
||
func setenv() error { | ||
dir, err := homedir() | ||
if err != nil { | ||
return err | ||
} | ||
return os.Setenv("HOME", dir) | ||
} |
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,125 @@ | ||
package needs | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
) | ||
|
||
func bindir() (string, error) { | ||
cachedir, err := os.UserCacheDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return filepath.Join(cachedir, "lunchpail", "bin"), nil | ||
} | ||
|
||
func installMinio(ctx context.Context, version string, verbose bool) error { | ||
if verbose { | ||
fmt.Printf("Installing %s release of minio \n", version) | ||
} | ||
|
||
dir, err := bindir() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := os.MkdirAll(dir, 0755); err != nil { | ||
return err | ||
} | ||
|
||
//Todo: versions other than latest | ||
cmd := exec.CommandContext(ctx, "/bin/sh", "-c", "apt update; apt -y install wget; wget https://dl.min.io/server/minio/release/linux-amd64/minio") | ||
cmd.Dir = dir | ||
if verbose { | ||
cmd.Stdout = os.Stdout | ||
} | ||
cmd.Stderr = os.Stderr | ||
if err := cmd.Run(); err != nil { | ||
return err | ||
} | ||
|
||
if err := setenv(dir); err != nil { //setting $PATH | ||
return err | ||
} | ||
|
||
return os.Chmod(filepath.Join(dir, "minio"), 0755) | ||
} | ||
|
||
func installPython(ctx context.Context, version string, verbose bool) error { | ||
/* | ||
if verbose { | ||
fmt.Fprintf(os.Stdout, "Installing %s release of python \n", version) | ||
} | ||
dir, err := bindir() | ||
if err != nil { | ||
return err | ||
} | ||
if err := os.MkdirAll(dir, 0755); err != nil { | ||
return err | ||
} | ||
//Todo: versions other than latest | ||
cmd := exec.Command("wget", "https://www.python.org/ftp/python/3.12.7/Python-3.12.7.tgz") | ||
cmd.Dir = dir | ||
if verbose { | ||
cmd.Stdout = os.Stdout | ||
} | ||
cmd.Stderr = os.Stderr | ||
if err := cmd.Run(); err != nil { | ||
return err | ||
} | ||
cmd = exec.Command("tar", "xf", "Python-3.12.7.tgz") | ||
cmd.Dir = dir | ||
if verbose { | ||
cmd.Stdout = os.Stdout | ||
} | ||
cmd.Stderr = os.Stderr | ||
if err := cmd.Run(); err != nil { | ||
return err | ||
} | ||
if err := setenv(dir); err != nil { //setting $PATH | ||
return err | ||
} | ||
os.Chmod(filepath.Join(dir, "python"), 0755) | ||
*/ | ||
|
||
return nil | ||
} | ||
|
||
func requirementsInstall(ctx context.Context, venvPath string, requirementsPath string, verbose bool) error { | ||
var cmd *exec.Cmd | ||
var verboseFlag string | ||
dir := filepath.Dir(venvPath) | ||
|
||
if verbose { | ||
verboseFlag = "--verbose" | ||
} | ||
|
||
venvRequirementsPath := filepath.Join(venvPath, filepath.Base(requirementsPath)) | ||
cmds := fmt.Sprintf(`python3 -m venv %s | ||
cp %s %s | ||
source %s/bin/activate | ||
python3 -m pip install --upgrade pip %s | ||
pip3 install -r %s %s 1>&2`, venvPath, requirementsPath, venvPath, venvPath, verboseFlag, venvRequirementsPath, verboseFlag) | ||
|
||
cmd = exec.CommandContext(ctx, "/bin/bash", "-c", cmds) | ||
cmd.Dir = dir | ||
if verbose { | ||
cmd.Stdout = os.Stdout | ||
} | ||
cmd.Stderr = os.Stderr | ||
return cmd.Run() | ||
} | ||
|
||
func setenv(dir string) error { | ||
return os.Setenv("PATH", os.Getenv("PATH")+":"+dir) | ||
} |
Oops, something went wrong.