Skip to content

Commit

Permalink
feat: Support for build image update when exec build or up again (#570)
Browse files Browse the repository at this point in the history
Signed-off-by: Qi Chen <aseaday@hotmail.com>
  • Loading branch information
aseaday authored Jul 8, 2022
1 parent 8f89e4b commit 6cfc0f1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/app/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ var CommandUp = &cli.Command{
Usage: "Launch the CPU container",
Value: false,
},
&cli.BoolFlag{
Name: "force",
Usage: "Force rebuild and run the container although the previous container is running",
Value: false,
},
},

Action: up,
Expand Down Expand Up @@ -185,6 +190,10 @@ func up(clicontext *cli.Context) error {
}
numGPUs := builder.NumGPUs()

err = dockerClient.CleanEnvdIfExists(clicontext.Context, ctr, clicontext.Bool("force"))
if err != nil {
return errors.Wrap(err, "failed to start the envd environment")
}
containerID, containerIP, err := dockerClient.StartEnvd(clicontext.Context,
tag, ctr, buildContext, gpu, numGPUs, sshPortInHost, *ir.DefaultGraph, clicontext.Duration("timeout"),
clicontext.StringSlice("volume"))
Expand Down
43 changes: 43 additions & 0 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,50 @@ func (b generalBuilder) NumGPUs() int {
return ir.NumGPUs()
}

// Always return updated when met error
func (b generalBuilder) CheckDepsFileUpdate(ctx context.Context, tag string, deps []string) (bool, error) {
dockerClient, err := docker.NewClient(ctx)
if err != nil {
return true, err
}
image, err := dockerClient.GetImage(ctx, tag)
if err != nil {
return true, err
}
imageCreatedTime := image.Created

latestTimestamp := int64(0)
for _, dep := range deps {
file, err := os.Stat(dep)
if err != nil {
return true, err
}
modifiedtime := file.ModTime().Unix()
// Only needt o use the latest modified time
if modifiedtime > latestTimestamp {
latestTimestamp = modifiedtime
}
}
if latestTimestamp > imageCreatedTime {
return true, nil
}
return false, nil
}

func (b generalBuilder) Build(ctx context.Context, pub string) error {
depsFiles := []string{
pub,
b.configFilePath,
b.manifestFilePath,
}
isUpdated, err := b.CheckDepsFileUpdate(ctx, b.tag, depsFiles)
if err != nil {
b.logger.Debugf("failed to check manifest update: %s", err)
}
if !isUpdated {
b.logger.Infof("manifest is not updated, skip building")
return nil
}
def, err := b.compile(ctx, pub)
if err != nil {
return errors.Wrap(err, "failed to compile")
Expand Down
28 changes: 28 additions & 0 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Client interface {
gpuEnabled bool, numGPUs int, sshPort int, g ir.Graph, timeout time.Duration,
mountOptionsStr []string) (string, string, error)
StartBuildkitd(ctx context.Context, tag, name, mirror string) (string, error)
CleanEnvdIfExists(ctx context.Context, name string, force bool) error

IsRunning(ctx context.Context, name string) (bool, error)
Exists(ctx context.Context, name string) (bool, error)
Expand Down Expand Up @@ -340,6 +341,33 @@ func (c generalClient) StartBuildkitd(ctx context.Context, tag, name, mirror str
return container.Name, nil
}

func (c generalClient) CleanEnvdIfExists(ctx context.Context, name string, force bool) error {
created, err := c.Exists(ctx, name)
if err != nil {
return err
}
if !created {
return nil
}

// force delete the container no matter it is running or not.
if force {
return c.ContainerRemove(ctx, name, types.ContainerRemoveOptions{
Force: true,
})
}

running, _ := c.IsRunning(ctx, name)
if err != nil {
return err
}
if running {
logrus.Errorf("container %s is running, cannot clean envd, please save your data and stop the running container if you need to envd up again.", name)
return errors.New("Still running envd container")
}
return c.ContainerRemove(ctx, name, types.ContainerRemoveOptions{})
}

// StartEnvd creates the container for the given tag and container name.
func (c generalClient) StartEnvd(ctx context.Context, tag, name, buildContext string,
gpuEnabled bool, numGPUs int, sshPortInHost int, g ir.Graph, timeout time.Duration,
Expand Down

0 comments on commit 6cfc0f1

Please sign in to comment.