@@ -15,6 +15,7 @@ import (
15
15
"github.com/spf13/afero/sftpfs"
16
16
17
17
"github.com/runfinch/finch/pkg/fssh"
18
+ "github.com/runfinch/finch/pkg/lima/wrapper"
18
19
)
19
20
20
21
const (
@@ -27,18 +28,20 @@ type nerdctlConfigApplier struct {
27
28
fs afero.Fs
28
29
privateKeyPath string
29
30
hostUser string
31
+ lima wrapper.LimaWrapper
30
32
}
31
33
32
34
var _ NerdctlConfigApplier = (* nerdctlConfigApplier )(nil )
33
35
34
36
// NewNerdctlApplier creates a new NerdctlConfigApplier that
35
37
// applies nerdctl configuration changes by SSHing to the lima VM to update the nerdctl configuration file in it.
36
- func NewNerdctlApplier (dialer fssh.Dialer , fs afero.Fs , privateKeyPath , hostUser string ) NerdctlConfigApplier {
38
+ func NewNerdctlApplier (dialer fssh.Dialer , fs afero.Fs , privateKeyPath string , hostUser string , lima wrapper. LimaWrapper ) NerdctlConfigApplier {
37
39
return & nerdctlConfigApplier {
38
40
dialer : dialer ,
39
41
fs : fs ,
40
42
privateKeyPath : privateKeyPath ,
41
43
hostUser : hostUser ,
44
+ lima : lima ,
42
45
}
43
46
}
44
47
@@ -53,45 +56,6 @@ func addLineToBashrc(fs afero.Fs, profileFilePath string, profStr string, cmd st
53
56
return profStr , nil
54
57
}
55
58
56
- // updateEnvironment adds variables to the user's shell's environment. Currently it uses ~/.bashrc because
57
- // Bash is the default shell and Bash will not load ~/.profile if ~/.bash_profile exists (which it does).
58
- // ~/.bash_profile sources ~/.bashrc, so ~/.bashrc is currently the best place to define additional variables.
59
- // The [GNU docs for Bash] explain how these files work together in more details.
60
- // The default location of DOCKER_CONFIG is ~/.docker/config.json. This config change sets the location to
61
- // ~/.finch/config.json, but from the perspective of macOS (/Users/<user>/.finch/config.json).
62
- // The reason that we don't set environment variables inside /root/.bashrc is that the vars inside it are
63
- // not able to be picked up even if we specify `sudo -E`. We have to switch to root user in order to access them, while
64
- // normally we would access the VM as the regular user.
65
- // For more information on the variable, see the registry nerdctl docs.
66
- //
67
- // [GNU docs for Bash]: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
68
- //
69
- // [registry nerdctl docs]: https://github.com/containerd/nerdctl/blob/master/docs/registry.md
70
-
71
- func updateEnvironment (fs afero.Fs , user string ) error {
72
- cmdArr := [4 ]string {
73
- fmt .Sprintf ("export DOCKER_CONFIG=\" /Users/%s/.finch\" " , user ),
74
- fmt .Sprintf ("[ -L /usr/local/bin/docker-credential-ecr-login ] " +
75
- "|| sudo ln -s /Users/%s/.finch/cred-helpers/docker-credential-ecr-login /usr/local/bin/" , user ),
76
- fmt .Sprintf ("[ -L /root/.aws ] || sudo ln -fs /Users/%s/.aws /root/.aws" , user ),
77
- }
78
-
79
- profileFilePath := fmt .Sprintf ("/home/%s.linux/.bashrc" , user )
80
- profBuf , err := afero .ReadFile (fs , profileFilePath )
81
- if err != nil {
82
- return fmt .Errorf ("failed to read config file: %w" , err )
83
- }
84
- profStr := string (profBuf )
85
- for _ , element := range cmdArr {
86
- profStr , err = addLineToBashrc (fs , profileFilePath , profStr , element )
87
- if err != nil {
88
- return err
89
- }
90
- }
91
-
92
- return nil
93
- }
94
-
95
59
// updateNerdctlConfig reads from the nerdctl config and updates values.
96
60
func updateNerdctlConfig (fs afero.Fs , user string , rootless bool ) error {
97
61
nerdctlRootlessCfgPath := fmt .Sprintf ("/home/%s.linux/.config/nerdctl/nerdctl.toml" , user )
@@ -137,6 +101,49 @@ func updateNerdctlConfig(fs afero.Fs, user string, rootless bool) error {
137
101
return nil
138
102
}
139
103
104
+ // updateEnvironment adds variables to the user's shell's environment. Currently it uses ~/.bashrc because
105
+ // Bash is the default shell and Bash will not load ~/.profile if ~/.bash_profile exists (which it does).
106
+ // ~/.bash_profile sources ~/.bashrc, so ~/.bashrc is currently the best place to define additional variables.
107
+ // The [GNU docs for Bash] explain how these files work together in more details.
108
+ // The default location of DOCKER_CONFIG is ~/.docker/config.json. This config change sets the location to
109
+ // ~/.finch/config.json, but from the perspective of macOS (/Users/<user>/.finch/config.json).
110
+ // The reason that we don't set environment variables inside /root/.bashrc is that the vars inside it are
111
+ // not able to be picked up even if we specify `sudo -E`. We have to switch to root user in order to access them, while
112
+ // normally we would access the VM as the regular user.
113
+ // For more information on the variable, see the registry nerdctl docs.
114
+ //
115
+ // [GNU docs for Bash]: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
116
+ //
117
+ // [registry nerdctl docs]: https://github.com/containerd/nerdctl/blob/master/docs/registry.md
118
+ func (nca * nerdctlConfigApplier ) updateEnvironment (fs afero.Fs ) error {
119
+ cmdArr := [4 ]string {
120
+ fmt .Sprintf ("export DOCKER_CONFIG=\" /Users/%s/.finch\" " , nca .hostUser ),
121
+ fmt .Sprintf ("[ -L /usr/local/bin/docker-credential-ecr-login ] " +
122
+ "|| sudo ln -s /Users/%s/.finch/cred-helpers/docker-credential-ecr-login /usr/local/bin/" , nca .hostUser ),
123
+ fmt .Sprintf ("[ -L /root/.aws ] || sudo ln -fs /Users/%s/.aws /root/.aws" , nca .hostUser ),
124
+ }
125
+
126
+ limaUser , err := nca .lima .LimaUser (false )
127
+ if err != nil {
128
+ return fmt .Errorf ("failed to get lima user: %w" , err )
129
+ }
130
+
131
+ profileFilePath := fmt .Sprintf ("/home/%s.linux/.bashrc" , limaUser .Username )
132
+ profBuf , err := afero .ReadFile (fs , profileFilePath )
133
+ if err != nil {
134
+ return fmt .Errorf ("failed to read config file: %w" , err )
135
+ }
136
+ profStr := string (profBuf )
137
+ for _ , element := range cmdArr {
138
+ profStr , err = addLineToBashrc (fs , profileFilePath , profStr , element )
139
+ if err != nil {
140
+ return err
141
+ }
142
+ }
143
+
144
+ return nil
145
+ }
146
+
140
147
// Apply gets SSH and SFTP clients and uses them to update the nerdctl config.
141
148
func (nca * nerdctlConfigApplier ) Apply (remoteAddr string ) error {
142
149
user := "root"
@@ -162,7 +169,7 @@ func (nca *nerdctlConfigApplier) Apply(remoteAddr string) error {
162
169
return fmt .Errorf ("failed to update the nerdctl config file: %w" , err )
163
170
}
164
171
165
- if err := updateEnvironment (sftpFs , nca . hostUser ); err != nil {
172
+ if err := nca . updateEnvironment (sftpFs ); err != nil {
166
173
return fmt .Errorf ("failed to update the user's .profile file: %w" , err )
167
174
}
168
175
return nil
0 commit comments