Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions apps/api/plane/license/management/commands/register_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
import json
import secrets
import os
import requests

# Django imports
from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone
from django.conf import settings


# Module imports
from plane.license.models import Instance, InstanceEdition
Expand All @@ -20,35 +21,50 @@ def add_arguments(self, parser):
# Positional argument
parser.add_argument("machine_signature", type=str, help="Machine signature")

def read_package_json(self):
with open("package.json", "r") as file:
# Load JSON content from the file
data = json.load(file)
def check_for_current_version(self):
if os.environ.get("APP_VERSION", False):
return os.environ.get("APP_VERSION")

try:
with open("package.json", "r") as file:
data = json.load(file)
return data.get("version", "v0.1.0")
except Exception:
self.stdout.write("Error checking for current version")
return "v0.1.0"

payload = {
"instance_key": settings.INSTANCE_KEY,
"version": data.get("version", 0.1),
}
return payload
def check_for_latest_version(self, fallback_version):
try:
response = requests.get(
"https://api.github.com/repos/makeplane/plane/releases/latest",
timeout=10,
)
response.raise_for_status()
data = response.json()
return data.get("tag_name", fallback_version)
except Exception:
self.stdout.write("Error checking for latest version")
return fallback_version

def handle(self, *args, **options):
# Check if the instance is registered
instance = Instance.objects.first()

current_version = self.check_for_current_version()
latest_version = self.check_for_latest_version(current_version)

# If instance is None then register this instance
if instance is None:
machine_signature = options.get("machine_signature", "machine-signature")

if not machine_signature:
raise CommandError("Machine signature is required")

payload = self.read_package_json()

instance = Instance.objects.create(
instance_name="Plane Community Edition",
instance_id=secrets.token_hex(12),
current_version=payload.get("version"),
latest_version=payload.get("version"),
current_version=current_version,
latest_version=latest_version,
last_checked_at=timezone.now(),
is_test=os.environ.get("IS_TEST", "0") == "1",
edition=InstanceEdition.PLANE_COMMUNITY.value,
Expand All @@ -57,11 +73,11 @@ def handle(self, *args, **options):
self.stdout.write(self.style.SUCCESS("Instance registered"))
else:
self.stdout.write(self.style.SUCCESS("Instance already registered"))
payload = self.read_package_json()

# Update the instance details
instance.last_checked_at = timezone.now()
instance.current_version = payload.get("version")
instance.latest_version = payload.get("version")
instance.current_version = current_version
instance.latest_version = latest_version
instance.is_test = os.environ.get("IS_TEST", "0") == "1"
instance.edition = InstanceEdition.PLANE_COMMUNITY.value
instance.save()
Expand Down
6 changes: 0 additions & 6 deletions apps/api/plane/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,10 @@
ANALYTICS_SECRET_KEY = os.environ.get("ANALYTICS_SECRET_KEY", False)
ANALYTICS_BASE_API = os.environ.get("ANALYTICS_BASE_API", False)


# Posthog settings
POSTHOG_API_KEY = os.environ.get("POSTHOG_API_KEY", False)
POSTHOG_HOST = os.environ.get("POSTHOG_HOST", False)

# instance key
INSTANCE_KEY = os.environ.get(
"INSTANCE_KEY", "ae6517d563dfc13d8270bd45cf17b08f70b37d989128a9dab46ff687603333c3"
)

# Skip environment variable configuration
SKIP_ENV_VAR = os.environ.get("SKIP_ENV_VAR", "1") == "1"

Expand Down
Loading