Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unlinkat error in make tools and bind mount cleanup #270

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"time"
"os"

dockerTypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
Expand All @@ -19,6 +20,21 @@ func CreateApplicationContainer(containerCfg types.ApplicationContainer) (string
ctx := context.Background()
volume := fmt.Sprintf("%s:%s", containerCfg.StoreDir, containerCfg.WorkDir)

// Create the host directory for bind mount with appropriate permissions
err := os.MkdirAll(containerCfg.StoreDir, 0755)
if err != nil {
return "", fmt.Errorf("failed to create directory: %w", err)
}
// Set proper permissions for the host directory
err = os.Chown(containerCfg.StoreDir, os.Getuid(), os.Getgid())
if err != nil {
return "", fmt.Errorf("failed to set ownership: %w", err)
}
err = os.Chmod(containerCfg.StoreDir, 0755)
if err != nil {
return "", fmt.Errorf("failed to set permissions: %w", err)
}

// convert map to list of strings
envArr := []string{}
for key, value := range containerCfg.Env {
Expand Down
34 changes: 33 additions & 1 deletion lib/docker/delete.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
package docker

import (
"fmt"
"github.com/sdslabs/gasper/lib/utils"
"github.com/docker/docker/api/types"
"golang.org/x/net/context"
)

// DeleteContainer deletes a docker container
func DeleteContainer(containerID string) error {
ctx := context.Background()
err := StopContainer(containerID)

// Inspect the container to get its working directory
containerJSON, err := cli.ContainerInspect(ctx, containerID)
if err != nil {
utils.LogError("Docker-DeleteContainer-1", err)
return err
}
workingDir := containerJSON.Config.WorkingDir
if workingDir != "" {
// Clear the working directory inside the container, including hidden files and directories
cmd := []string{"sh", "-c", fmt.Sprintf("rm -rf %s/* %s/.*", workingDir, workingDir)}
execConfig := types.ExecConfig{
Cmd: cmd,
AttachStdout: true,
AttachStderr: true,
Privileged: true,
}

execIDResp, err := cli.ContainerExecCreate(ctx, containerID, execConfig)
if err != nil {
utils.LogError("Docker-DeleteContainer-2", err)
return err
}

err = cli.ContainerExecStart(ctx, execIDResp.ID, types.ExecStartCheck{})
if err != nil {
utils.LogError("Docker-DeleteContainer-3", err)
return err
}
}

err = StopContainer(containerID)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions scripts/build/install_fresh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mkdir -p bin
tmp_dir=$(mktemp -d -t ci-XXXXXXXXXX)
cd $tmp_dir
GOPATH=$tmp_dir go install github.com/pilu/fresh@latest
chmod -R u+rw $tmp_dir
cp $tmp_dir/bin/fresh $project_dir/bin/fresh
rm -rf $tmp_dir

Expand Down
1 change: 1 addition & 0 deletions scripts/build/install_golint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mkdir -p bin
tmp_dir=$(mktemp -d -t ci-XXXXXXXXXX)
cd $tmp_dir
GOPATH=$tmp_dir go install golang.org/x/lint/golint@latest
chmod -R u+rw $tmp_dir
cp $tmp_dir/bin/golint $project_dir/bin/golint
rm -rf $tmp_dir

Expand Down
17 changes: 11 additions & 6 deletions services/appmaker/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ func containerCleanup(appName string) error {

// diskCleanup cleans the specified application's container and local storage
func diskCleanup(appName string) {
err := containerCleanup(appName)
if err != nil {
utils.LogError("AppMaker-Helper-5", fmt.Errorf("container cleanup failed for %s: %w", appName, err))
return
}

appDir := filepath.Join(path, fmt.Sprintf("storage/%s", appName))
storeCleanupChan := make(chan error)
go func() {
storeCleanupChan <- storageCleanup(appDir)
}()
containerCleanup(appName)
<-storeCleanupChan
err = storageCleanup(appDir)
if err != nil {
utils.LogError("AppMaker-Helper-6", fmt.Errorf("storage cleanup failed for %s: %w", appName, err))
return
}
}

// stateCleanup removes the application's data from MongoDB and Redis
Expand Down