Skip to content

Commit

Permalink
feat: change invocation of colony hardware discovery for colony disco…
Browse files Browse the repository at this point in the history
…very command
  • Loading branch information
jairoFernandez committed Jun 12, 2024
1 parent 455b175 commit b34bd90
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 87 deletions.
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-rc1

RUN curl -L -o colony-scout.tar.gz https://assets.kubefirst.com/colony/colony-scout_${COLONY_SCOUT_VERSION}_linux_amd64.tar.gz && \
tar -xzvf colony-scout.tar.gz && \
chmod +x colony-scout

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

0 comments on commit b34bd90

Please sign in to comment.