Skip to content

Commit

Permalink
tags: rudimentary support for tagging and filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
adamtulinius committed Sep 7, 2020
1 parent bc67f97 commit cedb084
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion data/eval-machines.nix
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ rec {

machines =
flip mapAttrs nodes (n: v': let v = scrubOptionValue v'; in
{ inherit (v.config.deployment) targetHost targetUser secrets healthChecks buildOnly substituteOnDestination;
{ inherit (v.config.deployment) targetHost targetUser secrets healthChecks buildOnly substituteOnDestination tags;
name = n;
nixosRelease = v.config.system.nixos.release or (removeSuffix v.config.system.nixos.version.suffix v.config.system.nixos.version);
nixConfig = mapAttrs
Expand Down
8 changes: 8 additions & 0 deletions data/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ in
'';
default = {};
};

tags = mkOption {
type = listOf str;
default = [];
description = ''
Host tags.
'';
};
};

# Creates a txt-file that lists all system healthcheck commands
Expand Down
15 changes: 15 additions & 0 deletions filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,18 @@ func FilterHosts(allHosts []nix.Host, skip int, every int, limit int) (hosts []n
return hosts
}
}

func FilterHostsTags(allHosts []nix.Host, selectedTag string) (hosts []nix.Host) {
if selectedTag == "" {
return allHosts
}
for _, host := range allHosts {
for _, tag := range host.GetTags() {
if tag == selectedTag {
hosts = append(hosts, host)
}
}
}

return
}
12 changes: 8 additions & 4 deletions morph.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
app = kingpin.New("morph", "NixOS host manager").Version(version)
dryRun = app.Flag("dry-run", "Don't do anything, just eval and print changes").Default("False").Bool()
selectGlob string
selectTag string
selectEvery int
selectSkip int
selectLimit int
Expand Down Expand Up @@ -78,6 +79,9 @@ func selectorFlags(cmd *kingpin.CmdClause) {
cmd.Flag("on", "Glob for selecting servers in the deployment").
Default("*").
StringVar(&selectGlob)
cmd.Flag("tagged", "Select hosts with these tags").
Default("").
StringVar(&selectTag)
cmd.Flag("every", "Select every n hosts").
Default("1").
IntVar(&selectEvery)
Expand Down Expand Up @@ -218,8 +222,6 @@ func setup() {
handleError(assetErr)
}



func main() {

clause := kingpin.MustParse(app.Parse(os.Args[1:]))
Expand Down Expand Up @@ -507,11 +509,13 @@ func getHosts(deploymentFile string) (hosts []nix.Host, err error) {
return hosts, err
}

filteredHosts := filter.FilterHosts(matchingHosts, selectSkip, selectEvery, selectLimit)
matchingHosts2 := filter.FilterHostsTags(matchingHosts, selectTag)

filteredHosts := filter.FilterHosts(matchingHosts2, selectSkip, selectEvery, selectLimit)

fmt.Fprintf(os.Stderr, "Selected %v/%v hosts (name filter:-%v, limits:-%v):\n", len(filteredHosts), len(allHosts), len(allHosts)-len(matchingHosts), len(matchingHosts)-len(filteredHosts))
for index, host := range filteredHosts {
fmt.Fprintf(os.Stderr, "\t%3d: %s (secrets: %d, health checks: %d)\n", index, host.Name, len(host.Secrets), len(host.HealthChecks.Cmd)+len(host.HealthChecks.Http))
fmt.Fprintf(os.Stderr, "\t%3d: %s (secrets: %d, health checks: %d, tags: %s)\n", index, host.Name, len(host.Secrets), len(host.HealthChecks.Cmd)+len(host.HealthChecks.Http), strings.Join(host.GetTags(), ","))
}
fmt.Fprintln(os.Stderr)

Expand Down
5 changes: 5 additions & 0 deletions nix/nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Host struct {
BuildOnly bool
SubstituteOnDestination bool
NixConfig map[string]string
Tags []string
}

type NixContext struct {
Expand Down Expand Up @@ -58,6 +59,10 @@ func (host *Host) GetHealthChecks() healthchecks.HealthChecks {
return host.HealthChecks
}

func (host *Host) GetTags() []string {
return host.Tags
}

func (host *Host) Reboot(sshContext *ssh.SSHContext) error {

var (
Expand Down

0 comments on commit cedb084

Please sign in to comment.