From c3cf5b5f9d6979cdcf276c28abeb75270ef7e1ee Mon Sep 17 00:00:00 2001 From: Anthony Lapenna Date: Thu, 20 Oct 2016 16:43:09 +1300 Subject: [PATCH] feat(templates): advanced template creation (#277) --- app/app.js | 3 ++ .../createContainerController.js | 23 +++++------ .../createContainer/createcontainer.html | 2 +- app/components/images/imagesController.js | 1 - app/components/templates/templates.html | 1 + app/shared/helpers.js | 40 ++++++++++++++++++- 6 files changed, 55 insertions(+), 15 deletions(-) diff --git a/app/app.js b/app/app.js index 5a1fda021..435cd23af 100644 --- a/app/app.js +++ b/app/app.js @@ -81,6 +81,9 @@ angular.module('portainer', [ }) .state('actions.create.container', { url: "/container", + params: { + template: null + }, templateUrl: 'app/components/createContainer/createcontainer.html', controller: 'CreateContainerController' }) diff --git a/app/components/createContainer/createContainerController.js b/app/components/createContainer/createContainerController.js index ba3c8fe04..e71855c9e 100644 --- a/app/components/createContainer/createContainerController.js +++ b/app/components/createContainer/createContainerController.js @@ -1,28 +1,29 @@ angular.module('createContainer', []) -.controller('CreateContainerController', ['$scope', '$state', 'Config', 'Info', 'Container', 'Image', 'Volume', 'Network', 'Messages', -function ($scope, $state, Config, Info, Container, Image, Volume, Network, Messages) { +.controller('CreateContainerController', ['$scope', '$state', '$stateParams', 'Config', 'Info', 'Container', 'Image', 'Volume', 'Network', 'TemplateHelper', 'Messages', +function ($scope, $state, $stateParams, Config, Info, Container, Image, Volume, Network, TemplateHelper, Messages) { - $scope.state = { - alwaysPull: true - }; + if ($stateParams.template) { + $scope.template = $stateParams.template; + } $scope.formValues = { + alwaysPull: true, Console: 'none', - Volumes: [], - AvailableRegistries: [], + Volumes: $scope.template && $scope.template.volumes ? TemplateHelper.getVolumeBindings($scope.template.volumes) : [], Registry: '' }; $scope.imageConfig = {}; $scope.config = { - Env: [], + Image: $scope.template ? $scope.template.image : '', + Env: $scope.template && $scope.template.env ? TemplateHelper.getEnvBindings($scope.template.env) : [], ExposedPorts: {}, HostConfig: { RestartPolicy: { Name: 'no' }, - PortBindings: [], + PortBindings: $scope.template ? TemplateHelper.getPortBindings($scope.template.ports) : [], Binds: [], NetworkMode: 'bridge', Privileged: false @@ -61,8 +62,6 @@ function ($scope, $state, Config, Info, Container, Image, Volume, Network, Messa } }); - $scope.formValues.AvailableRegistries = c.registries; - Volume.query({}, function (d) { $scope.availableVolumes = d.Volumes; }, function (e) { @@ -232,7 +231,7 @@ function ($scope, $state, Config, Info, Container, Image, Volume, Network, Messa $scope.create = function () { var config = prepareConfiguration(); $('#createContainerSpinner').show(); - if ($scope.state.alwaysPull) { + if ($scope.formValues.alwaysPull) { pullImageAndCreateContainer(config); } else { createContainer(config); diff --git a/app/components/createContainer/createcontainer.html b/app/components/createContainer/createcontainer.html index 7a9234741..4672963f3 100644 --- a/app/components/createContainer/createcontainer.html +++ b/app/components/createContainer/createcontainer.html @@ -31,7 +31,7 @@
diff --git a/app/components/images/imagesController.js b/app/components/images/imagesController.js index 0d17817c4..99b2ceb44 100644 --- a/app/components/images/imagesController.js +++ b/app/components/images/imagesController.js @@ -102,7 +102,6 @@ function ($scope, $state, Config, Image, Messages) { } Config.$promise.then(function (c) { - $scope.availableRegistries = c.registries; fetchImages(); }); }]); diff --git a/app/components/templates/templates.html b/app/components/templates/templates.html index 4c18a38a0..ef953029d 100644 --- a/app/components/templates/templates.html +++ b/app/components/templates/templates.html @@ -54,6 +54,7 @@
+
diff --git a/app/shared/helpers.js b/app/shared/helpers.js index ad200560d..4f700b99d 100644 --- a/app/shared/helpers.js +++ b/app/shared/helpers.js @@ -48,4 +48,42 @@ angular.module('portainer.helpers', []) }; } }; -}]); +}]) +.factory('TemplateHelper', [function TemplateHelperFactory() { + 'use strict'; + return { + getPortBindings: function(ports) { + var bindings = []; + ports.forEach(function (port) { + var portAndProtocol = _.split(port, '/'); + var binding = { + containerPort: portAndProtocol[0], + protocol: portAndProtocol[1] + }; + bindings.push(binding); + }); + return bindings; + }, + getVolumeBindings: function(volumes) { + var bindings = []; + volumes.forEach(function (volume) { + bindings.push({ containerPath: volume }); + }); + return bindings; + }, + getEnvBindings: function(env) { + var bindings = []; + env.forEach(function (envvar) { + var binding = { + name: envvar.name + }; + if (envvar.set) { + binding.value = envvar.set; + } + bindings.push(binding); + }); + return bindings; + } + }; +}]) +;