├── Dockerfile ├── README.md └── main.py /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.11 2 | 3 | ENV PYTHONUNBUFFERED 1 4 | 5 | WORKDIR /usr/src/app 6 | 7 | RUN apt-get update && apt-get install build-essential -y 8 | 9 | 10 | 11 | 12 | COPY . . 13 | 14 | CMD [ "python", "main.py"] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Liara-Adjust-Scale-Python 3 | 4 | This service is an automatic scaler for Liara's cloud infrastructure and does not require anything to run. It provides the ability to automatically check the app scale every few seconds and zoom in or out. 5 | 6 | 7 | 8 | ![Logo](https://middleware.io/static/79f5bb408800e7b54c2269add486c153/12bf8/auto-scale-2.jpg) 9 | 10 | 11 | ## Documentation 12 | 13 | 14 | ``` 15 | 1-First of all, make sure that Python is installed on your system 16 | 17 | 2-To connect to the program, you must receive your token from Liara 18 | 19 | 3- After receiving the token from Liara, change the token in the token variable in the main.py file 20 | 21 | 4-Your application conainer runs once every 1000 seconds and the scale of the program changes automatically 22 | 23 | 5-If the program needs to increase the resources, the scale will be bigger, otherwise, if it needs to reduce the resources, the scale of the program will be as small as possible. 24 | ``` 25 | [Get Token Api](https://console.liara.ir/API) 26 | 27 | 28 | 29 | ## Running Application 30 | 31 | To run tests, run the following command 32 | 33 | ```bash 34 | python main.py 35 | ``` 36 | 37 | 38 | ## Run the container on Liara 39 | 40 | [Deploy docker documentation](https://docs.liara.ir/app-deploy/docker/getting-started) 41 | 42 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import time 3 | liaraPlan = [{"title": "free", "ramLimitation": 134217728, "index": 0}, 4 | {"title": "ir-mini", "ramLimitation": 268435456, "index": 1}, 5 | {"title": "ir-small", "ramLimitation": 536870912, "index": 2}, 6 | {"title": "ir-medium", "ramLimitation": 1073741824, "index": 3}, 7 | {"title": "standard-base", "ramLimitation": 2147483648, "index": 4}, 8 | {"title": "standard-plus", "ramLimitation": 4294967296, "index": 5}, 9 | {"title": "pro", "ramLimitation": 8589934592, "index": 6}, 10 | {"title": "pro-plus", "ramLimitation": 17179869184, "index": 7},] 11 | 12 | token='token liara' 13 | liaraToken = "Bearer {}".format(token) 14 | 15 | 16 | def getProjects(): 17 | response = requests.get( 18 | 'https://api.iran.liara.ir/v1/projects', headers={'Authorization': liaraToken}) 19 | return response.json() 20 | 21 | 22 | def changeStageApplication(applicationName, newStage): 23 | changePlan = requests.post('https://api.iran.liara.ir/v1/projects/{}/resize'. format( 24 | applicationName), json={"planID": newStage}, headers={'Authorization': liaraToken}) 25 | print(changePlan) 26 | return changePlan 27 | 28 | 29 | def getPerformance(applicationName): 30 | response = requests.get('https://api.iran.liara.ir/v1/projects/{}/metrics/summary'.format( 31 | applicationName), headers={'Authorization': liaraToken}) 32 | return response.json() 33 | 34 | 35 | def calc(): 36 | getProject = getProjects()["projects"] 37 | for x in getProject: 38 | projectName = x["project_id"] 39 | planId = x["planID"] 40 | performanceApplication = getPerformance(projectName) 41 | realtimeCpu = performanceApplication["cpuUsage"][0]["value"][1] 42 | realtimeMemory = performanceApplication["memoryUsage"][0]["value"][1] 43 | amountOfCpuPercent = int(float(realtimeCpu)*100) 44 | finalMemory = int(realtimeMemory) 45 | amountOfRAMPercent = 0 46 | nextStageName = "" 47 | previousStageName = "" 48 | for y in liaraPlan: 49 | if y["title"] == planId: 50 | # for debug 51 | # print(planId) 52 | if y["index"] < 7: 53 | nextStageName = liaraPlan[y["index"]+1]["title"] 54 | if y["index"] > 0: 55 | previousStageName = liaraPlan[y["index"]-1]["title"] 56 | amountOfRAMPercent = y["ramLimitation"] 57 | break 58 | 59 | amountOfRAMPercent = round((finalMemory*100)/amountOfRAMPercent) 60 | totalLoadTIme = (int(amountOfRAMPercent)+int(amountOfCpuPercent))/2 61 | print("CPU: {} % , RAM : {} %".format( 62 | amountOfCpuPercent, amountOfRAMPercent)) 63 | 64 | if totalLoadTIme > 75 and nextStageName != "pro-plus": 65 | response = changeStageApplication(projectName, nextStageName) 66 | print("ApplicationName {} Az Stage {} Be Stage {} convert shod : BOOST ", format( 67 | projectName), format(planId), format(nextStageName)) 68 | print(response) 69 | 70 | if totalLoadTIme < 20 and previousStageName != "free": 71 | response = changeStageApplication(projectName, previousStageName) 72 | print("ApplicationName {} Az Stage {} Be Stage {} convert shod :NOBOOST ", format( 73 | projectName), format(planId), format(nextStageName)) 74 | print(response) 75 | 76 | 77 | while True: 78 | calc() 79 | time.sleep(1000) 80 | --------------------------------------------------------------------------------