github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/ocihook/state/state.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 state
    18  
    19  import (
    20  	"encoding/json"
    21  	"errors"
    22  	"fmt"
    23  	"os"
    24  	"path/filepath"
    25  	"time"
    26  
    27  	"github.com/containerd/nerdctl/v2/pkg/lockutil"
    28  )
    29  
    30  // This is meant to store stateful informations about containers that we receive from ocihooks
    31  // We are storing them inside the container statedir
    32  // Note that you MUST use WithLock to perform any operation (like Read or Write).
    33  // Typically:
    34  // lf.WithLock(func ()error {
    35  //   lf.Load()
    36  //   // Modify something on the object
    37  //   lf.StartedAt = ...
    38  //   lf.Save()
    39  // })
    40  
    41  const (
    42  	lifecycleFile = "lifecycle.json"
    43  )
    44  
    45  func NewLifecycleState(stateDir string) *LifecycleState {
    46  	return &LifecycleState{
    47  		stateDir: stateDir,
    48  	}
    49  }
    50  
    51  type LifecycleState struct {
    52  	stateDir  string
    53  	StartedAt time.Time `json:"started_at"`
    54  }
    55  
    56  func (lf *LifecycleState) WithLock(fun func() error) error {
    57  	err := lockutil.WithDirLock(lf.stateDir, fun)
    58  	if err != nil {
    59  		return fmt.Errorf("failed to lock state dir: %w", err)
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  func (lf *LifecycleState) Load() error {
    66  	data, err := os.ReadFile(filepath.Join(lf.stateDir, lifecycleFile))
    67  	if err != nil {
    68  		if !errors.Is(err, os.ErrNotExist) {
    69  			return fmt.Errorf("unable to read lifecycle file: %w", err)
    70  		}
    71  	} else {
    72  		err = json.Unmarshal(data, lf)
    73  		if err != nil {
    74  			return fmt.Errorf("unable to unmarshall lifecycle data: %w", err)
    75  		}
    76  	}
    77  	return nil
    78  }
    79  
    80  func (lf *LifecycleState) Save() error {
    81  	data, err := json.Marshal(lf)
    82  	if err != nil {
    83  		return fmt.Errorf("unable to marshall lifecycle data: %w", err)
    84  	}
    85  	err = os.WriteFile(filepath.Join(lf.stateDir, lifecycleFile), data, 0600)
    86  	if err != nil {
    87  		return fmt.Errorf("unable to write lifecycle file: %w", err)
    88  	}
    89  	return nil
    90  }