-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
- Loading branch information
There are no files selected for viewing
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 | ||
``` |
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 | ||
} | ||
} | ||
} | ||
} |
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/" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/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 | ||
Check notice on line 50 in scripts/rekick-rack.sh GitHub Actions / shellcheck[shellcheck] scripts/rekick-rack.sh#L50 <ShellCheck.SC2006>
Raw output
Check notice on line 50 in scripts/rekick-rack.sh GitHub Actions / shellcheck[shellcheck] scripts/rekick-rack.sh#L50 <ShellCheck.SC2086>
Raw output
|
||
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 for a customer instance: | ||
INSTANCE_CHECK=$(openstack baremetal node show $node_uuid -f json | jq -r --exit-status '.instance_uuid') | ||
Check notice on line 58 in scripts/rekick-rack.sh GitHub Actions / shellcheck[shellcheck] scripts/rekick-rack.sh#L58 <ShellCheck.SC2086>
Raw output
|
||
if [[ $INSTANCE_CHECK != "null" ]]; then | ||
echo "Node: $node_uuid has an instance. Skipping." ; | ||
else | ||
echo "Rekicking node: $node_uuid" ; | ||
# set maintenance mode in ironic in case the node is in a status preventing deletion | ||
openstack baremetal node maintenance set $node_uuid ; | ||
Check notice on line 64 in scripts/rekick-rack.sh GitHub Actions / shellcheck[shellcheck] scripts/rekick-rack.sh#L64 <ShellCheck.SC2086>
Raw output
|
||
# delete the node | ||
openstack baremetal node delete $node_uuid ; | ||
Check notice on line 66 in scripts/rekick-rack.sh GitHub Actions / shellcheck[shellcheck] scripts/rekick-rack.sh#L66 <ShellCheck.SC2086>
Raw output
|
||
# issue the enroll-server workflow using the node's drac ip from nautobot | ||
argo -n argo-events submit --from wftmpl/enroll-server --serviceaccount workflow -p ip_address=$drac_ip | ||
Check notice on line 68 in scripts/rekick-rack.sh GitHub Actions / shellcheck[shellcheck] scripts/rekick-rack.sh#L68 <ShellCheck.SC2086>
Raw output
|
||
fi | ||
done |