Rex-Rancher

 view release on metacpan or  search on metacpan

eg/lan.Rexfile  view on Meta::CPAN

#   LAN_CLUSTER_CIDR      cluster_cidr (pod network), e.g. 10.44.0.0/16
#   LAN_HOSTNAME / LAN_DOMAIN / LAN_TIMEZONE   passed to prepare_node when set
#   LAN_AGENT_GPU         1 = gpu => 1 on the joining agent, default 0
#   LAN_AGENT_HOSTNAME    hostname for the agent (when set)
#   LAN_JOIN_URL          override the agent's join URL, e.g. a wrong address
#                         to see the join hint of install_agent (karr k44)
#   LAN_USER / LAN_KEY    SSH user (root) and private key (~/.ssh/id_ed25519)
#
# Prerequisites on the machine(s): fresh Debian/Ubuntu/Rocky, root SSH login
# with your key. The LibSSH backend needs no SFTP subsystem -- to rehearse an
# SFTP-less host, comment out `Subsystem sftp` in /etc/ssh/sshd_config and
# restart sshd; `check` shows what sshd has configured.
#
# For development (both repos checked out):
#   PERL5LIB=lib:../rex-gpu/lib:$PERL5LIB rex -f eg/lan.Rexfile -H <IP> deploy

use Rex -feature => ['1.4'];
use Rex::LibSSH;
use Rex::Rancher;
use Rex::Rancher::Server;    # get_token
use Rex::Rancher::Cilium;    # upgrade_cilium
use Kubernetes::REST::Kubeconfig;

# --- Configuration ---

my $DIST = $ENV{LAN_DIST} || 'rke2';
die "LAN_DIST must be rke2 or k3s, not '$DIST'\n"
  unless $DIST eq 'rke2' || $DIST eq 'k3s';

my $GPU        = $ENV{LAN_GPU} ? 1 : 0;
my $REBOOT     = defined $ENV{LAN_REBOOT} ? ( $ENV{LAN_REBOOT} ? 1 : 0 ) : $GPU;
my $KUBECONFIG = $ENV{LAN_KUBECONFIG} || $ENV{HOME}.'/.kube/lan-'.$DIST.'.yaml';

# What differs between the two distributions, as far as this Rexfile has to
# look at the host itself. The library keeps its own table; this one is only
# for the `check` and `rerun` output.
my %HOST = (
  rke2 => {
    server_service => 'rke2-server',
    agent_service  => 'rke2-agent',
    binary         => 'rke2',
    data_dir       => '/var/lib/rancher/rke2',
    config_dir     => '/etc/rancher/rke2',
    join_port      => 9345
  },
  k3s => {
    server_service => 'k3s',
    agent_service  => 'k3s-agent',
    binary         => 'k3s',
    data_dir       => '/var/lib/rancher/k3s',
    config_dir     => '/etc/rancher/k3s',
    join_port      => 6443
  }
);
my $D = $HOST{$DIST};

# --- Connection ---

set connection  => 'LibSSH';
set user        => ( $ENV{LAN_USER} || 'root' );
set private_key => ( $ENV{LAN_KEY}  || $ENV{HOME}.'/.ssh/id_ed25519' );
set public_key  => ( $ENV{LAN_KEY}  ? $ENV{LAN_KEY}.'.pub' : $ENV{HOME}.'/.ssh/id_ed25519.pub' );
set auth        => 'key';

# ============================================================
#  Option builders
# ============================================================

sub server_address { $ENV{LAN_SERVER} || connection->server }

sub node_opts {
  my ( $prefix ) = @_;
  return (
    $ENV{$prefix.'HOSTNAME'} ? ( hostname => $ENV{$prefix.'HOSTNAME'} ) : (),
    $ENV{LAN_DOMAIN}         ? ( domain   => $ENV{LAN_DOMAIN} )         : (),
    $ENV{LAN_TIMEZONE}       ? ( timezone => $ENV{LAN_TIMEZONE} )       : ()
  );
}

sub server_opts {
  return (
    distribution    => $DIST,
    gpu             => $GPU,
    reboot          => $REBOOT,
    tls_san         => server_address(),
    kubeconfig_file => $KUBECONFIG,
    node_opts('LAN_'),
    $ENV{LAN_TOKEN}              ? ( token              => $ENV{LAN_TOKEN} )              : (),
    $ENV{LAN_VERSION}            ? ( version            => $ENV{LAN_VERSION} )            : (),
    $ENV{LAN_CLUSTER_CIDR}       ? ( cluster_cidr       => $ENV{LAN_CLUSTER_CIDR} )       : (),
    $ENV{LAN_CILIUM_VERSION}     ? ( cilium_version     => $ENV{LAN_CILIUM_VERSION} )     : (),
    $ENV{LAN_CILIUM_CLI_VERSION} ? ( cilium_cli_version => $ENV{LAN_CILIUM_CLI_VERSION} ) : ()
  );
}

sub deploy_server {
  rancher_deploy_server( server_opts() );
  untaint_node( kubeconfig => $KUBECONFIG );
}

# ============================================================
#  Test plan (karr k53)
# ============================================================

desc 'Full server deployment: prepare -> [GPU] -> '.$DIST.' -> kubeconfig -> Cilium -> [device plugin] -> untaint';
task 'deploy', sub {
  deploy_server();
  say '';
  say 'Done. export KUBECONFIG='.$KUBECONFIG;
  say 'Next: rex -f eg/lan.Rexfile -H '.connection->server.' check';
};

desc 'Same deploy again; compares the service and the Cilium release before and after';
task 'rerun', sub {
  my $before = snapshot();
  deploy_server();
  my $after = snapshot();

  say '';
  say '=== Re-run result ===';
  printf "  %-24s %s -> %s\n", $_, $before->{$_}, $after->{$_} for sort keys %$before;



( run in 0.418 second using v1.01-cache-2.11-cpan-036bef1c656 )