Use case: runner Docker summary with REST API

<< Click to Display Table of Contents >>

Raynet One > 1.1 u4 > User Guide > Technical overview > API 

Use case: runner Docker summary with REST API

In this use-case, we make use of the Docker container inventory collected to list all deployed runners inside of the IT landscape. This list of runners differs from the list in the web interface. While only registered runners of the instance are listed in the web interface, the computation of this use-case includes runners of different instances and detached ones.

Requirements

Python 3+

Requests library (https://docs.python-requests.org/en/latest/)

API key

frontend URL

Program content

import requests
 
APIKEY = "PUT API KEY HERE"
FRONTEND = "PUT FRONTEND URL HERE"
 
HEADERS = { "ApiKey": APIKEY }
 
def obtain_paged(EP):
    P = {}
    P["count"] = 1000
    
    items = []
    
    while True:
        result = requests.get(EP, params=P, headers=HEADERS)
        
        if result.ok == False:
            break
        
        objs = result.json()
        
        if objs == None:
            return
            
        if len(objs) == 0:
            return
        
        items.extend(objs)
        
        lastobj = objs[len(objs)-1]
        
        P["LastId"] = lastobj["id"]
        
    return items
    
def is_runner_image_uri(S):
    return S.startswith("raynetgmbh/raynet-one-runner:"or S.startswith("raynetnightly.azurecr.io/raynet/raynet-one-runner")
 
def main():
    devs = obtain_paged(FRONTEND + "/api/v1/Devices")
    docker = obtain_paged(FRONTEND + "/api/v1/Docker")
    
    if devs == None or docker == None:
        print( "failure to fetch data from frontend (" + FRONTEND + ")" )
        return
    
    print( "Raynet One Docker runner summary" )
    print( "========================" )
    
    for D in devs:
        for DC in docker:
            if "deviceId" in DC and DC["deviceId"] == D["id"]:
                has_runner = False
                
                if "containers" in DC:
                    for DI in DC["containers"]:
                        if "image" in DI and is_runner_image_uri(DI["image"]):
                            infoline = ""
                            if "name" in D:
                                infoline = "Device " + D["name"] + ": "
                            else:
                                infoline = "Device-id " + D["id"] + ": "
                            
                            if "status" in DI:
                                infoline = infoline + "status=" + DI["status"] + ", "
                                
                            infoline = infoline + DI["image"]
                                
                            print(infoline)