feat(cp-gql-schema): scale mutation

This commit is contained in:
Sérgio Ramos 2017-07-20 11:57:33 +01:00 committed by Sérgio Ramos
parent 7f9eada428
commit dd1124a608
3 changed files with 133 additions and 53 deletions

View File

@ -14,6 +14,7 @@
"dev": "nodemon src/index.js" "dev": "nodemon src/index.js"
}, },
"dependencies": { "dependencies": {
"build-array": "^1.0.0",
"camel-case": "^3.0.0", "camel-case": "^3.0.0",
"good": "^7.2.0", "good": "^7.2.0",
"good-console": "^6.4.0", "good-console": "^6.4.0",
@ -24,7 +25,9 @@
"joyent-cp-gql-schema": "^1.0.4", "joyent-cp-gql-schema": "^1.0.4",
"js-yaml": "^3.8.4", "js-yaml": "^3.8.4",
"lodash.find": "^4.6.0", "lodash.find": "^4.6.0",
"lodash.findindex": "^4.6.0",
"lodash.flatten": "^4.4.0", "lodash.flatten": "^4.4.0",
"lodash.random": "^3.2.0",
"lodash.uniq": "^4.5.0", "lodash.uniq": "^4.5.0",
"param-case": "^2.1.1", "param-case": "^2.1.1",
"uuid": "^3.1.0" "uuid": "^3.1.0"

View File

@ -1,8 +1,11 @@
const { v4: uuid } = require('uuid'); const { v4: uuid } = require('uuid');
const paramCase = require('param-case'); const paramCase = require('param-case');
const camelCase = require('camel-case'); const camelCase = require('camel-case');
const buildArray = require('build-array');
const lfind = require('lodash.find'); const lfind = require('lodash.find');
const findIndex = require('lodash.findindex');
const flatten = require('lodash.flatten'); const flatten = require('lodash.flatten');
const random = require('lodash.random');
const uniq = require('lodash.uniq'); const uniq = require('lodash.uniq');
const yaml = require('js-yaml'); const yaml = require('js-yaml');
@ -121,26 +124,34 @@ const createDeploymentGroup = ({ name }) => {
const deleteDeploymentGroup = options => { const deleteDeploymentGroup = options => {
const dgId = options.id; const dgId = options.id;
const deleteDeploymentGroup = getServices({ deploymentGroupId: dgId}) const deleteDeploymentGroup = getServices({ deploymentGroupId: dgId })
.then(services => Promise.all( .then(services =>
services.map(service => handleStatusUpdateRequest( Promise.all(
services.map(service =>
handleStatusUpdateRequest(
service.id, service.id,
'DELETING', 'DELETING',
'STOPPING', 'STOPPING',
'DELETED', 'DELETED',
'DELETED' 'DELETED'
)) )
)) )
)
)
.then(() => { .then(() => {
const deploymentGroup = deploymentGroups.filter(dg => dg.id === dgId).shift(); const deploymentGroup = deploymentGroups
.filter(dg => dg.id === dgId)
.shift();
deploymentGroup.status = 'DELETING'; deploymentGroup.status = 'DELETING';
return deploymentGroup; return deploymentGroup;
return ({ deploymentGroupId: dgId }); return { deploymentGroupId: dgId };
return getDeploymentGroups({id: dgId}); return getDeploymentGroups({ id: dgId });
}); });
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
const deploymentGroup = deploymentGroups.filter(dg => dg.id === dgId).shift(); const deploymentGroup = deploymentGroups
.filter(dg => dg.id === dgId)
.shift();
deploymentGroup.status = 'DELETED'; deploymentGroup.status = 'DELETED';
}, 5000); }, 5000);
@ -172,18 +183,56 @@ const createServicesFromManifest = ({ deploymentGroupId, raw }) => {
return Promise.resolve(undefined); return Promise.resolve(undefined);
}; };
const scale = options => { const scale = ({ serviceId, replicas }) => {
const service = getServices({ id: options.serviceId })[0]; const serviceIndex = findIndex(services, ['id', serviceId]);
const currentScale = instances.filter(
find({
serviceId
})
).length;
return { const version = {
scale: [ id: uuid()
{
id: service.id,
serviceName: service.name,
replicas: 2
}
]
}; };
if (currentScale === replicas) {
return version;
}
services[serviceIndex].status = 'SCALING';
const up = n => {
buildArray(n).forEach((_, i) => {
instances.push({
id: uuid(),
name: `${services[serviceIndex].slug}_${currentScale + i}`,
serviceId,
deploymentGroupId: services[serviceIndex].deploymentGroupId,
status: 'ACTIVE',
healthy: 'UNKNOWN'
});
});
};
const down = n => {
buildArray(n).forEach((_, i) => {
instances.splice(findIndex(instances, ['serviceId', serviceId]), 1);
});
};
setTimeout(() => {
const diff = replicas - currentScale;
if (diff >= 0) {
up(diff);
} else {
down(Math.abs(diff));
}
services[serviceIndex].status = 'ACTIVE';
}, random(1500, 3000));
return version;
}; };
const updateInstancesStatus = (is, status) => { const updateInstancesStatus = (is, status) => {
@ -336,7 +385,8 @@ module.exports = {
type: options.type, type: options.type,
format: options.format format: options.format
})), })),
deleteDeploymentGroup: (options, request, fn) => fn(null, deleteDeploymentGroup(options)), deleteDeploymentGroup: (options, request, fn) =>
fn(null, deleteDeploymentGroup(options)),
deleteServices: (options, request, fn) => fn(null, deleteServices(options)), deleteServices: (options, request, fn) => fn(null, deleteServices(options)),
scale: (options, reguest, fn) => fn(null, scale(options)), scale: (options, reguest, fn) => fn(null, scale(options)),
restartServices: (options, request, fn) => fn(null, restartServices(options)), restartServices: (options, request, fn) => fn(null, restartServices(options)),

View File

@ -1766,12 +1766,12 @@ camelcase@^4.0.0, camelcase@^4.1.0:
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd" resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd"
caniuse-db@^1.0.30000187, caniuse-db@^1.0.30000634, caniuse-db@^1.0.30000639: caniuse-db@^1.0.30000187, caniuse-db@^1.0.30000634, caniuse-db@^1.0.30000639:
version "1.0.30000703" version "1.0.30000704"
resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30000703.tgz#c7d899b8adea6da1aa587355d39ac8533f878403" resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30000704.tgz#8c5aa6fed8058e65c70f2c1f5d63f7088650705c"
caniuse-lite@^1.0.30000701: caniuse-lite@^1.0.30000701:
version "1.0.30000703" version "1.0.30000704"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000703.tgz#666e8c3f1e4f7abb1d16d48e04e7e9e8df934925" resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000704.tgz#adb6ea01134515663682db93abab291d4c02946b"
capture-stack-trace@^1.0.0: capture-stack-trace@^1.0.0:
version "1.0.0" version "1.0.0"
@ -2199,8 +2199,8 @@ content-type@^1.0.2, content-type@~1.0.2:
resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.2.tgz#b7d113aee7a8dd27bd21133c4dc2529df1721eed" resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.2.tgz#b7d113aee7a8dd27bd21133c4dc2529df1721eed"
content@3.x.x: content@3.x.x:
version "3.0.4" version "3.0.5"
resolved "https://registry.yarnpkg.com/content/-/content-3.0.4.tgz#ca3dde04480f12519b71526ec44bd488ddfb3fef" resolved "https://registry.yarnpkg.com/content/-/content-3.0.5.tgz#9e147dc7c838c4de9483096845ddb4de455ec509"
dependencies: dependencies:
boom "5.x.x" boom "5.x.x"
@ -3723,11 +3723,11 @@ extsprintf@1.0.2:
version "1.0.2" version "1.0.2"
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.0.2.tgz#e1080e0658e300b06294990cc70e1502235fd550" resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.0.2.tgz#e1080e0658e300b06294990cc70e1502235fd550"
extsprintf@1.2.0, extsprintf@^1.2.0: extsprintf@1.2.0:
version "1.2.0" version "1.2.0"
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.2.0.tgz#5ad946c22f5b32ba7f8cd7426711c6e8a3fc2529" resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.2.0.tgz#5ad946c22f5b32ba7f8cd7426711c6e8a3fc2529"
extsprintf@1.3.0: extsprintf@1.3.0, extsprintf@^1.2.0:
version "1.3.0" version "1.3.0"
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.3.0.tgz#96918440e3041a7a414f8c52e3c574eb3c3e1e05" resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.3.0.tgz#96918440e3041a7a414f8c52e3c574eb3c3e1e05"
@ -4303,8 +4303,8 @@ hapi-swagger@^7.7.0:
swagger-parser "^3.4.1" swagger-parser "^3.4.1"
hapi@^16.4.3: hapi@^16.4.3:
version "16.4.3" version "16.5.0"
resolved "https://registry.yarnpkg.com/hapi/-/hapi-16.4.3.tgz#be4daaf2dcdbee97957ce503061b09765078aa05" resolved "https://registry.yarnpkg.com/hapi/-/hapi-16.5.0.tgz#89e4770f0034e3b69ee99ed929cc5b573805f303"
dependencies: dependencies:
accept "2.x.x" accept "2.x.x"
ammo "2.x.x" ammo "2.x.x"
@ -4322,7 +4322,7 @@ hapi@^16.4.3:
podium "1.x.x" podium "1.x.x"
shot "3.x.x" shot "3.x.x"
statehood "5.x.x" statehood "5.x.x"
subtext "4.x.x" subtext "5.x.x"
topo "2.x.x" topo "2.x.x"
har-schema@^1.0.5: har-schema@^1.0.5:
@ -5833,6 +5833,10 @@ lodash.pick@^4.4.0:
version "4.4.0" version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3" resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"
lodash.random@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/lodash.random/-/lodash.random-3.2.0.tgz#96e24e763333199130d2c9e2fd57f91703cc262d"
lodash.remove@^4.7.0: lodash.remove@^4.7.0:
version "4.7.0" version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.remove/-/lodash.remove-4.7.0.tgz#f31d31e7c39a0690d5074ec0d3627162334ee626" resolved "https://registry.yarnpkg.com/lodash.remove/-/lodash.remove-4.7.0.tgz#f31d31e7c39a0690d5074ec0d3627162334ee626"
@ -5960,8 +5964,8 @@ markdown-escapes@^1.0.0:
resolved "https://registry.yarnpkg.com/markdown-escapes/-/markdown-escapes-1.0.1.tgz#1994df2d3af4811de59a6714934c2b2292734518" resolved "https://registry.yarnpkg.com/markdown-escapes/-/markdown-escapes-1.0.1.tgz#1994df2d3af4811de59a6714934c2b2292734518"
markdown-table@^1.1.0: markdown-table@^1.1.0:
version "1.1.0" version "1.1.1"
resolved "https://registry.yarnpkg.com/markdown-table/-/markdown-table-1.1.0.tgz#1f5ae61659ced8808d882554c32e8b3f38dd1143" resolved "https://registry.yarnpkg.com/markdown-table/-/markdown-table-1.1.1.tgz#4b3dd3a133d1518b8ef0dbc709bf2a1b4824bc8c"
matcher@^0.1.1: matcher@^0.1.1:
version "0.1.2" version "0.1.2"
@ -7275,8 +7279,8 @@ rc@1.1.7, rc@^1.0.1, rc@^1.1.6, rc@^1.1.7:
strip-json-comments "~2.0.1" strip-json-comments "~2.0.1"
react-apollo@^1.4.2: react-apollo@^1.4.2:
version "1.4.4" version "1.4.6"
resolved "https://registry.yarnpkg.com/react-apollo/-/react-apollo-1.4.4.tgz#31749237dfa133f47a688e0b7a91a479a7f0d8f3" resolved "https://registry.yarnpkg.com/react-apollo/-/react-apollo-1.4.6.tgz#b63002879680d0ebdcfb7623acf9df75a0e43bb7"
dependencies: dependencies:
apollo-client "^1.4.0" apollo-client "^1.4.0"
graphql-anywhere "^3.0.0" graphql-anywhere "^3.0.0"
@ -8425,7 +8429,7 @@ squad@^1.1.3:
version "1.1.3" version "1.1.3"
resolved "https://registry.yarnpkg.com/squad/-/squad-1.1.3.tgz#da09f68d1e0b0a60dca172878fe1f3c9e5272401" resolved "https://registry.yarnpkg.com/squad/-/squad-1.1.3.tgz#da09f68d1e0b0a60dca172878fe1f3c9e5272401"
sshpk-agent@1.4.2, sshpk-agent@^1.3.0: sshpk-agent@1.4.2:
version "1.4.2" version "1.4.2"
resolved "https://registry.yarnpkg.com/sshpk-agent/-/sshpk-agent-1.4.2.tgz#5739cc08f48f98c53950a1715d20872a53804541" resolved "https://registry.yarnpkg.com/sshpk-agent/-/sshpk-agent-1.4.2.tgz#5739cc08f48f98c53950a1715d20872a53804541"
dependencies: dependencies:
@ -8434,7 +8438,16 @@ sshpk-agent@1.4.2, sshpk-agent@^1.3.0:
readable-stream "^2.1.4" readable-stream "^2.1.4"
sshpk ">=1.9.1 < 1.11.0" sshpk ">=1.9.1 < 1.11.0"
sshpk@1.10.2, "sshpk@>=1.9.1 < 1.11.0", sshpk@^1.7.0, sshpk@^1.8.3: sshpk-agent@^1.3.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/sshpk-agent/-/sshpk-agent-1.6.0.tgz#56da869e02ad757991c247327e80b261682399af"
dependencies:
assert-plus "^1.0.0"
mooremachine "^2.0.1"
readable-stream "^2.1.4"
sshpk ">=1.13.0 < 1.14.0"
sshpk@1.10.2, "sshpk@>=1.9.1 < 1.11.0":
version "1.10.2" version "1.10.2"
resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.10.2.tgz#d5a804ce22695515638e798dbe23273de070a5fa" resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.10.2.tgz#d5a804ce22695515638e798dbe23273de070a5fa"
dependencies: dependencies:
@ -8449,6 +8462,20 @@ sshpk@1.10.2, "sshpk@>=1.9.1 < 1.11.0", sshpk@^1.7.0, sshpk@^1.8.3:
jsbn "~0.1.0" jsbn "~0.1.0"
tweetnacl "~0.14.0" tweetnacl "~0.14.0"
"sshpk@>=1.13.0 < 1.14.0", sshpk@^1.7.0, sshpk@^1.8.3:
version "1.13.1"
resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.13.1.tgz#512df6da6287144316dc4c18fe1cf1d940739be3"
dependencies:
asn1 "~0.2.3"
assert-plus "^1.0.0"
dashdash "^1.12.0"
getpass "^0.1.1"
optionalDependencies:
bcrypt-pbkdf "^1.0.0"
ecc-jsbn "~0.1.1"
jsbn "~0.1.0"
tweetnacl "~0.14.0"
stack-utils@^1.0.0: stack-utils@^1.0.0:
version "1.0.1" version "1.0.1"
resolved "https://registry.yarnpkg.com/stack-utils/-/stack-utils-1.0.1.tgz#d4f33ab54e8e38778b0ca5cfd3b3afb12db68620" resolved "https://registry.yarnpkg.com/stack-utils/-/stack-utils-1.0.1.tgz#d4f33ab54e8e38778b0ca5cfd3b3afb12db68620"
@ -8462,8 +8489,8 @@ state-toggle@^1.0.0:
resolved "https://registry.yarnpkg.com/state-toggle/-/state-toggle-1.0.0.tgz#d20f9a616bb4f0c3b98b91922d25b640aa2bc425" resolved "https://registry.yarnpkg.com/state-toggle/-/state-toggle-1.0.0.tgz#d20f9a616bb4f0c3b98b91922d25b640aa2bc425"
statehood@5.x.x: statehood@5.x.x:
version "5.0.2" version "5.0.3"
resolved "https://registry.yarnpkg.com/statehood/-/statehood-5.0.2.tgz#c6b3baa16ed8b121d3f09a3ffa85e22195a7f2a9" resolved "https://registry.yarnpkg.com/statehood/-/statehood-5.0.3.tgz#c07a75620db5379b60d2edd47f538002a8ac7dd6"
dependencies: dependencies:
boom "5.x.x" boom "5.x.x"
cryptiles "3.x.x" cryptiles "3.x.x"
@ -8745,12 +8772,12 @@ stylelint@^7.0.0, stylelint@^7.0.3, stylelint@^7.11.1:
table "^4.0.1" table "^4.0.1"
stylis@^3.2.1: stylis@^3.2.1:
version "3.2.5" version "3.2.6"
resolved "https://registry.yarnpkg.com/stylis/-/stylis-3.2.5.tgz#132087969b2c9e2c71504eaed24d3f1987bb5d0f" resolved "https://registry.yarnpkg.com/stylis/-/stylis-3.2.6.tgz#6291df64aae62e9f537ab24b7d8a62b2428a34e9"
subtext@4.x.x: subtext@5.x.x:
version "4.4.1" version "5.0.0"
resolved "https://registry.yarnpkg.com/subtext/-/subtext-4.4.1.tgz#2fcec945de429283c3d18b151ff0fa1f1b87aec9" resolved "https://registry.yarnpkg.com/subtext/-/subtext-5.0.0.tgz#9c3f083018bb1586b167ad8cfd87083f5ccdfe0f"
dependencies: dependencies:
boom "5.x.x" boom "5.x.x"
content "3.x.x" content "3.x.x"
@ -8895,8 +8922,8 @@ tap-xunit@^1.7.0:
xtend "~4.0.0" xtend "~4.0.0"
tapable@^0.2.5, tapable@~0.2.5: tapable@^0.2.5, tapable@~0.2.5:
version "0.2.6" version "0.2.7"
resolved "https://registry.yarnpkg.com/tapable/-/tapable-0.2.6.tgz#206be8e188860b514425375e6f1ae89bfb01fd8d" resolved "https://registry.yarnpkg.com/tapable/-/tapable-0.2.7.tgz#e46c0daacbb2b8a98b9b0cea0f4052105817ed5c"
tar-fs@~1.12.0: tar-fs@~1.12.0:
version "1.12.0" version "1.12.0"
@ -9250,8 +9277,8 @@ typedarray@^0.0.6, typedarray@~0.0.5:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
ua-parser-js@^0.7.9: ua-parser-js@^0.7.9:
version "0.7.13" version "0.7.14"
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.13.tgz#cd9dd2f86493b3f44dbeeef3780fda74c5ee14be" resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.14.tgz#110d53fa4c3f326c121292bbeac904d2e03387ca"
uglify-js@^2.6, uglify-js@^2.8.27: uglify-js@^2.6, uglify-js@^2.8.27:
version "2.8.29" version "2.8.29"