|
8 | 8 | "io/ioutil"
|
9 | 9 | "sync"
|
10 | 10 |
|
| 11 | + "github.com/projecteru2/core/store" |
11 | 12 | "github.com/projecteru2/core/types"
|
12 | 13 | "github.com/projecteru2/core/utils"
|
13 | 14 | log "github.com/sirupsen/logrus"
|
@@ -141,24 +142,43 @@ func (c *Calcium) doReplaceContainer(
|
141 | 142 | ctx,
|
142 | 143 | // if
|
143 | 144 | func(ctx context.Context) error {
|
144 |
| - createMessage = c.doCreateAndStartContainer(ctx, index, node, &opts.DeployOptions, container.CPU, container.VolumePlan) |
145 |
| - return createMessage.Error |
| 145 | + return utils.Txn( |
| 146 | + ctx, |
| 147 | + func(ctx context.Context) error { |
| 148 | + createMessage = c.doCreateAndStartContainer(ctx, index, node, &opts.DeployOptions, container.CPU, container.VolumePlan) |
| 149 | + return createMessage.Error |
| 150 | + }, |
| 151 | + nil, |
| 152 | + func(ctx context.Context) error { |
| 153 | + log.Errorf("[doReplaceContainer] Error when create and start a container, %v", createMessage.Error) |
| 154 | + if createMessage.ContainerID != "" { |
| 155 | + log.Warnf("[doReplaceContainer] Create container failed %v, and container %s not removed", createMessage.Error, createMessage.ContainerID) |
| 156 | + return nil |
| 157 | + } |
| 158 | + if err = c.withNodeLocked(ctx, node.Name, func(node *types.Node) error { |
| 159 | + return c.store.UpdateNodeResource(ctx, node, createMessage.CPU, createMessage.Quota, createMessage.Memory, createMessage.Storage, createMessage.VolumePlan.IntoVolumeMap(), store.ActionIncr) |
| 160 | + }); err != nil { |
| 161 | + log.Errorf("[doReplaceContainer] Reset node resource %s failed %v", node.Name, err) |
| 162 | + } |
| 163 | + return nil |
| 164 | + }, |
| 165 | + c.config.GlobalTimeout, |
| 166 | + ) |
146 | 167 | },
|
147 | 168 | // then
|
148 | 169 | func(ctx context.Context) (err error) {
|
149 | 170 | if err = c.doRemoveContainer(ctx, container, true); err != nil {
|
150 |
| - log.Errorf("[replaceAndRemove] the new started but the old failed to stop") |
| 171 | + log.Errorf("[doReplaceContainer] the new started but the old failed to stop") |
151 | 172 | return
|
152 | 173 | }
|
153 | 174 | removeMessage.Success = true
|
154 | 175 | return
|
155 | 176 | },
|
156 |
| - // else |
157 | 177 | nil,
|
158 | 178 | c.config.GlobalTimeout,
|
159 | 179 | )
|
160 | 180 | },
|
161 |
| - // else |
| 181 | + // rollback |
162 | 182 | func(ctx context.Context) (err error) {
|
163 | 183 | messages, err := c.doStartContainer(ctx, container, opts.IgnoreHook)
|
164 | 184 | if err != nil {
|
|
0 commit comments