mirror of
https://github.com/portainer/portainer.git
synced 2025-08-04 13:25:26 +02:00
feat(state): introduce endpoint state (#529)
This commit is contained in:
parent
7ebe4af77d
commit
fa4ec04c47
26 changed files with 267 additions and 122 deletions
|
@ -240,44 +240,11 @@ angular.module('portainer.services', ['ngResource', 'ngSanitize'])
|
|||
initAdminUser: { method: 'POST', params: { username: 'admin', action: 'init' } }
|
||||
});
|
||||
}])
|
||||
.factory('EndpointMode', ['$rootScope', 'Info', function EndpointMode($rootScope, Info) {
|
||||
'use strict';
|
||||
return {
|
||||
determineEndpointMode: function() {
|
||||
Info.get({}, function(d) {
|
||||
var mode = {
|
||||
provider: '',
|
||||
role: ''
|
||||
};
|
||||
if (_.startsWith(d.ServerVersion, 'swarm')) {
|
||||
mode.provider = "DOCKER_SWARM";
|
||||
if (d.SystemStatus[0][1] === 'primary') {
|
||||
mode.role = "PRIMARY";
|
||||
} else {
|
||||
mode.role = "REPLICA";
|
||||
}
|
||||
} else {
|
||||
if (!d.Swarm || _.isEmpty(d.Swarm.NodeID)) {
|
||||
mode.provider = "DOCKER_STANDALONE";
|
||||
} else {
|
||||
mode.provider = "DOCKER_SWARM_MODE";
|
||||
if (d.Swarm.ControlAvailable) {
|
||||
mode.role = "MANAGER";
|
||||
} else {
|
||||
mode.role = "WORKER";
|
||||
}
|
||||
}
|
||||
}
|
||||
$rootScope.endpointMode = mode;
|
||||
});
|
||||
}
|
||||
};
|
||||
}])
|
||||
.factory('Authentication', ['$q', '$rootScope', 'Auth', 'jwtHelper', 'localStorageService', function AuthenticationFactory($q, $rootScope, Auth, jwtHelper, localStorageService) {
|
||||
.factory('Authentication', ['$q', '$rootScope', 'Auth', 'jwtHelper', 'LocalStorage', function AuthenticationFactory($q, $rootScope, Auth, jwtHelper, LocalStorage) {
|
||||
'use strict';
|
||||
return {
|
||||
init: function() {
|
||||
var jwt = localStorageService.get('JWT');
|
||||
var jwt = LocalStorage.getJWT();
|
||||
if (jwt) {
|
||||
var tokenPayload = jwtHelper.decodeToken(jwt);
|
||||
$rootScope.username = tokenPayload.username;
|
||||
|
@ -287,7 +254,7 @@ angular.module('portainer.services', ['ngResource', 'ngSanitize'])
|
|||
return $q(function (resolve, reject) {
|
||||
Auth.login({username: username, password: password}).$promise
|
||||
.then(function(data) {
|
||||
localStorageService.set('JWT', data.jwt);
|
||||
LocalStorage.storeJWT(data.jwt);
|
||||
$rootScope.username = username;
|
||||
resolve();
|
||||
}, function() {
|
||||
|
@ -296,10 +263,10 @@ angular.module('portainer.services', ['ngResource', 'ngSanitize'])
|
|||
});
|
||||
},
|
||||
logout: function() {
|
||||
localStorageService.remove('JWT');
|
||||
LocalStorage.deleteJWT();
|
||||
},
|
||||
isAuthenticated: function() {
|
||||
var jwt = localStorageService.get('JWT');
|
||||
var jwt = LocalStorage.getJWT();
|
||||
return jwt && !jwtHelper.isTokenExpired(jwt);
|
||||
}
|
||||
};
|
||||
|
@ -359,7 +326,69 @@ angular.module('portainer.services', ['ngResource', 'ngSanitize'])
|
|||
setActiveEndpoint: { method: 'POST', params: { id: '@id', action: 'active' } }
|
||||
});
|
||||
}])
|
||||
.factory('EndpointService', ['$q', '$timeout', 'Endpoints', 'FileUploadService', function EndpointServiceFactory($q, $timeout, Endpoints, FileUploadService) {
|
||||
.factory('LocalStorage', ['localStorageService', function LocalStorageFactory(localStorageService) {
|
||||
'use strict';
|
||||
return {
|
||||
storeEndpointState: function(state) {
|
||||
localStorageService.set('ENDPOINT_STATE', state);
|
||||
},
|
||||
getEndpointState: function() {
|
||||
return localStorageService.get('ENDPOINT_STATE');
|
||||
},
|
||||
storeJWT: function(jwt) {
|
||||
localStorageService.set('JWT', jwt);
|
||||
},
|
||||
getJWT: function() {
|
||||
return localStorageService.get('JWT');
|
||||
},
|
||||
deleteJWT: function() {
|
||||
localStorageService.remove('JWT');
|
||||
}
|
||||
};
|
||||
}])
|
||||
.factory('StateManager', ['$q', 'Info', 'InfoHelper', 'Version', 'LocalStorage', function StateManagerFactory($q, Info, InfoHelper, Version, LocalStorage) {
|
||||
'use strict';
|
||||
|
||||
var state = {
|
||||
loading: true,
|
||||
application: {},
|
||||
endpoint: {}
|
||||
};
|
||||
|
||||
return {
|
||||
init: function() {
|
||||
var endpointState = LocalStorage.getEndpointState();
|
||||
if (endpointState) {
|
||||
state.endpoint = endpointState;
|
||||
}
|
||||
state.loading = false;
|
||||
},
|
||||
updateEndpointState: function(loading) {
|
||||
var deferred = $q.defer();
|
||||
if (loading) {
|
||||
state.loading = true;
|
||||
}
|
||||
$q.all([Info.get({}).$promise, Version.get({}).$promise])
|
||||
.then(function success(data) {
|
||||
var endpointMode = InfoHelper.determineEndpointMode(data[0]);
|
||||
var endpointAPIVersion = parseFloat(data[1].ApiVersion);
|
||||
state.endpoint.mode = endpointMode;
|
||||
state.endpoint.apiVersion = endpointAPIVersion;
|
||||
LocalStorage.storeEndpointState(state.endpoint);
|
||||
state.loading = false;
|
||||
deferred.resolve();
|
||||
}, function error(err) {
|
||||
state.loading = false;
|
||||
deferred.reject({msg: 'Unable to connect to the Docker endpoint', err: err});
|
||||
});
|
||||
return deferred.promise;
|
||||
},
|
||||
getState: function() {
|
||||
return state;
|
||||
}
|
||||
};
|
||||
}])
|
||||
.factory('EndpointService', ['$q', 'Endpoints', 'FileUploadService', function EndpointServiceFactory($q, Endpoints, FileUploadService) {
|
||||
'use strict';
|
||||
return {
|
||||
getActive: function() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue