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

@ -109,9 +109,12 @@ class AppManger:
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. # Check the endpointId is exists.
if endpointId: if endpointId:
check_endpointId(endpointId, portainerManager) check_endpointId(endpointId, portainerManager)
else: else:
endpointId = portainerManager.get_local_endpoint_id() endpointId = portainerManager.get_local_endpoint_id()
# validate the app_id is exists in portainer # validate the app_id is exists in portainer
is_stack_exists = portainerManager.check_stack_exists(app_id,endpointId) is_stack_exists = portainerManager.check_stack_exists(app_id,endpointId)
if not is_stack_exists: if not is_stack_exists:
raise CustomException( raise CustomException(
status_code=400, status_code=400,
message="Invalid Request", message="Invalid Request",
details=f"{app_id} Not Found" details=f"{app_id} Not Found"
) )
# Get stack_info by app_id from portainer # Get stack_info by app_id from portainer
stack_info = portainerManager.get_stack_by_name(app_id,endpointId) stack_info = portainerManager.get_stack_by_name(app_id,endpointId)
# Get the stack_id # Get the stack_id
stack_id = stack_info.get("Id",None) stack_id = stack_info.get("Id",None)
# Check the stack_id is exists # Check the stack_id is exists
if stack_id is None: if stack_id is None:
raise CustomException( raise CustomException(
status_code=400, status_code=400,
message="Invalid Request", message="Invalid Request",
details=f"{app_id} Not Found" details=f"{app_id} Not Found"
) )
# Get the stack_status # Get the stack_status
stack_status = stack_info.get("Status",0) stack_status = stack_info.get("Status",0)
# Get the gitConfig # Get the gitConfig
gitConfig = stack_info.get("GitConfig",{}) or {} gitConfig = stack_info.get("GitConfig",{}) or {}
# Get the creationDate # Get the creationDate
creationDate = stack_info.get("CreationDate","") creationDate = stack_info.get("CreationDate","")
# Get the domain_names by app_id from nginx proxy manager # Get the domain_names by app_id from nginx proxy manager
domain_names = ProxyManager().get_proxy_host_by_app(app_id) domain_names = ProxyManager().get_proxy_host_by_app(app_id)
# Set the proxy_enabled # Set the proxy_enabled
if not domain_names: if not domain_names:
proxy_enabled = False proxy_enabled = False
else : else :
proxy_enabled = True proxy_enabled = True
# Get the volumes by app_id from portainer # Get the volumes by app_id from portainer
app_volumes = portainerManager.get_volumes_by_stack_name(app_id,endpointId,False) 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 is empty(status=2-inactive),can not get it
if stack_status == 1: if stack_status == 1:
# Get the containers by app_id from portainer # Get the containers by app_id from portainer
app_containers = portainerManager.get_containers_by_stack_name(app_id,endpointId) app_containers = portainerManager.get_containers_by_stack_name(app_id,endpointId)
# Get the main container # Get the main container
main_container_id = None main_container_id = None
app_env = [] app_env = []
app_env_format = {} # format app_env to dict app_env_format = {} # format app_env to dict
for container in app_containers: for container in app_containers:
if f"/{app_id}" in container.get("Names", []): if f"/{app_id}" in container.get("Names", []):
main_container_id = container.get("Id", "") main_container_id = container.get("Id", "")
break break
if main_container_id: if main_container_id:
# Get the main container info by main_container_id from portainer # Get the main container info by main_container_id from portainer
main_container_info = portainerManager.get_container_by_id(endpointId, main_container_id) main_container_info = portainerManager.get_container_by_id(endpointId, main_container_id)
# Get the env from main_container_info # Get the env from main_container_info
app_env = main_container_info.get("Config", {}).get("Env", []) app_env = main_container_info.get("Config", {}).get("Env", [])
# Get info from app_env logger.access(f"app_env:{app_env}")
app_name = None
app_dist = None
app_version = None
w9_url = None
w9_url_replace = False
for item in app_env:
key, value = item.split("=", 1)
app_env_format[key] = value
if key == "W9_APP_NAME":
app_name = value
elif key == "W9_DIST":
app_dist = value
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: # Get info from app_env
domain["w9_url_replace"] = w9_url_replace app_name = None
domain["w9_url"] = w9_url app_dist = None
app_version = None
w9_url = None
w9_url_replace = False
for item in app_env:
parts = item.split("=", 1)
if len(parts) == 2:
key, value = parts
else:
key = parts[0]
value = ""
app_env_format[key] = value
if key == "W9_APP_NAME":
app_name = value
elif key == "W9_DIST":
app_dist = value
elif key == "W9_VERSION":
app_version = value
elif key == "W9_URL_REPLACE":
w9_url_replace = value
elif key == "W9_URL":
w9_url = value
# Set the appResponse for domain in domain_names:
appResponse = AppResponse( domain["w9_url_replace"] = w9_url_replace
app_id = app_id, domain["w9_url"] = w9_url
endpointId = endpointId,
app_name = app_name, # Set the appResponse
app_dist = app_dist, appResponse = AppResponse(
app_version = app_version, app_id = app_id,
app_official = True, endpointId = endpointId,
proxy_enabled = proxy_enabled, app_name = app_name,
domain_names = domain_names, app_dist = app_dist,
status = stack_status, app_version = app_version,
creationDate = creationDate, app_official = True,
gitConfig = gitConfig, proxy_enabled = proxy_enabled,
containers = app_containers, domain_names = domain_names,
volumes = app_volumes, status = stack_status,
env = app_env_format creationDate = creationDate,
) gitConfig = gitConfig,
return appResponse containers = app_containers,
else: volumes = app_volumes,
appResponse = AppResponse( env = app_env_format
app_id = app_id, )
endpointId = endpointId, logger.access(appResponse)
app_name = "", return appResponse
app_dist = "", else:
app_version = "", appResponse = AppResponse(
app_official = True, app_id = app_id,
proxy_enabled = proxy_enabled, endpointId = endpointId,
domain_names = domain_names, app_name = "",
status = stack_status, app_dist = "",
creationDate = creationDate, app_version = "",
gitConfig = gitConfig, app_official = True,
containers = [], proxy_enabled = proxy_enabled,
volumes = app_volumes, domain_names = domain_names,
env = {} status = stack_status,
) creationDate = creationDate,
return appResponse 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):
""" """