Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use colony scout "discovery" command #16

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions discovery/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ FROM alpine:3.13.1

RUN apk add --no-cache util-linux curl

ARG COLONY_SCOUT_VERSION=0.0.8-rc14
RUN curl -L -o colony-scout.tar.gz https://assets.kubefirst.com/colony/colony-scout_.${COLONY_SCOUT_VERSION}_Linux_x86_64.tar.gz && \
tar -xzvf colony-scout.tar.gz && \
chmod +x colony-scout && \
mv colony-scout /usr/local/bin/

COPY . .

ENTRYPOINT ["/bin/sh","./entrypoint.sh"]
103 changes: 16 additions & 87 deletions discovery/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,104 +2,33 @@

set -x

if [ -z "${K1_COLONY_API_URL}" ]; then
echo "K1_COLONY_API_URL NEEDS SETTING"
exit 1
fi

if [ -z "${K1_COLONY_HARDWARE_ID}" ]; then
echo "K1_COLONY_HARDWARE_ID NEEDS SETTING"
exit 1
fi

# Function to get architecture info
get_architecture_info() {
architecture_info=$(lscpu | grep Architecture: | awk '{print $2}')
echo "$architecture_info"
}

# Function to get number of cpu
get_cpu_info() {
cpu_info=$(nproc)
echo "$cpu_info"
}

# Function to get memory info
get_mem_info() {
mem_info=$(free -m | grep Mem | awk '{print $2}')
echo "$mem_info"
}

# Function to get physical disk info
get_disk_info() {
disk_info=$(lsblk -dpno NAME,SIZE | awk '$2 != "0B" {print $1, $2}' | sort -k2 | awk '{printf "\"%s\",", $1}' | sed 's/,$//')
echo "[${disk_info}]"
}

# Function to get physical interface info
get_interface_info() {
interface_info=$(ip -o link show | awk -F': ' 'NR>1{print ",\""$2"\""}')
echo "["${interface_info:1}"]"
}

# Main function to generate JSON output
generate_json() {
architecture_info=$(get_architecture_info)
cpu_info=$(get_cpu_info)
mem_info=$(get_mem_info)
disk_info=$(get_disk_info)
interface_info=$(get_interface_info)

json_output=$(cat <<EOF
{
"cpuArchitecture": "$architecture_info",
"cpuCores": "$cpu_info",
"memory": "$mem_info",
"blockDevices": $disk_info,
"interfaceInfo": $interface_info
}
EOF
)

echo "$json_output"

update_hardware "$json_output"
}
if [ -z "${COLONY_API_KEY}" ]; then
echo "COLONY_API_KEY NEEDS SETTING"
exit 1
fi

update_hardware() {
local body=$1
local response
local http_code
local response_body

response=$(curl --request PUT \
--url "${K1_COLONY_API_URL}/api/v1/hardwares/${K1_COLONY_HARDWARE_ID}" \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data "$body" \
--silent --output - --write-out "\n%{http_code}")

response_body=$(echo "$response" | head -n -1)
http_code=$(echo "$response" | tail -n1)
output=$(colony-scout discovery \
--token="${COLONY_API_KEY}" \
--hardware-id="${K1_COLONY_HARDWARE_ID}" 2>&1)

echo "----------------------------"
echo "Server ${K1_COLONY_API_URL}/api/v1/hardwares/${K1_COLONY_HARDWARE_ID}"
echo "body: $body"
echo "http_code: $http_code"
echo "response_body: $response_body"
echo "----------------------------"
exit_status=$?

if [[ $http_code -ge 200 && $http_code -le 299 ]]; then
echo "Request success."
else
echo "Error request updating hardware."
exit 1
fi
if [[ $exit_status -eq 0 ]]; then
echo "Request success."
else
echo "Error request updating hardware."
echo "Output: $output"
exit 1
fi
}

# Call the main function to generate JSON output
generate_json
echo "Successfully"
update_hardware

set +x

Expand Down