Change renaming behaviour
Create new container with name *.lb.new and then rename it instead of renaming old container first
This commit is contained in:
@ -46,7 +46,7 @@ func GetLabeledContainers(cli *client.Client) ([]LabeledContainer, error) {
|
||||
|
||||
inspect, _, err := cli.ImageInspectWithRaw(context.Background(), container.ImageID)
|
||||
if err != nil {
|
||||
errors.Wrapf(err, "failed to inspect container %s", lc.GetName())
|
||||
return nil, errors.Wrapf(err, "failed to inspect container %s", lc.GetName())
|
||||
}
|
||||
|
||||
if len(inspect.RepoDigests) >= 2 {
|
||||
@ -110,36 +110,30 @@ func (lc LabeledContainer) UpdateTo(cli *client.Client, tag registry.Tag) error
|
||||
return errors.Wrapf(err, `failed to inspect container "%s"`, lc.GetName())
|
||||
}
|
||||
|
||||
oldTmpName := fmt.Sprintf("%s.lb.old", lc.GetName())
|
||||
tmpName := fmt.Sprintf("%s.lb.new", lc.GetName())
|
||||
|
||||
config := *oldContainer.Config
|
||||
config.Image = image
|
||||
|
||||
hostConfig := *oldContainer.HostConfig
|
||||
hostConfig.VolumesFrom = []string{oldTmpName}
|
||||
hostConfig.VolumesFrom = []string{lc.GetName()}
|
||||
|
||||
l.Logger.Infof(`renaming container %s to %s`, lc.GetName(), oldTmpName)
|
||||
err = cli.ContainerRename(ctx, lc.Container.ID, oldTmpName)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, `failed to rename container "%s" to "%s"`, lc.GetName(), oldTmpName)
|
||||
}
|
||||
|
||||
l.Logger.Infof("creating new container %s", lc.GetName())
|
||||
l.Logger.Infof("creating new container %s", tmpName)
|
||||
new, err := cli.ContainerCreate(ctx, &config, &hostConfig, &network.NetworkingConfig{
|
||||
EndpointsConfig: oldContainer.NetworkSettings.Networks,
|
||||
}, lc.GetName())
|
||||
}, tmpName)
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, `failed to create container for new version of %s`, image)
|
||||
}
|
||||
|
||||
l.Logger.Infof("starting new container %s with id %s", lc.GetName(), new.ID)
|
||||
l.Logger.Infof("starting new container %s with id %s", tmpName, new.ID)
|
||||
err = cli.ContainerStart(ctx, new.ID, types.ContainerStartOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
l.Logger.Infof("removing old container %s", oldTmpName)
|
||||
l.Logger.Infof("removing old container %s", lc.GetName())
|
||||
err = cli.ContainerRemove(ctx, oldContainer.ID, types.ContainerRemoveOptions{
|
||||
RemoveVolumes: false,
|
||||
RemoveLinks: false,
|
||||
@ -149,5 +143,11 @@ func (lc LabeledContainer) UpdateTo(cli *client.Client, tag registry.Tag) error
|
||||
return err
|
||||
}
|
||||
|
||||
l.Logger.Infof(`renaming container %s to %s`, tmpName, lc.GetName())
|
||||
err = cli.ContainerRename(ctx, new.ID, lc.GetName())
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, `failed to rename container "%s" to "%s"`, tmpName, lc.GetName())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user