fix apphub get env bug

This commit is contained in:
zhaojing1987 2023-12-05 08:21:40 +08:00
parent 2f6db91285
commit 8881551679
2 changed files with 138 additions and 121 deletions

View file

@ -22,5 +22,5 @@ key = 689899a928b91838e98abd6ac50fc6f6f1fab73e44932a6fd5167e4ce63e72e0
wildcard_domain = wildcard_domain =
[cockpit] [cockpit]
port = 9000 port = 98

View file

@ -108,10 +108,13 @@ class AppManger:
# Get the app info by stack_name from portainer # Get the app info by stack_name from portainer
app_info = self.get_app_by_id(stack_name,endpointId) app_info = self.get_app_by_id(stack_name,endpointId)
apps_info.append(app_info) apps_info.append(app_info)
logger.access(apps_info)
# Get the not stacks(not installed apps) # Get the not stacks(not installed apps)
all_containers = portainerManager.get_containers(endpointId) # Get all containers by endpointId from portainer all_containers = portainerManager.get_containers(endpointId) # Get all containers by endpointId from portainer
# Set the not stacks info for response(app not install by portainer) # Set the not stacks info for response(app not install by portainer)
not_stacks = [] not_stacks = []
for container in all_containers: for container in all_containers:
@ -184,129 +187,143 @@ class AppManger:
app_id (str): The app id. app_id (str): The app id.
endpointId (int, optional): The endpoint id. Defaults to None. endpointId (int, optional): The endpoint id. Defaults to None.
""" """
portainerManager = PortainerManager() try:
portainerManager = PortainerManager()
# Check the endpointId is exists.
if endpointId:
check_endpointId(endpointId, portainerManager)
else:
endpointId = portainerManager.get_local_endpoint_id()
# validate the app_id is exists in portainer
is_stack_exists = portainerManager.check_stack_exists(app_id,endpointId)
if not is_stack_exists:
raise CustomException(
status_code=400,
message="Invalid Request",
details=f"{app_id} Not Found"
)
# Get stack_info by app_id from portainer
stack_info = portainerManager.get_stack_by_name(app_id,endpointId)
# Get the stack_id
stack_id = stack_info.get("Id",None)
# Check the stack_id is exists
if stack_id is None:
raise CustomException(
status_code=400,
message="Invalid Request",
details=f"{app_id} Not Found"
)
# Get the stack_status
stack_status = stack_info.get("Status",0)
# Get the gitConfig
gitConfig = stack_info.get("GitConfig",{}) or {}
# Get the creationDate
creationDate = stack_info.get("CreationDate","")
# Get the domain_names by app_id from nginx proxy manager
domain_names = ProxyManager().get_proxy_host_by_app(app_id)
# Set the proxy_enabled
if not domain_names:
proxy_enabled = False
else :
proxy_enabled = True
# Get the volumes by app_id from portainer
app_volumes = portainerManager.get_volumes_by_stack_name(app_id,endpointId,False)
# if stack is empty(status=2-inactive),can not get it
if stack_status == 1:
# Get the containers by app_id from portainer
app_containers = portainerManager.get_containers_by_stack_name(app_id,endpointId)
# Get the main container # Check the endpointId is exists.
main_container_id = None if endpointId:
app_env = [] check_endpointId(endpointId, portainerManager)
app_env_format = {} # format app_env to dict else:
for container in app_containers: endpointId = portainerManager.get_local_endpoint_id()
if f"/{app_id}" in container.get("Names", []):
main_container_id = container.get("Id", "") # validate the app_id is exists in portainer
break is_stack_exists = portainerManager.check_stack_exists(app_id,endpointId)
if main_container_id: if not is_stack_exists:
# Get the main container info by main_container_id from portainer raise CustomException(
main_container_info = portainerManager.get_container_by_id(endpointId, main_container_id) status_code=400,
# Get the env from main_container_info message="Invalid Request",
app_env = main_container_info.get("Config", {}).get("Env", []) details=f"{app_id} Not Found"
)
# Get stack_info by app_id from portainer
stack_info = portainerManager.get_stack_by_name(app_id,endpointId)
# Get the stack_id
stack_id = stack_info.get("Id",None)
# Check the stack_id is exists
if stack_id is None:
raise CustomException(
status_code=400,
message="Invalid Request",
details=f"{app_id} Not Found"
)
# Get the stack_status
stack_status = stack_info.get("Status",0)
# Get the gitConfig
gitConfig = stack_info.get("GitConfig",{}) or {}
# Get the creationDate
creationDate = stack_info.get("CreationDate","")
# Get the domain_names by app_id from nginx proxy manager
domain_names = ProxyManager().get_proxy_host_by_app(app_id)
# Set the proxy_enabled
if not domain_names:
proxy_enabled = False
else :
proxy_enabled = True
# Get the volumes by app_id from portainer
app_volumes = portainerManager.get_volumes_by_stack_name(app_id,endpointId,False)
# if stack is empty(status=2-inactive),can not get it
if stack_status == 1:
# Get the containers by app_id from portainer
app_containers = portainerManager.get_containers_by_stack_name(app_id,endpointId)
# Get info from app_env # Get the main container
app_name = None main_container_id = None
app_dist = None app_env = []
app_version = None app_env_format = {} # format app_env to dict
w9_url = None for container in app_containers:
w9_url_replace = False if f"/{app_id}" in container.get("Names", []):
for item in app_env: main_container_id = container.get("Id", "")
key, value = item.split("=", 1) break
app_env_format[key] = value if main_container_id:
if key == "W9_APP_NAME": # Get the main container info by main_container_id from portainer
app_name = value main_container_info = portainerManager.get_container_by_id(endpointId, main_container_id)
elif key == "W9_DIST": # Get the env from main_container_info
app_dist = value app_env = main_container_info.get("Config", {}).get("Env", [])
elif key == "W9_VERSION":
app_version = value
elif key == "W9_URL_REPLACE":
w9_url_replace = value
elif key == "W9_URL":
w9_url = value
for domain in domain_names: logger.access(f"app_env:{app_env}")
domain["w9_url_replace"] = w9_url_replace
domain["w9_url"] = w9_url # Get info from app_env
app_name = None
# Set the appResponse app_dist = None
appResponse = AppResponse( app_version = None
app_id = app_id, w9_url = None
endpointId = endpointId, w9_url_replace = False
app_name = app_name, for item in app_env:
app_dist = app_dist, parts = item.split("=", 1)
app_version = app_version, if len(parts) == 2:
app_official = True, key, value = parts
proxy_enabled = proxy_enabled, else:
domain_names = domain_names, key = parts[0]
status = stack_status, value = ""
creationDate = creationDate, app_env_format[key] = value
gitConfig = gitConfig, if key == "W9_APP_NAME":
containers = app_containers, app_name = value
volumes = app_volumes, elif key == "W9_DIST":
env = app_env_format app_dist = value
) elif key == "W9_VERSION":
return appResponse app_version = value
else: elif key == "W9_URL_REPLACE":
appResponse = AppResponse( w9_url_replace = value
app_id = app_id, elif key == "W9_URL":
endpointId = endpointId, w9_url = value
app_name = "",
app_dist = "", for domain in domain_names:
app_version = "", domain["w9_url_replace"] = w9_url_replace
app_official = True, domain["w9_url"] = w9_url
proxy_enabled = proxy_enabled,
domain_names = domain_names, # Set the appResponse
status = stack_status, appResponse = AppResponse(
creationDate = creationDate, app_id = app_id,
gitConfig = gitConfig, endpointId = endpointId,
containers = [], app_name = app_name,
volumes = app_volumes, app_dist = app_dist,
env = {} app_version = app_version,
) app_official = True,
return appResponse proxy_enabled = proxy_enabled,
domain_names = domain_names,
status = stack_status,
creationDate = creationDate,
gitConfig = gitConfig,
containers = app_containers,
volumes = app_volumes,
env = app_env_format
)
logger.access(appResponse)
return appResponse
else:
appResponse = AppResponse(
app_id = app_id,
endpointId = endpointId,
app_name = "",
app_dist = "",
app_version = "",
app_official = True,
proxy_enabled = proxy_enabled,
domain_names = domain_names,
status = stack_status,
creationDate = creationDate,
gitConfig = gitConfig,
containers = [],
volumes = app_volumes,
env = {}
)
return appResponse
except CustomException as e:
raise e
except Exception as e:
logger.error(f"Get app by app_id:{app_id} error:{e}")
raise CustomException()
def install_app(self,appInstall: appInstall, endpointId: int = None): def install_app(self,appInstall: appInstall, endpointId: int = None):
""" """