Unverified Commit d3ff0e41 authored by Jonas Chevalier's avatar Jonas Chevalier Committed by GitHub
Browse files

Merge pull request #229111 from zimbatm/cloud-init-settings

cloud-init module: adopt the settings format
parents 190295af 406a6dfa
Loading
Loading
Loading
Loading
+160 −127
Original line number Diff line number Diff line
@@ -2,7 +2,8 @@

with lib;

let cfg = config.services.cloud-init;
let
  cfg = config.services.cloud-init;
  path = with pkgs; [
    cloud-init
    iproute2
@@ -11,9 +12,12 @@ let cfg = config.services.cloud-init;
    shadow
    util-linux
    busybox
    ] ++ optional cfg.btrfs.enable btrfs-progs
  ]
  ++ optional cfg.btrfs.enable btrfs-progs
  ++ optional cfg.ext4.enable e2fsprogs
  ;
  settingsFormat = pkgs.formats.yaml { };
  cfgfile = settingsFormat.generate "cloud.cfg" cfg.settings;
in
{
  options = {
@@ -21,7 +25,7 @@ in
      enable = mkOption {
        type = types.bool;
        default = false;
        description = lib.mdDoc ''
        description = mdDoc ''
          Enable the cloud-init service. This services reads
          configuration metadata in a cloud environment and configures
          the machine according to this metadata.
@@ -40,7 +44,7 @@ in
      btrfs.enable = mkOption {
        type = types.bool;
        default = false;
        description = lib.mdDoc ''
        description = mdDoc ''
          Allow the cloud-init service to operate `btrfs` filesystem.
        '';
      };
@@ -48,7 +52,7 @@ in
      ext4.enable = mkOption {
        type = types.bool;
        default = true;
        description = lib.mdDoc ''
        description = mdDoc ''
          Allow the cloud-init service to operate `ext4` filesystem.
        '';
      };
@@ -56,81 +60,106 @@ in
      network.enable = mkOption {
        type = types.bool;
        default = false;
        description = lib.mdDoc ''
        description = mdDoc ''
          Allow the cloud-init service to configure network interfaces
          through systemd-networkd.
        '';
      };

      settings = mkOption {
        description = mdDoc ''
          Structured cloud-init configuration.
        '';
        type = types.submodule {
          freeformType = settingsFormat.type;
        };
        default = { };
      };

      config = mkOption {
        type = types.str;
        default = ''
          system_info:
            distro: nixos
            network:
              renderers: [ 'networkd' ]
          users:
             - root

          disable_root: false
          preserve_hostname: false

          cloud_init_modules:
           - migrator
           - seed_random
           - bootcmd
           - write-files
           - growpart
           - resizefs
           - update_hostname
           - resolv_conf
           - ca-certs
           - rsyslog
           - users-groups

          cloud_config_modules:
           - disk_setup
           - mounts
           - ssh-import-id
           - set-passwords
           - timezone
           - disable-ec2-metadata
           - runcmd
           - ssh

          cloud_final_modules:
           - rightscale_userdata
           - scripts-vendor
           - scripts-per-once
           - scripts-per-boot
           - scripts-per-instance
           - scripts-user
           - ssh-authkey-fingerprints
           - keys-to-console
           - phone-home
           - final-message
           - power-state-change
        default = "";
        description = mdDoc ''
          raw cloud-init configuration.

          Takes precedence over the `settings` option if set.
        '';
        description = lib.mdDoc "cloud-init configuration.";
      };

    };

  };

  config = mkIf cfg.enable {

    environment.etc."cloud/cloud.cfg".text = cfg.config;
  config = {
    services.cloud-init.settings = {
      system_info = mkDefault {
        distro = "nixos";
        network = {
          renderers = [ "networkd" ];
        };
      };

      users = mkDefault [ "root" ];
      disable_root = mkDefault false;
      preserve_hostname = mkDefault false;

      cloud_init_modules = mkDefault [
        "migrator"
        "seed_random"
        "bootcmd"
        "write-files"
        "growpart"
        "resizefs"
        "update_hostname"
        "resolv_conf"
        "ca-certs"
        "rsyslog"
        "users-groups"
      ];

      cloud_config_modules = mkDefault [
        "disk_setup"
        "mounts"
        "ssh-import-id"
        "set-passwords"
        "timezone"
        "disable-ec2-metadata"
        "runcmd"
        "ssh"
      ];

      cloud_final_modules = mkDefault [
        "rightscale_userdata"
        "scripts-vendor"
        "scripts-per-once"
        "scripts-per-boot"
        "scripts-per-instance"
        "scripts-user"
        "ssh-authkey-fingerprints"
        "keys-to-console"
        "phone-home"
        "final-message"
        "power-state-change"
      ];
    };
  } // (mkIf cfg.enable {

    environment.etc."cloud/cloud.cfg" =
      if cfg.config == "" then
        { source = cfgfile; }
      else
        { text = cfg.config; }
    ;

    systemd.network.enable = cfg.network.enable;

    systemd.services.cloud-init-local =
      { description = "Initial cloud-init job (pre-networking)";
    systemd.services.cloud-init-local = {
      description = "Initial cloud-init job (pre-networking)";
      wantedBy = [ "multi-user.target" ];
      before = [ "systemd-networkd.service" ];
      path = path;
        serviceConfig =
          { Type = "oneshot";
      serviceConfig = {
        Type = "oneshot";
        ExecStart = "${pkgs.cloud-init}/bin/cloud-init init --local";
        RemainAfterExit = "yes";
        TimeoutSec = "infinity";
@@ -138,17 +167,21 @@ in
      };
    };

    systemd.services.cloud-init =
      { description = "Initial cloud-init job (metadata service crawler)";
    systemd.services.cloud-init = {
      description = "Initial cloud-init job (metadata service crawler)";
      wantedBy = [ "multi-user.target" ];
        wants = [ "network-online.target" "cloud-init-local.service"
                  "sshd.service" "sshd-keygen.service" ];
      wants = [
        "network-online.target"
        "cloud-init-local.service"
        "sshd.service"
        "sshd-keygen.service"
      ];
      after = [ "network-online.target" "cloud-init-local.service" ];
      before = [ "sshd.service" "sshd-keygen.service" ];
      requires = [ "network.target" ];
      path = path;
        serviceConfig =
          { Type = "oneshot";
      serviceConfig = {
        Type = "oneshot";
        ExecStart = "${pkgs.cloud-init}/bin/cloud-init init";
        RemainAfterExit = "yes";
        TimeoutSec = "infinity";
@@ -156,15 +189,15 @@ in
      };
    };

    systemd.services.cloud-config =
      { description = "Apply the settings specified in cloud-config";
    systemd.services.cloud-config = {
      description = "Apply the settings specified in cloud-config";
      wantedBy = [ "multi-user.target" ];
      wants = [ "network-online.target" ];
      after = [ "network-online.target" "syslog.target" "cloud-config.target" ];

      path = path;
        serviceConfig =
          { Type = "oneshot";
      serviceConfig = {
        Type = "oneshot";
        ExecStart = "${pkgs.cloud-init}/bin/cloud-init modules --mode=config";
        RemainAfterExit = "yes";
        TimeoutSec = "infinity";
@@ -172,15 +205,15 @@ in
      };
    };

    systemd.services.cloud-final =
      { description = "Execute cloud user/final scripts";
    systemd.services.cloud-final = {
      description = "Execute cloud user/final scripts";
      wantedBy = [ "multi-user.target" ];
      wants = [ "network-online.target" ];
      after = [ "network-online.target" "syslog.target" "cloud-config.service" "rc-local.service" ];
      requires = [ "cloud-config.target" ];
      path = path;
        serviceConfig =
          { Type = "oneshot";
      serviceConfig = {
        Type = "oneshot";
        ExecStart = "${pkgs.cloud-init}/bin/cloud-init modules --mode=final";
        RemainAfterExit = "yes";
        TimeoutSec = "infinity";
@@ -188,9 +221,9 @@ in
      };
    };

    systemd.targets.cloud-config =
      { description = "Cloud-config availability";
    systemd.targets.cloud-config = {
      description = "Cloud-config availability";
      requires = [ "cloud-init-local.service" "cloud-init.service" ];
    };
  };
  });
}