forked from github/plane
dev: auto instance registration script
This commit is contained in:
parent
efbf834e77
commit
32a8133a9f
@ -43,7 +43,7 @@ USER captain
|
|||||||
COPY manage.py manage.py
|
COPY manage.py manage.py
|
||||||
COPY plane plane/
|
COPY plane plane/
|
||||||
COPY templates templates/
|
COPY templates templates/
|
||||||
|
COPY package.json package.json
|
||||||
COPY gunicorn.config.py ./
|
COPY gunicorn.config.py ./
|
||||||
USER root
|
USER root
|
||||||
RUN apk --no-cache add "bash~=5.2"
|
RUN apk --no-cache add "bash~=5.2"
|
||||||
|
@ -3,12 +3,10 @@ import os, sys
|
|||||||
import json
|
import json
|
||||||
import uuid
|
import uuid
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
# Django imports
|
# Django imports
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
# Module imports
|
|
||||||
from plane.db.models import User
|
|
||||||
from plane.license.models import Instance
|
|
||||||
|
|
||||||
sys.path.append("/code")
|
sys.path.append("/code")
|
||||||
|
|
||||||
@ -20,61 +18,76 @@ django.setup()
|
|||||||
|
|
||||||
|
|
||||||
def instance_registration():
|
def instance_registration():
|
||||||
# Check if the instance is registered
|
try:
|
||||||
instance = Instance.objects.first()
|
# Module imports
|
||||||
|
from plane.db.models import User
|
||||||
|
from plane.license.models import Instance
|
||||||
|
|
||||||
# If instance is None then register this instance
|
# Check if the instance is registered
|
||||||
if instance is None:
|
instance = Instance.objects.first()
|
||||||
with open("package.json", "r") as file:
|
|
||||||
# Load JSON content from the file
|
|
||||||
data = json.load(file)
|
|
||||||
|
|
||||||
admin_email = os.environ.get("ADMIN_EMAIL")
|
# If instance is None then register this instance
|
||||||
# Raise an exception if the admin email is not provided
|
if instance is None:
|
||||||
if not admin_email:
|
with open("/code/package.json", "r") as file:
|
||||||
raise Exception("ADMIN_EMAIL is required")
|
# Load JSON content from the file
|
||||||
|
data = json.load(file)
|
||||||
|
|
||||||
# Check if the admin email user exists
|
admin_email = os.environ.get("ADMIN_EMAIL")
|
||||||
user = User.objects.filter(email=admin_email).first()
|
# Raise an exception if the admin email is not provided
|
||||||
|
if not admin_email:
|
||||||
|
raise Exception("ADMIN_EMAIL is required")
|
||||||
|
|
||||||
# If the user does not exist create the user and add him to the database
|
# Check if the admin email user exists
|
||||||
if user is None:
|
user = User.objects.filter(email=admin_email).first()
|
||||||
user = User.objects.create(email=admin_email, username=uuid.uuid4().hex)
|
|
||||||
user.set_password(uuid.uuid4().hex)
|
|
||||||
user.save()
|
|
||||||
|
|
||||||
license_engine_base_url = os.environ.get("LICENSE_ENGINE_BASE_URL")
|
# If the user does not exist create the user and add him to the database
|
||||||
|
if user is None:
|
||||||
|
user = User.objects.create(email=admin_email, username=uuid.uuid4().hex)
|
||||||
|
user.set_password(uuid.uuid4().hex)
|
||||||
|
user.save()
|
||||||
|
|
||||||
if not license_engine_base_url:
|
license_engine_base_url = os.environ.get("LICENSE_ENGINE_BASE_URL")
|
||||||
raise Exception("LICENSE_ENGINE_BASE_URL is required")
|
|
||||||
|
|
||||||
headers = {"Content-Type": "application/json"}
|
|
||||||
|
|
||||||
payload = {
|
if not license_engine_base_url:
|
||||||
"email": user.email,
|
raise Exception("LICENSE_ENGINE_BASE_URL is required")
|
||||||
"version": data.get("version", 0.1),
|
|
||||||
}
|
|
||||||
|
|
||||||
response = requests.post(
|
headers = {"Content-Type": "application/json"}
|
||||||
f"{license_engine_base_url}/api/instances",
|
|
||||||
headers=headers,
|
|
||||||
data=json.dumps(payload),
|
|
||||||
)
|
|
||||||
|
|
||||||
if response.status_code == 201:
|
payload = {
|
||||||
data = response.json()
|
"email": user.email,
|
||||||
instance = Instance.objects.create(
|
"version": data.get("version", 0.1),
|
||||||
instance_name="Plane Free",
|
}
|
||||||
instance_id=data.get("id"),
|
|
||||||
license_key=data.get("license_key"),
|
response = requests.post(
|
||||||
api_key=data.get("api_key"),
|
f"{license_engine_base_url}/api/instances",
|
||||||
version=data.get("version"),
|
headers=headers,
|
||||||
email=data.get("email"),
|
data=json.dumps(payload),
|
||||||
owner=user,
|
|
||||||
last_checked_at=timezone.now(),
|
|
||||||
)
|
)
|
||||||
print("Instance succesfully registered")
|
|
||||||
|
|
||||||
print("Instance could not be registered")
|
if response.status_code == 201:
|
||||||
else:
|
data = response.json()
|
||||||
print(f"Instance already registered with instance owner: {instance.owner.email}")
|
instance = Instance.objects.create(
|
||||||
|
instance_name="Plane Free",
|
||||||
|
instance_id=data.get("id"),
|
||||||
|
license_key=data.get("license_key"),
|
||||||
|
api_key=data.get("api_key"),
|
||||||
|
version=0.1,
|
||||||
|
email=data.get("email"),
|
||||||
|
owner=user,
|
||||||
|
last_checked_at=timezone.now(),
|
||||||
|
)
|
||||||
|
print(f"Instance succesfully registered with owner: {instance.owner.email}")
|
||||||
|
return
|
||||||
|
print("Instance could not be registered")
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
print(
|
||||||
|
f"Instance already registered with instance owner: {instance.owner.email}"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
except ImportError:
|
||||||
|
raise ImportError
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
instance_registration()
|
||||||
|
Loading…
Reference in New Issue
Block a user