talos
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5package vm6
7import (8"context"9"fmt"10"os"11"path/filepath"12
13yaml "gopkg.in/yaml.v3"14
15"github.com/siderolabs/talos/pkg/provision"16)
17
18// Reflect decode state file.
19func (p *Provisioner) Reflect(ctx context.Context, clusterName, stateDirectory string) (provision.Cluster, error) {20statePath := filepath.Join(stateDirectory, clusterName)21
22st, err := os.Stat(statePath)23if err != nil {24if os.IsNotExist(err) {25return nil, fmt.Errorf("cluster %q not found: %w", clusterName, err)26}27
28return nil, err29}30
31if !st.IsDir() {32return nil, fmt.Errorf("state path %q is not a directory: %s", statePath, st.Mode())33}34
35stateFile, err := os.Open(filepath.Join(statePath, stateFileName))36if err != nil {37return nil, err38}39
40defer stateFile.Close() //nolint:errcheck41
42state := &State{}43
44if err = yaml.NewDecoder(stateFile).Decode(state); err != nil {45return nil, fmt.Errorf("error unmarshalling state file: %w", err)46}47
48if state.ProvisionerName != p.Name {49return nil, fmt.Errorf("cluster %q was created with different provisioner %q", clusterName, state.ProvisionerName)50}51
52state.statePath = statePath53
54return state, nil55}
56