Skip to content

Commit

Permalink
Added ctp project update-inventory for updating tab-completable inv…
Browse files Browse the repository at this point in the history
…entory
  • Loading branch information
AllRWeak committed Oct 7, 2024
1 parent 1637ae7 commit bc9cbf3
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 20 deletions.
3 changes: 3 additions & 0 deletions defaults/autocomplete.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ autocomplete:
- subcommand_name: select
subcommand_description: Lists all available projects under /srv/inventories and allows you to select one
subcommand_to_execute: source /srv/scripts/general/select-inventory.sh
- subcommand_name: update-inventory
subcommand_description: Updates the tab-completable inventory for currently selected project
subcommand_to_execute: source /srv/scripts/general/generate-inventory-completion.sh

- command: dev
description: Commands for Catapult developers
Expand Down
2 changes: 1 addition & 1 deletion defaults/start.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: Hurling boulders...
- name: Hurling boulders... # noqa: var-naming[non-string]
hosts: all
serial: "{{ playbook_serial_number | default('100%') }}"
gather_facts: false
Expand Down
14 changes: 11 additions & 3 deletions docs/02-how-to-use.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
Catapult's commands fall into two categories:

- External commands - All commands that start with `make` are external commands and are used to manage the Catapult container itself.
- Internal commands - All commands that start with `ctp ` are internal commands and are used when the user is inside the Catapult container.
- Internal commands - All commands that start with `ctp` are internal commands and are used when the user is inside the Catapult container.

## Variables

When running catapult for the first time it'll ask you to create a new Ansible Vault file and fill it out with your secrets. The secrets are used to connect to the hypervisors, cloud providers, and other services. Besides the required `deployer_username` & `deployer_password` here are some other variables that you might need when using Catapult with [nova.core](https://github.com/ClarifiedSecurity/nova.core) collection:

- `aws_access_key_id:` - AWS access key ID - can be generated from the AWS Identity and Access Management (IAM) console
- `aws_access_key:` - AWS access key itself - can be generated from the AWS Identity and Access Management (IAM) console
- `linode_api_token:` - Linode API token - can be generated from the Linode Cloud Manager https://cloud.linode.com/profile/tokens
- `linode_api_token:` - Linode API token - can be generated from the [Linode Cloud Manager](https://cloud.linode.com/profile/tokens)

## Enter the Catapult

Expand All @@ -33,7 +33,7 @@ Catapult is designed to work with multiple projects. Each project has its own in

## Tips and tricks

- Use tab completion to get the list of available commands. For example, type `ctp ` and press tab to get the list of available commands.
- Use tab completion to get the list of available commands. For example, type `ctp` and press tab to get the list of available commands.
- Use `Ctrl + R` to search through the command history and find the command you have used before. Fuzzy search is enabled so you can type parts of the commands or separate words that must be contained in the command.
- Use arrow up after you have started typing the command to get the previous command that starts the same way. For example, if you have used `ctp host deploy` before you can use arrow up to get the previous `ctp host deploy` commands.

Expand Down Expand Up @@ -63,6 +63,14 @@ ctp secrets change-password
ctp project select
```

### ctp project update-inventory

- Updates the tab-completable inventory cache.

```zsh
ctp project update-inventory
```

## Host commands

Examples for the commands that are run against the inventory_hostname or group in Ansible. These commands usually interact with the target OS and the hypervisor. Use Ansible's documentation for [Advanced deploy patterns](https://docs.ansible.com/ansible/latest/user_guide/intro_patterns.html)
Expand Down
33 changes: 33 additions & 0 deletions scripts/general/generate-inventory-completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

echo -n -e "${C_RST}"

#set -x

function inventory_generator(){

# For some reaseon some Ansible commands cannot detect the vault file from an environment variable
ansible-inventory --playbook-dir /srv/inventories -e @/home/builder/.vault/vlt --graph | sed 's/[|@:]*//g' | sed 's/--//g' | sed 's/^[ \t]*//' | sort | uniq > "/tmp/$(basename "$(pwd)")_hosts"

}

if [[ -d "$(pwd)/group_vars" ]]; then

echo -n -e "${C_YELLOW}"
echo -e "Generating tab-completable inventory for ${C_CYAN}$(basename "$(pwd)")${C_YELLOW}..."
echo -n -e "${C_RST}"

# Generating tab-completable inventory file
( inventory_generator >/dev/null 2>&1 & disown >/dev/null 2>&1 )

# Fetching the latest changes from the remote repository
( git fetch > /dev/null 2>&1 2>&1 & disown >/dev/null 2>&1 )

else

echo -n -e "${C_RED}"
echo -e "Cannot generate tab-completable inventory!"
echo -e "No group_vars folder found in the current directory. Run ${C_CYAN}ctp project select${C_RED} to select a project first."
echo -n -e "${C_RST}"

fi
25 changes: 10 additions & 15 deletions scripts/general/select-inventory.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bash

echo -n -e "${C_RST}"
# shellcheck disable=SC1091
source /srv/scripts/general/colors.sh

SEARCH_DIR="/srv/inventories"
SEARCH_FOLDER=".git"
Expand Down Expand Up @@ -65,15 +66,15 @@ function inventory_selector() {
echo -e "Your project's path is: ${C_GREEN}$selected_folder${C_RST}"
project_customization_loader
# shellcheck disable=SC2164
# https://github.com/spaceship-prompt/spaceship-prompt/issues/1193
cd "$selected_folder" > /dev/null 2>/dev/null
cd "$selected_folder"
touch "/tmp/$(basename "$selected_folder")_hosts" # This is to avoid completion erros if the inventory_generator function fails

else

echo "Invalid selection."
# https://github.com/spaceship-prompt/spaceship-prompt/issues/1193
cd /srv > /dev/null 2>/dev/null || exit
echo -n -e "${C_RED}"
echo "Invalid project selection."
echo -n -e "${C_RST}"
cd /srv || exit

fi

Expand All @@ -85,13 +86,7 @@ function inventory_selector() {

}

function inventory_generator(){

# For some reaseon some Ansible commands cannot detect the vault file from an environment variable
ansible-inventory --playbook-dir /srv/inventories -e @/home/builder/.vault/vlt --graph | sed 's/[|@:]*//g' | sed 's/--//g' | sed 's/^[ \t]*//' | sort | uniq > "/tmp/$(basename "$selected_folder")_hosts"

}

inventory_selector
( inventory_generator >/dev/null 2>&1 & disown >/dev/null 2>&1 )
git fetch > /dev/null 2>&1 # Fetching the latest changes from the remote repository

# Including the inventory completion generator
/srv/scripts/general/generate-inventory-completion.sh
2 changes: 1 addition & 1 deletion version.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version: 5.6.35
version: 5.7.13

0 comments on commit bc9cbf3

Please sign in to comment.