Skip to content

Commit

Permalink
feat: adds some infrastructure management scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaskuechler committed Jan 14, 2025
1 parent ebd5512 commit 9c6ab4e
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
38 changes: 38 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# UnderStack Scripts

## Setup

UnderStack scripts and tools use the following environment variables for configuration:

``` bash
# Nautobot instance URL
export NAUTOBOT_URL=https://nautobot.dev.understack
# Nautobot token
export NAUTOBOT_TOKEN=0123456789abcdefghijklmnopqrstuvwxyz
# OpenStack cloud credentials
export OS_CLOUD=understack-dev
```

For more about OpenStack cloud configuration, see: <https://rackerlabs.github.io/understack/user-guide/openstack-cli/>

For more about Nautobot tokens, see: <https://docs.nautobot.com/projects/core/en/stable/user-guide/platform-functionality/users/token/>

## nbgql.sh

Query Nautobot's GraphQL API using a query template in the `nautobot_graphql_queries` directory.

For example, to find the servers in the rack named "F20-3", you can run:

``` bash
./nbgql.sh nautobot_graphql_queries/get_hosts_in_rack.gql F20-3
```

## rekick-rack.sh

Rekicks the servers in the specified Nautobot rack name.

For example, to rekick the servers in the rack named "F20-3", you can run:

``` bash
./rekick-rack.sh F20-3
```
11 changes: 11 additions & 0 deletions scripts/nautobot_graphql_queries/get_hosts_in_rack.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
query {
devices(role: "server", rack: "${QUERY_VARIABLE}") {
id
name
interfaces(name: ["iDRAC", "iLO"]) {
ip_addresses {
host
}
}
}
}
50 changes: 50 additions & 0 deletions scripts/nbgql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash

function usage() {
echo "$(basename "$0") graphql_query_file.gql [variable]" >&2
echo "" >&2
echo "Queries Nautobot GraphQL API using the GQL input file with an optional variable to be substituted in your gql." >&2
echo "" >&2
echo "Required environment variables:" >&2
echo "" >&2
echo "NAUTOBOT_URL= URL to the nautobot instance" >&2
echo "NAUTOBOT_TOKEN= Nautobot authentication token for API use" >&2
echo "" >&2

exit 1
}

# check for inputs and required environment variables

if [[ -z "${NAUTOBOT_TOKEN}" ]]; then
echo "Error: NAUTOBOT_TOKEN environment variable not found."
usage
fi

if [[ -z "${NAUTOBOT_URL}" ]]; then
echo "Error: NAUTOBOT_URL environment variable not found."
usage
fi

if [[ -z "$1" ]]; then
echo "Error: GraphQL template not specified."
echo ""
usage
fi

IFS=$'\n'

# read the gql query from the file named in the argument
QUERY_VARIABLE="$2"

Check warning on line 38 in scripts/nbgql.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] scripts/nbgql.sh#L38 <ShellCheck.SC2034>

QUERY_VARIABLE appears unused. Verify use (or export if used externally).
Raw output
./scripts/nbgql.sh:38:1: warning: QUERY_VARIABLE appears unused. Verify use (or export if used externally). (ShellCheck.SC2034)

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

Check warning on line 40 in scripts/nbgql.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] scripts/nbgql.sh#L40 <ShellCheck.SC2034>

DIR appears unused. Verify use (or export if used externally).
Raw output
./scripts/nbgql.sh:40:1: warning: DIR appears unused. Verify use (or export if used externally). (ShellCheck.SC2034)
QUERY=$(jq -n \
--arg q "$(cat $1 | envsubst | tr -d '\n')" \

Check notice on line 42 in scripts/nbgql.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] scripts/nbgql.sh#L42 <ShellCheck.SC2086>

Double quote to prevent globbing and word splitting.
Raw output
./scripts/nbgql.sh:42:27: info: Double quote to prevent globbing and word splitting. (ShellCheck.SC2086)
'{ query: $q }')

# perform the nautobot graphql query
curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Token $NAUTOBOT_TOKEN" \
--data "$QUERY" \
"${NAUTOBOT_URL}/api/graphql/"
82 changes: 82 additions & 0 deletions scripts/rekick-rack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env bash

function usage() {
echo "$(basename "$0") <nautobot.cab.name>" >&2
echo "" >&2
echo "Rekicks ironic baremetal nodes in the specified cabinet" >&2
echo "" >&2
echo "Example: $(basename "$0") F20-3" >&2
echo "" >&2
echo "Required environment variables:" >&2
echo "" >&2
echo "NAUTOBOT_URL= URL to the nautobot instance" >&2
echo "NAUTOBOT_TOKEN= Nautobot authentication token for API use" >&2
echo "OS_CLOUD= OpenStack cloud config to use for ironic baremetal node management (infra)" >&2
echo "" >&2

exit 1
}

# check for inputs and required environment variables

if [[ -z "${NAUTOBOT_TOKEN}" ]]; then
echo "Error: NAUTOBOT_TOKEN environment variable not found."
usage
fi

if [[ -z "${NAUTOBOT_URL}" ]]; then
echo "Error: NAUTOBOT_URL environment variable not found."
usage
fi

if [[ -z "${OS_CLOUD}" ]]; then
echo "Error: OS_CLOUD environment variable not found."
usage
fi

if [[ -z "$1" ]]; then
echo "Error: Rack not specified"
echo ""
usage
fi

RACK="$1"

IFS=$'\n'

# uses the nbgql.sh helper script to query nautobot's graphql api,
# then for each node, we want to delete it and re-enroll it.
# skip nodes which have a customer instance on them.
for item in $(./nbgql.sh nautobot_graphql_queries/get_hosts_in_rack.gql "$RACK" | jq -c -r '.data.devices[]') ; do
echo "" ;
node_uuid=$(jq -r '.id' <<< "$item");
name=$(jq -r '.name' <<< "$item");
drac_ip=$(jq -r '.interfaces[0].ip_addresses[0].host' <<< "$item") ;
echo "working on node: $node_uuid name: $name drac: $drac_ip ";

# check if node is in ironic
NODE_CHECK=$(openstack baremetal node show "$node_uuid" -f json)

Check warning on line 58 in scripts/rekick-rack.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] scripts/rekick-rack.sh#L58 <ShellCheck.SC2034>

NODE_CHECK appears unused. Verify use (or export if used externally).
Raw output
./scripts/rekick-rack.sh:58:5: warning: NODE_CHECK appears unused. Verify use (or export if used externally). (ShellCheck.SC2034)
EXIT_STATUS=$?
if [ $EXIT_STATUS -eq 0 ]; then
# check for a customer instance:
INSTANCE_CHECK=$(openstack baremetal node show "$node_uuid" -f json | jq -r --exit-status '.instance_uuid')

if [[ $INSTANCE_CHECK != "null" ]]; then
echo "Node: $node_uuid has an instance. Skipping." ;
else
echo "Node: $node_uuid setting ironic maintenance mode" ;
# set maintenance mode in ironic in case the node is in a status preventing deletion
openstack baremetal node maintenance set "$node_uuid" ;
# delete the node
echo "Node: $node_uuid deleting node in ironic" ;
openstack baremetal node delete "$node_uuid" ;
fi
else
echo "Node: $node_uuid does not exist in ironic."
fi

# issue the enroll-server workflow using the node's drac ip from nautobot
echo "Node: $node_uuid issuing argo enroll-server workflow" ;
argo -n argo-events submit --from wftmpl/enroll-server --serviceaccount workflow -p ip_address="$drac_ip"

done

0 comments on commit 9c6ab4e

Please sign in to comment.