forked from runfinch/finch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
179 lines (155 loc) · 6.72 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// Package config handles parsing and applying options from finch's config
// file. These options can be applied to any aspect of the project, from the VMM
// to components running inside the VM.
//
// Currently, VMM options are applied to one of Lima's configuration files and options
// within the VM are applied via running SSH commands and writing files via SFTP.
package config
import (
"errors"
"fmt"
"path"
"strconv"
"strings"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/spf13/afero"
"gopkg.in/yaml.v3"
"github.com/runfinch/finch/pkg/command"
"github.com/runfinch/finch/pkg/flog"
"github.com/runfinch/finch/pkg/fmemory"
"github.com/runfinch/finch/pkg/system"
)
// AdditionalDirectory represents the additional directory used in Finch config.
type AdditionalDirectory struct {
Path *string `yaml:"path"`
}
// Finch represents the configuration file for Finch CLI.
type Finch struct {
CPUs *int `yaml:"cpus"`
Memory *string `yaml:"memory"`
// AdditionalDirectories are the work directories that are not supported by default. In macOS, only home directory is supported by default.
// For example, if you want to mount a directory into a container, and that directory is not under your home directory,
// then you'll need to specify this field to add that directory or any ascendant of it as a work directory.
AdditionalDirectories []AdditionalDirectory `yaml:"additional_directories,omitempty"`
// VMType sets which technology to use for Finch's VM.
// Currently supports `qemu` and `vz` (Virtualization.framework).
// Also sets mountType to "virtiofs", instead of the default "reverse-sshfs"
// Requires macOS 13.0 or later.
// This setting will only be applied on vm init.
VMType *limayaml.VMType `yaml:"vmType,omitempty"`
// Use Rosetta 2 when available. Forces vmType to "vz" (Virtualization.framework) if set to `true`.
// Requires macOS 13.0 or later and an Apple Silicon (ARM64) mac.
// Has no effect on systems where Rosetta 2 is not available (Intel/AMD64 macs, or macOS < 13.0).
// This setting will only be applied on vm init.
Rosetta *bool `yaml:"rosetta,omitempty"`
}
// Nerdctl is a copy from github.com/containerd/nerdctl/cmd/nerdctl/main.go
// TODO: make PR to nerdctl repo to move this config out of the main package
// so it can be imported on macOS.
type Nerdctl struct {
Debug bool `toml:"debug,omitempty"`
DebugFull bool `toml:"debug_full1,omitempty"`
Address string `toml:"address,omitempty"`
Namespace string `toml:"namespace,omitempty"`
Snapshotter string `toml:"snapshotter,omitempty"`
CNIPath string `toml:"cni_path,omitempty"`
CNINetConfPath string `toml:"cni_netconfpath,omitempty"`
DataRoot string `toml:"data_root,omitempty"`
CgroupManager string `toml:"cgroup_manager,omitempty"`
InsecureRegistry bool `toml:"insecure_registry,omitempty"`
HostsDir []string `toml:"hosts_dir,omitempty"`
}
// LimaConfigApplier applies lima configuration changes.
//
//go:generate mockgen -copyright_file=../../copyright_header -destination=../mocks/pkg_config_lima_config_applier.go -package=mocks -mock_names LimaConfigApplier=LimaConfigApplier . LimaConfigApplier
type LimaConfigApplier interface {
Apply(isInit bool) error
}
// NerdctlConfigApplier applies nerdctl configuration changes.
//
//go:generate mockgen -copyright_file=../../copyright_header -destination=../mocks/pkg_config_nerdctl_config_applier.go -package=mocks -mock_names NerdctlConfigApplier=NerdctlConfigApplier . NerdctlConfigApplier
type NerdctlConfigApplier interface {
Apply(remoteAddr string) error
}
// LoadSystemDeps contains the system dependencies for Load.
//
//go:generate mockgen -copyright_file=../../copyright_header -destination=../mocks/pkg_config_load_system_deps.go -package=mocks -mock_names LoadSystemDeps=LoadSystemDeps . LoadSystemDeps
type LoadSystemDeps interface {
system.RuntimeCPUGetter
}
// writeConfig writes a config struct back to a YAML file at a path.
func writeConfig(cfg *Finch, fs afero.Fs, path string) error {
cfgBuf, err := yaml.Marshal(cfg)
if err != nil {
return fmt.Errorf("failed to write to marshal config: %w", err)
}
if err := afero.WriteFile(fs, path, cfgBuf, 0o755); err != nil {
return fmt.Errorf("failed to write to config file: %w", err)
}
return nil
}
func ensureConfigDir(fs afero.Fs, path string, log flog.Logger) error {
dirExists, err := afero.DirExists(fs, path)
if err != nil {
return fmt.Errorf("failed to get status of config directory: %w", err)
}
if !dirExists {
log.Infof("%q directory doesn't exist, attempting to create it", path)
if err := fs.Mkdir(path, 0o755); err != nil {
return fmt.Errorf("failed to create config directory: %w", err)
}
}
return nil
}
// Load loads Finch's configuration from a YAML file and initializes default values.
func Load(fs afero.Fs, cfgPath string, log flog.Logger, systemDeps LoadSystemDeps, mem fmemory.Memory) (*Finch, error) {
b, err := afero.ReadFile(fs, cfgPath)
if err != nil {
if errors.Is(err, afero.ErrFileNotFound) {
log.Infof("Using default values due to missing config file at %q", cfgPath)
defCfg := applyDefaults(&Finch{}, systemDeps, mem)
if err := ensureConfigDir(fs, path.Dir(cfgPath), log); err != nil {
return nil, fmt.Errorf("failed to ensure %q directory: %w", cfgPath, err)
}
if err := writeConfig(defCfg, fs, cfgPath); err != nil {
return nil, err
}
return defCfg, nil
}
return nil, fmt.Errorf("failed to read the config file: %w", err)
}
var cfg Finch
if err := yaml.Unmarshal(b, &cfg); err != nil {
return nil, fmt.Errorf("failed to unmarshal config file: %w", err)
}
defCfg := applyDefaults(&cfg, systemDeps, mem)
if err := writeConfig(defCfg, fs, cfgPath); err != nil {
return nil, err
}
if err := validate(defCfg, log, systemDeps, mem); err != nil {
return nil, fmt.Errorf("failed to validate config file: %w", err)
}
return defCfg, nil
}
// SupportsVirtualizationFramework checks if the user's system supports Virtualization.framework.
func SupportsVirtualizationFramework(cmdCreator command.Creator) (bool, error) {
cmd := cmdCreator.Create("sw_vers", "-productVersion")
out, err := cmd.Output()
if err != nil {
return false, fmt.Errorf("failed to run sw_vers command: %w", err)
}
splitVer := strings.Split(string(out), ".")
if len(splitVer) == 0 {
return false, fmt.Errorf("unexpected result from string split: %v", splitVer)
}
majorVersionInt, err := strconv.ParseInt(splitVer[0], 10, 64)
if err != nil {
return false, fmt.Errorf("failed to parse split sw_vers output (%s) into int: %w", splitVer[0], err)
}
if majorVersionInt >= 13 {
return true, nil
}
return false, nil
}