Skip to content

Commit

Permalink
prepare and run shell command file in container
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammedikinci committed Apr 24, 2022
1 parent 7cf28c4 commit da4615d
Showing 1 changed file with 77 additions and 9 deletions.
86 changes: 77 additions & 9 deletions pkg/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,8 @@ func (r *runner) jobRunner() error {
return err
}

for _, cmd := range r.currentJob.Script {
if err := r.commandRunner(cmd); err != nil {
return err
}
if err := r.prepareAndRunShellCommandScript(); err != nil {
return err
}

if err := r.stopCurrentContainer(); err != nil {
Expand All @@ -104,14 +102,47 @@ func (r *runner) jobRunner() error {
return nil
}

func (r *runner) commandRunner(command string) error {
args := strings.Split(command, " ")
func (r *runner) prepareAndRunShellCommandScript() error {
shellFileContains := ""

if args[0] == "cd" && len(args) == 2 {
r.workDir = args[1]
return nil
for _, cmd := range r.currentJob.Script {
shellFileContains += cmd + "\n"
}

if _, err := os.Stat(".pin"); os.IsNotExist(err) {
err = os.Mkdir(".pin", 0644)

if err != nil {
return err
}
}

err := os.WriteFile(".pin/shell_command.sh", []byte(shellFileContains), 0644)

if err != nil {
return err
}

err = r.sendShellCommandFile()

if err != nil {
return err
}

if err := r.commandRunner("chmod +x ./shell_command.sh"); err != nil {
return err
}

if err := r.commandRunner("sh ./shell_command.sh"); err != nil {
return err
}

return nil
}

func (r *runner) commandRunner(command string) error {
args := strings.Split(command, " ")

r.infoLog.Printf("Execute command: %s", command)

exec, err := r.cli.ContainerExecCreate(r.ctx, r.containerResponse.ID, types.ExecConfig{
Expand Down Expand Up @@ -259,6 +290,43 @@ func (r runner) copyToContainer() error {
return nil
}

func (r runner) sendShellCommandFile() error {
var buf bytes.Buffer

data, err := os.ReadFile(".pin/shell_command.sh")

if err != nil {
return err
}

tw := tar.NewWriter(&buf)
defer tw.Close()

err = tw.WriteHeader(&tar.Header{
Name: "shell_command.sh",
Mode: 0777,
Size: int64(len(data)),
})

if err != nil {
return err
}

_, err = tw.Write(data)

if err != nil {
return err
}

err = r.cli.CopyToContainer(r.ctx, r.containerResponse.ID, r.workDir, &buf, types.CopyToContainerOptions{})

if err != nil {
return err
}

return nil
}

func (r runner) stopCurrentContainer() error {
r.infoLog.Println("Container stopping")

Expand Down

0 comments on commit da4615d

Please sign in to comment.