diff --git a/create-project.sh b/create-project.sh new file mode 100755 index 0000000000000000000000000000000000000000..4ac112e5379f8ad7f21a733f7b5b5b3bbdd0ce01 --- /dev/null +++ b/create-project.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +DIR="$(cd "$(dirname "$0")" && pwd)" +PROJECT=${PROJECT:=minishift-demo} +APP_NAME=${APP_NAME:=hello-server} + +echo "Creating demo application..." + +echo "==========================" +echo "(1/4) Applying extra permissions..." + +# This adds anyuid and hostaccess security context constraints to default service account +# This is acceptable for a dev environment only +oc login -u system:admin +oc adm policy add-scc-to-user anyuid system:serviceaccount:$PROJECT:default +oc adm policy add-scc-to-user hostaccess system:serviceaccount:$PROJECT:default +echo "developer" | oc login -u developer +echo "==========================" +echo "(2/4) Creating new project..." + +oc new-project $PROJECT --display-name="Demo Project" --description="A demo project designed to help you start developing with Minishift" > /dev/null + +echo "==========================" +echo "(3/4) Deploying hello-server..." + +oc process -f "$DIR"/minishift-demo.yaml \ + -p NAMESPACE=$PROJECT \ + -p NAME=$APP_NAME \ + -p PROBE=/healthz \ + -p SERVER_PORT=8080 \ + -p LOG_LEVEL=debug \ + -p APP_VOLUME="${DIR}" | oc create -f - + + +echo "(4/4) Building hello-server Image" +oc start-build $APP_NAME --from-dir $DIR --follow diff --git a/index.js b/index.js index c9883f0df6aab46cdb7b076cec31771b15e637e5..b1baad4e1a325dd6b10688ab248a5abf3db7a352 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,7 @@ const pino = require('pino')({ const server = restify.createServer({ name: 'hello' }) server.get('/', function (req, res, next) { - res.json({ msg: 'Hello World' }) + res.json({ msg: 'Hello Universe, kjfheskfhsekfhe' }) return next() }) @@ -23,21 +23,21 @@ server.get('/healthz', function (req, res, next) { return next() }) -server.get('/version', function (req, res, next){ +server.get('/version', function (req, res, next) { res.json({ version: '1.1' }) - + return next() }) -server.get('/time', function(req, res, next) { +server.get('/time', function (req, res, next) { var t = new Date() db.put('time', t, function (err) { if (err) { res.status(500) - res.json({msg: 'An error occurred'}) + res.json({ msg: 'An error occurred' }) return next(err) } - res.json({msg: t}) + res.json({ msg: t }) return next() }) }) diff --git a/minishift-demo.yaml b/minishift-demo.yaml new file mode 100644 index 0000000000000000000000000000000000000000..823f81ca8ab5517e971558be1b3220e3da483694 --- /dev/null +++ b/minishift-demo.yaml @@ -0,0 +1,180 @@ +--- +apiVersion: v1 +kind: Template +labels: + template: minishift-demo +parameters: +- description: The name assigned to all of the frontend objects defined in this template. + displayName: Name + name: NAME + required: true + value: minishift-demo + +- description: The OpenShift Namespace where the ImageStream resides. + displayName: Namespace + name: NAMESPACE + required: true + value: minishift-demo + +- description: Maximum amount of memory the container can use. + displayName: Memory Limit + name: MEMORY_LIMIT + required: true + value: 128Mi + +- description: Set this to the relative path to your project if it is not in the root of your repository. + displayName: Context Directory + name: CONTEXT_DIR + +- description: The exposed hostname that will route to the Node.js service, if left blank a value will be defaulted. + displayName: Application Hostname + name: APPLICATION_DOMAIN + value: '' + +- description: The path of the readiness probe and liveness probe + displayName: Path Probe + name: PROBE + value: '' + +- description: The local path where the application is stored + displayName: App volume + name: APP_VOLUME + required: true + value: '' + +- description: The port the server will run on inside the container + displayName: Server Port + name: SERVER_PORT + value: '8080' + +- description: Log level applied to the process + displayName: Log Level + name: LOG_LEVEL + value: 'debug' + +objects: +- apiVersion: v1 + kind: BuildConfig + metadata: + annotations: + description: Defines how to build the application + name: "${NAME}" + spec: + runPolicy: Serial + output: + to: + kind: ImageStreamTag + name: "${NAME}:latest" + source: + type: Binary + strategy: + type: Docker + triggers: + - type: ConfigChange + +- apiVersion: v1 + kind: ImageStream + metadata: + annotations: + description: Keeps track of changes in the application image + name: "${NAME}" + +- apiVersion: v1 + kind: Service + metadata: + annotations: + description: Exposes and load balances the application pods + name: "${NAME}" + spec: + ports: + - name: http + port: 8080 + targetPort: 8080 + selector: + name: "${NAME}" + +- apiVersion: v1 + kind: Route + metadata: + name: "${NAME}" + spec: + host: "${APPLICATION_DOMAIN}" + to: + kind: Service + name: "${NAME}" + +- apiVersion: v1 + kind: DeploymentConfig + metadata: + annotations: + description: Defines how to deploy the application server + name: "${NAME}" + spec: + replicas: 1 + selector: + name: "${NAME}" + strategy: + type: Recreate + template: + metadata: + labels: + name: "${NAME}" + name: "${NAME}" + spec: + containers: + - name: ${NAME} + command: + - npm + - run + - start:dev + env: + - name: "SERVER_PORT" + value: "${SERVER_PORT}" + - name: "LOG_LEVEL" + value: "${LOG_LEVEL}" + image: "${NAMESPACE}/${NAME}:latest" + livenessProbe: + httpGet: + path: "${PROBE}" + port: 8080 + initialDelaySeconds: 30 + timeoutSeconds: 3 + volumeMounts: + - mountPath: /usr/src/app/bin + name: app-volume + - mountPath: /usr/src/app/bin/testdb + name: leveldb-volume + - mountPath: /usr/src/app/bin/node_modules + name: empty-nodemodules + - mountPath: /.config + name: nodemon-volume + ports: + - containerPort: 8080 + readinessProbe: + httpGet: + path: "${PROBE}" + port: 8080 + initialDelaySeconds: 3 + timeoutSeconds: 3 + resources: + limits: + memory: "${MEMORY_LIMIT}" + requests: + memory: 50Mi + volumes: + - name: app-volume + hostPath: + path: "${APP_VOLUME}" + - name: nodemon-volume + - name: empty-nodemodules + - name: leveldb-volume + triggers: + - type: ConfigChange + - type: ImageChange + imageChangeParams: + automatic: true + containerNames: + - ${NAME} + from: + kind: ImageStreamTag + name: "${NAME}:latest"