-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Spike: todo example on IBM Cloud [DO NOT MERGE] #1574
Conversation
df28ab7
to
7fa075c
Compare
7fa075c
to
2a72bac
Compare
Nice start! Could you please start writing a guide explaining how to deploy your modified todo example to bluemix? For example, you can put the content into a new file |
@@ -8,6 +8,13 @@ import {ApplicationConfig} from '@loopback/core'; | |||
import {RestServer} from '@loopback/rest'; | |||
|
|||
export async function main(options?: ApplicationConfig) { | |||
// IBM Cloud app port is set on process.env.PORT | |||
if (!options) { | |||
options = {rest: {port: process.env.PORT}}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't needed since the port default to process.env.PORT
in the else
statement below. So this can be just:
options.rest = options.rest ? options.rest : {};
options.rest.port = process.env.PORT || options.rest.port;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is options
defined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooops. For some reason I through the check was for options.rest
.
e19396f
to
81e099f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like deploying LB4 apps to IBM Cloud requires only small changes - I like that.
IIUC, this deployment is based on CloudFoundry. I wish we were targeting Kubernetes based deployments, as that seems to be the new way forward, but CloudFoundry is good enough for the initial spike.
"localStorage": "", | ||
"file": "./data/db.json" | ||
"connector": "cloudant", | ||
"url": "http://admin:pass@localhost:8080", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please confirm that this URL works with Cloudant running locally via Docker?
See #1446:
When developing and testing locally, the app should use local docker-based Cloudant Developer Edition.
In my experience, Docker containers cannot be accessed at localhost
.
If there is a reasonably easy way how to run Cloudant locally on localhost
, perhaps without Docker, then I am fine with your current approach, but we need instructions for users how to set up such Cloudant instance.
See also acceptance criteria:
A deployment guide for IBM cloud added to our docs. If the current LB4 feature-set is not sufficient to support such deployments, then create a new wiki page instead of an official doc article.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it works with Cloudant at http://admin:pass@localhost:8080
works fine.
Will push the "deployment guide" in a while.
|
||
// tslint:disable-next-line:no-any | ||
serviceGroup.forEach((service: VcapService) => { | ||
if (service.name === config.name) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered using an npm library to handle VCAP_SERVICES processing for you?
Few examples found by my quick search:
The latter package says in its README: Unfortunately the instance name is rarely known in advance, so you may pass it as a separate environment variable. Is this something we need to take care of?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't looked at them, since I was focussed on just making the app work. Will take a look at them.
81e099f
to
b3a1a46
Compare
add todo example on IBM Cloud
b3a1a46
to
c21fb60
Compare
}; | ||
|
||
if (shouldConfigureForIbmCloud()) { | ||
const services = JSON.parse(process.env.VCAP_SERVICES!); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized just now: we should not be accessing process.env
from inside our application code (e.g. db.datasource.ts
). Any environment-specific overrides should happen as close to the top-level main function as possible.
In LoopBack 3.x, it's the job of loopback-boot to deal with environment variables.
For the purpose of this spike, we can add VCAP_SERVICES
processing code to the main
function as you have already did for PORT
variable.
Instead of modifying the config
object loaded directly from datasource JSON file, we should iterate over all datasource configurations bound to app
context, match them with VCAP_SERVICES entries and update the bound value using the URL from VCAP_SERVICES.
Later on, we can extract the code processing VCAP_SERVICES and other CF-related configuration into an extension people can add to their app.
|
||
function shouldConfigureForIbmCloud() { | ||
if ( | ||
process.env.HOME === '/home/vcap/app' && // relatively reliable way to detect the app is running on IBM Cloud |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should not be necessary to detect IBM Cloud. If the environment provides VCAP_SERVICES variable, then we should apply it, regardless of the specific cloud we are running on. (Isn't CloudFoundry an open source project that anyone can run on their own machines?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. If there is VCAP_SERVICES
in env, it is CF env.
function shouldConfigureForIbmCloud() { | ||
if ( | ||
process.env.HOME === '/home/vcap/app' && // relatively reliable way to detect the app is running on IBM Cloud | ||
config.ibmCloud && // have to explicitly config datasource to mark it as an IBM Cloud service with a matching name locally |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about changing this flag from boolean ibmCloud: true
into a VCAP SERVICE name to look up (e.g. vcapServiceName: 'db'
?
It looks like VCAP_SERVICES provide binding_name
and instance_name
properties in addition to name
, maybe we should use binding_name
to find the right service record? See this presentation linked from cf-services for some background info.
To be honest, my knowledge of CloudFoundry and VCAP_SERVICES is pretty minimal, please correct me if I am talking nonsense :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great suggestion.
examples/todo/DEPLOYING.md
Outdated
|
||
### LoopBack | ||
|
||
1. Model configuration change is drastic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add more details? AFAICT, there is no change in model/repository configuration, it's the DataSource config that requires modification.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is just a note about first-time experience with LB4: model-config.json
in LB3 was simpler. I don't think anything has to be done about this.
examples/todo/DEPLOYING.md
Outdated
|
||
1. Model configuration change is drastic. | ||
2. Repository creation and linking to a model is manual. There should be a | ||
provision to do these from the `lb` commands. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this referring to #1588? If so, then please add a link to your document.
examples/todo/DEPLOYING.md
Outdated
1. Model configuration change is drastic. | ||
2. Repository creation and linking to a model is manual. There should be a | ||
provision to do these from the `lb` commands. | ||
3. There no API Explorer to play around with the models. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please explain more? We do have API explorer hosted at loopback.io, LB4 apps provide /swagger-ui
endpoint to access it, e.g. http://[::1]:3000/swagger-ui
. Is this perhaps an issue of a missing documentation? Or maybe we should modify scaffolded app code to print explorer (swagger-ui) URL to console at startup?
The hosted API Explorer works well in Chrome and Firefox. It does not work in Safari for me, because swagger-ui is served from HTTPS while the app is running on HTTP and Safari does not allow mixed content. I guess we have a bug to fix here - see #1603
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, due to mixed content the API Explorer does not load on IBM Cloud. Tested on Firefox, Chrome, Safari; fails on all. Works on Firefox, fails on Chrome and Safari.
af409a6
to
5bec856
Compare
review 1
5bec856
to
139a702
Compare
review 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the updated code is good enough for spike purposes now 👍
@hacksparrow , since your spike is done, and the docs are out, is this PR good to close? |
This is done - #1446 |
Add todo example on IBM Cloud - see #1446
Checklist
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated