Ensure loop closes properly and provider update
* Ensure the pool loop exits properly when the pool is not yet in a running state. * Use ListInstances() when cleaning orphaned runners. This ensures We only run one API call per pool to list instances, instead of running a GetInstance() for each individual instance we are checking. Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
parent
bf844a40b3
commit
b954038624
4 changed files with 113 additions and 50 deletions
|
|
@ -226,7 +226,7 @@ function CreateInstance() {
|
|||
OS_VERSION=$(echo "${IMAGE_URN}" | cut -d ':' -f3)
|
||||
ARCH="amd64"
|
||||
|
||||
TAGS="garm-controller-id=${GARM_CONTROLLER_ID} garm-pool-id=${GARM_POOL_ID}"
|
||||
TAGS="garm_controller_id=${GARM_CONTROLLER_ID} garm_pool_id=${GARM_POOL_ID} os_type=${OS_TYPE} os_name=${OS_NAME} os_version=${OS_VERSION} os_arch=${ARCH}"
|
||||
|
||||
set +e
|
||||
|
||||
|
|
@ -298,6 +298,37 @@ function StopServer() {
|
|||
az vm deallocate -g "${instance_id}" -n "${instance_id}" -o none --only-show-errors
|
||||
}
|
||||
|
||||
function GetInstance() {
|
||||
local instance_id="${GARM_INSTANCE_ID}"
|
||||
info=$(az vm show -d -n $instance_id -g $instance_id -o json --only-show-errors 2>&1)
|
||||
echo $info | jq -r '
|
||||
{
|
||||
provider_id: .name,
|
||||
name: .name,
|
||||
os_type: .tags.os_type,
|
||||
os_name: .tags.os_name,
|
||||
os_version: .tags.os_version,
|
||||
os_arch: .tags.os_arch,
|
||||
pool_id: .tags.garm_pool_id,
|
||||
status: {"VM starting": "pending_create", "VM running": "running", "VM stopping": "stopped", "VM stopped": "stopped", "VM deallocating": "stopped", "VM deallocated": "stopped"}[.powerState]
|
||||
}'
|
||||
}
|
||||
|
||||
function ListInstances() {
|
||||
INSTANCES=$(az vm list --query "[?tags.garm_pool_id == '${GARM_POOL_ID}']" -o json --only-show-errors 2>&1)
|
||||
echo $info | jq -r '
|
||||
.[] | {
|
||||
provider_id: .name,
|
||||
name: .name,
|
||||
os_type: .tags.os_type,
|
||||
os_name: .tags.os_name,
|
||||
os_version: .tags.os_version,
|
||||
os_arch: .tags.os_arch,
|
||||
pool_id: .tags.garm_pool_id,
|
||||
status: {"Creating": "pending_create", "Migrating": "pending_create", "Failed": "error", "Succeeded": "running", "Deleting": "pending_delete"}[.provisioningState]
|
||||
}'
|
||||
}
|
||||
|
||||
# Login to Azure
|
||||
checkValNotNull "${AZURE_SUBSCRIPTION_ID}" "AZURE_SUBSCRIPTION_ID"
|
||||
checkValNotNull "${AZURE_TENANT_ID}" "AZURE_TENANT_ID"
|
||||
|
|
@ -317,12 +348,10 @@ case "$GARM_COMMAND" in
|
|||
DeleteInstance
|
||||
;;
|
||||
"GetInstance")
|
||||
echo "GetInstance not implemented"
|
||||
exit 1
|
||||
GetInstance
|
||||
;;
|
||||
"ListInstances")
|
||||
echo "ListInstances not implemented"
|
||||
exit 1
|
||||
ListInstances
|
||||
;;
|
||||
"StartInstance")
|
||||
StartInstance
|
||||
|
|
|
|||
|
|
@ -279,7 +279,7 @@ function CreateInstance() {
|
|||
OS_TYPE=$(echo "${IMAGE_DETAILS}" | jq -c -r '.properties.os_type')
|
||||
checkValNotNull "${OS_TYPE}" "os_type" || return $?
|
||||
DISTRO=$(echo "${IMAGE_DETAILS}" | jq -c -r '.properties.os_distro')
|
||||
checkValNotNull "${OS_TYPE}" "os_distro" || return $?
|
||||
checkValNotNull "${DISTRO}" "os_distro" || return $?
|
||||
VERSION=$(echo "${IMAGE_DETAILS}" | jq -c -r '.properties.os_version')
|
||||
checkValNotNull "${VERSION}" "os_version" || return $?
|
||||
ARCH=$(echo "${IMAGE_DETAILS}" | jq -c -r '.properties.architecture')
|
||||
|
|
@ -306,7 +306,8 @@ function CreateInstance() {
|
|||
set +e
|
||||
|
||||
TAGS="--tag garm-controller-id=${GARM_CONTROLLER_ID} --tag garm-pool-id=${GARM_POOL_ID}"
|
||||
SRV_DETAILS=$(openstack server create --os-compute-api-version 2.52 ${SOURCE_ARGS} ${TAGS} --flavor "${FLAVOR}" --user-data="${CC_FILE}" --network="${NET}" "${INSTANCE_NAME}")
|
||||
PROPERTIES="--property os_type=${OS_TYPE} --property os_name=${DISTRO} --property os_version=${VERSION} --property os_arch=${GH_ARCH} --property pool_id=${GARM_POOL_ID}"
|
||||
SRV_DETAILS=$(openstack server create --os-compute-api-version 2.52 ${SOURCE_ARGS} ${TAGS} ${PROPERTIES} --flavor "${FLAVOR}" --user-data="${CC_FILE}" --network="${NET}" "${INSTANCE_NAME}")
|
||||
if [ $? -ne 0 ];then
|
||||
openstack volume delete "${INSTANCE_NAME}" || true
|
||||
exit 1
|
||||
|
|
@ -394,6 +395,25 @@ function StopServer() {
|
|||
openstack server stop "${instance_id}"
|
||||
}
|
||||
|
||||
function ListInstances() {
|
||||
INSTANCES=$(openstack server list --os-compute-api-version 2.52 --tags garm-pool-id=${GARM_POOL_ID} --long -f json)
|
||||
echo ${INSTANCES} | jq -r '
|
||||
.[] | .Properties * {
|
||||
provider_id: .ID,
|
||||
name: .Name,
|
||||
status: {"ACTIVE": "running", "SHUTOFF": "stopped", "BUILD": "pending_create", "ERROR": "error", "DELETING": "pending_delete"}[.Status]
|
||||
}'
|
||||
}
|
||||
|
||||
function GetInstance() {
|
||||
INSTANCE=$(openstack server show --os-compute-api-version 2.52 ${GARM_INSTANCE_ID} -f json)
|
||||
echo ${INSTANCES} | jq -r '.properties * {
|
||||
provider_id: .id,
|
||||
name: .name,
|
||||
status: {"ACTIVE": "running", "SHUTOFF": "stopped", "BUILD": "pending_create", "ERROR": "error", "DELETING": "pending_delete"}[.status]
|
||||
}'
|
||||
}
|
||||
|
||||
case "$GARM_COMMAND" in
|
||||
"CreateInstance")
|
||||
CreateInstance
|
||||
|
|
@ -402,12 +422,10 @@ case "$GARM_COMMAND" in
|
|||
DeleteInstance
|
||||
;;
|
||||
"GetInstance")
|
||||
echo "GetInstance not implemented"
|
||||
exit 1
|
||||
GetInstance
|
||||
;;
|
||||
"ListInstances")
|
||||
echo "ListInstances not implemented"
|
||||
exit 1
|
||||
ListInstances
|
||||
;;
|
||||
"StartInstance")
|
||||
StartInstance
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue