diff --git a/api/bolt/endpoint_service.go b/api/bolt/endpoint_service.go index 9046bf30f..16bd36984 100644 --- a/api/bolt/endpoint_service.go +++ b/api/bolt/endpoint_service.go @@ -160,3 +160,15 @@ func (service *EndpointService) SetActive(endpoint *portainer.Endpoint) error { return nil }) } + +// DeleteActive deletes the active endpoint. +func (service *EndpointService) DeleteActive() error { + return service.store.db.Update(func(tx *bolt.Tx) error { + bucket := tx.Bucket([]byte(activeEndpointBucketName)) + err := bucket.Delete(internal.Itob(activeEndpointID)) + if err != nil { + return err + } + return nil + }) +} diff --git a/api/http/endpoint_handler.go b/api/http/endpoint_handler.go index 42a77cbec..9f5ca31c0 100644 --- a/api/http/endpoint_handler.go +++ b/api/http/endpoint_handler.go @@ -260,6 +260,7 @@ type putEndpointsRequest struct { } // handleDeleteEndpoint handles DELETE requests on /endpoints/:id +// DELETE /endpoints/0 deletes the active endpoint func (handler *EndpointHandler) handleDeleteEndpoint(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id := vars["id"] @@ -270,7 +271,14 @@ func (handler *EndpointHandler) handleDeleteEndpoint(w http.ResponseWriter, r *h return } - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + var endpoint *portainer.Endpoint + if id == "0" { + endpoint, err = handler.EndpointService.GetActive() + endpointID = int(endpoint.ID) + } else { + endpoint, err = handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + } + if err == portainer.ErrEndpointNotFound { Error(w, err, http.StatusNotFound, handler.Logger) return @@ -284,6 +292,13 @@ func (handler *EndpointHandler) handleDeleteEndpoint(w http.ResponseWriter, r *h Error(w, err, http.StatusInternalServerError, handler.Logger) return } + if id == "0" { + err = handler.EndpointService.DeleteActive() + if err != nil { + Error(w, err, http.StatusInternalServerError, handler.Logger) + return + } + } if endpoint.TLS { err = handler.FileService.DeleteTLSFiles(portainer.EndpointID(endpointID)) diff --git a/api/portainer.go b/api/portainer.go index c03679e84..24a9d0f27 100644 --- a/api/portainer.go +++ b/api/portainer.go @@ -94,6 +94,7 @@ type ( DeleteEndpoint(ID EndpointID) error GetActive() (*Endpoint, error) SetActive(endpoint *Endpoint) error + DeleteActive() error } // CryptoService represents a service for encrypting/hashing data. diff --git a/app/app.js b/app/app.js index 2c2a58589..7e5c61290 100644 --- a/app/app.js +++ b/app/app.js @@ -49,8 +49,8 @@ angular.module('portainer', [ .setPrefix('portainer'); jwtOptionsProvider.config({ - tokenGetter: ['localStorageService', function(localStorageService) { - return localStorageService.get('JWT'); + tokenGetter: ['LocalStorage', function(LocalStorage) { + return LocalStorage.getJWT(); }], unauthenticatedRedirector: ['$state', function($state) { $state.go('auth', {error: 'Your session has expired'}); @@ -528,21 +528,18 @@ angular.module('portainer', [ }; }); }]) - .run(['$rootScope', '$state', 'Authentication', 'authManager', 'EndpointMode', function ($rootScope, $state, Authentication, authManager, EndpointMode) { + .run(['$rootScope', '$state', 'Authentication', 'authManager', 'StateManager', function ($rootScope, $state, Authentication, authManager, StateManager) { authManager.checkAuthOnRefresh(); authManager.redirectWhenUnauthenticated(); + Authentication.init(); + StateManager.init(); + $rootScope.$state = $state; $rootScope.$on('tokenHasExpired', function($state) { $state.go('auth', {error: 'Your session has expired'}); }); - - $rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams) { - if (toState.name !== 'endpointInit' && (fromState.name === 'auth' || fromState.name === '' || fromState.name === 'endpointInit') && Authentication.isAuthenticated()) { - EndpointMode.determineEndpointMode(); - } - }); }]) // This is your docker url that the api will use to make requests // You need to set this to the api endpoint without the port i.e. http://192.168.1.9 diff --git a/app/components/auth/authController.js b/app/components/auth/authController.js index 89d99b6d3..2e30f2e5d 100644 --- a/app/components/auth/authController.js +++ b/app/components/auth/authController.js @@ -1,6 +1,6 @@ angular.module('auth', []) -.controller('AuthenticationController', ['$scope', '$state', '$stateParams', '$window', '$timeout', '$sanitize', 'Config', 'Authentication', 'Users', 'EndpointService', 'Messages', -function ($scope, $state, $stateParams, $window, $timeout, $sanitize, Config, Authentication, Users, EndpointService, Messages) { +.controller('AuthenticationController', ['$scope', '$state', '$stateParams', '$window', '$timeout', '$sanitize', 'Config', 'Authentication', 'Users', 'EndpointService', 'StateManager', 'Messages', +function ($scope, $state, $stateParams, $window, $timeout, $sanitize, Config, Authentication, Users, EndpointService, StateManager, Messages) { $scope.authData = { username: 'admin', @@ -60,7 +60,12 @@ function ($scope, $state, $stateParams, $window, $timeout, $sanitize, Config, Au var password = $sanitize($scope.authData.password); Authentication.login(username, password).then(function success() { EndpointService.getActive().then(function success(data) { - $state.go('dashboard'); + StateManager.updateEndpointState(true) + .then(function success() { + $state.go('dashboard'); + }, function error(err) { + Messages.error("Failure", err, 'Unable to connect to the Docker endpoint'); + }); }, function error(err) { if (err.status === 404) { $state.go('endpointInit'); diff --git a/app/components/containers/containers.html b/app/components/containers/containers.html index 1aaa749f8..89cb19f04 100644 --- a/app/components/containers/containers.html +++ b/app/components/containers/containers.html @@ -68,7 +68,7 @@ -