mirror of
https://github.com/usmannasir/cyberpanel.git
synced 2025-11-02 19:35:49 +01:00
Improve container management and Docker Compose generation
- Enhanced port validation in ContainerManager to check for valid port numbers and handle errors gracefully. - Updated volume handling to ensure proper structure and existence checks. - Added a new helper function to generate Docker Compose YAML, consolidating logic for service configuration, including ports, volumes, and environment variables. - Removed duplicate Docker Compose generation code to streamline functionality.
This commit is contained in:
@@ -323,17 +323,26 @@ class ContainerManager(multi.Thread):
|
|||||||
|
|
||||||
if 'ExposedPorts' in inspectImage['Config']:
|
if 'ExposedPorts' in inspectImage['Config']:
|
||||||
for item in inspectImage['Config']['ExposedPorts']:
|
for item in inspectImage['Config']['ExposedPorts']:
|
||||||
|
# Check if port data exists and is valid
|
||||||
|
if item in data and data[item]:
|
||||||
|
try:
|
||||||
|
port_num = int(data[item])
|
||||||
# Do not allow priviledged port numbers
|
# Do not allow priviledged port numbers
|
||||||
if int(data[item]) < 1024 or int(data[item]) > 65535:
|
if port_num < 1024 or port_num > 65535:
|
||||||
data_ret = {'createContainerStatus': 0, 'error_message': "Choose port between 1024 and 65535"}
|
data_ret = {'createContainerStatus': 0, 'error_message': "Choose port between 1024 and 65535"}
|
||||||
json_data = json.dumps(data_ret)
|
json_data = json.dumps(data_ret)
|
||||||
return HttpResponse(json_data)
|
return HttpResponse(json_data)
|
||||||
portConfig[item] = data[item]
|
portConfig[item] = data[item]
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
data_ret = {'createContainerStatus': 0, 'error_message': f"Invalid port number: {data[item]}"}
|
||||||
|
json_data = json.dumps(data_ret)
|
||||||
|
return HttpResponse(json_data)
|
||||||
|
|
||||||
volumes = {}
|
volumes = {}
|
||||||
|
if volList:
|
||||||
for index, volume in volList.items():
|
for index, volume in volList.items():
|
||||||
volumes[volume['src']] = {'bind': volume['dest'],
|
if isinstance(volume, dict) and 'src' in volume and 'dest' in volume:
|
||||||
'mode': 'rw'}
|
volumes[volume['src']] = {'bind': volume['dest'], 'mode': 'rw'}
|
||||||
|
|
||||||
## Create Configurations
|
## Create Configurations
|
||||||
admin = Administrator.objects.get(userName=dockerOwner)
|
admin = Administrator.objects.get(userName=dockerOwner)
|
||||||
@@ -351,10 +360,15 @@ class ContainerManager(multi.Thread):
|
|||||||
try:
|
try:
|
||||||
container = client.containers.create(**containerArgs)
|
container = client.containers.create(**containerArgs)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
if "port is already allocated" in err: # We need to delete container if port is not available
|
# Check if it's a port allocation error by converting to string first
|
||||||
|
error_message = str(err)
|
||||||
|
if "port is already allocated" in error_message: # We need to delete container if port is not available
|
||||||
print("Deleting container")
|
print("Deleting container")
|
||||||
|
try:
|
||||||
container.remove(force=True)
|
container.remove(force=True)
|
||||||
data_ret = {'createContainerStatus': 0, 'error_message': str(err)}
|
except:
|
||||||
|
pass # Container might not exist yet
|
||||||
|
data_ret = {'createContainerStatus': 0, 'error_message': error_message}
|
||||||
json_data = json.dumps(data_ret)
|
json_data = json.dumps(data_ret)
|
||||||
return HttpResponse(json_data)
|
return HttpResponse(json_data)
|
||||||
|
|
||||||
@@ -376,7 +390,9 @@ class ContainerManager(multi.Thread):
|
|||||||
|
|
||||||
|
|
||||||
except Exception as msg:
|
except Exception as msg:
|
||||||
data_ret = {'createContainerStatus': 0, 'error_message': str(msg)}
|
# Ensure error message is properly converted to string
|
||||||
|
error_message = str(msg) if msg else "Unknown error occurred"
|
||||||
|
data_ret = {'createContainerStatus': 0, 'error_message': error_message}
|
||||||
json_data = json.dumps(data_ret)
|
json_data = json.dumps(data_ret)
|
||||||
return HttpResponse(json_data)
|
return HttpResponse(json_data)
|
||||||
|
|
||||||
|
|||||||
@@ -273,6 +273,54 @@ app.controller('runContainer', function ($scope, $http) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Helper function to generate Docker Compose YAML
|
||||||
|
function generateDockerComposeYml(containerInfo) {
|
||||||
|
var yml = 'version: \'3.8\'\n\n';
|
||||||
|
yml += 'services:\n';
|
||||||
|
yml += ' ' + containerInfo.name + ':\n';
|
||||||
|
yml += ' image: ' + containerInfo.image + '\n';
|
||||||
|
yml += ' container_name: ' + containerInfo.name + '\n';
|
||||||
|
|
||||||
|
// Add ports
|
||||||
|
var ports = Object.keys(containerInfo.ports);
|
||||||
|
if (ports.length > 0) {
|
||||||
|
yml += ' ports:\n';
|
||||||
|
for (var i = 0; i < ports.length; i++) {
|
||||||
|
var port = ports[i];
|
||||||
|
if (containerInfo.ports[port]) {
|
||||||
|
yml += ' - "' + containerInfo.ports[port] + ':' + port + '"\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add volumes
|
||||||
|
var volumes = Object.keys(containerInfo.volumes);
|
||||||
|
if (volumes.length > 0) {
|
||||||
|
yml += ' volumes:\n';
|
||||||
|
for (var i = 0; i < volumes.length; i++) {
|
||||||
|
var volume = volumes[i];
|
||||||
|
if (containerInfo.volumes[volume]) {
|
||||||
|
yml += ' - ' + containerInfo.volumes[volume] + ':' + volume + '\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add environment variables
|
||||||
|
var envVars = Object.keys(containerInfo.environment);
|
||||||
|
if (envVars.length > 0) {
|
||||||
|
yml += ' environment:\n';
|
||||||
|
for (var i = 0; i < envVars.length; i++) {
|
||||||
|
var envVar = envVars[i];
|
||||||
|
yml += ' - ' + envVar + '=' + containerInfo.environment[envVar] + '\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add restart policy
|
||||||
|
yml += ' restart: unless-stopped\n';
|
||||||
|
|
||||||
|
return yml;
|
||||||
|
}
|
||||||
|
|
||||||
// Docker Compose Functions for runContainer
|
// Docker Compose Functions for runContainer
|
||||||
$scope.generateDockerCompose = function() {
|
$scope.generateDockerCompose = function() {
|
||||||
// Get container information from form
|
// Get container information from form
|
||||||
@@ -1434,101 +1482,6 @@ app.controller('viewContainer', function ($scope, $http, $interval, $timeout) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Helper function to generate Docker Compose YAML
|
|
||||||
function generateDockerComposeYml(containerInfo) {
|
|
||||||
var yml = 'version: \'3.8\'\n\n';
|
|
||||||
yml += 'services:\n';
|
|
||||||
yml += ' ' + containerInfo.name + ':\n';
|
|
||||||
yml += ' image: ' + containerInfo.image + '\n';
|
|
||||||
yml += ' container_name: ' + containerInfo.name + '\n';
|
|
||||||
|
|
||||||
// Add ports
|
|
||||||
var ports = Object.keys(containerInfo.ports);
|
|
||||||
if (ports.length > 0) {
|
|
||||||
yml += ' ports:\n';
|
|
||||||
for (var i = 0; i < ports.length; i++) {
|
|
||||||
var port = ports[i];
|
|
||||||
if (containerInfo.ports[port]) {
|
|
||||||
yml += ' - "' + containerInfo.ports[port] + ':' + port + '"\n';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add volumes
|
|
||||||
var volumes = Object.keys(containerInfo.volumes);
|
|
||||||
if (volumes.length > 0) {
|
|
||||||
yml += ' volumes:\n';
|
|
||||||
for (var i = 0; i < volumes.length; i++) {
|
|
||||||
var volume = volumes[i];
|
|
||||||
if (containerInfo.volumes[volume]) {
|
|
||||||
yml += ' - ' + containerInfo.volumes[volume] + ':' + volume + '\n';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add environment variables
|
|
||||||
var envVars = Object.keys(containerInfo.environment);
|
|
||||||
if (envVars.length > 0) {
|
|
||||||
yml += ' environment:\n';
|
|
||||||
for (var i = 0; i < envVars.length; i++) {
|
|
||||||
var envVar = envVars[i];
|
|
||||||
yml += ' - ' + envVar + '=' + containerInfo.environment[envVar] + '\n';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add restart policy
|
|
||||||
yml += ' restart: unless-stopped\n';
|
|
||||||
|
|
||||||
return yml;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper function to generate Docker Compose YAML (for runContainer)
|
|
||||||
function generateDockerComposeYml(containerInfo) {
|
|
||||||
var yml = 'version: \'3.8\'\n\n';
|
|
||||||
yml += 'services:\n';
|
|
||||||
yml += ' ' + containerInfo.name + ':\n';
|
|
||||||
yml += ' image: ' + containerInfo.image + '\n';
|
|
||||||
yml += ' container_name: ' + containerInfo.name + '\n';
|
|
||||||
|
|
||||||
// Add ports
|
|
||||||
var ports = Object.keys(containerInfo.ports);
|
|
||||||
if (ports.length > 0) {
|
|
||||||
yml += ' ports:\n';
|
|
||||||
for (var i = 0; i < ports.length; i++) {
|
|
||||||
var port = ports[i];
|
|
||||||
if (containerInfo.ports[port]) {
|
|
||||||
yml += ' - "' + containerInfo.ports[port] + ':' + port + '"\n';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add volumes
|
|
||||||
var volumes = Object.keys(containerInfo.volumes);
|
|
||||||
if (volumes.length > 0) {
|
|
||||||
yml += ' volumes:\n';
|
|
||||||
for (var i = 0; i < volumes.length; i++) {
|
|
||||||
var volume = volumes[i];
|
|
||||||
if (containerInfo.volumes[volume]) {
|
|
||||||
yml += ' - ' + containerInfo.volumes[volume] + ':' + volume + '\n';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add environment variables
|
|
||||||
var envVars = Object.keys(containerInfo.environment);
|
|
||||||
if (envVars.length > 0) {
|
|
||||||
yml += ' environment:\n';
|
|
||||||
for (var i = 0; i < envVars.length; i++) {
|
|
||||||
var envVar = envVars[i];
|
|
||||||
yml += ' - ' + envVar + '=' + containerInfo.environment[envVar] + '\n';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add restart policy
|
|
||||||
yml += ' restart: unless-stopped\n';
|
|
||||||
|
|
||||||
return yml;
|
|
||||||
}
|
|
||||||
|
|
||||||
$scope.showTop = function () {
|
$scope.showTop = function () {
|
||||||
$scope.topHead = [];
|
$scope.topHead = [];
|
||||||
|
|||||||
Reference in New Issue
Block a user