Last active
February 18, 2019 20:12
-
-
Save rook2pawn/9bccfa7fd8123a26772ff8710c098050 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given a stack object of microservices to deploy, | |
// implement a deploy function that takes this object | |
// with a provided variable syntax, and runs the | |
// referenced microservice functions in order | |
// according to microservice dependencies. | |
const sleep = async (wait) => new Promise((resolve) => setTimeout(() => resolve(), wait)) | |
const supportedServices = { | |
database: async (inputs) => { | |
console.log(`deploying database "${inputs.name}"`) | |
await sleep(2000) | |
return { | |
url: 'https://database.com/url' | |
} | |
}, | |
backend: async (inputs) => { | |
console.log( | |
`deploying backend "${inputs.name}" and connecting with database "${inputs.databaseUrl.url}"` | |
) | |
await sleep(3000) | |
return { | |
url: 'https://backend.com/url' | |
} | |
}, | |
frontend: async (inputs) => { | |
console.log( | |
`deploying frontend "${inputs.name}" and connecting with backend "${inputs.backendUrl.url}"` | |
) | |
await sleep(3000) | |
return { | |
url: 'https://frontend.com/url' | |
} | |
} | |
} | |
const hash = {}; | |
const deploy = (stack) => { | |
const fillIn = (item) => { | |
Object.keys(item).forEach((key) => { | |
const val = item[key]; | |
let result = val.match(/\${(\w+)\.\w+}/) | |
if (result !== null) { | |
const service = result[1]; | |
item[key] = hash[service]; | |
} | |
}) | |
} | |
Object.keys(stack).forEach((key) => { | |
const item = stack[key]; | |
const dependencies = [] | |
Object.keys(item).forEach((key) => { | |
const val = item[key]; | |
let result = val.match(/\${(\w+)\.\w+}/) | |
if (result !== null) { | |
const service = result[1]; | |
dependencies.push(service); | |
} | |
}) | |
hash[key] = dependencies.reduce((p, c) => { | |
return p.then(() => hash[c]) | |
}, Promise.resolve(0)) | |
.then(() => { | |
fillIn(item); | |
return supportedServices[key](item) | |
}) | |
.then((val) => { | |
hash[key] = val; | |
return val | |
}) | |
}) | |
return Promise.all(Object.keys(hash).map((key) => hash[key])) | |
} | |
deploy({ | |
database: { | |
name: 'users' | |
}, | |
backend: { | |
name: 'users', | |
databaseUrl: '${database.url}' | |
}, | |
frontend: { | |
name: 'users', | |
backendUrl: '${backend.url}' | |
} | |
}) | |
.then(() => { | |
console.log("All done!!") | |
console.log("Final hash:", hash); | |
}) | |
/* | |
output | |
deploying database "users" | |
deploying backend "users" and connecting with database "https://database.com/url" | |
deploying frontend "users" and connecting with backend "https://backend.com/url" | |
All done!! | |
Final hash: { database: { url: 'https://database.com/url' }, | |
backend: { url: 'https://backend.com/url' }, | |
frontend: { url: 'https://frontend.com/url' } } | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment