krebs / krops

Mirror of https://cgit.krebsco.de/krops/about/ (PRs / issues accepted, as can be seen by not having them disabled)
Do What The F*ck You Want To Public License
132 stars 18 forks source link

support subdirectories as target paths #14

Closed ghost closed 4 years ago

ghost commented 4 years ago

I need this to be able to selectively deploy some subdirectories to the target. This way I can do something like this:

  lib.evalSource [
    {
      "foo/bar".file = toString ./foo/bar;
    }
  ]

The broader goal is to prevent my wallpaper files from being copied to my servers, while keeping my workspace directory structure in sync with the remote (./foo/bar will always end up in /var/src/foo/bar). I'm open for alternative suggestions, this is what I currently have:


let
  hosts = {
    amalthea = {
      hostname = "pbb@amalthea.pbb.lc:62954";
      extraSources = [
        "configuration/desktop"
        "configuration/mdns"
        "configuration/docker"
        "sources/module.krops"
        "sources/nixpkgs-mozilla"
        "sources/jblock"
      ];
    };
    giga = {
      hostname = "pbb@giga.pbb.lc:62954";
      extraSources = [
        "configuration/bgp"
        "configuration/home-network"
        "sources/module.krops"
      ];
    };
  };

  dirsListToSources = dirs: lib.listToAttrs (map (dir: lib.nameValuePair "${dir}" {
    file = "${toString ../.}/${dir}";
  }) dirs);

  mkSource = name: host: lib.evalSource [
    (dirsListToSources (host.extraSources ++ [
      "modules"
      "pkgs"
      "configuration/common"
      "configuration/hosts/${name}"
      "sources/nixpkgs"
      "sources/home-manager"
    ]))
    {
      secrets.pass = {
        dir = toString ../secrets;
        inherit name;
      };
      nixos-config.file = toString (pkgs.writeText "nixos-config" ''
        import <configuration/hosts/${name}/configuration.nix>
      '');
      nixpkgs.symlink = "sources/nixpkgs";
    }
  ];
in
  lib.mapAttrs (name: host: pkgs.krops.writeDeploy "deploy" {
    source = mkSource name host;
    target = lib.mkTarget host.hostname // {
      sudo = true;
    };
  }) hosts
4z3 commented 4 years ago

It's not documented, yet, but the file source type supports rsync filter files using rsync's -F option. With this, it's possible create the file ./foo/.rsync-filter with following contents to copy only ./foo/bar (recursively) to the target:

+ /bar
- /*

See FILTER RULES in rsync's man page for details about the file format.

mrVanDalo commented 4 years ago

@petabyteboy I don't have a problem with the pull-request, but I don't see why you actually have that problem, what stops you from doing "asset-wallpapers".file = ... this way you can reference it in you scripts as <asset-wallpapers/awesome-wallpaper.jpg> ?

ghost commented 4 years ago

@petabyteboy I don't have a problem with the pull-request , but I don't see why you actually have that problem, what stops you from doing "asset-wallpapers".file = ... this w ay you can reference it in you scripts as <asset-wallpaper s/awesome-wallpaper.jpg> ?

I could do that, but in that case I would have to create each of those directories I want to copy only to some servers in the root directory of my workspace, so it would have to look a bit like this:

nixfiles/sources-nixpkgs-mozilla/
nixfiles/sources-nixpkgs/
nixfiles/sources-home-manager/
nixfiles/sources-jblock/
nixfiles/sources-nixos-mailserver/
nixfiles/sources-yel-o.de/
nixfiles/sources-website/
nixfiles/configuration-desktop/
nixfiles/configuration-dns/
nixfiles/configuration-common/
nixfiles/configuration-docker/
nixfiles/configuration-mdns/
nixfiles/configuration-bgp/
nixfiles/configuration-hosts-salat/
nixfiles/configuration-hosts-onyx/
nixfiles/configuration-hosts-schinken/
nixfiles/configuration-hosts-amalthea/
nixfiles/configuration-hosts-combahton/
nixfiles/configuration-hosts-vultr/
nixfiles/configuration-hosts-hifipi/
nixfiles/configuration-hosts-unifipi/
nixfiles/configuration-hosts-amber/
nixfiles/configuration-hosts-regolith/
nixfiles/configuration-hosts-mozarella/
nixfiles/configuration-hosts-tomate/

Or at least it would have to look like that in /var/src, but I want to have the same layout in /var/src so I can for example use nixos-rebuild build-vm -I nixfiles/ -I nixos-config=nixfiles/configuration-hosts-tomate/configuration.nix, which would not be possible when the workspace layout differs from the directory layout in /var/src.

ghost commented 4 years ago

It's not documented, yet, but the file source type supports rsync filter files using rsync's -F option. With this, it's possible create the file ./foo/.rsync-filter with following contents to copy only ./foo/bar (recursively) to the target:

+ /bar
- /*

See FILTER RULES in rsync's man page for details about the file format.

These filter files are the same for every host. Allowing more complex filter (include/exclude) flags of the rsync command from the source definition would be an alternative solution for my problem. Right now I can only set the exclude flag, which does not allow me to copy a list of directories.

4z3 commented 4 years ago

Allowing more complex filter (include/exclude) flags of the rsync command

That sounds good to me.

4z3 commented 4 years ago

Replaced by https://github.com/krebs/krops/pull/15.