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:
2021-06-01 19:34:53 +02:00
parent 86816daab1
commit 01f61b3f88

View File

@ -46,7 +46,7 @@ func GetLabeledContainers(cli *client.Client) ([]LabeledContainer, error) {
inspect, _, err := cli.ImageInspectWithRaw(context.Background(), container.ImageID) inspect, _, err := cli.ImageInspectWithRaw(context.Background(), container.ImageID)
if err != nil { 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 { 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()) 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 := *oldContainer.Config
config.Image = image config.Image = image
hostConfig := *oldContainer.HostConfig hostConfig := *oldContainer.HostConfig
hostConfig.VolumesFrom = []string{oldTmpName} hostConfig.VolumesFrom = []string{lc.GetName()}
l.Logger.Infof(`renaming container %s to %s`, lc.GetName(), oldTmpName) l.Logger.Infof("creating new container %s", tmpName)
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())
new, err := cli.ContainerCreate(ctx, &config, &hostConfig, &network.NetworkingConfig{ new, err := cli.ContainerCreate(ctx, &config, &hostConfig, &network.NetworkingConfig{
EndpointsConfig: oldContainer.NetworkSettings.Networks, EndpointsConfig: oldContainer.NetworkSettings.Networks,
}, lc.GetName()) }, tmpName)
if err != nil { if err != nil {
return errors.Wrapf(err, `failed to create container for new version of %s`, image) 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{}) err = cli.ContainerStart(ctx, new.ID, types.ContainerStartOptions{})
if err != nil { if err != nil {
return err 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{ err = cli.ContainerRemove(ctx, oldContainer.ID, types.ContainerRemoveOptions{
RemoveVolumes: false, RemoveVolumes: false,
RemoveLinks: false, RemoveLinks: false,
@ -149,5 +143,11 @@ func (lc LabeledContainer) UpdateTo(cli *client.Client, tag registry.Tag) error
return err 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 return nil
} }