Skip to content

Commit

Permalink
feat: make finch config available in VM at $HOME/.finch
Browse files Browse the repository at this point in the history
NerdctlConfigApplier updates the user's bash rc in the
VM to point DOCKER_CONFIG to the mounted .finch dir from
the host OS. This works when the bash rc is loaded, but
doesn't work for, e.g., systemd user services.

This change makes the finch config available available at
$HOME/.finch in the VM so there is a clear place to load
config via systemd user services without relying on any
bash rcs.

Signed-off-by: Kern Walster <[email protected]>
  • Loading branch information
Kern-- committed Dec 9, 2024
1 parent d573949 commit 18c2123
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
10 changes: 10 additions & 0 deletions pkg/config/nerdctl_config_applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func addLineToBashrc(fs afero.Fs, profileFilePath string, profStr string, cmd st
// The [GNU docs for Bash] explain how these files work together in more details.
// The default location of DOCKER_CONFIG is ~/.docker/config.json. This config change sets the location to
// ~/.finch/config.json, but from the perspective of macOS (/Users/<user>/.finch/config.json).
// This config change also symlinks ~/.finch in the macOS/Windows perspective (e.g. /User/<user>/.finch) to
// ~/.finch in the vm's perspective (/home/<user>/.finch) so that it can be found in a well known location
// that doesn't require loading the user's bash rc.
// The reason that we don't set environment variables inside /root/.bashrc is that the vars inside it are
// 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
// normally we would access the VM as the regular user.
Expand Down Expand Up @@ -107,6 +110,13 @@ func updateEnvironment(fs afero.Fs, fc *Finch, finchDir, homeDir, limaVMHomeDir
}, cmdArr...)
}

// Ensure we don't end up with a double '/'
// Normally, we would use filepath.Join to do this, but
// we don't want to follow host filepath semantics. This
// is a linux path inside the VM.
limaVMFinchDir := strings.TrimRight(limaVMHomeDir, "/") + "/.finch"
cmdArr = append(cmdArr, fmt.Sprintf(`[ -L %s ] || ln -s $FINCH_DIR %s`, limaVMFinchDir, limaVMFinchDir))

profileFilePath := fmt.Sprintf("%s/.bashrc", limaVMHomeDir)
profBuf, err := afero.ReadFile(fs, profileFilePath)
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions pkg/config/nerdctl_config_applier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ FINCH_DIR=/finch/dir
AWS_DIR=/home/dir/.aws
export DOCKER_CONFIG="$FINCH_DIR"
[ -L /usr/local/bin/docker-credential-ecr-login ] || sudo ln -s "$FINCH_DIR"/cred-helpers/docker-credential-ecr-login /usr/local/bin/
[ -L /root/.aws ] || sudo ln -fs "$AWS_DIR" /root/.aws`), string(fileBytes))
[ -L /root/.aws ] || sudo ln -fs "$AWS_DIR" /root/.aws
[ -L /home/mock_user.linux/.finch ] || ln -s $FINCH_DIR /home/mock_user.linux/.finch`), string(fileBytes))
},
want: nil,
},
Expand All @@ -95,7 +96,8 @@ FINCH_DIR=/finch/dir
AWS_DIR=/home/dir/.aws
export DOCKER_CONFIG="$FINCH_DIR"
[ -L /usr/local/bin/docker-credential-ecr-login ] || sudo ln -s "$FINCH_DIR"/cred-helpers/docker-credential-ecr-login /usr/local/bin/
[ -L /root/.aws ] || sudo ln -fs "$AWS_DIR" /root/.aws)`,
[ -L /root/.aws ] || sudo ln -fs "$AWS_DIR" /root/.aws)
[ -L /home/mock_user.linux/.finch ] || ln -s $FINCH_DIR /home/mock_user.linux/.finch`,
),
0o644,
),
Expand All @@ -109,7 +111,8 @@ FINCH_DIR=/finch/dir
AWS_DIR=/home/dir/.aws
export DOCKER_CONFIG="$FINCH_DIR"
[ -L /usr/local/bin/docker-credential-ecr-login ] || sudo ln -s "$FINCH_DIR"/cred-helpers/docker-credential-ecr-login /usr/local/bin/
[ -L /root/.aws ] || sudo ln -fs "$AWS_DIR" /root/.aws)`), string(fileBytes))
[ -L /root/.aws ] || sudo ln -fs "$AWS_DIR" /root/.aws)
[ -L /home/mock_user.linux/.finch ] || ln -s $FINCH_DIR /home/mock_user.linux/.finch`), string(fileBytes))
},
want: nil,
},
Expand Down

0 comments on commit 18c2123

Please sign in to comment.