-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds some infrastructure management scripts
- Loading branch information
1 parent
ebd5512
commit 9c6ab4e
Showing
4 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / shellcheck[shellcheck] scripts/nbgql.sh#L38 <ShellCheck.SC2034>
Raw output
|
||
|
||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | ||
Check warning on line 40 in scripts/nbgql.sh GitHub Actions / shellcheck[shellcheck] scripts/nbgql.sh#L40 <ShellCheck.SC2034>
Raw output
|
||
QUERY=$(jq -n \ | ||
--arg q "$(cat $1 | envsubst | tr -d '\n')" \ | ||
Check notice on line 42 in scripts/nbgql.sh GitHub Actions / shellcheck[shellcheck] scripts/nbgql.sh#L42 <ShellCheck.SC2086>
Raw output
|
||
'{ 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/" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / shellcheck[shellcheck] scripts/rekick-rack.sh#L58 <ShellCheck.SC2034>
Raw output
|
||
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 |