|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +require('app') |
| 4 | + .controller('SetupMirrorServerModalController', SetupMirrorServerModalController); |
| 5 | + |
| 6 | +var tabVisibility = { |
| 7 | + buildfiles: { advanced: true, mirror: true }, |
| 8 | + repository: { advanced: true, mirror: true }, |
| 9 | + whitelist: { advanced: true, mirror: 3, featureFlagName: 'whitelist' }, |
| 10 | + ports: { advanced: false, mirror: false }, |
| 11 | + env: { advanced: true, mirror: true }, |
| 12 | + translation: { advanced: true, mirror: false }, |
| 13 | + logs: { advanced: true, mirror: false }, |
| 14 | +}; |
| 15 | + |
| 16 | +function SetupMirrorServerModalController( |
| 17 | + $scope, |
| 18 | + $controller, |
| 19 | + $q, |
| 20 | + $rootScope, |
| 21 | + createAndBuildNewContainer, |
| 22 | + createBuildFromContextVersionId, |
| 23 | + errs, |
| 24 | + eventTracking, |
| 25 | + fetchUser, |
| 26 | + helpCards, |
| 27 | + keypather, |
| 28 | + loading, |
| 29 | + loadingPromises, |
| 30 | + promisify, |
| 31 | + cardInfoTypes, |
| 32 | + OpenItems, |
| 33 | + close, |
| 34 | + repo, |
| 35 | + build, |
| 36 | + masterBranch |
| 37 | +) { |
| 38 | + var SMC = this; // Server Modal Controller (shared with EditServerModalController) |
| 39 | + window.SMC = SMC; |
| 40 | + SMC.helpCards = helpCards; |
| 41 | + |
| 42 | + var parentController = $controller('ServerModalController as SMC', { $scope: $scope }); |
| 43 | + angular.extend(SMC, { |
| 44 | + 'closeWithConfirmation': parentController.closeWithConfirmation.bind(SMC), |
| 45 | + 'changeTab': parentController.changeTab.bind(SMC), |
| 46 | + 'insertHostName': parentController.insertHostName.bind(SMC), |
| 47 | + 'isDirty': parentController.isDirty.bind(SMC), |
| 48 | + 'getNumberOfOpenTabs': parentController.getNumberOfOpenTabs.bind(SMC), |
| 49 | + 'getUpdatePromise': parentController.getUpdatePromise.bind(SMC), |
| 50 | + 'openDockerfile': parentController.openDockerfile.bind(SMC), |
| 51 | + 'populateStateFromData': parentController.populateStateFromData.bind(SMC), |
| 52 | + 'rebuildAndOrRedeploy': parentController.rebuildAndOrRedeploy.bind(SMC), |
| 53 | + 'requiresRebuild': parentController.requiresRebuild.bind(SMC), |
| 54 | + 'requiresRedeploy': parentController.requiresRedeploy.bind(SMC), |
| 55 | + 'resetStateContextVersion': parentController.resetStateContextVersion.bind(SMC), |
| 56 | + 'saveInstanceAndRefreshCards': parentController.saveInstanceAndRefreshCards.bind(SMC), |
| 57 | + 'updateInstanceAndReset': parentController.updateInstanceAndReset.bind(SMC) |
| 58 | + }); |
| 59 | + |
| 60 | + var mainRepoContainerFile = new cardInfoTypes.MainRepository(); |
| 61 | + // Set initial state |
| 62 | + angular.extend(SMC, { |
| 63 | + name: 'setupServerModal', |
| 64 | + isLoading: $rootScope.isLoading, |
| 65 | + portsSet: false, |
| 66 | + isNewContainer: true, |
| 67 | + openItems: new OpenItems(), |
| 68 | + getDisplayName: function () { |
| 69 | + if (SMC.instance) { |
| 70 | + return SMC.instance.getDisplayName(); |
| 71 | + } |
| 72 | + return SMC.state.repo.attrs.name; |
| 73 | + }, |
| 74 | + getElasticHostname: function () { |
| 75 | + if (keypather.get(SMC, 'state.repo.attrs')) { |
| 76 | + // NOTE: Is SMC the best way to get the hostname? |
| 77 | + var repo = SMC.state.repo; |
| 78 | + var repoName = repo.attrs.name; |
| 79 | + var repoOwner = repo.attrs.owner.login.toLowerCase(); |
| 80 | + var domain = SMC.state.repo.opts.userContentDomain; |
| 81 | + // NOTE: How can I know whether it will be staging or not? |
| 82 | + var hostname = repoName + '-staging-' + repoOwner + '.' + domain; |
| 83 | + return hostname; |
| 84 | + } |
| 85 | + return ''; |
| 86 | + }, |
| 87 | + state: { |
| 88 | + advanced: false, |
| 89 | + containerFiles: [ |
| 90 | + mainRepoContainerFile |
| 91 | + ], |
| 92 | + mainRepoContainerFile: mainRepoContainerFile, |
| 93 | + ports: [], |
| 94 | + packages: new cardInfoTypes.Packages(), |
| 95 | + promises: {}, |
| 96 | + opts: { |
| 97 | + masterPod: true, |
| 98 | + name: '', |
| 99 | + env: [], |
| 100 | + ipWhitelist: { |
| 101 | + enabled: false |
| 102 | + } |
| 103 | + }, |
| 104 | + whitelist: [ |
| 105 | + {address: ['1.1.1.1', '1.1.1.10'], description: ''}, |
| 106 | + {address: ['1.1.1.3'], description: 'Test'}, |
| 107 | + {address: ['1.1.1.9'], description: 'Runnable'}, |
| 108 | + {address: ['1.1.1.4', '1.1.1.5'], description: ''} |
| 109 | + ] |
| 110 | + }, |
| 111 | + actions: { |
| 112 | + close: SMC.closeWithConfirmation.bind(SMC, close) |
| 113 | + }, |
| 114 | + data: {}, |
| 115 | + selectedTab: 'buildfiles' |
| 116 | + }); |
| 117 | + loading.reset(SMC.name); |
| 118 | + loadingPromises.clear(SMC.name); |
| 119 | + loading.reset(SMC.name + 'IsBuilding'); |
| 120 | + |
| 121 | + if (!(repo && build && masterBranch)) { |
| 122 | + throw new Error('Repo, build and masterBranch are needed'); |
| 123 | + } |
| 124 | + |
| 125 | + // If a repo is passed into this controller, select that repo |
| 126 | + angular.extend(SMC.state, { |
| 127 | + repo: repo, |
| 128 | + build: build, |
| 129 | + contextVersion: build.contextVersion, |
| 130 | + acv: build.contextVersion.getMainAppCodeVersion(), |
| 131 | + branch: masterBranch, |
| 132 | + repoSelected: true, |
| 133 | + advanced: false |
| 134 | + }); |
| 135 | + SMC.state.mainRepoContainerFile.name = repo.attrs.name; |
| 136 | + SMC.state.opts.name = normalizeRepoName(repo); |
| 137 | + SMC.state.promises.contextVersion = $q.when(SMC.state.contextVersion); |
| 138 | + SMC.state.isMirroingDockerfile = true; |
| 139 | + var fullpath = keypather.get(SMC, 'state.build.contextVersion.attrs.buildDockerfilePath'); |
| 140 | + // Get everything before the last '/' and add a '/' at the end |
| 141 | + var path = fullpath.replace(/^(.*)\/.*$/, '$1') + '/'; |
| 142 | + // Get everything after the last '/' |
| 143 | + var name = fullpath.replace(/^.*\/(.*)$/, '$1'); |
| 144 | + fetchUser() |
| 145 | + .then(function (user) { |
| 146 | + // TODO: Match with dockefile path |
| 147 | + SMC.state.dockerfile = SMC.state.contextVersion.newFile({ |
| 148 | + _id: repo.dockerfiles[0].sha, |
| 149 | + id: repo.dockerfiles[0].sha, |
| 150 | + body: atob(repo.dockerfiles[0].content), |
| 151 | + name: name, |
| 152 | + path: path |
| 153 | + }); |
| 154 | + SMC.openItems.add(SMC.state.dockerfile); |
| 155 | + }); |
| 156 | + |
| 157 | + $scope.$on('resetStateContextVersion', function ($event, contextVersion, showSpinner) { |
| 158 | + $event.stopPropagation(); |
| 159 | + if (showSpinner) { |
| 160 | + loading(SMC.name, true); |
| 161 | + } |
| 162 | + SMC.resetStateContextVersion(contextVersion, showSpinner) |
| 163 | + .catch(errs.handler) |
| 164 | + .finally(function () { |
| 165 | + if (showSpinner) { |
| 166 | + loading(SMC.name, false); |
| 167 | + } |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + function normalizeRepoName(repo) { |
| 172 | + return repo.attrs.name.replace(/[^a-zA-Z0-9-]/g, '-'); |
| 173 | + } |
| 174 | + |
| 175 | + SMC.showStackSelector = function () { |
| 176 | + return false; |
| 177 | + }; |
| 178 | + |
| 179 | + SMC.rebuild = function (noCache, forceRebuild) { |
| 180 | + loading(SMC.name, true); |
| 181 | + return SMC.rebuildAndOrRedeploy(noCache, forceRebuild) |
| 182 | + .then(function () { |
| 183 | + return SMC.resetStateContextVersion(SMC.instance.contextVersion, true); |
| 184 | + }) |
| 185 | + .then(function (contextVersion) { |
| 186 | + return contextVersion; |
| 187 | + }) |
| 188 | + .catch(errs.handler) |
| 189 | + .finally(function () { |
| 190 | + loading(SMC.name, false); |
| 191 | + }); |
| 192 | + }; |
| 193 | + |
| 194 | + SMC.createServer = function () { |
| 195 | + // Wait until all changes to the context version have been resolved before |
| 196 | + // creating a build with that context version |
| 197 | + return loadingPromises.finished(SMC.name) |
| 198 | + .then(function () { |
| 199 | + console.log('1'); |
| 200 | + if (SMC.state.acv.attrs.branch !== SMC.state.branch.attrs.name) { |
| 201 | + return promisify(SMC.state.acv, 'update')({ |
| 202 | + repo: SMC.state.repo.attrs.full_name, |
| 203 | + branch: SMC.state.branch.attrs.name, |
| 204 | + commit: SMC.state.branch.attrs.commit.sha |
| 205 | + }); |
| 206 | + } |
| 207 | + }) |
| 208 | + .then(function () { |
| 209 | + return promisify(SMC.state.build, 'build')({ |
| 210 | + message: 'Initial Build' |
| 211 | + }); |
| 212 | + }) |
| 213 | + .then(function (build) { |
| 214 | + console.log('2', build); |
| 215 | + console.log('.', keypather.get(build, 'contextVersion')); |
| 216 | + console.log('.', keypather.get(build, 'contextVersion.attrs.build')); |
| 217 | + console.log('.', keypather.get(build, 'contextVersion.attrs.build.started')); |
| 218 | + var opts = { |
| 219 | + masterPod: true, |
| 220 | + name: SMC.state.name, |
| 221 | + build: build.id(), |
| 222 | + owner: { |
| 223 | + github: $rootScope.dataApp.data.activeAccount.oauthId() |
| 224 | + }, |
| 225 | + env: [], |
| 226 | + ipWhitelist: { |
| 227 | + enabled: false |
| 228 | + } |
| 229 | + }; |
| 230 | + return fetchUser().then(function (user) { |
| 231 | + console.log('createNewInstance 3.2', opts); |
| 232 | + return promisify(user, 'createInstance')(opts); |
| 233 | + }); |
| 234 | + |
| 235 | + }) |
| 236 | + .then(function instanceSetHandler (instance) { |
| 237 | + console.log('2.5'); |
| 238 | + if (instance) { |
| 239 | + SMC.instance = instance; |
| 240 | + SMC.state.instance = instance; |
| 241 | + // Reset the opts, in the same way as `EditServerModalController` |
| 242 | + SMC.state.opts = { |
| 243 | + env: keypather.get(instance, 'attrs.env') || [], |
| 244 | + ipWhitelist: angular.copy(keypather.get(instance, 'attrs.ipWhitelist')) || { |
| 245 | + enabled: false |
| 246 | + } |
| 247 | + }; |
| 248 | + return instance; |
| 249 | + } |
| 250 | + return $q.reject(new Error('Instance not created properly')); |
| 251 | + }) |
| 252 | + .then(function () { |
| 253 | + console.log('3'); |
| 254 | + eventTracking.createdRepoContainer(SMC.instance.attrs.owner.github, SMC.state.repo.attrs.name); |
| 255 | + return SMC.resetStateContextVersion(SMC.instance.contextVersion, true); |
| 256 | + }) |
| 257 | + .catch(function (err) { |
| 258 | + // If creating the server fails, reset the context version |
| 259 | + return SMC.resetStateContextVersion(SMC.state.contextVersion, false) |
| 260 | + .then(function () { |
| 261 | + // Since we failed to build, we need loading promises to have something in it |
| 262 | + loadingPromises.add(SMC.name, $q.when(true)); |
| 263 | + return $q.reject(err); |
| 264 | + }); |
| 265 | + }); |
| 266 | + }; |
| 267 | + |
| 268 | + /** |
| 269 | + * This function determines if a tab chooser should be shown |
| 270 | + * |
| 271 | + * @param tabname |
| 272 | + * @returns {Boolean} |
| 273 | + */ |
| 274 | + SMC.isTabVisible = function (tabName) { |
| 275 | + if (!tabVisibility[tabName]) { |
| 276 | + return false; |
| 277 | + } |
| 278 | + if (tabVisibility[tabName].featureFlagName && !$rootScope.featureFlags[tabVisibility[tabName].featureFlagName]) { |
| 279 | + return false; |
| 280 | + } |
| 281 | + if (SMC.state.isMirroingDockerfile) { |
| 282 | + return tabVisibility[tabName].mirror; |
| 283 | + } |
| 284 | + if (SMC.state.sadvanced) { |
| 285 | + return tabVisibility[tabName].advanced; |
| 286 | + } |
| 287 | + return false; |
| 288 | + }; |
| 289 | + |
| 290 | + SMC.isPrimaryButtonDisabled = function (serverFormInvalid) { |
| 291 | + return SMC.repositoryForm && SMC.repositoryForm.$invalid; |
| 292 | + }; |
| 293 | + |
| 294 | + SMC.needToBeDirtyToSaved = function () { |
| 295 | + if (!SMC.instance) { |
| 296 | + return false; |
| 297 | + } |
| 298 | + return true; |
| 299 | + }; |
| 300 | + |
| 301 | +} |
0 commit comments