Skip to content

Bash cli

Kevin Poorman requested to merge Bash-cli into dev

Alright Team, sit back, cause this is a big'un.

This MR introduces a 100% bash only command line interface. It currently lives beside the collection of shell scripts and Makefile that users interface with to configure, deploy and update their HomelabOS install.

This removes the dependency on Make.

The CLI is based on TaskIt, however it has been modified to be cross-platform. (Currently tested in Ubuntu, OSX w/ bash 5 and Ubuntu under WSL2.0).

Tasks are sorted by their rough category into a handful of files in Tasks/*.sh. These files are automatically sourced by the Taskfile, which is automatically read at the invocation of the cli. The CLI is named hlos

Hlos is rather a bit more documented than the makefile for those who aren't familiar with the syntax. Users can see what tasks are available by running ./hlos -t and can see detailed instructions for a specific task by running ./hlos -h <taskname>. For example, this MR includes the following public (visible) tasks:

Available tasks:

  config               -    Creates or Updates the config file as necessary.
  config_reset         -    Resets the Configuration
  set                  -    Set a configuration variable
  deploy               -    Main deployment task - used to deploy HLOS
  restart              -    Restart all enabled services
  restart_one          -    Restarts the specified service
  stop                 -    Restart all enabled services
  stop_one             -    Restarts the specified service
  remove_one           -    Removes the specified service on the HomelabOS server
  reset_one            -    Resets the specified service' files on the HomelabOS server
  terraform            -    Spin up a cloud server with Terraform
  terraform_destroy    -    Destroys servers created by terraform
  update               -    Updates all services on the HomelabOS Server
  update_one           -    Updates the specified service on the HomelabOS server
  logo                 -    Prints the Logo
  build                -    Builds the Docker Image used to deploy
  git_sync             -    Manually forces a settings sync via git
  encrypt              -    Encrypts the vault
  decrypt              -    Decrypts the vault
  uninstall            -    Uninstalls HomelabOS
  restore              -    Restore a server from backups. Assuming backups were running
  shell                -    Opens a shell in the HomelabOS deploy container
  track                -    Switches you to the specified branch or tag. use branch=<branchname>
  check_version        -    Checks the current version
  install_cli          -    Links the hlos cli into /usr/local/bin so you can call hlos without the ./

And the help details for set are:

hlos -h set
TASK
        set -- Set a configuration variable.

DESCRIPTION
	Set a configuration variable.  Requires that you specify
	key=value pair where Key is equal to the configuration key
	you'd like to set, and the value represents what it will
	be set to.

PARAMETERS
        rest%    Configuration Key to set.

At this point, hlos is at parity with the Makefile and various .sh files. However, it adds a couple of new features, that address common issues found in Zulip. For instance: By the time you see the HomelabOS logo, several 'sanity' checks have completed that ensure:

  1. not executing ./hlos as sudo.
  2. that you have ssh keys at $HOME/.ssh/id_rsa*
  3. that a vault pass exists in $HOME/.homelabos_vault_pass
  4. While optional, there is a sanity check verifying that the docker container can successfully ssh to the server given the IP, username and ssh keys.

Docs for extending this have not yet been written, but the tl;dr; is

Tasks are bash functions that start with Task:: Comments on the line immediately preceding the function signature are shown to the Right of the task name when -t is run Within the task function lines that start with a : are directives to the framework, and you can specify things like @desc "the description" and @param. Here's an example:

Task::check_ssh_with_keys(){
  IP=$(Task::run_docker yq r "settings/config.yml" "homelab_ip" | tr -d '[:space:]')
  USERNAME=$(Task::run_docker yq r "settings/config.yml" "homelab_ssh_user" | tr -d '[:space:]')
  Task::run_docker ssh -q -o StrictHostKeyChecking=no -o ConnectTimeout=3 "$USERNAME@$IP" exit 2>&1 && echo $?
  if ! [ $? -eq 0 ]; then
    colorize red "HomelabOS is unable to ssh to your server using the information in your config.yml: $USERNAME at $IP, and your $HOME/.ssh/id_rsa keypair to SSH into your server. Because the HomelabOS docker container cannot ssh to your server with the specified key, HomelabOS cannot deploy"
  fi
}

Merge request reports