Fixed installation code

This commit is contained in:
FlUxIuS 2024-05-27 16:03:08 +02:00
parent 040237a5ab
commit f39702d314
2 changed files with 17 additions and 7 deletions

View File

@ -123,7 +123,7 @@ func init() {
rootCmd.AddCommand(commitCmd) rootCmd.AddCommand(commitCmd)
rootCmd.AddCommand(pullCmd) rootCmd.AddCommand(pullCmd)
rootCmd.AddCommand(renameCmd) rootCmd.AddCommand(renameCmd)
//rootCmd.AddCommand(installCmd) // TODO: fix this function rootCmd.AddCommand(installCmd) // TODO: fix this function
installCmd.Flags().StringVarP(&ExecCmd, "install", "i", "", "function for installation") installCmd.Flags().StringVarP(&ExecCmd, "install", "i", "", "function for installation")
installCmd.Flags().StringVarP(&ContID, "container", "c", "", "container to run") installCmd.Flags().StringVarP(&ContID, "container", "c", "", "container to run")
pullCmd.Flags().StringVarP(&ImageRef, "image", "i", "", "image reference") pullCmd.Flags().StringVarP(&ImageRef, "image", "i", "", "image reference")

View File

@ -252,6 +252,8 @@ func DockerExec(contid string, WorkingDir string) {
*/ */
ctx := context.Background() ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
execShell := []string{}
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -268,7 +270,11 @@ func DockerExec(contid string, WorkingDir string) {
var oldState *terminal.State var oldState *terminal.State
if (dockerObj.shell != "/bin/bash") { // Attach and Exec the binarry if (dockerObj.shell != "") {
execShell = append(execShell, strings.Split(dockerObj.shell, " ")...)
}
if (dockerObj.shell != "/bin/bash") { // Attach and Exec the binarr
optionsCreate := types.ExecConfig{ optionsCreate := types.ExecConfig{
WorkingDir: WorkingDir, WorkingDir: WorkingDir,
AttachStdin: true, AttachStdin: true,
@ -277,16 +283,18 @@ func DockerExec(contid string, WorkingDir string) {
Detach: false, Detach: false,
Privileged: true, Privileged: true,
Tty: true, Tty: true,
Cmd: []string{dockerObj.shell}, Cmd: execShell,
} }
fmt.Println(WorkingDir)
rst, err := cli.ContainerExecCreate(ctx, contid, optionsCreate) rst, err := cli.ContainerExecCreate(ctx, contid, optionsCreate)
if err != nil { if err != nil {
panic(err) panic(err)
} }
optionsStartCheck := types.ExecStartCheck{ optionsStartCheck := types.ExecStartCheck{
Detach: true, Detach: false,
Tty: true, Tty: true,
} }
@ -295,6 +303,9 @@ func DockerExec(contid string, WorkingDir string) {
panic(err) panic(err)
} }
go io.Copy(os.Stdout, response.Reader)
go io.Copy(os.Stderr, response.Reader)
go io.Copy(response.Conn, os.Stdin)
defer response.Close() defer response.Close()
statusCh, errCh := cli.ContainerWait(ctx, contid, container.WaitConditionNextExit) statusCh, errCh := cli.ContainerWait(ctx, contid, container.WaitConditionNextExit)
@ -305,7 +316,6 @@ func DockerExec(contid string, WorkingDir string) {
} }
case <-statusCh: case <-statusCh:
} }
} else { // Interactive mode } else { // Interactive mode
response, err := cli.ContainerAttach(ctx, contid, container.AttachOptions{ response, err := cli.ContainerAttach(ctx, contid, container.AttachOptions{
Stderr: true, Stderr: true,
@ -354,12 +364,12 @@ func DockerExec(contid string, WorkingDir string) {
} }
} }
// TODO: fix this function // TODO: Optimize it and handle errors
func DockerInstallFromScript(contid string) { func DockerInstallFromScript(contid string) {
/* Hot install inside a created Docker container /* Hot install inside a created Docker container
in(1): string function script to use in(1): string function script to use
*/ */
s := fmt.Sprintf("/root/scripts/postinstall.sh %s", dockerObj.shell) s := fmt.Sprintf("./entrypoint.sh %s", dockerObj.shell)
fmt.Println(s) fmt.Println(s)
dockerObj.shell = s dockerObj.shell = s
DockerExec(contid, "/root/scripts") DockerExec(contid, "/root/scripts")