refactoring 2

This commit is contained in:
Kasra Bigdeli
2017-12-03 20:16:01 -08:00
parent 3ed9404b5a
commit e18629457e
11 changed files with 351 additions and 252 deletions
+7 -2
View File
@@ -374,8 +374,13 @@
callback(null);
});
},
updateConfigAndSave: function (appName, instanceCount, envVars,
notExposeAsWebApp, volumes, callback) {
updateConfigAndSave: function (appName, appDefinition, callback) {
var instanceCount = appDefinition.instanceCount;
var envVars = appDefinition.envVars;
var notExposeAsWebApp = appDefinition.notExposeAsWebApp;
var volumes = appDefinition.volumes;
$http
.post(BASE_API + 'user/appDefinitions/update', {
appName: appName,
@@ -241,18 +241,24 @@ function AppDefinitionCtrl($scope, $cookieStore, $rootScope, pageDefinitions,
$scope.onUpdateConfigAndSave = function (app) {
$scope.loadingState.enabled = true;
apiManager.updateConfigAndSave(app.appName, app.instanceCount,
app.envVars, app.notExposeAsWebApp, app.volumes, function (data) {
var appDefinition = {
instanceCount: app.instanceCount,
envVars: app.envVars,
notExposeAsWebApp: app.notExposeAsWebApp,
volumes: app.volumes,
}
if (captainToast.showErrorToastIfNeeded(data)) {
$scope.loadingState.enabled = false;
return;
}
apiManager.updateConfigAndSave(app.appName, appConfig, function (data) {
captainToast.showToastSuccess('You app data and configuration is successfully updated.');
$state.reload();
if (captainToast.showErrorToastIfNeeded(data)) {
$scope.loadingState.enabled = false;
return;
}
});
captainToast.showToastSuccess('You app data and configuration is successfully updated.');
$state.reload();
});
};
}())
+9 -1
View File
@@ -100,7 +100,15 @@
}
function setupAppDefinition() {
apiManager.updateConfigAndSave(appName, 1, envVars, false, volumes, function (data) {
var appDefinition = {
instanceCount: 1,
envVars: envVars,
notExposeAsWebApp: false,
volumes: volumes,
};
apiManager.updateConfigAndSave(appName, appDefinition, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
@@ -169,7 +169,15 @@
}
function setupAppDefinition() {
apiManager.updateConfigAndSave(appName, 1, envVars, false, volumes, function (data) {
var appDefinition = {
instanceCount: 1,
envVars: envVars,
notExposeAsWebApp: false,
volumes: volumes,
};
apiManager.updateConfigAndSave(appName, appDefinition, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
+9 -1
View File
@@ -108,7 +108,15 @@
}
function setupAppDefinition() {
apiManager.updateConfigAndSave(appName, 1, envVars, true, volumes, function (data) {
var appDefinition = {
instanceCount: 1,
envVars: envVars,
notExposeAsWebApp: true,
volumes: volumes,
};
apiManager.updateConfigAndSave(appName, appDefinition, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
+9 -1
View File
@@ -94,7 +94,15 @@
}
function setupAppDefinition() {
apiManager.updateConfigAndSave(appName, 1, envVars, true, volumes, function (data) {
var appDefinition = {
instanceCount: 1,
envVars: envVars,
notExposeAsWebApp: true,
volumes: volumes,
};
apiManager.updateConfigAndSave(appName, appDefinition, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
+248 -232
View File
@@ -1,237 +1,253 @@
(function () {
var SUCCESS = 'success';
var ERROR = 'danger';
var INFO = 'info';
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
oneClickAppsRepository["parse"] = function (apiManager) {
var getErrorMessageIfExists = apiManager.getErrorMessageIfExists;
var PARSE_NAME = 'PARSE_NAME';
var MONGODB_ROOT_PASSWORD = 'MONGODB_ROOT_PASSWORD';
var PARSE_SERVER_APPLICATION_ID = 'PARSE_SERVER_APPLICATION_ID';
var PARSE_SERVER_MASTER_KEY = 'PARSE_SERVER_MASTER_KEY';
var step1next = {};
step1next.data = [];
step1next.data.push({
label: 'Parser Server Name',
id: PARSE_NAME,
type: 'text'
});
step1next.data.push({
label: 'OPTINAL: Choose a MongoDB Root Password - or leave empty for randomly generated value',
id: MONGODB_ROOT_PASSWORD,
type: 'text'
});
step1next.data.push({
label: 'OPTINAL: Choose an Application ID - or leave empty for randomly generated value',
id: PARSE_SERVER_APPLICATION_ID,
type: 'text'
});
step1next.data.push({
label: 'OPTINAL: Choose a Master Key - or leave empty for randomly generated value',
id: PARSE_SERVER_MASTER_KEY,
type: 'text'
});
step1next.process = function (data, step1Callback) {
// create containers and set persistent
// set env vars and volumes
// deploy image via dockerfile
function endWithSuccess() {
step1Callback({
message: {
text: 'Parse is deployed and available as ' + data[PARSE_NAME],
type: SUCCESS
},
next: null // this can be similar to step1next, in that case the flow continues...
});
}
function endWithError(errorMessage) {
step1Callback({
message: {
text: errorMessage,
type: ERROR
},
next: step1next
});
}
// process the inputs:
var errorMessage = null;
data[MONGODB_ROOT_PASSWORD] = data[MONGODB_ROOT_PASSWORD] || uuidv4();
data[PARSE_SERVER_APPLICATION_ID] = data[PARSE_SERVER_APPLICATION_ID] || uuidv4();
data[PARSE_SERVER_MASTER_KEY] = data[PARSE_SERVER_MASTER_KEY] || uuidv4();
if (!data[PARSE_NAME]) {
errorMessage = 'Container name is required!';
}
if (errorMessage) {
endWithError(errorMessage);
return;
}
var appName = data[PARSE_NAME];
var mongoDbContainerName = data[PARSE_NAME] + '-mongodb';
var envVarsMongo = [{
key: 'MONGO_INITDB_ROOT_USERNAME',
value: 'root'
}, {
key: 'MONGO_INITDB_ROOT_PASSWORD',
value: data[MONGODB_ROOT_PASSWORD]
}];
var volumesMongoDb = [{
volumeName: appName + '-mongo-db-vol',
containerPath: '/data/db'
}, {
volumeName: appName + '-mongo-cfg-vol',
containerPath: '/data/configdb'
}];
function createMongoDbContainer() {
var hasPersistentData = true;
apiManager.registerNewApp(mongoDbContainerName, hasPersistentData, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
}
setupMongoDbAppDefinition();
});
}
function setupMongoDbAppDefinition() {
apiManager.updateConfigAndSave(mongoDbContainerName, 1, envVarsMongo, true, volumesMongoDb, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
}
deployMongoDbDockerfile();
});
}
function deployMongoDbDockerfile() {
var captainDefinitionContent = {
schemaVersion: 1,
dockerfileLines: [
"FROM mongo:latest"
]
}
apiManager.uploadCaptainDefinitionContent(mongoDbContainerName,
JSON.stringify(captainDefinitionContent), function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
}
createParseContainer();
})
}
var envVarParse = [{
key: 'PORT',
value: 80
}, {
key: PARSE_SERVER_APPLICATION_ID,
value: data[PARSE_SERVER_APPLICATION_ID]
}, {
key: PARSE_SERVER_MASTER_KEY,
value: data[PARSE_SERVER_MASTER_KEY]
}, {
key: 'PARSE_SERVER_DATABASE_URI',
value: 'mongodb://root:'
+ encodeURIComponent(data[MONGODB_ROOT_PASSWORD])
+ '@srv-captain--' + mongoDbContainerName + ':27017/parse?authSource=admin'
}];
var volumesParse = [];
function createParseContainer() {
var hasPersistentData = false;
apiManager.registerNewApp(appName, hasPersistentData, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
}
setupParseAppDefinition();
});
}
function setupParseAppDefinition() {
apiManager.updateConfigAndSave(appName, 1, envVarParse, false, volumesParse, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
}
deployParseDockerfile();
});
}
function deployParseDockerfile() {
var captainDefinitionContent = {
schemaVersion: 1,
dockerfileLines: [
"FROM parseplatform/parse-server:latest"
]
}
apiManager.uploadCaptainDefinitionContent(appName,
JSON.stringify(captainDefinitionContent), function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
}
endWithSuccess();
})
}
createMongoDbContainer();
var SUCCESS = 'success';
var ERROR = 'danger';
var INFO = 'info';
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
oneClickAppsRepository["parse"] = function (apiManager) {
var getErrorMessageIfExists = apiManager.getErrorMessageIfExists;
var PARSE_NAME = 'PARSE_NAME';
var MONGODB_ROOT_PASSWORD = 'MONGODB_ROOT_PASSWORD';
var PARSE_SERVER_APPLICATION_ID = 'PARSE_SERVER_APPLICATION_ID';
var PARSE_SERVER_MASTER_KEY = 'PARSE_SERVER_MASTER_KEY';
var step1next = {};
step1next.data = [];
step1next.data.push({
label: 'Parser Server Name',
id: PARSE_NAME,
type: 'text'
});
step1next.data.push({
label: 'OPTINAL: Choose a MongoDB Root Password - or leave empty for randomly generated value',
id: MONGODB_ROOT_PASSWORD,
type: 'text'
});
step1next.data.push({
label: 'OPTINAL: Choose an Application ID - or leave empty for randomly generated value',
id: PARSE_SERVER_APPLICATION_ID,
type: 'text'
});
step1next.data.push({
label: 'OPTINAL: Choose a Master Key - or leave empty for randomly generated value',
id: PARSE_SERVER_MASTER_KEY,
type: 'text'
});
step1next.process = function (data, step1Callback) {
// create containers and set persistent
// set env vars and volumes
// deploy image via dockerfile
function endWithSuccess() {
step1Callback({
message: {
text: 'Parse is deployed and available as ' + data[PARSE_NAME],
type: SUCCESS
},
next: null // this can be similar to step1next, in that case the flow continues...
});
}
var step1 = {};
step1.message = {
type: INFO,
text: 'Parse Server is an open source version of the Parse backend that can be deployed to any infrastructure that can run Node.js.' +
' For more information on Parse platform see http://parseplatform.org/' +
' Enter your Parse Configuration parameters and click on next. A MongoDB (database) and a Parse container will be created for you. ' +
' The process will take about a minute for the process to finish.'
function endWithError(errorMessage) {
step1Callback({
message: {
text: errorMessage,
type: ERROR
},
next: step1next
});
}
step1.next = step1next;
return step1;
// process the inputs:
var errorMessage = null;
data[MONGODB_ROOT_PASSWORD] = data[MONGODB_ROOT_PASSWORD] || uuidv4();
data[PARSE_SERVER_APPLICATION_ID] = data[PARSE_SERVER_APPLICATION_ID] || uuidv4();
data[PARSE_SERVER_MASTER_KEY] = data[PARSE_SERVER_MASTER_KEY] || uuidv4();
if (!data[PARSE_NAME]) {
errorMessage = 'Container name is required!';
}
if (errorMessage) {
endWithError(errorMessage);
return;
}
var appName = data[PARSE_NAME];
var mongoDbContainerName = data[PARSE_NAME] + '-mongodb';
var envVarsMongo = [{
key: 'MONGO_INITDB_ROOT_USERNAME',
value: 'root'
}, {
key: 'MONGO_INITDB_ROOT_PASSWORD',
value: data[MONGODB_ROOT_PASSWORD]
}];
var volumesMongoDb = [{
volumeName: appName + '-mongo-db-vol',
containerPath: '/data/db'
}, {
volumeName: appName + '-mongo-cfg-vol',
containerPath: '/data/configdb'
}];
function createMongoDbContainer() {
var hasPersistentData = true;
apiManager.registerNewApp(mongoDbContainerName, hasPersistentData, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
}
setupMongoDbAppDefinition();
});
}
function setupMongoDbAppDefinition() {
var appDefinitionMongo = {
instanceCount: 1,
envVars: envVarsMongo,
notExposeAsWebApp: true,
volumes: volumesMongoDb,
};
apiManager.updateConfigAndSave(mongoDbContainerName, appDefinitionMongo, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
}
deployMongoDbDockerfile();
});
}
function deployMongoDbDockerfile() {
var captainDefinitionContent = {
schemaVersion: 1,
dockerfileLines: [
"FROM mongo:latest"
]
}
apiManager.uploadCaptainDefinitionContent(mongoDbContainerName,
JSON.stringify(captainDefinitionContent), function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
}
createParseContainer();
})
}
var envVarParse = [{
key: 'PORT',
value: 80
}, {
key: PARSE_SERVER_APPLICATION_ID,
value: data[PARSE_SERVER_APPLICATION_ID]
}, {
key: PARSE_SERVER_MASTER_KEY,
value: data[PARSE_SERVER_MASTER_KEY]
}, {
key: 'PARSE_SERVER_DATABASE_URI',
value: 'mongodb://root:'
+ encodeURIComponent(data[MONGODB_ROOT_PASSWORD])
+ '@srv-captain--' + mongoDbContainerName + ':27017/parse?authSource=admin'
}];
var volumesParse = [];
function createParseContainer() {
var hasPersistentData = false;
apiManager.registerNewApp(appName, hasPersistentData, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
}
setupParseAppDefinition();
});
}
function setupParseAppDefinition() {
var appDefinitionParse = {
instanceCount: 1,
envVars: envVarParse,
notExposeAsWebApp: false,
volumes: volumesParse,
};
apiManager.updateConfigAndSave(appName, appDefinitionParse, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
}
deployParseDockerfile();
});
}
function deployParseDockerfile() {
var captainDefinitionContent = {
schemaVersion: 1,
dockerfileLines: [
"FROM parseplatform/parse-server:latest"
]
}
apiManager.uploadCaptainDefinitionContent(appName,
JSON.stringify(captainDefinitionContent), function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
}
endWithSuccess();
})
}
createMongoDbContainer();
}
})();
var step1 = {};
step1.message = {
type: INFO,
text: 'Parse Server is an open source version of the Parse backend that can be deployed to any infrastructure that can run Node.js.' +
' For more information on Parse platform see http://parseplatform.org/' +
' Enter your Parse Configuration parameters and click on next. A MongoDB (database) and a Parse container will be created for you. ' +
' The process will take about a minute for the process to finish.'
}
step1.next = step1next;
return step1;
}
})();
+9 -1
View File
@@ -83,7 +83,15 @@
}
function setupAppDefinition() {
apiManager.updateConfigAndSave(appName, 1, envVars, false, volumes, function (data) {
var appDefinition = {
instanceCount: 1,
envVars: envVars,
notExposeAsWebApp: false,
volumes: volumes,
};
apiManager.updateConfigAndSave(appName, appDefinition, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
+9 -1
View File
@@ -132,7 +132,15 @@
}
function setupAppDefinition() {
apiManager.updateConfigAndSave(appName, 1, envVars, true, volumes, function (data) {
var appDefinition = {
instanceCount: 1,
envVars: envVars,
notExposeAsWebApp: true,
volumes: volumes,
};
apiManager.updateConfigAndSave(appName, appDefinition, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
+9 -1
View File
@@ -85,7 +85,15 @@
}
function setupAppDefinition() {
apiManager.updateConfigAndSave(appName, 1, envVars, false, volumes, function (data) {
var appDefinition = {
instanceCount: 1,
envVars: envVars,
notExposeAsWebApp: false,
volumes: volumes,
};
apiManager.updateConfigAndSave(appName, appDefinition, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
+18 -2
View File
@@ -92,7 +92,15 @@
}
function setupMySqlAppDefinition() {
apiManager.updateConfigAndSave(mySqlAppName, 1, envVarsMySql, true, volumesMySql, function (data) {
var appDefinitionMySql = {
instanceCount: 1,
envVars: envVarsMySql,
notExposeAsWebApp: true,
volumes: volumesMySql,
};
apiManager.updateConfigAndSave(mySqlAppName, appDefinitionMySql, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;
@@ -151,7 +159,15 @@
}
function setupWordPressAppDefinition() {
apiManager.updateConfigAndSave(wordPressContainerName, 1, envVarsWordPress, false, volumesWordPress, function (data) {
var appDefinitionWordpress = {
instanceCount: 1,
envVars: envVarsWordPress,
notExposeAsWebApp: false,
volumes: volumesWordPress,
};
apiManager.updateConfigAndSave(wordPressContainerName, appDefinitionWordpress, function (data) {
if (getErrorMessageIfExists(data)) {
endWithError(getErrorMessageIfExists(data));
return;