github.com/containerd/nerdctl@v1.7.7/pkg/composer/stop.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package composer
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"sync"
    23  
    24  	"github.com/containerd/containerd"
    25  	"github.com/containerd/log"
    26  	"github.com/containerd/nerdctl/pkg/composer/serviceparser"
    27  	"github.com/containerd/nerdctl/pkg/labels"
    28  	"github.com/containerd/nerdctl/pkg/strutil"
    29  )
    30  
    31  // StopOptions stores all option input from `nerdctl compose stop`
    32  type StopOptions struct {
    33  	Timeout *uint
    34  }
    35  
    36  // Stop stops containers in `services` without removing them. It calls
    37  // `nerdctl stop CONTAINER_ID` to do the actual job.
    38  func (c *Composer) Stop(ctx context.Context, opt StopOptions, services []string) error {
    39  	serviceNames, err := c.ServiceNames(services...)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	// reverse dependency order
    44  	for _, svc := range strutil.ReverseStrSlice(serviceNames) {
    45  		containers, err := c.Containers(ctx, svc)
    46  		if err != nil {
    47  			return err
    48  		}
    49  		if err := c.stopContainers(ctx, containers, opt); err != nil {
    50  			return err
    51  		}
    52  	}
    53  	return nil
    54  }
    55  
    56  func (c *Composer) stopContainers(ctx context.Context, containers []containerd.Container, opt StopOptions) error {
    57  	var timeoutArg string
    58  	if opt.Timeout != nil {
    59  		// `nerdctl stop` uses `--time` instead of `--timeout`
    60  		timeoutArg = fmt.Sprintf("--time=%d", *opt.Timeout)
    61  	}
    62  
    63  	var rmWG sync.WaitGroup
    64  	for _, container := range containers {
    65  		container := container
    66  		rmWG.Add(1)
    67  		go func() {
    68  			defer rmWG.Done()
    69  			info, _ := container.Info(ctx, containerd.WithoutRefreshedMetadata)
    70  			log.G(ctx).Infof("Stopping container %s", info.Labels[labels.Name])
    71  			args := []string{"stop"}
    72  			if opt.Timeout != nil {
    73  				args = append(args, timeoutArg)
    74  			}
    75  			args = append(args, container.ID())
    76  			if err := c.runNerdctlCmd(ctx, args...); err != nil {
    77  				log.G(ctx).Warn(err)
    78  			}
    79  		}()
    80  	}
    81  	rmWG.Wait()
    82  
    83  	return nil
    84  }
    85  
    86  func (c *Composer) stopContainersFromParsedServices(ctx context.Context, containers map[string]serviceparser.Container) {
    87  	var rmWG sync.WaitGroup
    88  	for id, container := range containers {
    89  		id := id
    90  		container := container
    91  		rmWG.Add(1)
    92  		go func() {
    93  			defer rmWG.Done()
    94  			log.G(ctx).Infof("Stopping container %s", container.Name)
    95  			if err := c.runNerdctlCmd(ctx, "stop", id); err != nil {
    96  				log.G(ctx).Warn(err)
    97  			}
    98  		}()
    99  	}
   100  	rmWG.Wait()
   101  }