mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 13:29:41 +02:00
* feat(stack): backport changes to CE EE-1189 * feat(stack): front end backport changes to CE EE-1199 (#5455) * feat(stack): front end backport changes to CE EE-1199 * fix k8s deploy logic * fixed web editor confirmation message typo. EE-1501 * fix(stack): fixed issue auth detail not remembered EE-1502 (#5459) * show status in buttons * removed onChangeRef function. * moved buttons in git form to its own component * removed unused variable. Co-authored-by: ArrisLee <arris_li@hotmail.com> * moved formvalue to kube app component * fix(stack): failed to pull and redeploy compose format k8s stack * fixed form value * fix(k8s): file content overridden when deployment failed with compose format EE-1548 * updated API response to get IsComposeFormat and show appropriate text. * feat(k8s): front end backport to CE * feat(kube): kube app auto update backend (#5547) * error message updates for different file type * not display creation source for external application * added confirmation modal to advanced app created by web editor * stop showing confirmation modal when updating application * disable rollback button when application type is not applicatiom form * only update file after deployment succeded * Revert "only update file after deployment succeded" This reverts commitb94bd2e96f
. * fix(k8s): file content overridden when deployment failed with compose format EE-1556 * added analytics-on directive to pull and redeploy button * fix(kube): don't valide resource control access for kube (#5568) * added missing question mark to k8s confirmation modal * fixed webhook format issue * added question marks to k8s app confirmation modal * added space in additional file list. * ignoring error on deletion * fix(k8s): Git authentication info not persisted * added RepositoryMechanismTypes constant * updated analytics functions * covert RepositoryMechanism to constant * fixed typo * removed unused function. * post tech review updates * fixed save settings n redeploy button * refact kub deploy logic * Revert "refact kub deploy logic" This reverts commitcbfdd58ece
. * feat(k8s): utilize user token for k8s auto update EE-1594 * feat(k8s): persist kub stack name EE-1630 * feat(k8s): support delete kub stack * fix(app): updated logic to delete stack for different kind apps. (#5648) * fix(app): updated logic to delete stack for different kind apps. * renamed variable * fix import * added StackName field. * fixed stack id not found issue. * fix(k8s): fixed qusetion mark alignment issue in PAT field. (#5611) * fix(k8s): fixed qusetion mark alignment issue in PAT field. * moved inline css to file. * fix(git-form: made auth input text full width * add ignore deleted arg * tech review updates * typo fix * fix(k8s): added console error when deleting k8s service. * fix(console): added no-console config * fix(deploy): added missing service. * fix: use stack editor as an owner when exists (#5678) * fix: tempalte/content based stacks edit/delete * fix(stack): remove stack when no app. (#5769) * fix(stack): remove stack when no app. * support compose format in delete Co-authored-by: ArrisLee <arris_li@hotmail.com> Co-authored-by: Hui <arris_li@hotmail.com> Co-authored-by: fhanportainer <79428273+fhanportainer@users.noreply.github.com> Co-authored-by: Felix Han <felix.han@portainer.io>
491 lines
16 KiB
JavaScript
491 lines
16 KiB
JavaScript
import _ from 'lodash-es';
|
|
import { RepositoryMechanismTypes } from 'Kubernetes/models/deploy';
|
|
import { StackViewModel, OrphanedStackViewModel } from '../../models/stack';
|
|
|
|
angular.module('portainer.app').factory('StackService', [
|
|
'$q',
|
|
'$async',
|
|
'Stack',
|
|
'FileUploadService',
|
|
'StackHelper',
|
|
'ServiceService',
|
|
'ContainerService',
|
|
'SwarmService',
|
|
'EndpointProvider',
|
|
function StackServiceFactory($q, $async, Stack, FileUploadService, StackHelper, ServiceService, ContainerService, SwarmService, EndpointProvider) {
|
|
'use strict';
|
|
var service = {
|
|
updateGit,
|
|
updateKubeGit,
|
|
};
|
|
|
|
service.stack = function (id) {
|
|
var deferred = $q.defer();
|
|
|
|
Stack.get({ id: id })
|
|
.$promise.then(function success(data) {
|
|
var stack = new StackViewModel(data);
|
|
deferred.resolve(stack);
|
|
})
|
|
.catch(function error(err) {
|
|
deferred.reject({ msg: 'Unable to retrieve stack details', err: err });
|
|
});
|
|
|
|
return deferred.promise;
|
|
};
|
|
|
|
service.getStackFile = function (id) {
|
|
var deferred = $q.defer();
|
|
|
|
Stack.getStackFile({ id: id })
|
|
.$promise.then(function success(data) {
|
|
deferred.resolve(data.StackFileContent);
|
|
})
|
|
.catch(function error(err) {
|
|
deferred.reject({ msg: 'Unable to retrieve stack content', err: err });
|
|
});
|
|
|
|
return deferred.promise;
|
|
};
|
|
|
|
service.migrateSwarmStack = function (stack, targetEndpointId, newName) {
|
|
var deferred = $q.defer();
|
|
|
|
EndpointProvider.setEndpointID(targetEndpointId);
|
|
|
|
SwarmService.swarm()
|
|
.then(function success(data) {
|
|
var swarm = data;
|
|
if (swarm.Id === stack.SwarmId) {
|
|
deferred.reject({ msg: 'Target environment is located in the same Swarm cluster as the current environment', err: null });
|
|
return;
|
|
}
|
|
return Stack.migrate({ id: stack.Id, endpointId: stack.EndpointId }, { EndpointID: targetEndpointId, SwarmID: swarm.Id, Name: newName }).$promise;
|
|
})
|
|
.then(function success() {
|
|
deferred.resolve();
|
|
})
|
|
.catch(function error(err) {
|
|
deferred.reject({ msg: 'Unable to migrate stack', err: err });
|
|
})
|
|
.finally(function final() {
|
|
EndpointProvider.setEndpointID(stack.EndpointId);
|
|
});
|
|
|
|
return deferred.promise;
|
|
};
|
|
|
|
service.migrateComposeStack = function (stack, targetEndpointId, newName) {
|
|
var deferred = $q.defer();
|
|
|
|
EndpointProvider.setEndpointID(targetEndpointId);
|
|
|
|
Stack.migrate({ id: stack.Id, endpointId: stack.EndpointId }, { EndpointID: targetEndpointId, Name: newName })
|
|
.$promise.then(function success() {
|
|
deferred.resolve();
|
|
})
|
|
.catch(function error(err) {
|
|
EndpointProvider.setEndpointID(stack.EndpointId);
|
|
deferred.reject({ msg: 'Unable to migrate stack', err: err });
|
|
});
|
|
|
|
return deferred.promise;
|
|
};
|
|
|
|
service.stacks = function (compose, swarm, endpointId, includeOrphanedStacks = false) {
|
|
var deferred = $q.defer();
|
|
|
|
var queries = [];
|
|
if (compose) {
|
|
queries.push(service.composeStacks(endpointId, true, { EndpointID: endpointId, IncludeOrphanedStacks: includeOrphanedStacks }));
|
|
}
|
|
if (swarm) {
|
|
queries.push(service.swarmStacks(endpointId, true, { IncludeOrphanedStacks: includeOrphanedStacks }));
|
|
}
|
|
|
|
$q.all(queries)
|
|
.then(function success(data) {
|
|
var stacks = [];
|
|
if (data[0]) {
|
|
stacks = stacks.concat(data[0]);
|
|
}
|
|
if (data[1]) {
|
|
stacks = stacks.concat(data[1]);
|
|
}
|
|
deferred.resolve(stacks);
|
|
})
|
|
.catch(function error(err) {
|
|
deferred.reject({ msg: 'Unable to retrieve stacks', err: err });
|
|
});
|
|
|
|
return deferred.promise;
|
|
};
|
|
|
|
service.externalSwarmStacks = function () {
|
|
var deferred = $q.defer();
|
|
|
|
ServiceService.services()
|
|
.then(function success(services) {
|
|
deferred.resolve(StackHelper.getExternalStacksFromServices(services));
|
|
})
|
|
.catch(function error(err) {
|
|
deferred.reject({ msg: 'Unable to retrieve external stacks', err: err });
|
|
});
|
|
|
|
return deferred.promise;
|
|
};
|
|
|
|
service.externalComposeStacks = function () {
|
|
var deferred = $q.defer();
|
|
|
|
ContainerService.containers(1)
|
|
.then(function success(containers) {
|
|
deferred.resolve(StackHelper.getExternalStacksFromContainers(containers));
|
|
})
|
|
.catch(function error(err) {
|
|
deferred.reject({ msg: 'Unable to retrieve external stacks', err: err });
|
|
});
|
|
|
|
return deferred.promise;
|
|
};
|
|
|
|
service.unionStacks = function (stacks, externalStacks) {
|
|
stacks.forEach((stack) => {
|
|
externalStacks.forEach((externalStack) => {
|
|
if (stack.Orphaned && stack.Name == externalStack.Name) {
|
|
stack.OrphanedRunning = true;
|
|
}
|
|
});
|
|
});
|
|
const result = _.unionWith(stacks, externalStacks, function (a, b) {
|
|
return a.Name === b.Name;
|
|
});
|
|
|
|
return result;
|
|
};
|
|
|
|
service.composeStacks = function (endpointId, includeExternalStacks, filters) {
|
|
var deferred = $q.defer();
|
|
|
|
$q.all({
|
|
stacks: Stack.query({ filters: filters }).$promise,
|
|
externalStacks: includeExternalStacks ? service.externalComposeStacks() : [],
|
|
})
|
|
.then(function success(data) {
|
|
var stacks = data.stacks.map(function (item) {
|
|
if (item.EndpointId == endpointId) {
|
|
return new StackViewModel(item);
|
|
} else {
|
|
return new OrphanedStackViewModel(item);
|
|
}
|
|
});
|
|
|
|
var externalStacks = data.externalStacks;
|
|
const result = service.unionStacks(stacks, externalStacks);
|
|
deferred.resolve(result);
|
|
})
|
|
.catch(function error(err) {
|
|
deferred.reject({ msg: 'Unable to retrieve stacks', err: err });
|
|
});
|
|
|
|
return deferred.promise;
|
|
};
|
|
|
|
service.swarmStacks = function (endpointId, includeExternalStacks, filters = {}) {
|
|
var deferred = $q.defer();
|
|
|
|
SwarmService.swarm()
|
|
.then(function success(data) {
|
|
var swarm = data;
|
|
filters = { SwarmID: swarm.Id, ...filters };
|
|
|
|
return $q.all({
|
|
stacks: Stack.query({ filters: filters }).$promise,
|
|
externalStacks: includeExternalStacks ? service.externalSwarmStacks() : [],
|
|
});
|
|
})
|
|
.then(function success(data) {
|
|
var stacks = data.stacks.map(function (item) {
|
|
if (item.EndpointId == endpointId) {
|
|
return new StackViewModel(item);
|
|
} else {
|
|
return new OrphanedStackViewModel(item);
|
|
}
|
|
});
|
|
|
|
var externalStacks = data.externalStacks;
|
|
const result = service.unionStacks(stacks, externalStacks);
|
|
deferred.resolve(result);
|
|
})
|
|
.catch(function error(err) {
|
|
deferred.reject({ msg: 'Unable to retrieve stacks', err: err });
|
|
});
|
|
|
|
return deferred.promise;
|
|
};
|
|
|
|
service.remove = function (stack, external, endpointId) {
|
|
var deferred = $q.defer();
|
|
|
|
Stack.remove({ id: stack.Id ? stack.Id : stack.Name, external: external, endpointId: endpointId })
|
|
.$promise.then(function success() {
|
|
deferred.resolve();
|
|
})
|
|
.catch(function error(err) {
|
|
deferred.reject({ msg: 'Unable to remove the stack', err: err });
|
|
});
|
|
|
|
return deferred.promise;
|
|
};
|
|
|
|
service.associate = function (stack, endpointId, orphanedRunning) {
|
|
var deferred = $q.defer();
|
|
|
|
if (stack.Type == 1) {
|
|
SwarmService.swarm()
|
|
.then(function success(data) {
|
|
const swarm = data;
|
|
return Stack.associate({ id: stack.Id, endpointId: endpointId, swarmId: swarm.Id, orphanedRunning }).$promise;
|
|
})
|
|
.then(function success(data) {
|
|
deferred.resolve(data);
|
|
})
|
|
.catch(function error(err) {
|
|
deferred.reject({ msg: 'Unable to associate the stack', err: err });
|
|
});
|
|
} else {
|
|
Stack.associate({ id: stack.Id, endpointId: endpointId, orphanedRunning })
|
|
.$promise.then(function success(data) {
|
|
deferred.resolve(data);
|
|
})
|
|
.catch(function error(err) {
|
|
deferred.reject({ msg: 'Unable to associate the stack', err: err });
|
|
});
|
|
}
|
|
|
|
return deferred.promise;
|
|
};
|
|
|
|
service.updateStack = function (stack, stackFile, env, prune) {
|
|
return Stack.update({ endpointId: stack.EndpointId }, { id: stack.Id, StackFileContent: stackFile, Env: env, Prune: prune }).$promise;
|
|
};
|
|
|
|
service.updateKubeStack = function (stack, stackFile, gitConfig) {
|
|
let payload = {};
|
|
|
|
if (stackFile) {
|
|
payload = {
|
|
StackFileContent: stackFile,
|
|
};
|
|
} else {
|
|
const autoUpdate = {};
|
|
if (gitConfig.AutoUpdate && gitConfig.AutoUpdate.RepositoryAutomaticUpdates) {
|
|
if (gitConfig.AutoUpdate.RepositoryMechanism === RepositoryMechanismTypes.INTERVAL) {
|
|
autoUpdate.Interval = gitConfig.AutoUpdate.RepositoryFetchInterval;
|
|
} else if (gitConfig.AutoUpdate.RepositoryMechanism === RepositoryMechanismTypes.WEBHOOK) {
|
|
autoUpdate.Webhook = gitConfig.AutoUpdate.RepositoryWebhookURL.split('/').reverse()[0];
|
|
}
|
|
}
|
|
|
|
payload = {
|
|
AutoUpdate: autoUpdate,
|
|
RepositoryReferenceName: gitConfig.RefName,
|
|
RepositoryAuthentication: gitConfig.RepositoryAuthentication,
|
|
RepositoryUsername: gitConfig.RepositoryUsername,
|
|
RepositoryPassword: gitConfig.RepositoryPassword,
|
|
};
|
|
}
|
|
|
|
return Stack.update({ id: stack.Id, endpointId: stack.EndpointId }, payload).$promise;
|
|
};
|
|
|
|
service.createComposeStackFromFileUpload = function (name, stackFile, env, endpointId) {
|
|
return FileUploadService.createComposeStack(name, stackFile, env, endpointId);
|
|
};
|
|
|
|
service.createSwarmStackFromFileUpload = function (name, stackFile, env, endpointId) {
|
|
var deferred = $q.defer();
|
|
|
|
SwarmService.swarm()
|
|
.then(function success(data) {
|
|
var swarm = data;
|
|
return FileUploadService.createSwarmStack(name, swarm.Id, stackFile, env, endpointId);
|
|
})
|
|
.then(function success(data) {
|
|
deferred.resolve(data.data);
|
|
})
|
|
.catch(function error(err) {
|
|
deferred.reject({ msg: 'Unable to create the stack', err: err });
|
|
});
|
|
|
|
return deferred.promise;
|
|
};
|
|
service.createComposeStackFromFileContent = function (name, stackFileContent, env, endpointId) {
|
|
var payload = {
|
|
Name: name,
|
|
StackFileContent: stackFileContent,
|
|
Env: env,
|
|
};
|
|
return Stack.create({ method: 'string', type: 2, endpointId: endpointId }, payload).$promise;
|
|
};
|
|
|
|
service.createSwarmStackFromFileContent = function (name, stackFileContent, env, endpointId) {
|
|
var deferred = $q.defer();
|
|
|
|
SwarmService.swarm()
|
|
.then(function success(swarm) {
|
|
var payload = {
|
|
Name: name,
|
|
SwarmID: swarm.Id,
|
|
StackFileContent: stackFileContent,
|
|
Env: env,
|
|
};
|
|
return Stack.create({ method: 'string', type: 1, endpointId: endpointId }, payload).$promise;
|
|
})
|
|
.then(function success(data) {
|
|
deferred.resolve(data);
|
|
})
|
|
.catch(function error(err) {
|
|
deferred.reject({ msg: 'Unable to create the stack', err: err });
|
|
});
|
|
|
|
return deferred.promise;
|
|
};
|
|
|
|
service.createComposeStackFromGitRepository = function (name, repositoryOptions, env, endpointId) {
|
|
var payload = {
|
|
Name: name,
|
|
RepositoryURL: repositoryOptions.RepositoryURL,
|
|
RepositoryReferenceName: repositoryOptions.RepositoryReferenceName,
|
|
ComposeFile: repositoryOptions.ComposeFilePathInRepository,
|
|
AdditionalFiles: repositoryOptions.AdditionalFiles,
|
|
RepositoryAuthentication: repositoryOptions.RepositoryAuthentication,
|
|
RepositoryUsername: repositoryOptions.RepositoryUsername,
|
|
RepositoryPassword: repositoryOptions.RepositoryPassword,
|
|
Env: env,
|
|
};
|
|
|
|
if (repositoryOptions.AutoUpdate) {
|
|
payload.AutoUpdate = repositoryOptions.AutoUpdate;
|
|
}
|
|
|
|
return Stack.create({ method: 'repository', type: 2, endpointId: endpointId }, payload).$promise;
|
|
};
|
|
|
|
service.createSwarmStackFromGitRepository = function (name, repositoryOptions, env, endpointId) {
|
|
var deferred = $q.defer();
|
|
|
|
SwarmService.swarm()
|
|
.then(function success(data) {
|
|
var swarm = data;
|
|
var payload = {
|
|
Name: name,
|
|
SwarmID: swarm.Id,
|
|
RepositoryURL: repositoryOptions.RepositoryURL,
|
|
RepositoryReferenceName: repositoryOptions.RepositoryReferenceName,
|
|
ComposeFile: repositoryOptions.ComposeFilePathInRepository,
|
|
AdditionalFiles: repositoryOptions.AdditionalFiles,
|
|
RepositoryAuthentication: repositoryOptions.RepositoryAuthentication,
|
|
RepositoryUsername: repositoryOptions.RepositoryUsername,
|
|
RepositoryPassword: repositoryOptions.RepositoryPassword,
|
|
Env: env,
|
|
};
|
|
|
|
if (repositoryOptions.AutoUpdate) {
|
|
payload.AutoUpdate = repositoryOptions.AutoUpdate;
|
|
}
|
|
|
|
return Stack.create({ method: 'repository', type: 1, endpointId: endpointId }, payload).$promise;
|
|
})
|
|
.then(function success(data) {
|
|
deferred.resolve(data);
|
|
})
|
|
.catch(function error(err) {
|
|
deferred.reject({ msg: 'Unable to create the stack', err: err });
|
|
});
|
|
|
|
return deferred.promise;
|
|
};
|
|
|
|
service.duplicateStack = function duplicateStack(name, stackFileContent, env, endpointId, type) {
|
|
var action = type === 1 ? service.createSwarmStackFromFileContent : service.createComposeStackFromFileContent;
|
|
return action(name, stackFileContent, env, endpointId);
|
|
};
|
|
|
|
async function kubernetesDeployAsync(endpointId, method, payload) {
|
|
try {
|
|
await Stack.create({ endpointId: endpointId, method: method, type: 3 }, payload).$promise;
|
|
} catch (err) {
|
|
throw { err: err };
|
|
}
|
|
}
|
|
|
|
service.kubernetesDeploy = function (endpointId, method, payload) {
|
|
return $async(kubernetesDeployAsync, endpointId, method, payload);
|
|
};
|
|
|
|
service.start = start;
|
|
function start(id) {
|
|
return Stack.start({ id }).$promise;
|
|
}
|
|
|
|
service.stop = stop;
|
|
function stop(id) {
|
|
return Stack.stop({ id }).$promise;
|
|
}
|
|
|
|
function updateGit(id, endpointId, env, prune, gitConfig) {
|
|
return Stack.updateGit(
|
|
{ endpointId, id },
|
|
{
|
|
env,
|
|
prune,
|
|
RepositoryReferenceName: gitConfig.RefName,
|
|
RepositoryAuthentication: gitConfig.RepositoryAuthentication,
|
|
RepositoryUsername: gitConfig.RepositoryUsername,
|
|
RepositoryPassword: gitConfig.RepositoryPassword,
|
|
}
|
|
).$promise;
|
|
}
|
|
|
|
function updateKubeGit(id, endpointId, namespace, gitConfig) {
|
|
return Stack.updateGit(
|
|
{ endpointId, id },
|
|
{
|
|
Namespace: namespace,
|
|
RepositoryReferenceName: gitConfig.RefName,
|
|
RepositoryAuthentication: gitConfig.RepositoryAuthentication,
|
|
RepositoryUsername: gitConfig.RepositoryUsername,
|
|
RepositoryPassword: gitConfig.RepositoryPassword,
|
|
}
|
|
).$promise;
|
|
}
|
|
|
|
service.updateGitStackSettings = function (id, endpointId, env, gitConfig) {
|
|
// prepare auto update
|
|
const autoUpdate = {};
|
|
|
|
if (gitConfig.AutoUpdate.RepositoryAutomaticUpdates) {
|
|
if (gitConfig.AutoUpdate.RepositoryMechanism === RepositoryMechanismTypes.INTERVAL) {
|
|
autoUpdate.Interval = gitConfig.AutoUpdate.RepositoryFetchInterval;
|
|
} else if (gitConfig.AutoUpdate.RepositoryMechanism === RepositoryMechanismTypes.WEBHOOK) {
|
|
autoUpdate.Webhook = gitConfig.AutoUpdate.RepositoryWebhookURL.split('/').reverse()[0];
|
|
}
|
|
}
|
|
|
|
return Stack.updateGitStackSettings(
|
|
{ endpointId, id },
|
|
{
|
|
AutoUpdate: autoUpdate,
|
|
Env: env,
|
|
RepositoryReferenceName: gitConfig.RefName,
|
|
RepositoryAuthentication: gitConfig.RepositoryAuthentication,
|
|
RepositoryUsername: gitConfig.RepositoryUsername,
|
|
RepositoryPassword: gitConfig.RepositoryPassword,
|
|
}
|
|
).$promise;
|
|
};
|
|
|
|
return service;
|
|
},
|
|
]);
|