Skip to content

Commit

Permalink
Fix libvirt existing container check
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Weiße <[email protected]>
  • Loading branch information
daniel-weisse committed Oct 30, 2023
1 parent a52039f commit df61985
Showing 1 changed file with 38 additions and 43 deletions.
81 changes: 38 additions & 43 deletions cli/internal/libvirt/libvirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,55 +53,50 @@ func (r *Runner) Start(ctx context.Context, name, imageName string) error {
}
defer docker.Close()

// check for an existing name file
// check for an existing container
if containerName, err := r.file.Read(r.nameFile); err == nil {
return r.checkExistingContainer(ctx, docker, string(containerName), imageName)
} else if !errors.Is(err, afero.ErrFileNotFound) {
return err
}

return r.startNewContainer(ctx, docker, name+"-libvirt", imageName)
}

// checkExistingContainer performs checks on an existing libvirt container to evaluate if it can be used to run Constellation.
func (r *Runner) checkExistingContainer(ctx context.Context, docker *docker.Client, containerName, imageName string) error {
// check if a container with the same name already exists
containers, err := docker.ContainerList(ctx, types.ContainerListOptions{
Filters: filters.NewArgs(
filters.KeyValuePair{
Key: "name",
Value: fmt.Sprintf("^%s$", containerName),
},
),
All: true,
})
if err != nil {
return err
}
if len(containers) > 1 {
return fmt.Errorf("more than one container with name %q found", containerName)
}
if len(containers) > 1 {
return fmt.Errorf("more than one container with name %q found", containerName)
}
if len(containers) == 1 {
// make sure the container we listed is using the correct image
imageBase := strings.Split(imageName, ":")[0]
if containers[0].Image != imageBase {
return fmt.Errorf("existing libvirt container %q is using a different image: expected %q, got %q", containerName, imageBase, containers[0].Image)
// check if a container with the same name already exists
containers, err := docker.ContainerList(ctx, types.ContainerListOptions{
Filters: filters.NewArgs(
filters.KeyValuePair{
Key: "name",
Value: fmt.Sprintf("^%s$", containerName),
},
),
All: true,
})
if err != nil {
return err
}

// container already exists, check if its running
if containers[0].State == "running" {
return nil
if len(containers) > 1 {
return fmt.Errorf("more than one container with name %q found", containerName)
}
// container exists but is not running, remove it
if err := docker.ContainerRemove(ctx, containers[0].ID, types.ContainerRemoveOptions{Force: true}); err != nil {
return err

// if a container with the same name exists,
// check if it is using the correct image and if it is running
if len(containers) == 1 {
// make sure the container we listed is using the correct image
imageBase := strings.Split(imageName, ":")[0]
if containers[0].Image != imageBase {
return fmt.Errorf("existing libvirt container %q is using a different image: expected %q, got %q", containerName, imageBase, containers[0].Image)
}

// container already exists, check if its running
if containers[0].State == "running" {
// container is up, nothing to do
return nil
}
// container exists but is not running, remove it
// so we can start a new one
if err := docker.ContainerRemove(ctx, containers[0].ID, types.ContainerRemoveOptions{Force: true}); err != nil {
return err
}
}
} else if !errors.Is(err, afero.ErrFileNotFound) {
return err
}

return nil
return r.startNewContainer(ctx, docker, name+"-libvirt", imageName)
}

// startNewContainer starts a new libvirt container using the given image.
Expand Down

0 comments on commit df61985

Please sign in to comment.