Compare commits

...

140 Commits
v1.0 ... master

Author SHA1 Message Date
Honza Horak e59fe7571f
Merge pull request #206 from liangxia/okd
Update url to reflect OKD rebranding for nodejs-ex
2018-10-16 15:45:10 +02:00
liangxia 38584bb52a Update url to reflect OKD rebranding 2018-10-16 20:49:41 +08:00
Honza Horak 905416d88f
Merge pull request #204 from jeffdyoung/ppc64le
add example templates for ppc64le imagestreams
2018-09-25 07:47:27 +02:00
Honza Horak f9a97276a0
Merge pull request #199 from sspeiche/binding-update
Add support for binding style env vars
2018-09-19 08:22:06 +02:00
root e56c06e04a Edit templates to only include imagestreams that support both x86 and ppc64le 2018-09-18 10:53:06 -04:00
sspeiche 5f15c1edb0 Add support for binding style env vars 2018-08-19 11:12:02 -04:00
Ben Parees b078bcff2f
Merge pull request #195 from rubenhulzebos/patch-1
Update index.html
2018-08-03 09:39:42 -04:00
rubenhulzebos 58e352aa69
Update index.html
Added Github webhook settings description.
2018-08-02 09:00:41 +02:00
Honza Horak e79929f0b6
Merge pull request #191 from adambkaplan/sclorg-rename
Rename github org references to sclorg
2018-07-24 18:24:51 +02:00
Adam Kaplan 9b0e5f1539 Rename github org references to sclorg
Addresses openshift/origin##19998
2018-07-24 11:32:08 -04:00
Ben Parees 24e7e684ac
Merge pull request #187 from Gl4di4torRr/master
remove unnecessary $ from documentation
2018-06-26 08:44:42 -04:00
Gl4di4torRr 0cb2d44935 remove unnecessary $ from documentation 2018-06-26 07:20:30 -04:00
Honza Horak a2aa814098
Merge pull request #178 from wozniakjan/issue/origin_19379/generalization_of_probes
mongodb openshift template: generalize probes
2018-05-04 09:16:28 +02:00
Jan Wozniak 003d0ac8d6 mongodb openshift template: generalize probes
fixes: https://github.com/openshift/origin/issues/19379
2018-05-02 12:17:13 +02:00
Honza Horak 8d138f94d7
Merge pull request #177 from bparees/parameterize
parameterize imagestreamtag refs in templates
2018-04-30 12:00:39 +02:00
Ben Parees d416548286 parameterize imagestreamtag refs in templates 2018-04-26 13:59:59 -04:00
Ben Parees 93fc25fd35
Merge pull request #170 from dharmit/add-minishift-to-readme
Add documentation about using minishift
2018-03-06 13:34:31 -05:00
Dharmit Shah dd9d5afa3b Add documentation about using minishif 2018-03-06 23:55:13 +05:30
Ben Parees ef1b71a300
Merge pull request #160 from aliok/patch-1
Fix invalid JSON in template: nodejs.json
2018-01-12 10:16:26 -05:00
Ali Ok e8d39a4936
Fix invalid JSON in template: nodejs.json
Got rid of a duplicate key
<done over Github UI>
2018-01-12 13:35:25 +03:00
Ben Parees 5faaac6933
Merge pull request #159 from sclorg/fix-template
Remove ' (Persistent)'
2018-01-05 17:19:04 -05:00
Pavel Valena e79d273ea5 Remove ' (Persistent)'
https://github.com/openshift/origin/pull/17922#pullrequestreview-86940496
2018-01-05 22:39:15 +01:00
Ben Parees debf979e0d
Merge pull request #156 from jim-minter/issue17649
add debug if error happens in count()
2017-12-06 23:18:36 -05:00
Jim Minter c8e18b759c add debug if error happens in count() 2017-12-06 18:59:42 -06:00
Ben Parees d1af14dc9f
Merge pull request #149 from gabemontero/app-label
add template 'app' label
2017-11-09 11:13:37 -05:00
gabemontero 4759b1bf2d add template 'app' label 2017-11-09 11:10:12 -05:00
Ben Parees e598bfbe1f
Merge pull request #148 from jim-minter/issue16726
remove expose annotations from quickstarts and add bindable: false
2017-11-07 21:13:16 +01:00
Jim Minter b84f207d3d remove expose annotations from quickstarts and add bindable: false 2017-11-07 13:03:41 -06:00
Ben Parees 96e651f58a Merge pull request #143 from bparees/annotations
remove template prefix from tsb annotations
2017-10-19 14:56:53 +02:00
Ben Parees 8ac25c1e3c remove template prefix from tsb annotations 2017-10-19 14:52:59 +02:00
Ben Parees b9ba509aff Merge pull request #138 from jim-minter/helmchart
add basic helm chart
2017-09-14 21:54:13 -04:00
Jim Minter d6a27c221f add basic helm chart 2017-09-14 17:48:56 -05:00
Ben Parees 44abae3f81 Merge pull request #136 from mordicool/patch-2
Update server.js - remove fs module
2017-09-04 21:45:45 -04:00
Mordechai Weinberg 64c46cb6ee Update server.js
No need for fs module here
2017-09-03 21:44:36 +03:00
Ben Parees 253f8095a9 Merge pull request #135 from mordicool/patch-1
Update server.js
2017-09-03 00:10:56 -04:00
mordicool 08397daddd Update server.js 2017-09-02 21:37:58 +03:00
Ben Parees eb75af97ef Merge pull request #132 from jim-minter/wait-completion-fixups
add missing template.alpha.openshift.io/wait-for-ready annotations to examples
2017-08-18 13:22:39 -04:00
Jim Minter 5647b2a31c add missing template.alpha.openshift.io/wait-for-ready annotations to examples 2017-08-18 12:15:45 -05:00
Ben Parees 7c814b0d2c Merge pull request #131 from jim-minter/trello135-completion-detection
add template completion detection
2017-08-16 14:19:01 -04:00
Jim Minter 5ffcf0a43c add template completion detection 2017-08-14 12:04:44 -05:00
Ben Parees 863b14eab0 Merge pull request #130 from coreydaley/adding_compatibility_section
Adding compatibility section
2017-08-04 11:23:15 -04:00
Corey Daley 324865499e
Adding compatibility section 2017-08-04 11:22:35 -04:00
Ben Parees f12494cff0 Merge pull request #128 from coreydaley/bugzilla_1365656_github_webhook_description_update
Updating GITHUB_WEBHOOK_SECRET description
2017-06-25 23:27:32 -04:00
Corey Daley 1a31042c81
Updating GITHUB_WEBHOOK_SECRET description 2017-06-25 23:08:48 -04:00
Ben Parees 68b66caa93 Merge pull request #125 from jim-minter/trello133-tsb-bind
update templates to use template.openshift.io/expose-uri
2017-06-13 12:24:08 -04:00
Jim Minter 5d9081445f update templates to use template.openshift.io/expose-uri 2017-06-13 17:20:59 +01:00
Ben Parees 3d44de3ba8 Merge pull request #117 from openshift/revert-112-revert-111-nodejs6
re-enable nodejs:6 in templates
2017-05-23 15:54:29 -04:00
Ben Parees 9a913714c7 Revert "Revert "Changed templates to use nodejs:6"" 2017-05-23 15:53:51 -04:00
Ben Parees 95c092763d Merge pull request #112 from openshift/revert-111-nodejs6
Revert "Changed templates to use nodejs:6"
2017-04-29 12:12:55 -04:00
Ben Parees 814cde009f Revert "Changed templates to use nodejs:6" 2017-04-29 12:12:31 -04:00
Ben Parees b8d7dd7328 Merge pull request #111 from sspeiche/nodejs6
Changed templates to use nodejs:6
2017-04-28 13:36:37 -04:00
sspeiche 6b92fdf4cd Changed templates to use nodejs:6 2017-04-28 13:23:33 -04:00
Ben Parees c1faa896c2 Merge pull request #110 from dinhxuanvu/recreate
Change DC strategy from Rolling to Recreate for DB templates
2017-04-25 16:53:35 -04:00
Vu Dinh 2631c4a95e Change DC strategy from Rolling to Recreate for DB templates
Recreate strategy will allow the redeployment process to stay within
1Gi memory limit instead of requiring more memory due to extra pod
is created for Rolling strategy.

Signed-off-by: Vu Dinh <vdinh@redhat.com>
2017-04-25 16:47:41 -04:00
Ben Parees e81cacfa03 Merge pull request #95 from bparees/sc_metadata
add service catalog metadata to templates
2017-02-15 16:27:18 -05:00
Ben Parees 513d4703ad add service catalog metadata to templates 2017-02-15 16:26:20 -05:00
Ben Parees 7f7c9dd258 Merge pull request #90 from coreydaley/update_templates_to_use_secrets
updating to use secrets
2017-01-18 14:06:52 -05:00
Corey Daley 214b5bb005
updating to use secrets 2017-01-18 13:41:10 -05:00
Ben Parees 36bb782477 Merge pull request #89 from bparees/template
fix invalid key names
2017-01-17 14:09:41 -05:00
Ben Parees fafa1c4893 fix invalid key names 2017-01-17 14:07:48 -05:00
Ben Parees 343689f74d Merge pull request #83 from guangxuli/fix_branch
update some incorrect description
2016-12-12 08:26:46 -05:00
guangxuli 3f51ad1438 update some incorrect description
fix format

add enter
2016-12-12 16:20:45 +08:00
Ben Parees 81e1afb25e Merge pull request #75 from jupierce/migrate-passwords-to-secrets
Moving credentials into secrets
2016-11-29 14:24:31 -05:00
jupierce 7d73fbd2a8 Moving credentials into secrets 2016-11-29 11:18:16 -05:00
Ben Parees fb4415ce65 Merge pull request #73 from jim-minter/pipeline
add Jenkinsfile
2016-11-10 11:47:06 -05:00
Jim Minter 4344883eb6 add Jenkinsfile 2016-11-10 15:24:07 +00:00
Ben Parees 69b359bdfc Merge pull request #71 from luciddreamz/master
Updates template display name, tags, description, message, and grouping
2016-10-24 16:52:07 -04:00
luciddreamz 572d5d4af1 Updates template display name, tags, description, message, and service grouping 2016-10-24 16:34:46 -04:00
Ben Parees 9ebc30eb44 Merge pull request #70 from sallyom/group-services
group services in quickstarts
2016-10-19 13:53:40 -04:00
Sally O'Malley 36aeea7271 group services in quickstarts 2016-10-19 13:39:23 -04:00
Jacob Lucky 9457131bdb Adds NodeJS/MongoDB template with persistent storage. (#64) 2016-09-20 09:12:04 -04:00
Ionut Palade 0e748ed338 add an alternate http address for the tests to execute on (#60) 2016-08-01 10:47:07 -04:00
Ben Parees e61190a1a1 use latest imagestreams for nodejs and mongo (#56)
* use latest mongo and nodejs versions

* update readiness check for mongo 3.2
2016-07-13 13:24:50 -04:00
Ben Parees 8b9463d1d4 Merge pull request #55 from bparees/fix_db_init
add object assign
2016-07-09 11:43:27 -04:00
Ben Parees b450b768b5 add object assign 2016-07-09 11:39:32 -04:00
Ben Parees c984686993 Merge pull request #54 from dinhxuanvu/master
Add NPM_MIRROR parameter and fix required parameter
2016-07-06 14:02:45 -04:00
Vu Dinh 325aaf4483 Add NPM_MIRROR parameter and fix required parameter
Add new NPM_MIRROR parameter for custom NPM mirror URL to be used
if needed.

The required parameters are explicitly marked as required (*) to
avoid confusion.

Signed-off-by: Vu Dinh <vdinh@redhat.com>
2016-07-06 13:51:15 -04:00
Ben Parees c56041b2c7 Merge pull request #52 from bparees/db_toleration
attempt to init the db on every request
2016-06-29 14:17:48 -04:00
Ben Parees 3b643fc6bc attempt to init the db on every request 2016-06-29 14:05:02 -04:00
Ben Parees 46537887f2 Merge pull request #50 from gabemontero/fixWebhookInstructions
update webhook instructions
2016-05-20 17:30:21 -04:00
gabemontero 9a6db38edb update webhook instructions 2016-05-20 17:19:39 -04:00
Ben Parees f8edda4f04 Merge pull request #49 from gabemontero/fixContainerImageField
fix container image field - get as close to no value as currently pos…
2016-05-16 12:06:45 -04:00
gabemontero d8a485dbb7 fix container image field - get as close to no value as currently possible 2016-05-16 12:02:57 -04:00
Ben Parees 17ff3cdbce Merge pull request #48 from gabemontero/fixTemplatesAutoFalse
change BC ICT to automatic=true (more deterministic)
2016-05-12 11:14:02 -04:00
gabemontero c074be2189 change BC ICT to automatic=true (more deterministic) 2016-05-12 10:28:32 -04:00
Ben Parees 3f08ff2b61 Merge pull request #47 from bparees/emptydir
use an emptydir volume for mongodb instead of no volume
2016-04-26 16:44:34 -04:00
Ben Parees eaed1b5e10 use an emptydir volume for mongodb instead of no volume 2016-04-26 15:40:59 -04:00
Ben Parees 21c5ab2cbf Merge pull request #45 from PI-Victor/pi-victor/add_license_and_help
add help and LICENSE information
2016-04-18 10:17:38 -04:00
PI-Victor 1335e5640f add HELP and LICENSE information 2016-04-18 12:29:03 +02:00
Ben Parees 81f62ffe6a fix your's to yours 2016-04-13 09:39:16 -04:00
Ben Parees 4303514cc1 Merge pull request #44 from brennv/master
Added MongoDB notes
2016-04-13 09:38:35 -04:00
brennv 416e234522 Added MongoDB notes 2016-04-13 05:01:06 -07:00
Ben Parees ec380095d4 Merge pull request #43 from bparees/master
fix trailing comma in template
2016-04-11 22:02:12 -04:00
Ben Parees d3bfc8b240 fix trailing comma in template 2016-04-11 22:01:26 -04:00
Ben Parees 0e8b9b4009 Merge pull request #42 from PI-Victor/pi-victor/add_tests
add unit tests for post commit hook
2016-04-11 21:53:34 -04:00
pi-victor 4166119035 add sample tests for post commit hook 2016-04-12 01:01:56 +02:00
Ben Parees 6263b25a02 Merge pull request #39 from gabemontero/3563
parameterize IS namespace
2016-03-14 22:26:18 -04:00
gabemontero bab042519e parameterize IS namespace 2016-03-14 21:19:12 -04:00
Ben Parees 02352585f8 Merge pull request #38 from gabemontero/issue3571
parameterize name
2016-03-11 21:33:23 +00:00
gabemontero ee1068b426 parameterize name 2016-03-11 14:28:52 -05:00
Ben Parees c7f0e817af Merge pull request #37 from gabemontero/onlineMotivatedWordsmithing
edits consistent with UI philosophy
2016-03-07 21:15:03 +00:00
gabemontero 33d299b625 edits consistent with UI philosophy 2016-03-07 14:07:41 -05:00
Ben Parees 155485968b Merge pull request #36 from gabemontero/moveTo512Mi
bump all template mem limits to 512 Mi
2016-02-29 22:22:46 -05:00
gabemontero ee0de00cdf bump all template mem limits to 512 Mi 2016-02-29 22:11:07 -05:00
Ben Parees b17a07bf0a Merge pull request #35 from mfojtik/probe
Add liveness and readiness probe
2016-02-29 11:21:58 -05:00
Michal Fojtik 6015715f91 Add liveness and readiness probe 2016-02-29 17:12:14 +01:00
Ben Parees 933ca6638c Merge pull request #34 from gabemontero/addMemLimit
add default memory limit for deployments
2016-02-25 11:24:06 -05:00
gabemontero 5aa4e30ad9 add default memory limit for deployments 2016-02-24 23:01:42 -05:00
Ben Parees 34dafc6cdf Merge pull request #33 from bparees/master
change tag to quickstart
2016-02-24 15:41:03 -05:00
Ben Parees 4efa27f2ac change tag to quickstart 2016-02-24 15:40:45 -05:00
Ben Parees df1f4ae1c4 namespace typo 2015-11-20 22:41:34 -05:00
Ben Parees cafa1f4398 Merge pull request #29 from bparees/version
use openshift namespace
2015-11-20 21:34:20 -05:00
Ben Parees ca985b6d4b use openshift namespace 2015-11-20 21:31:20 -05:00
Ben Parees 7eef122c09 Merge pull request #28 from bparees/version
update image version
2015-11-20 19:36:09 -05:00
Ben Parees eb3e57d679 update image version 2015-11-20 18:37:56 -05:00
Michal Fojtik d9b1b3a154 Merge pull request #26 from sspeiche/generic-webhook
Added Generic webhook trigger to buildconfig and supporting env var
2015-10-05 21:23:46 +02:00
sspeiche c9f8146151 Added Generic webhook trigger to buildconfig and supporting env var for secret. 2015-10-05 15:16:22 -04:00
Ben Parees bf57558da2 Merge pull request #25 from bparees/master
remove bad default route hostname
2015-08-20 09:34:07 -04:00
Ben Parees bb2d4ff9d4 remove bad default route hostname 2015-08-18 13:19:24 -04:00
Michal Fojtik 457a80a7be Merge pull request #24 from bparees/param
parameterize db image
2015-08-03 10:37:06 +02:00
Ben Parees b10d0e8e1a parameterize db image 2015-07-31 14:38:41 -04:00
Ben Parees 1b284ff6a5 Merge pull request #23 from gabemontero/issue3358
get names consistent; associated doc updates based on test walkthrough
2015-07-01 15:06:58 -04:00
gabemontero 1ca4fbfd5a get names consistent; associated doc updates based on test walkthrough; Ben's comments; minor fixes / frontend rename; fix double colon 2015-07-01 15:00:17 -04:00
Ben Parees c647e25455 Merge pull request #22 from bparees/s2i
sti to s2i
2015-06-22 23:24:22 -04:00
Ben Parees b0a4206bbe sti to s2i 2015-06-22 15:41:19 -04:00
Clayton Coleman 9204cad248 Update README.md 2015-06-21 15:51:48 -04:00
Ben Parees a4f2d9482e Merge pull request #20 from bparees/cleanup
polish the readme some more
2015-06-19 18:06:13 -04:00
Ben Parees 605ce4e6a9 more minor readme cleanup 2015-06-19 18:05:40 -04:00
Ben Parees c2630834d2 Merge pull request #19 from bparees/cleanup
minor cleanup to welcome page
2015-06-19 17:40:09 -04:00
Ben Parees 654a6bce07 minor cleanup to welcome page 2015-06-19 17:39:55 -04:00
Rodolfo Carvalho ea308ce51f Merge pull request #13 from sspeiche/v2tmpl
Move towards a more common welcome page and use MongoDB
2015-06-19 17:10:19 +02:00
sspeiche ddeacadac2 Add simple sample and usage with MongoDB 2015-06-19 11:07:11 -04:00
Michal Fojtik 9d30467d90 Merge pull request #16 from rhcarvalho/env-vars
Pass relevant ENV VARs to application container
2015-06-19 13:02:38 +02:00
Rodolfo Carvalho c0a4e7bebb Pass relevant ENV VARs to application container 2015-06-19 11:22:02 +02:00
Ben Parees 970919d0cc Merge pull request #12 from mnagy/add_database_template
Add database template
2015-06-18 11:09:03 -04:00
Martin Nagy b6cf9e1cd4 Add database template 2015-06-18 16:59:23 +02:00
Ben Parees 7484bd5cb1 Merge pull request #15 from rhcarvalho/osc-to-oc
Rename osc -> oc
2015-06-18 09:57:17 -04:00
Rodolfo Carvalho e3b78520d9 Rename osc -> oc 2015-06-18 15:00:02 +02:00
Steve Speicher 9c7c6c9578 Merge pull request #14 from jhadvig/osc
LGTM
2015-06-18 08:22:50 -04:00
jhadvig a4afe7cc93 Update README 2015-06-18 11:58:31 +02:00
19 changed files with 2092 additions and 120 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
/node_modules/
/node_modules/
npm-debug.log

250
README.md
View File

@ -1,63 +1,253 @@
Node.js sample app on OpenShift!
<!-- toc -->
- [Node.js sample app on OpenShift!](#nodejs-sample-app-on-openshift)
* [OpenShift Origin v3 setup](#openshift-origin-v3-setup)
+ [Running a virtual machine with Vagrant](#running-a-virtual-machine-with-vagrant)
+ [Running a virtual machine managed by minishift](#running-a-virtual-machine-managed-by-minishift)
+ [Starting a Docker container](#starting-a-docker-container)
+ [Downloading the Binary](#downloading-the-binary)
+ [Running an Ansible playbook](#running-an-ansible-playbook)
* [Creating a project](#creating-a-project)
* [Creating new apps](#creating-new-apps)
+ [Create a new app from source code (method 1)](#create-a-new-app-from-source-code-method-1)
+ [Create a new app from a template (method 2)](#create-a-new-app-from-a-template-method-2)
+ [Build the app](#build-the-app)
+ [Deploy the app](#deploy-the-app)
+ [Configure routing](#configure-routing)
+ [Create a new app from an image (method 3)](#create-a-new-app-from-an-image-method-3)
+ [Setting environment variables](#setting-environment-variables)
+ [Success](#success)
+ [Pushing updates](#pushing-updates)
* [Debugging](#debugging)
* [Web UI](#web-ui)
* [Looking for help](#looking-for-help)
* [Compatibility](#compatibility)
* [License](#license)
<!-- tocstop -->
## Node.js sample app on OpenShift!
-----------------
This example will serve an http response of various "machine" stats from the "machine" the Node.js app is running on to [http://host:8080](http://host:8080).
This example will serve a welcome page and the current hit count as stored in a database.
### OpenShift setup ###
### OpenShift Origin v3 setup
One possible option is to use the Docker all-in-one launch as described in the [origins project](https://github.com/openshift/origins).
There are four methods to get started with OpenShift v3:
### The project ###
- Running a virtual machine with Vagrant
- Running a virtual machine managed by minishift
- Starting a Docker container
- Downloading the binary
- Running an Ansible playbook
If you don't have a project setup all ready, go ahead and take care of that
#### Running a virtual machine with Vagrant
$ osc new-project nodejs-echo --display-name="nodejs" --description="Sample Node.js app"
One option is to use the Vagrant all-in-one launch as described in the [OpenShift Origin All-In-One Virtual Machine](https://www.okd.io/vm/). This option works on Mac, Windows and Linux, but requires that you install [Vagrant](https://www.vagrantup.com/downloads.html) running [VirtualBox](https://www.virtualbox.org/wiki/Downloads).
#### Running a virtual machine managed by minishift
Another option to run virtual machine but not having to using Vagrant is to download and use the `minishift` binary as described in its [getting started](https://github.com/minishift/minishift/#getting-started) section. `minishift` can be used to spin up a VM on any of Windows, Linux or Mac with the help of supported underlying virtualization technologies like KVM, xhyve, Hyper-V, VirtualBox.
#### Starting a Docker container
Another option is running the OpenShift Origin Docker container image from [Docker Hub](https://hub.docker.com/r/openshift/origin/) launch as described in the [Getting Started for Administrators](https://docs.okd.io/latest/getting_started/administrators.html#running-in-a-docker-container). This method is supported on Fedora, CentOS, and Red Hat Enterprise Linux (RHEL) hosts only.
#### Downloading the Binary
Red Hat periodically publishes OpenShift Origin Server binaries for Linux, which you can download on the OpenShift Origin GitHub [Release](https://github.com/openshift/origin/releases) page. Instructions on how to install and launch the Openshift Origin Server from binary are described in [Getting Started for Administrators](https://docs.okd.io/latest/getting_started/administrators.html#downloading-the-binary).
#### Running an Ansible playbook
Outlined as the [Advanced Installation](https://docs.okd.io/latest/install_config/install/advanced_install.html) method for poduction environments, OpenShift Origin is also installable via Ansible playbook made avaialble on the GitHub [openshift-ansible](https://github.com/openshift/openshift-ansible) repo.
### Creating a project
After logging in with `oc login` (default username/password: openshift), if you don't have a project setup all ready, go ahead and take care of that:
$ oc new-project nodejs-echo \
--display-name="nodejs" --description="Sample Node.js app"
That's it, project has been created. Though it would probably be good to set your current project to this (thought new-project does it automatically as well), such as:
$ osc project nodejs
$ oc project nodejs-echo
### The app ###
### Creating new apps
Now let's pull in the app source code from [GitHub repo](https://github.com/openshift/nodejs-ex) (fork if you like)
You can create a new OpenShift application using the web console or by running the `oc new-app` command from the CLI. With the OpenShift CLI there are three ways to create a new application, by specifying either:
#### create ####
- [source code](https://docs.openshift.com/enterprise/3.0/dev_guide/new_app.html#specifying-source-code)
- [OpenShift templates](https://docs.openshift.com/enterprise/3.0/dev_guide/new_app.html#specifying-a-template)
- [DockerHub images](https://docs.openshift.com/enterprise/3.0/dev_guide/new_app.html#specifying-an-image)
$ osc new-app https://github.com/openshift/nodejs-ex
That should be it, `new-app` will take care of creating the right build configuration, deployment configuration and service definition. Next you'll be able to kick off the build.
#### Create a new app from source code (method 1)
Note, you can follow along with the web console (located at https://ip-address:8443/console) to see what new resources have been created and watch the progress of the build and deployment.
Pointing `oc new-app` at source code kicks off a chain of events, for our example run:
#### build ####
$ oc new-app https://github.com/sclorg/nodejs-ex -l name=myapp
$ osc start-build nodejs --follow
The tool will inspect the source code, locate an appropriate image on DockerHub, create an ImageStream for that image, and then create the right build configuration, deployment configuration and service definition.
You can alternatively leave off `--follow` and use `osc build-logs nodejs-n` where n is the number of the build (output of start-build).
(The -l flag will apply a label of "name=myapp" to all the resources created by new-app, for easy management later.)
#### deploy ####
#### Create a new app from a template (method 2)
happens automatically, to monitor its status either watch the web console or `osc get pods` to see when the pod is up. Another helpful command is
We can also [create new apps using OpenShift template files](https://docs.openshift.com/enterprise/3.0/dev_guide/new_app.html#specifying-a-template). Clone the demo app source code from [GitHub repo](https://github.com/sclorg/nodejs-ex) (fork if you like).
$ osc status
$ git clone https://github.com/sclorg/nodejs-ex
This will help indicate what IP address the service is running, the default port for it to deploy at is 8080.
Looking at the repo, you'll notice three files in the openshift/template directory:
#### enjoy ####
nodejs-ex
├── openshift
│   └── templates
│   ├── nodejs.json
│   ├── nodejs-mongodb.json
│   └── nodejs-mongodb-persistent.json
├── package.json
├── README.md
├── server.js
├── tests
│   └── app_test.js
└── views
└── index.html
Run/test our app by simply doing an HTTP GET request
We can create the the new app from the `nodejs.json` template by using the `-f` flag and pointing the tool at a path to the template file:
$ curl ip-address:8080
$ oc new-app -f /path/to/nodejs.json
#### update ####
#### Build the app
Assuming you used the URL of your own forked report, we can easily push changes to that hosted repo and simply repeat the steps above to build (this is obviously just demonstrating the manually kicking off of builds) which will trigger the new built image to be deployed.
`oc new-app` will kick off a build once all required dependencies are confirmed.
### Web UI ###
Check the status of your new nodejs app with the command:
To run this example from the Web UI, you can same steps following done on the CLI as defined above by [The project](#the-project). Here's a video showing it in motion:
$ oc status
Which should return something like:
In project nodejs (nodejs-echo) on server https://10.2.2.2:8443
svc/nodejs-ex - 172.30.108.183:8080
dc/nodejs-ex deploys istag/nodejs-ex:latest <-
bc/nodejs-ex builds https://github.com/sclorg/nodejs-ex with openshift/nodejs:0.10
build #1 running for 7 seconds
deployment #1 waiting on image or update
Note the server address for the web console, as yours will likely differ if you're not using the Vagrant set-up. You can follow along with the web console to see what new resources have been created and watch the progress of builds and deployments.
If the build is not yet started (you can check by running `oc get builds`), start one and stream the logs with:
$ oc start-build nodejs-ex --follow
You can alternatively leave off `--follow` and use `oc logs build/nodejs-ex-n` where *n* is the number of the build to track the output of the build.
#### Deploy the app
Deployment happens automatically once the new application image is available. To monitor its status either watch the web console or execute `oc get pods` to see when the pod is up. Another helpful command is
$ oc get svc
This will help indicate what IP address the service is running, the default port for it to deploy at is 8080. Output should look like:
NAME CLUSTER-IP EXTERNAL-IP PORT(S) SELECTOR AGE
nodejs-ex 172.30.249.251 <none> 8080/TCP deploymentconfig=nodejs-ex,name=myapp 17m
#### Configure routing
An OpenShift route exposes a service at a host name, like www.example.com, so that external clients can reach it by name.
DNS resolution for a host name is handled separately from routing; you may wish to configure a cloud domain that will always correctly resolve to the OpenShift router, or if using an unrelated host name you may need to modify its DNS records independently to resolve to the router.
That aside, let's explore our new web console, which for our example is running at [https://10.2.2.2:8443](https://10.2.2.2:8443).
After logging into the web console with your same CLI `oc login` credentials, click on the project we just created, then click `Create route`.
If you're running OpenShift on a local machine, you can preview the new app by setting the Hostname to a localhost like: *10.2.2.2*.
This could also be accomplished by running:
$ oc expose svc/nodejs-ex --hostname=www.example.com
Now navigate to the newly created Node.js web app at the hostname we just configured, for our example it was simply [https://10.2.2.2](https://10.2.2.2).
#### Create a new app from an image (method 3)
You may have noticed the index page "Page view count" reads "No database configured". Let's fix that by adding a MongoDB service. We could use the second OpenShift template example (`nodejs-mongodb.json`) but for the sake of demonstration let's point `oc new-app` at a DockerHub image:
$ oc new-app centos/mongodb-26-centos7 \
-e MONGODB_USER=admin,MONGODB_DATABASE=mongo_db,MONGODB_PASSWORD=secret,MONGODB_ADMIN_PASSWORD=super-secret
The `-e` flag sets the environment variables we want used in the configuration of our new app.
Running `oc status` or checking the web console will reveal the address of the newly created MongoDB:
In project nodejs-echo on server https://10.2.2.2:8443
svc/mongodb-26-centos7 - 172.30.0.112:27017
dc/mongodb-26-centos7 deploys istag/mongodb-26-centos7:latest
deployment #1 running for 43 seconds - 1 pod
http://10.2.2.2 to pod port 8080-tcp (svc/nodejs-ex)
dc/nodejs-ex deploys istag/nodejs-ex:latest <-
bc/nodejs-ex builds https://github.com/sclorg/nodejs-ex with openshift/nodejs:0.10
deployment #1 deployed 14 minutes ago - 1 pod
Note that the url for our new Mongo instance, for our example, is `172.30.0.112:27017`, yours will likely differ.
#### Setting environment variables
To take a look at environment variables set for each pod, run `oc env pods --all --list`.
We need to add the environment variable `MONGO_URL` to our Node.js web app so that it will utilize our MongoDB, and enable the "Page view count" feature. Run:
$ oc set env dc/nodejs-ex MONGO_URL='mongodb://admin:secret@172.30.0.112:27017/mongo_db'
Then check `oc status` to see that an updated deployment has been kicked off:
In project nodejs-echo on server https://10.2.2.2:8443
svc/mongodb-26-centos7 - 172.30.0.112:27017
dc/mongodb-26-centos7 deploys istag/mongodb-26-centos7:latest
deployment #1 deployed 2 hours ago - 1 pod
http://10.2.2.2 to pod port 8080-tcp (svc/nodejs-ex)
dc/nodejs-ex deploys istag/nodejs-ex:latest <-
bc/nodejs-ex builds https://github.com/sclorg/nodejs-ex with openshift/nodejs:0.10
deployment #2 deployed about a minute ago - 1 pod
deployment #1 deployed 2 hours ago
#### Success
You should now have a Node.js welcome page showing the current hit count, as stored in a MongoDB database.
#### Pushing updates
Assuming you used the URL of your own forked repository, we can easily push changes and simply repeat the steps above which will trigger the newly built image to be deployed.
### Debugging
Review some of the common tips and suggestions [here](https://github.com/openshift/origin/blob/master/docs/debugging-openshift.md).
### Web UI
To run this example from the Web UI, you can same steps following done on the CLI as defined above. Here's a video showing it in motion:
<a href="http://www.youtube.com/watch?feature=player_embedded&v=uocucZqg_0I&t=225" target="_blank">
<img src="http://img.youtube.com/vi/uocucZqg_0I/0.jpg"
<img src="http://img.youtube.com/vi/uocucZqg_0I/0.jpg"
alt="OpenShift 3: Node.js Sample" width="240" height="180" border="10" /></a>
### Looking for help
If you get stuck at some point, or think that this document needs further details or clarification, you can give feedback and look for help using the channels mentioned in [the OpenShift Origin repo](https://github.com/openshift/origin), or by filing an issue.
### Compatibility
This repository is compatible with Node.js 4 and higher, excluding any alpha or beta versions.
### License
This code is dedicated to the public domain to the maximum extent permitted by applicable law, pursuant to [CC0](http://creativecommons.org/publicdomain/zero/1.0/).

9
helm/nodejs/Chart.yaml Executable file
View File

@ -0,0 +1,9 @@
name: nodejs
version: 0.1
description: An example Node.js application with no database. For more information about using this template, including OpenShift considerations, see https://github.com/sclorg/nodejs-ex/blob/master/README.md.
keywords:
- quickstart
- nodejs
home: https://access.redhat.com
sources:
- https://github.com/sclorg/nodejs-ex

View File

@ -0,0 +1,3 @@
The following service(s) have been created in your project: {{ .Values.name }}.
For more information about using this template, including OpenShift considerations, see https://github.com/sclorg/nodejs-ex/blob/master/README.md.

View File

@ -0,0 +1,38 @@
kind: BuildConfig
apiVersion: build.openshift.io/v1
metadata:
name: {{ .Values.name | quote }}
annotations:
description: Defines how to build the application
spec:
source:
type: Git
git:
uri: {{ .Values.source_repository_url | quote }}
ref: {{ .Values.source_repository_ref | quote }}
contextDir: {{ .Values.context_dir | quote }}
strategy:
type: Source
sourceStrategy:
from:
kind: ImageStreamTag
namespace: {{ .Values.namespace | quote }}
name: nodejs:6
env:
- name: NPM_MIRROR
value: {{ .Values.npm_mirror | quote }}
output:
to:
kind: ImageStreamTag
name: {{ print .Values.name ":latest" | quote }}
triggers:
- type: ImageChange
- type: ConfigChange
- type: GitHub
github:
secret: {{ .Values.github_webhook_secret | default (randAlphaNum 40) | quote }}
- type: Generic
generic:
secret: {{ .Values.generic_webhook_secret | default (randAlphaNum 40) | quote }}
postCommit:
script: npm test

View File

@ -0,0 +1,49 @@
kind: DeploymentConfig
apiVersion: apps.openshift.io/v1
metadata:
name: {{ .Values.name | quote }}
annotations:
description: Defines how to deploy the application server
template.alpha.openshift.io/wait-for-ready: 'true'
spec:
strategy:
type: Rolling
triggers:
- type: ImageChange
imageChangeParams:
automatic: true
containerNames:
- nodejs-example
from:
kind: ImageStreamTag
name: {{ print .Values.name ":latest" | quote }}
- type: ConfigChange
replicas: 1
selector:
name: {{ .Values.name | quote }}
template:
metadata:
name: {{ .Values.name | quote }}
labels:
name: {{ .Values.name | quote }}
spec:
containers:
- name: nodejs-example
image: " "
ports:
- containerPort: 8080
readinessProbe:
timeoutSeconds: 3
initialDelaySeconds: 3
httpGet:
path: "/"
port: 8080
livenessProbe:
timeoutSeconds: 3
initialDelaySeconds: 30
httpGet:
path: "/"
port: 8080
resources:
limits:
memory: {{ .Values.memory_limit | quote }}

View File

@ -0,0 +1,6 @@
kind: ImageStream
apiVersion: image.openshift.io/v1
metadata:
name: {{ .Values.name | quote }}
annotations:
description: Keeps track of changes in the application image

View File

@ -0,0 +1,9 @@
kind: Route
apiVersion: route.openshift.io/v1
metadata:
name: {{ .Values.name | quote }}
spec:
host: {{ .Values.application_domain | quote }}
to:
kind: Service
name: {{ .Values.name | quote }}

View File

@ -0,0 +1,13 @@
kind: Service
apiVersion: v1
metadata:
name: {{ .Values.name | quote }}
annotations:
description: Exposes and load balances the application pods
spec:
ports:
- name: web
port: 8080
targetPort: 8080
selector:
name: {{ .Values.name | quote }}

20
helm/nodejs/values.yaml Normal file
View File

@ -0,0 +1,20 @@
# Name: The name assigned to all of the frontend objects defined in this template.
name: nodejs-example
# Namespace: The OpenShift Namespace where the ImageStream resides.
namespace: openshift
# Memory Limit: Maximum amount of memory the container can use.
memory_limit: 512Mi
# Git Repository URL: The URL of the repository with your application source code.
source_repository_url: https://github.com/sclorg/nodejs-ex.git
# Git Reference: Set this to a branch name, tag or other ref of your repository if you are not using the default branch.
source_repository_ref:
# Context Directory: Set this to the relative path to your project if it is not in the root of your repository.
context_dir:
# Application Hostname: The exposed hostname that will route to the Node.js service, if left blank a value will be defaulted.
application_domain:
# GitHub Webhook Secret: Github trigger secret. A difficult to guess string encoded as part of the webhook URL. Not encrypted.
github_webhook_secret:
# Generic Webhook Secret: A secret string used to configure the Generic webhook.
generic_webhook_secret:
# Custom NPM Mirror URL: The custom NPM mirror URL
npm_mirror:

6
openshift/pipeline/Jenkinsfile vendored Normal file
View File

@ -0,0 +1,6 @@
node('nodejs') {
stage 'build'
openshiftBuild(buildConfig: 'nodejs-ex', showBuildLogs: 'true')
stage 'deploy'
openshiftDeploy(deploymentConfig: 'nodejs-ex')
}

View File

@ -0,0 +1,14 @@
This directory contains a Jenkinsfile which can be used to build
nodejs-ex using an OpenShift build pipeline.
To do this, run:
```bash
# create the nodejs example as usual
oc new-app https://github.com/sclorg/nodejs-ex
# now create the pipeline build controller from the openshift/pipeline
# subdirectory
oc new-app https://github.com/sclorg/nodejs-ex \
--context-dir=openshift/pipeline --name nodejs-ex-pipeline
```

View File

@ -0,0 +1,563 @@
{
"kind": "Template",
"apiVersion": "v1",
"metadata": {
"name": "nodejs-mongo-persistent",
"annotations": {
"openshift.io/display-name": "Node.js + MongoDB",
"description": "An example Node.js application with a MongoDB database. For more information about using this template, including OpenShift considerations, see https://github.com/sclorg/nodejs-ex/blob/master/README.md.",
"tags": "quickstart,nodejs",
"iconClass": "icon-nodejs",
"openshift.io/long-description": "This template defines resources needed to develop a NodeJS application, including a build configuration, application deployment configuration, and database deployment configuration.",
"openshift.io/provider-display-name": "Red Hat, Inc.",
"openshift.io/documentation-url": "https://github.com/sclorg/nodejs-ex",
"openshift.io/support-url": "https://access.redhat.com",
"template.openshift.io/bindable": "false"
}
},
"message": "The following service(s) have been created in your project: ${NAME}, ${DATABASE_SERVICE_NAME}.\n\nFor more information about using this template, including OpenShift considerations, see https://github.com/sclorg/nodejs-ex/blob/master/README.md.",
"labels": {
"template": "nodejs-mongo-persistent"
},
"objects": [
{
"kind": "Secret",
"apiVersion": "v1",
"metadata": {
"name": "${NAME}"
},
"stringData": {
"database-user": "${DATABASE_USER}",
"database-password": "${DATABASE_PASSWORD}",
"database-admin-password" : "${DATABASE_ADMIN_PASSWORD}"
}
},
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "${NAME}",
"annotations": {
"description": "Exposes and load balances the application pods",
"service.alpha.openshift.io/dependencies": "[{\"name\": \"${DATABASE_SERVICE_NAME}\", \"kind\": \"Service\"}]"
}
},
"spec": {
"ports": [
{
"name": "web",
"port": 8080,
"targetPort": 8080
}
],
"selector": {
"name": "${NAME}"
}
}
},
{
"kind": "Route",
"apiVersion": "v1",
"metadata": {
"name": "${NAME}"
},
"spec": {
"host": "${APPLICATION_DOMAIN}",
"to": {
"kind": "Service",
"name": "${NAME}"
}
}
},
{
"kind": "ImageStream",
"apiVersion": "v1",
"metadata": {
"name": "${NAME}",
"annotations": {
"description": "Keeps track of changes in the application image"
}
}
},
{
"kind": "BuildConfig",
"apiVersion": "v1",
"metadata": {
"name": "${NAME}",
"annotations": {
"description": "Defines how to build the application",
"template.alpha.openshift.io/wait-for-ready": "true"
}
},
"spec": {
"source": {
"type": "Git",
"git": {
"uri": "${SOURCE_REPOSITORY_URL}",
"ref": "${SOURCE_REPOSITORY_REF}"
},
"contextDir": "${CONTEXT_DIR}"
},
"strategy": {
"type": "Source",
"sourceStrategy": {
"from": {
"kind": "ImageStreamTag",
"namespace": "${NAMESPACE}",
"name": "nodejs:${NODEJS_VERSION}"
},
"env": [
{
"name": "NPM_MIRROR",
"value": "${NPM_MIRROR}"
}
]
}
},
"output": {
"to": {
"kind": "ImageStreamTag",
"name": "${NAME}:latest"
}
},
"triggers": [
{
"type": "ImageChange"
},
{
"type": "ConfigChange"
},
{
"type": "GitHub",
"github": {
"secret": "${GITHUB_WEBHOOK_SECRET}"
}
},
{
"type": "Generic",
"generic": {
"secret": "${GENERIC_WEBHOOK_SECRET}"
}
}
],
"postCommit": {
"script": "npm test"
}
}
},
{
"kind": "DeploymentConfig",
"apiVersion": "v1",
"metadata": {
"name": "${NAME}",
"annotations": {
"description": "Defines how to deploy the application server",
"template.alpha.openshift.io/wait-for-ready": "true"
}
},
"spec": {
"strategy": {
"type": "Recreate"
},
"triggers": [
{
"type": "ImageChange",
"imageChangeParams": {
"automatic": true,
"containerNames": [
"nodejs-mongo-persistent"
],
"from": {
"kind": "ImageStreamTag",
"name": "${NAME}:latest"
}
}
},
{
"type": "ConfigChange"
}
],
"replicas": 1,
"selector": {
"name": "${NAME}"
},
"template": {
"metadata": {
"name": "${NAME}",
"labels": {
"name": "${NAME}"
}
},
"spec": {
"containers": [
{
"name": "nodejs-mongo-persistent",
"image": " ",
"ports": [
{
"containerPort": 8080
}
],
"env": [
{
"name": "DATABASE_SERVICE_NAME",
"value": "${DATABASE_SERVICE_NAME}"
},
{
"name": "MONGODB_USER",
"valueFrom": {
"secretKeyRef" : {
"name" : "${NAME}",
"key" : "database-user"
}
}
},
{
"name": "MONGODB_PASSWORD",
"valueFrom": {
"secretKeyRef" : {
"name" : "${NAME}",
"key" : "database-password"
}
}
},
{
"name": "MONGODB_DATABASE",
"value": "${DATABASE_NAME}"
},
{
"name": "MONGODB_ADMIN_PASSWORD",
"valueFrom": {
"secretKeyRef" : {
"name" : "${NAME}",
"key" : "database-admin-password"
}
}
}
],
"readinessProbe": {
"timeoutSeconds": 3,
"initialDelaySeconds": 3,
"httpGet": {
"path": "/",
"port": 8080
}
},
"livenessProbe": {
"timeoutSeconds": 3,
"initialDelaySeconds": 30,
"httpGet": {
"path": "/",
"port": 8080
}
},
"resources": {
"limits": {
"memory": "${MEMORY_LIMIT}"
}
}
}
]
}
}
}
},
{
"kind": "PersistentVolumeClaim",
"apiVersion": "v1",
"metadata": {
"name": "${DATABASE_SERVICE_NAME}"
},
"spec": {
"accessModes": [
"ReadWriteOnce"
],
"resources": {
"requests": {
"storage": "${VOLUME_CAPACITY}"
}
}
}
},
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "${DATABASE_SERVICE_NAME}",
"annotations": {
"description": "Exposes the database server"
}
},
"spec": {
"ports": [
{
"name": "mongodb",
"port": 27017,
"targetPort": 27017
}
],
"selector": {
"name": "${DATABASE_SERVICE_NAME}"
}
}
},
{
"kind": "DeploymentConfig",
"apiVersion": "v1",
"metadata": {
"name": "${DATABASE_SERVICE_NAME}",
"annotations": {
"description": "Defines how to deploy the database",
"template.alpha.openshift.io/wait-for-ready": "true"
}
},
"spec": {
"strategy": {
"type": "Recreate"
},
"triggers": [
{
"type": "ImageChange",
"imageChangeParams": {
"automatic": true,
"containerNames": [
"mongodb"
],
"from": {
"kind": "ImageStreamTag",
"namespace": "${NAMESPACE}",
"name": "mongodb:${MONGODB_VERSION}"
}
}
},
{
"type": "ConfigChange"
}
],
"replicas": 1,
"selector": {
"name": "${DATABASE_SERVICE_NAME}"
},
"template": {
"metadata": {
"name": "${DATABASE_SERVICE_NAME}",
"labels": {
"name": "${DATABASE_SERVICE_NAME}"
}
},
"spec": {
"containers": [
{
"name": "mongodb",
"image": " ",
"ports": [
{
"containerPort": 27017
}
],
"env": [
{
"name": "MONGODB_USER",
"valueFrom": {
"secretKeyRef" : {
"name" : "${NAME}",
"key" : "database-user"
}
}
},
{
"name": "MONGODB_PASSWORD",
"valueFrom": {
"secretKeyRef" : {
"name" : "${NAME}",
"key" : "database-password"
}
}
},
{
"name": "MONGODB_DATABASE",
"value": "${DATABASE_NAME}"
},
{
"name": "MONGODB_ADMIN_PASSWORD",
"valueFrom": {
"secretKeyRef" : {
"name" : "${NAME}",
"key" : "database-admin-password"
}
}
}
],
"readinessProbe": {
"timeoutSeconds": 1,
"initialDelaySeconds": 3,
"exec": {
"command": [
"/bin/sh",
"-i",
"-c",
"mongo 127.0.0.1:27017/$MONGODB_DATABASE -u $MONGODB_USER -p $MONGODB_PASSWORD --eval=\"quit()\""
]
}
},
"livenessProbe": {
"timeoutSeconds": 1,
"initialDelaySeconds": 30,
"tcpSocket": {
"port": 27017
}
},
"resources": {
"limits": {
"memory": "${MEMORY_MONGODB_LIMIT}"
}
},
"volumeMounts": [
{
"name": "${DATABASE_SERVICE_NAME}-data",
"mountPath": "/var/lib/mongodb/data"
}
]
}
],
"volumes": [
{
"name": "${DATABASE_SERVICE_NAME}-data",
"persistentVolumeClaim": {
"claimName": "${DATABASE_SERVICE_NAME}"
}
}
]
}
}
}
}
],
"parameters": [
{
"name": "NAME",
"displayName": "Name",
"description": "The name assigned to all of the frontend objects defined in this template.",
"required": true,
"value": "nodejs-mongo-persistent"
},
{
"name": "NAMESPACE",
"displayName": "Namespace",
"description": "The OpenShift Namespace where the ImageStream resides.",
"required": true,
"value": "openshift"
},
{
"name": "NODEJS_VERSION",
"displayName": "Version of NodeJS Image",
"description": "Version of NodeJS image to be used (6, 8, or latest).",
"value": "8",
"required": true
},
{
"name": "MONGODB_VERSION",
"displayName": "Version of MongoDB Image",
"description": "Version of MongoDB image to be used (3.6 or latest).",
"value": "3.6",
"required": true
},
{
"name": "MEMORY_LIMIT",
"displayName": "Memory Limit",
"description": "Maximum amount of memory the Node.js container can use.",
"required": true,
"value": "512Mi"
},
{
"name": "MEMORY_MONGODB_LIMIT",
"displayName": "Memory Limit (MongoDB)",
"description": "Maximum amount of memory the MongoDB container can use.",
"required": true,
"value": "512Mi"
},
{
"name": "VOLUME_CAPACITY",
"displayName": "Volume Capacity",
"description": "Volume space available for data, e.g. 512Mi, 2Gi",
"value": "1Gi",
"required": true
},
{
"name": "SOURCE_REPOSITORY_URL",
"displayName": "Git Repository URL",
"description": "The URL of the repository with your application source code.",
"required": true,
"value": "https://github.com/sclorg/nodejs-ex.git"
},
{
"name": "SOURCE_REPOSITORY_REF",
"displayName": "Git Reference",
"description": "Set this to a branch name, tag or other ref of your repository if you are not using the default branch."
},
{
"name": "CONTEXT_DIR",
"displayName": "Context Directory",
"description": "Set this to the relative path to your project if it is not in the root of your repository."
},
{
"name": "APPLICATION_DOMAIN",
"displayName": "Application Hostname",
"description": "The exposed hostname that will route to the Node.js service, if left blank a value will be defaulted.",
"value": ""
},
{
"name": "GITHUB_WEBHOOK_SECRET",
"displayName": "GitHub Webhook Secret",
"description": "Github trigger secret. A difficult to guess string encoded as part of the webhook URL. Not encrypted.",
"generate": "expression",
"from": "[a-zA-Z0-9]{40}"
},
{
"name": "GENERIC_WEBHOOK_SECRET",
"displayName": "Generic Webhook Secret",
"description": "A secret string used to configure the Generic webhook.",
"generate": "expression",
"from": "[a-zA-Z0-9]{40}"
},
{
"name": "DATABASE_SERVICE_NAME",
"displayName": "Database Service Name",
"required": true,
"value": "mongodb"
},
{
"name": "DATABASE_USER",
"displayName": "MongoDB Username",
"description": "Username for MongoDB user that will be used for accessing the database.",
"generate": "expression",
"from": "user[A-Z0-9]{3}"
},
{
"name": "DATABASE_PASSWORD",
"displayName": "MongoDB Password",
"description": "Password for the MongoDB user.",
"generate": "expression",
"from": "[a-zA-Z0-9]{16}"
},
{
"name": "DATABASE_NAME",
"displayName": "Database Name",
"required": true,
"value": "sampledb"
},
{
"name": "DATABASE_ADMIN_PASSWORD",
"displayName": "Database Administrator Password",
"description": "Password for the database admin user.",
"generate": "expression",
"from": "[a-zA-Z0-9]{16}"
},
{
"name": "NPM_MIRROR",
"displayName": "Custom NPM Mirror URL",
"description": "The custom NPM mirror URL",
"value": ""
}
]
}

View File

@ -0,0 +1,540 @@
{
"kind": "Template",
"apiVersion": "v1",
"metadata": {
"name": "nodejs-mongodb-example",
"annotations": {
"openshift.io/display-name": "Node.js + MongoDB (Ephemeral)",
"description": "An example Node.js application with a MongoDB database. For more information about using this template, including OpenShift considerations, see https://github.com/sclorg/nodejs-ex/blob/master/README.md.\n\nWARNING: Any data stored will be lost upon pod destruction. Only use this template for testing.",
"tags": "quickstart,nodejs",
"iconClass": "icon-nodejs",
"openshift.io/long-description": "This template defines resources needed to develop a NodeJS application, including a build configuration, application deployment configuration, and database deployment configuration. The database is stored in non-persistent storage, so this configuration should be used for experimental purposes only.",
"openshift.io/provider-display-name": "Red Hat, Inc.",
"openshift.io/documentation-url": "https://github.com/sclorg/nodejs-ex",
"openshift.io/support-url": "https://access.redhat.com",
"template.openshift.io/bindable": "false"
}
},
"message": "The following service(s) have been created in your project: ${NAME}, ${DATABASE_SERVICE_NAME}.\n\nFor more information about using this template, including OpenShift considerations, see https://github.com/sclorg/nodejs-ex/blob/master/README.md.",
"labels": {
"template": "nodejs-mongodb-example",
"app": "nodejs-mongodb-example"
},
"objects": [
{
"kind": "Secret",
"apiVersion": "v1",
"metadata": {
"name": "${NAME}"
},
"stringData": {
"database-user": "${DATABASE_USER}",
"database-password": "${DATABASE_PASSWORD}",
"database-admin-password" : "${DATABASE_ADMIN_PASSWORD}"
}
},
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "${NAME}",
"annotations": {
"description": "Exposes and load balances the application pods",
"service.alpha.openshift.io/dependencies": "[{\"name\": \"${DATABASE_SERVICE_NAME}\", \"kind\": \"Service\"}]"
}
},
"spec": {
"ports": [
{
"name": "web",
"port": 8080,
"targetPort": 8080
}
],
"selector": {
"name": "${NAME}"
}
}
},
{
"kind": "Route",
"apiVersion": "v1",
"metadata": {
"name": "${NAME}"
},
"spec": {
"host": "${APPLICATION_DOMAIN}",
"to": {
"kind": "Service",
"name": "${NAME}"
}
}
},
{
"kind": "ImageStream",
"apiVersion": "v1",
"metadata": {
"name": "${NAME}",
"annotations": {
"description": "Keeps track of changes in the application image"
}
}
},
{
"kind": "BuildConfig",
"apiVersion": "v1",
"metadata": {
"name": "${NAME}",
"annotations": {
"description": "Defines how to build the application",
"template.alpha.openshift.io/wait-for-ready": "true"
}
},
"spec": {
"source": {
"type": "Git",
"git": {
"uri": "${SOURCE_REPOSITORY_URL}",
"ref": "${SOURCE_REPOSITORY_REF}"
},
"contextDir": "${CONTEXT_DIR}"
},
"strategy": {
"type": "Source",
"sourceStrategy": {
"from": {
"kind": "ImageStreamTag",
"namespace": "${NAMESPACE}",
"name": "nodejs:${NODEJS_VERSION}"
},
"env": [
{
"name": "NPM_MIRROR",
"value": "${NPM_MIRROR}"
}
]
}
},
"output": {
"to": {
"kind": "ImageStreamTag",
"name": "${NAME}:latest"
}
},
"triggers": [
{
"type": "ImageChange"
},
{
"type": "ConfigChange"
},
{
"type": "GitHub",
"github": {
"secret": "${GITHUB_WEBHOOK_SECRET}"
}
},
{
"type": "Generic",
"generic": {
"secret": "${GENERIC_WEBHOOK_SECRET}"
}
}
],
"postCommit": {
"script": "npm test"
}
}
},
{
"kind": "DeploymentConfig",
"apiVersion": "v1",
"metadata": {
"name": "${NAME}",
"annotations": {
"description": "Defines how to deploy the application server",
"template.alpha.openshift.io/wait-for-ready": "true"
}
},
"spec": {
"strategy": {
"type": "Recreate"
},
"triggers": [
{
"type": "ImageChange",
"imageChangeParams": {
"automatic": true,
"containerNames": [
"nodejs-mongodb-example"
],
"from": {
"kind": "ImageStreamTag",
"name": "${NAME}:latest"
}
}
},
{
"type": "ConfigChange"
}
],
"replicas": 1,
"selector": {
"name": "${NAME}"
},
"template": {
"metadata": {
"name": "${NAME}",
"labels": {
"name": "${NAME}"
}
},
"spec": {
"containers": [
{
"name": "nodejs-mongodb-example",
"image": " ",
"ports": [
{
"containerPort": 8080
}
],
"env": [
{
"name": "DATABASE_SERVICE_NAME",
"value": "${DATABASE_SERVICE_NAME}"
},
{
"name": "MONGODB_USER",
"valueFrom": {
"secretKeyRef" : {
"name" : "${NAME}",
"key" : "database-user"
}
}
},
{
"name": "MONGODB_PASSWORD",
"valueFrom": {
"secretKeyRef" : {
"name" : "${NAME}",
"key" : "database-password"
}
}
},
{
"name": "MONGODB_DATABASE",
"value": "${DATABASE_NAME}"
},
{
"name": "MONGODB_ADMIN_PASSWORD",
"valueFrom": {
"secretKeyRef" : {
"name" : "${NAME}",
"key" : "database-admin-password"
}
}
}
],
"readinessProbe": {
"timeoutSeconds": 3,
"initialDelaySeconds": 3,
"httpGet": {
"path": "/",
"port": 8080
}
},
"livenessProbe": {
"timeoutSeconds": 3,
"initialDelaySeconds": 30,
"httpGet": {
"path": "/",
"port": 8080
}
},
"resources": {
"limits": {
"memory": "${MEMORY_LIMIT}"
}
}
}
]
}
}
}
},
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "${DATABASE_SERVICE_NAME}",
"annotations": {
"description": "Exposes the database server"
}
},
"spec": {
"ports": [
{
"name": "mongodb",
"port": 27017,
"targetPort": 27017
}
],
"selector": {
"name": "${DATABASE_SERVICE_NAME}"
}
}
},
{
"kind": "DeploymentConfig",
"apiVersion": "v1",
"metadata": {
"name": "${DATABASE_SERVICE_NAME}",
"annotations": {
"description": "Defines how to deploy the database",
"template.alpha.openshift.io/wait-for-ready": "true"
}
},
"spec": {
"strategy": {
"type": "Recreate"
},
"triggers": [
{
"type": "ImageChange",
"imageChangeParams": {
"automatic": true,
"containerNames": [
"mongodb"
],
"from": {
"kind": "ImageStreamTag",
"namespace": "${NAMESPACE}",
"name": "mongodb:${MONGODB_VERSION}"
}
}
},
{
"type": "ConfigChange"
}
],
"replicas": 1,
"selector": {
"name": "${DATABASE_SERVICE_NAME}"
},
"template": {
"metadata": {
"name": "${DATABASE_SERVICE_NAME}",
"labels": {
"name": "${DATABASE_SERVICE_NAME}"
}
},
"spec": {
"containers": [
{
"name": "mongodb",
"image": " ",
"ports": [
{
"containerPort": 27017
}
],
"env": [
{
"name": "MONGODB_USER",
"valueFrom": {
"secretKeyRef" : {
"name" : "${NAME}",
"key" : "database-user"
}
}
},
{
"name": "MONGODB_PASSWORD",
"valueFrom": {
"secretKeyRef" : {
"name" : "${NAME}",
"key" : "database-password"
}
}
},
{
"name": "MONGODB_DATABASE",
"value": "${DATABASE_NAME}"
},
{
"name": "MONGODB_ADMIN_PASSWORD",
"valueFrom": {
"secretKeyRef" : {
"name" : "${NAME}",
"key" : "database-admin-password"
}
}
}
],
"readinessProbe": {
"timeoutSeconds": 1,
"initialDelaySeconds": 3,
"exec": {
"command": [
"/bin/sh",
"-i",
"-c",
"mongo 127.0.0.1:27017/$MONGODB_DATABASE -u $MONGODB_USER -p $MONGODB_PASSWORD --eval=\"quit()\""
]
}
},
"livenessProbe": {
"timeoutSeconds": 1,
"initialDelaySeconds": 30,
"tcpSocket": {
"port": 27017
}
},
"resources": {
"limits": {
"memory": "${MEMORY_MONGODB_LIMIT}"
}
},
"volumeMounts": [
{
"name": "${DATABASE_SERVICE_NAME}-data",
"mountPath": "/var/lib/mongodb/data"
}
]
}
],
"volumes": [
{
"name": "${DATABASE_SERVICE_NAME}-data",
"emptyDir": {
"medium": ""
}
}
]
}
}
}
}
],
"parameters": [
{
"name": "NAME",
"displayName": "Name",
"description": "The name assigned to all of the frontend objects defined in this template.",
"required": true,
"value": "nodejs-mongodb-example"
},
{
"name": "NAMESPACE",
"displayName": "Namespace",
"description": "The OpenShift Namespace where the ImageStream resides.",
"required": true,
"value": "openshift"
},
{
"name": "NODEJS_VERSION",
"displayName": "Version of NodeJS Image",
"description": "Version of NodeJS image to be used (6, 8, or latest).",
"value": "8",
"required": true
},
{
"name": "MONGODB_VERSION",
"displayName": "Version of MongoDB Image",
"description": "Version of MongoDB image to be used (3.6 or latest).",
"value": "3.6",
"required": true
},
{
"name": "MEMORY_LIMIT",
"displayName": "Memory Limit",
"description": "Maximum amount of memory the Node.js container can use.",
"required": true,
"value": "512Mi"
},
{
"name": "MEMORY_MONGODB_LIMIT",
"displayName": "Memory Limit (MongoDB)",
"description": "Maximum amount of memory the MongoDB container can use.",
"required": true,
"value": "512Mi"
},
{
"name": "SOURCE_REPOSITORY_URL",
"displayName": "Git Repository URL",
"description": "The URL of the repository with your application source code.",
"required": true,
"value": "https://github.com/sclorg/nodejs-ex.git"
},
{
"name": "SOURCE_REPOSITORY_REF",
"displayName": "Git Reference",
"description": "Set this to a branch name, tag or other ref of your repository if you are not using the default branch."
},
{
"name": "CONTEXT_DIR",
"displayName": "Context Directory",
"description": "Set this to the relative path to your project if it is not in the root of your repository."
},
{
"name": "APPLICATION_DOMAIN",
"displayName": "Application Hostname",
"description": "The exposed hostname that will route to the Node.js service, if left blank a value will be defaulted.",
"value": ""
},
{
"name": "GITHUB_WEBHOOK_SECRET",
"displayName": "GitHub Webhook Secret",
"description": "Github trigger secret. A difficult to guess string encoded as part of the webhook URL. Not encrypted.",
"generate": "expression",
"from": "[a-zA-Z0-9]{40}"
},
{
"name": "GENERIC_WEBHOOK_SECRET",
"displayName": "Generic Webhook Secret",
"description": "A secret string used to configure the Generic webhook.",
"generate": "expression",
"from": "[a-zA-Z0-9]{40}"
},
{
"name": "DATABASE_SERVICE_NAME",
"displayName": "Database Service Name",
"required": true,
"value": "mongodb"
},
{
"name": "DATABASE_USER",
"displayName": "MongoDB Username",
"description": "Username for MongoDB user that will be used for accessing the database.",
"generate": "expression",
"from": "user[A-Z0-9]{3}"
},
{
"name": "DATABASE_PASSWORD",
"displayName": "MongoDB Password",
"description": "Password for the MongoDB user.",
"generate": "expression",
"from": "[a-zA-Z0-9]{16}"
},
{
"name": "DATABASE_NAME",
"displayName": "Database Name",
"required": true,
"value": "sampledb"
},
{
"name": "DATABASE_ADMIN_PASSWORD",
"displayName": "Database Administrator Password",
"description": "Password for the database admin user.",
"generate": "expression",
"from": "[a-zA-Z0-9]{16}"
},
{
"name": "NPM_MIRROR",
"displayName": "Custom NPM Mirror URL",
"description": "The custom NPM mirror URL",
"value": ""
}
]
}

View File

@ -4,20 +4,28 @@
"metadata": {
"name": "nodejs-example",
"annotations": {
"description": "An example Node.js application with no database",
"tags": "instant-app,nodejs",
"iconClass": "icon-nodejs"
"openshift.io/display-name": "Node.js",
"description": "An example Node.js application with no database. For more information about using this template, including OpenShift considerations, see https://github.com/sclorg/nodejs-ex/blob/master/README.md.",
"tags": "quickstart,nodejs",
"iconClass": "icon-nodejs",
"openshift.io/long-description": "This template defines resources needed to develop a NodeJS application, including a build configuration and application deployment configuration. It does not include a database.",
"openshift.io/provider-display-name": "Red Hat, Inc.",
"openshift.io/documentation-url": "https://github.com/sclorg/nodejs-ex",
"openshift.io/support-url": "https://access.redhat.com",
"template.openshift.io/bindable": "false"
}
},
"message": "The following service(s) have been created in your project: ${NAME}.\n\nFor more information about using this template, including OpenShift considerations, see https://github.com/sclorg/nodejs-ex/blob/master/README.md.",
"labels": {
"template": "nodejs-example"
"template": "nodejs-example",
"app": "nodejs-example"
},
"objects": [
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "nodejs-frontend",
"name": "${NAME}",
"annotations": {
"description": "Exposes and load balances the application pods"
}
@ -31,7 +39,7 @@
}
],
"selector": {
"name": "nodejs-frontend"
"name": "${NAME}"
}
}
},
@ -39,13 +47,13 @@
"kind": "Route",
"apiVersion": "v1",
"metadata": {
"name": "nodejs-route"
"name": "${NAME}"
},
"spec": {
"host": "${APPLICATION_DOMAIN}",
"to": {
"kind": "Service",
"name": "nodejs-frontend"
"name": "${NAME}"
}
}
},
@ -53,7 +61,7 @@
"kind": "ImageStream",
"apiVersion": "v1",
"metadata": {
"name": "nodejs-example",
"name": "${NAME}",
"annotations": {
"description": "Keeps track of changes in the application image"
}
@ -63,9 +71,10 @@
"kind": "BuildConfig",
"apiVersion": "v1",
"metadata": {
"name": "nodejs-example",
"name": "${NAME}",
"annotations": {
"description": "Defines how to build the application"
"description": "Defines how to build the application",
"template.alpha.openshift.io/wait-for-ready": "true"
}
},
"spec": {
@ -82,37 +91,56 @@
"sourceStrategy": {
"from": {
"kind": "ImageStreamTag",
"namespace": "openshift",
"name": "nodejs:0.10"
}
"namespace": "${NAMESPACE}",
"name": "nodejs:${NODEJS_VERSION}"
},
"env": [
{
"name": "NPM_MIRROR",
"value": "${NPM_MIRROR}"
}
]
}
},
"output": {
"to": {
"kind": "ImageStreamTag",
"name": "nodejs-example:latest"
"name": "${NAME}:latest"
}
},
"triggers": [
{
"type": "ImageChange"
},
{
"type": "ConfigChange"
},
{
"type": "GitHub",
"github": {
"secret": "${GITHUB_WEBHOOK_SECRET}"
}
},
{
"type": "Generic",
"generic": {
"secret": "${GENERIC_WEBHOOK_SECRET}"
}
}
]
],
"postCommit": {
"script": "npm test"
}
}
},
{
"kind": "DeploymentConfig",
"apiVersion": "v1",
"metadata": {
"name": "nodejs-frontend",
"name": "${NAME}",
"annotations": {
"description": "Defines how to deploy the application server"
"description": "Defines how to deploy the application server",
"template.alpha.openshift.io/wait-for-ready": "true"
}
},
"spec": {
@ -129,7 +157,7 @@
],
"from": {
"kind": "ImageStreamTag",
"name": "nodejs-example:latest"
"name": "${NAME}:latest"
}
}
},
@ -139,24 +167,47 @@
],
"replicas": 1,
"selector": {
"name": "nodejs-frontend"
"name": "${NAME}"
},
"template": {
"metadata": {
"name": "nodejs-frontend",
"name": "${NAME}",
"labels": {
"name": "nodejs-frontend"
"name": "${NAME}"
}
},
"spec": {
"containers": [
{
"name": "nodejs-example",
"image": "nodejs-example",
"image": " ",
"ports": [
{
"containerPort": 8080
}
],
"readinessProbe": {
"timeoutSeconds": 3,
"initialDelaySeconds": 3,
"httpGet": {
"path": "/",
"port": 8080
}
},
"livenessProbe": {
"timeoutSeconds": 3,
"initialDelaySeconds": 30,
"httpGet": {
"path": "/",
"port": 8080
}
},
"resources": {
"limits": {
"memory": "${MEMORY_LIMIT}"
}
},
"env": [
]
}
]
@ -166,29 +217,76 @@
}
],
"parameters": [
{
"name": "NAME",
"displayName": "Name",
"description": "The name assigned to all of the frontend objects defined in this template.",
"required": true,
"value": "nodejs-example"
},
{
"name": "NAMESPACE",
"displayName": "Namespace",
"description": "The OpenShift Namespace where the ImageStream resides.",
"required": true,
"value": "openshift"
},
{
"name": "NODEJS_VERSION",
"displayName": "Version of NodeJS Image",
"description": "Version of NodeJS image to be used (6, 8, or latest).",
"value": "8",
"required": true
},
{
"name": "MEMORY_LIMIT",
"displayName": "Memory Limit",
"description": "Maximum amount of memory the container can use.",
"required": true,
"value": "512Mi"
},
{
"name": "SOURCE_REPOSITORY_URL",
"description": "The URL of the repository with your application source code",
"value": "https://github.com/openshift/nodejs-ex.git"
"displayName": "Git Repository URL",
"description": "The URL of the repository with your application source code.",
"required": true,
"value": "https://github.com/sclorg/nodejs-ex.git"
},
{
"name": "SOURCE_REPOSITORY_REF",
"description": "Set this to a branch name, tag or other ref of your repository if you are not using the default branch"
"displayName": "Git Reference",
"description": "Set this to a branch name, tag or other ref of your repository if you are not using the default branch."
},
{
"name": "CONTEXT_DIR",
"description": "Set this to the relative path to your project if it is not in the root of your repository"
"displayName": "Context Directory",
"description": "Set this to the relative path to your project if it is not in the root of your repository."
},
{
"name": "APPLICATION_DOMAIN",
"description": "The exposed hostname that will route to the Node.js service",
"value": "nodejs-example.openshiftapps.com"
"displayName": "Application Hostname",
"description": "The exposed hostname that will route to the Node.js service, if left blank a value will be defaulted.",
"value": ""
},
{
"name": "GITHUB_WEBHOOK_SECRET",
"description": "A secret string used to configure the GitHub webhook",
"displayName": "GitHub Webhook Secret",
"description": "Github trigger secret. A difficult to guess string encoded as part of the webhook URL. Not encrypted.",
"generate": "expression",
"from": "[a-zA-Z0-9]{40}"
},
{
"name": "GENERIC_WEBHOOK_SECRET",
"displayName": "Generic Webhook Secret",
"description": "A secret string used to configure the Generic webhook.",
"generate": "expression",
"from": "[a-zA-Z0-9]{40}"
},
{
"name": "NPM_MIRROR",
"displayName": "Custom NPM Mirror URL",
"description": "The custom NPM mirror URL",
"value": ""
}
]
}

View File

@ -4,30 +4,31 @@
"description": "Node.js sample app for OpenShift 3",
"main": "server.js",
"dependencies": {
"express": "*"
},
"devDependencies": {
"nodemon": "*"
"chai": "^3.5.0",
"chai-http": "^2.0.1",
"ejs": "^2.4.1",
"express": "^4.13.4",
"mocha": "^2.4.5",
"mongodb": "^2.1.16",
"morgan": "^1.7.0",
"object-assign":"4.1.0"
},
"engine": {
"node": "*",
"npm": "*"
},
"scripts": {
"dev": "nodemon --ignore node_modules/ server.js",
"start": "node server.js"
"start": "node server.js",
"test": "IP=0.0.0.0 PORT=3030 mocha --timeout 5000 tests/*_test.js"
},
"repository": {
"type": "git",
"url": "http://github.com/openshift/nodejs-ex.git"
"url": "http://github.com/sclorg/nodejs-ex.git"
},
"keywords": [
"Echo"
],
"author": "Steve Speicher <sspeiche@gmail.com>",
"license": "",
"license": "CC-BY-1.0",
"bugs": {
"url": "http://github.com/openshift/nodejs-ex/issues"
"url": "http://github.com/sclorg/nodejs-ex/issues"
},
"homepage": "http://github.com/openshift/nodejs-ex"
"homepage": "http://github.com/sclorg/nodejs-ex"
}

163
server.js
View File

@ -1,54 +1,125 @@
var util = require('util');
var url = require('url');
var qs = require('querystring');
var os = require('os')
var port = process.env.PORT || process.env.port || process.env.OPENSHIFT_NODEJS_PORT || 8080;
var ip = process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
var nodeEnv = process.env.NODE_ENV || 'unknown';
// OpenShift sample Node application
var express = require('express'),
app = express(),
morgan = require('morgan');
Object.assign=require('object-assign')
var express = require('express');
var app = express();
app.engine('html', require('ejs').renderFile);
app.use(morgan('combined'))
app.use(function(req, res, next) {
var url_parts = url.parse(req.url, true);
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0',
mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL,
mongoURLLabel = "";
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
var formattedBody = qs.parse(body);
if (mongoURL == null) {
var mongoHost, mongoPort, mongoDatabase, mongoPassword, mongoUser;
// If using plane old env vars via service discovery
if (process.env.DATABASE_SERVICE_NAME) {
var mongoServiceName = process.env.DATABASE_SERVICE_NAME.toUpperCase();
mongoHost = process.env[mongoServiceName + '_SERVICE_HOST'];
mongoPort = process.env[mongoServiceName + '_SERVICE_PORT'];
mongoDatabase = process.env[mongoServiceName + '_DATABASE'];
mongoPassword = process.env[mongoServiceName + '_PASSWORD'];
mongoUser = process.env[mongoServiceName + '_USER'];
res.writeHead(200, {'Content-Type': 'text/plain'});
// If using env vars from secret from service binding
} else if (process.env.database_name) {
mongoDatabase = process.env.database_name;
mongoPassword = process.env.password;
mongoUser = process.env.username;
var mongoUriParts = process.env.uri && process.env.uri.split("//");
if (mongoUriParts.length == 2) {
mongoUriParts = mongoUriParts[1].split(":");
if (mongoUriParts && mongoUriParts.length == 2) {
mongoHost = mongoUriParts[0];
mongoPort = mongoUriParts[1];
}
}
}
res.write('Hello OpenShift World! This is a Node.js-based sample application.\n');
res.write('Host: ' + req.headers.host + '\n');
res.write('\n');
res.write('node.js Production Mode: ' + (nodeEnv == 'production' ? 'yes' : 'no') + '\n');
res.write('\n');
res.write('HTTP/' + req.httpVersion +'\n');
res.write('Request headers:\n');
res.write(util.inspect(req.headers, null) + '\n');
res.write('Request query:\n');
res.write(util.inspect(url_parts.query, null) + '\n');
res.write('Request body:\n');
res.write(util.inspect(formattedBody, null) + '\n');
res.write('\n');
res.write('Host: ' + os.hostname() + '\n');
res.write('OS Type: ' + os.type() + '\n');
res.write('OS Platform: ' + os.platform() + '\n');
res.write('OS Arch: ' + os.arch() + '\n');
res.write('OS Release: ' + os.release() + '\n');
res.write('OS Uptime: ' + os.uptime() + '\n');
res.write('OS Free memory: ' + os.freemem() / 1024 / 1024 + 'mb\n');
res.write('OS Total memory: ' + os.totalmem() / 1024 / 1024 + 'mb\n');
res.write('OS CPU count: ' + os.cpus().length + '\n');
res.write('OS CPU model: ' + os.cpus()[0].model + '\n');
res.write('OS CPU speed: ' + os.cpus()[0].speed + 'mhz\n');
res.end('\n');
});
next();
if (mongoHost && mongoPort && mongoDatabase) {
mongoURLLabel = mongoURL = 'mongodb://';
if (mongoUser && mongoPassword) {
mongoURL += mongoUser + ':' + mongoPassword + '@';
}
// Provide UI label that excludes user id and pw
mongoURLLabel += mongoHost + ':' + mongoPort + '/' + mongoDatabase;
mongoURL += mongoHost + ':' + mongoPort + '/' + mongoDatabase;
}
}
var db = null,
dbDetails = new Object();
var initDb = function(callback) {
if (mongoURL == null) return;
var mongodb = require('mongodb');
if (mongodb == null) return;
mongodb.connect(mongoURL, function(err, conn) {
if (err) {
callback(err);
return;
}
db = conn;
dbDetails.databaseName = db.databaseName;
dbDetails.url = mongoURLLabel;
dbDetails.type = 'MongoDB';
console.log('Connected to MongoDB at: %s', mongoURL);
});
};
app.get('/', function (req, res) {
// try to initialize the db on every request if it's not already
// initialized.
if (!db) {
initDb(function(err){});
}
if (db) {
var col = db.collection('counts');
// Create a document with request IP and current time of request
col.insert({ip: req.ip, date: Date.now()});
col.count(function(err, count){
if (err) {
console.log('Error running count. Message:\n'+err);
}
res.render('index.html', { pageCountMessage : count, dbInfo: dbDetails });
});
} else {
res.render('index.html', { pageCountMessage : null});
}
});
app.get('/pagecount', function (req, res) {
// try to initialize the db on every request if it's not already
// initialized.
if (!db) {
initDb(function(err){});
}
if (db) {
db.collection('counts').count(function(err, count ){
res.send('{ pageCount: ' + count + '}');
});
} else {
res.send('{ pageCount: -1 }');
}
});
// error handling
app.use(function(err, req, res, next){
console.error(err.stack);
res.status(500).send('Something bad happened!');
});
initDb(function(err){
console.log('Error connecting to Mongo. Message:\n'+err);
});
app.listen(port, ip);
console.log('Server running on ' + ip + ':' + port);
console.log('Server running on http://%s:%s', ip, port);
module.exports = app ;

31
tests/app_test.js Normal file
View File

@ -0,0 +1,31 @@
var server = require('../server'),
chai = require('chai'),
chaiHTTP = require('chai-http'),
should = chai.should();
chai.use(chaiHTTP);
reqServer = process.env.HTTP_TEST_SERVER || server
describe('Basic routes tests', function() {
it('GET to / should return 200', function(done){
chai.request(reqServer)
.get('/')
.end(function(err, res) {
res.should.have.status(200);
done();
})
})
it('GET to /pagecount should return 200', function(done){
chai.request(reqServer)
.get('/pagecount')
.end(function(err, res) {
res.should.have.status(200);
done();
})
})
})

310
views/index.html Normal file

File diff suppressed because one or more lines are too long