-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from nlewo/rm-profile
Remove the profile path when a deployment gets evicted
- Loading branch information
Showing
5 changed files
with
95 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package profile | ||
|
||
import "os" | ||
import "fmt" | ||
import "github.com/sirupsen/logrus" | ||
import "os/exec" | ||
import "path" | ||
|
||
var systemProfilesDir string = "/nix/var/nix/profiles/system-profiles" | ||
var cominProfileDir string = systemProfilesDir + "/comin" | ||
|
||
// setSystemProfile creates a link into the directory | ||
// /nix/var/nix/profiles/system-profiles/comin to the built system | ||
// store path. This is used by the switch-to-configuration script to | ||
// install all entries into the bootloader. | ||
// Note also comin uses these links as gcroots | ||
// See https://github.com/nixos/nixpkgs/blob/df98ab81f908bed57c443a58ec5230f7f7de9bd3/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh#L711 | ||
// and https://github.com/nixos/nixpkgs/blob/df98ab81f908bed57c443a58ec5230f7f7de9bd3/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py#L247 | ||
func SetSystemProfile(operation string, outPath string, dryRun bool) (profilePath string, err error) { | ||
if operation == "switch" || operation == "boot" { | ||
err := os.MkdirAll(systemProfilesDir, os.ModeDir) | ||
if err != nil && !os.IsExist(err) { | ||
return profilePath, fmt.Errorf("nix: failed to create the profile directory: %s", systemProfilesDir) | ||
} | ||
cmdStr := fmt.Sprintf("nix-env --profile %s --set %s", cominProfileDir, outPath) | ||
logrus.Infof("nix: running '%s'", cmdStr) | ||
cmd := exec.Command("nix-env", "--profile", cominProfileDir, "--set", outPath) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
if dryRun { | ||
logrus.Infof("nix: dry-run enabled: '%s' has not been executed", cmdStr) | ||
} else { | ||
err := cmd.Run() | ||
if err != nil { | ||
return profilePath, fmt.Errorf("nix: command '%s' fails with %s", cmdStr, err) | ||
} | ||
logrus.Infof("nix: command '%s' succeeded", cmdStr) | ||
dst, err := os.Readlink(cominProfileDir) | ||
if err != nil { | ||
return profilePath, fmt.Errorf("nix: failed to os.Readlink(%s)", cominProfileDir) | ||
} | ||
profilePath = path.Join(systemProfilesDir, dst) | ||
logrus.Infof("nix: the profile %s has been created", profilePath) | ||
} | ||
} | ||
return | ||
} | ||
|
||
// RemoveProfilePath removes a profile path. | ||
func RemoveProfilePath(profilePath string) (err error) { | ||
logrus.Infof("Removing profile path %s", profilePath) | ||
err = os.Remove(profilePath) | ||
if err != nil { | ||
logrus.Errorf("Failed to remove profile path %s: %s", profilePath, err) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package profile | ||
|
||
import ( | ||
"path" | ||
"testing" | ||
|
||
"os" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRemoveProfilePath(t *testing.T) { | ||
dir := t.TempDir() | ||
file1 := path.Join(dir, "file1") | ||
os.Create(file1) | ||
file2 := path.Join(dir, "file2") | ||
os.Create(file2) | ||
|
||
RemoveProfilePath(file1) | ||
entries, _ := os.ReadDir(dir) | ||
files := make([]string, len(entries)) | ||
for i, e := range entries { | ||
files[i] = e.Name() | ||
} | ||
expected := []string{"file2"} | ||
assert.Equal(t, expected, files) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters