Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions 00_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,61 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
oc login --insecure-skip-tls-verify=true -u developer -p developer https://$OPENSHIFT_IP:8443

if [[ -f openshift-env ]]; then
source openshift-env
else
echo "openshift-env not found!!"
exit 1
fi


echo "Logging into OpenShift as ${USER}"
echo "*********************************"
oc login --insecure-skip-tls-verify=true -u ${USER} --server=https://${OPENSHIFT_ENDPOINT}:${PORTNUMBER}
sleep 2s

echo "Creating New Project called 12factor-dev"
echo "*********************************"
oc new-project 12factor-dev
sleep 2s

echo "Starting build of my12factorapp"
echo "*********************************"
oc new-build --binary --name=my12factorapp
sleep 2s

echo "Creating New Project called ci"
echo "*********************************"
oc new-project ci
sleep 2s

oc get templates -n openshift | grep ${JENKINS_TEMPLATE_NAME} || exit 1
JENKINS_POD=$(oc get templates -n openshift | grep ${JENKINS_TEMPLATE_NAME} | awk '{print $1}')
oc new-app $JENKINS_POD

echo "Login to Jenkins pod to test"
echo "*********************************"
oc get routes

echo "Creating pipeline"
echo "*********************************"
oc create -f pipeline.yml -n ci
sleep 2s

echo "Confiuring Jenkins"
echo "*********************************"
#Setup Jenkins credentials
oc login -u system:admin
echo "Giving Jenkins SA a cluster-admin permission"
oc adm policy add-cluster-role-to-user cluster-admin system:serviceaccount:ci:jenkins -n ci
oc login --insecure-skip-tls-verify=true -u developer -p developer https://$OPENSHIFT_IP:8443
echo "Project setup"
if [[ ${USING_MINISHIFT} != "FALSE" ]]; then
oc login -u system:admin
echo "Giving Jenkins SA cluster-admin permission"
oc adm policy add-cluster-role-to-user cluster-admin system:serviceaccount:ci:jenkins -n ci
sleep 2s

echo "Logging into OpenShift as ${USER}"
echo "*********************************"
oc login --insecure-skip-tls-verify=true -u ${USER} --server=https://${OPENSHIFT_ENDPOINT}:${PORTNUMBER}
fi

echo "Switch back to"
oc project 12factor-dev
File renamed without changes.
File renamed without changes.
12 changes: 10 additions & 2 deletions 05_run.sh → 03_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if [[ -f openshift-env ]]; then
source openshift-env
else
echo "openshift-env not found!!"
exit 1
fi

oc new-app my12factorapp
oc expose svc/my12factorapp --hostname 12factorappdemo.$OPENSHIFT_IP.nip.io
oc expose svc/my12factorapp
oc set probe dc/my12factorapp --readiness --get-url=http://:8080/api/health
echo "Application executed. Check the URL: http://12factorappdemo.$OPENSHIFT_IP.nip.io/api/hello/Rafael"
echo "Application executed. Check the URL: http://$(oc get route | grep my12factorapp| awk '{print $2}')/api/hello/developer"
14 changes: 13 additions & 1 deletion 03_config.sh → 04_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,17 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

###
##Change to config map
###
if [[ -f openshift-env ]]; then
source openshift-env
else
echo "openshift-env not found!!"
exit 1
fi

echo "Setting Enviornment using Enviornment variable"
oc set env dc/my12factorapp GREETING="Hi {name}! - My Configuration has changed"
echo "Configuration updated. Please check again http://12factorappdemo.$OPENSHIFT_IP.nip.io/api/hello/Rafael"
echo "Configuration updated. Please check again http://$(oc get route | grep my12factorapp| awk '{print $2}')/api/hello/developer"
2 changes: 1 addition & 1 deletion 04_backing.sh → 05_backing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ echo "Deploying a database"
oc new-app --name mysql -e MYSQL_USER=myuser -e MYSQL_PASSWORD=mypassword -e MYSQL_DATABASE=mydatabase openshift/mysql-56-centos7
echo "Attach it to the app"
oc set env dc/my12factorapp host=mysql username=myuser password=mypassword database=mydatabase GREETING-
echo "Open the URL: http://12factorappdemo.$OPENSHIFT_IP.nip.io/api/db"
echo "Open the URL: http://$(oc get route | grep my12factorapp| awk '{print $2}')/api/db"
6 changes: 3 additions & 3 deletions 04_populatedb.sh → 06_populatedb.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/bin/bash
# JBoss, Home of Professional Open Source
# Copyright 2016, Red Hat, Inc. and/or its affiliates, and individual
# contributors by the @authors tag. See the copyright.txt in the
# contributors by the @authors tag. See the copyright.txt in the
# distribution for a full listing of individual contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
oc exec -it `oc get pods -l app=mysql |grep mysql| awk '{ print $1 }'` -- bash -c "mysql -u myuser -pmypassword -h 127.0.0.1 mydatabase -te \"CREATE TABLE mytable (name varchar(50)); INSERT INTO mytable VALUES ('Rafael'); INSERT INTO mytable VALUES ('Benevides');\""
oc exec -it `oc get pods -l app=mysql | grep mysql | awk '{ print $1 }'` -- bash -c "mysql -u myuser -pmypassword -h 127.0.0.1 mydatabase -te \"CREATE TABLE mytable (name varchar(50)); INSERT INTO mytable VALUES ('Mark'); INSERT INTO mytable VALUES ('Mary');\""
echo "Database populated"
2 changes: 1 addition & 1 deletion 07_port-binding.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
oc scale dc/my12factorapp --replicas=1
oc patch svc/mysql -p '{"spec":{"ports":[{"name": "5000-tcp", "port": 5000, "targetPort": 3306}]}}'
oc set env dc/my12factorapp port=5000
open http://12factorappdemo.$OPENSHIFT_IP.nip.io/api/db
echo "Open the URL: http://$(oc get route | grep my12factorapp| awk '{print $2}')/api/db"
echo "Database port 3306 was bound to port 5000"
7 changes: 3 additions & 4 deletions 08_concurrency.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#!/bin/bash
# JBoss, Home of Professional Open Source
# Copyright 2016, Red Hat, Inc. and/or its affiliates, and individual
# contributors by the @authors tag. See the copyright.txt in the
# contributors by the @authors tag. See the copyright.txt in the
# distribution for a full listing of individual contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
oc scale dc/my12factorapp --replicas=3
oc scale dc/my12factorapp --replicas=3
echo "App Scaled to three instances"
while true; do curl -m 1 -s http://12factorappdemo.$OPENSHIFT_IP.nip.io/api/hello/Rafael ; echo; sleep 1; done
while true; do curl -m 1 -s http://$(oc get route | grep my12factorapp| awk '{print $2}')/api/hello/developer ; echo; sleep 1; done
6 changes: 3 additions & 3 deletions 09_disposability.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/bin/bash
# JBoss, Home of Professional Open Source
# Copyright 2016, Red Hat, Inc. and/or its affiliates, and individual
# contributors by the @authors tag. See the copyright.txt in the
# contributors by the @authors tag. See the copyright.txt in the
# distribution for a full listing of individual contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
echo "Destroying two processes"
oc delete pod `oc get pods -l app=my12factorapp --no-headers=true| grep my12factorapp -m 2| awk '{ print $1 }'`
while true; do curl http://12factorappdemo.$OPENSHIFT_IP.nip.io/api/hello/Rafael ; echo; sleep 1; done
while true; do curl http://$(oc get route | grep my12factorapp| awk '{print $2}')/api/hello/developer ; echo; sleep 1; done
9 changes: 4 additions & 5 deletions 10_deploy-staging.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/bin/bash
# JBoss, Home of Professional Open Source
# Copyright 2016, Red Hat, Inc. and/or its affiliates, and individual
# contributors by the @authors tag. See the copyright.txt in the
# contributors by the @authors tag. See the copyright.txt in the
# distribution for a full listing of individual contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Expand All @@ -18,8 +18,7 @@ oc new-project 12factor-staging
oc policy add-role-to-user system:image-puller system:serviceaccount:12factor-staging:default -n 12factor-dev
oc tag 12factor-dev/my12factorapp:latest 12factor-staging/my12factorapp:latest
oc new-app my12factorapp
oc expose svc/my12factorapp --hostname 12factorappdemo-staging.$OPENSHIFT_IP.nip.io
oc expose svc/my12factorapp
oc set probe dc/my12factorapp --readiness --get-url=http://:8080/api/health
open http://12factorappdemo-staging.$OPENSHIFT_IP.nip.io/api/hello/Rafael
open http://$(oc get route | grep my12factorapp| awk '{print $2}')/api/hello/developer
oc project 12factor-dev

87 changes: 83 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,96 @@
# Helloworld

## Requirements on OpenShift cluster
Ensure Jenkins has the appropriate permissions to project if you are not cluster-admin
After the Jenkins pod has deployed under the 00_setup.sh
```
oc new-project ci
oc adm policy add-cluster-role-to-user cluster-admin system:serviceaccount:ci:jenkins -n ci
```

The slides of this presentation are available at <http://bit.ly/12factors-app>

To use the scripts, specify the OPENSHIFT_IP environment variable.
To use the scripts, Create and Source the following file.

## Sample OpenShift Endpoint
You will need to edit the file called openshift-env. It currently looks like below. Fill in the appropriate information. Examples of OCP 3.x and 4.x can be found below.
```
$ cat openshift-env
export OPENSHIFT_ENDPOINT=""
export PORTNUMBER=""
export USER=""
export USING_MINISHIFT=FALSE
export JENKINS_TEMPLATE_NAME="jenkins-ephemeral"
```

Example: export OPENSHIFT_IP=35.185.41.87;
## Examples:
**Sample OpenShift 4.x Endpoint**
```
$ cat openshift-env
export OPENSHIFT_ENDPOINT="api.example.com"
export PORTNUMBER="6443"
export USER="developer"
export USING_MINISHIFT=FALSE
export JENKINS_TEMPLATE_NAME="jenkins-ephemeral"
```

Build and Deploy helloworld-service locally
**Sample OpenShift 3.11.x Endpoint**
```
$ cat openshift-env
export OPENSHIFT_ENDPOINT="lb.example.com"
export PORTNUMBER="8443"
export USER="developer"
export USING_MINISHIFT=FALSE
export JENKINS_TEMPLATE_NAME="jenkins-ephemeral"
```

**Build and Deploy helloworld-service locally**
------------------------------------------

1. Open a command prompt and navigate to the root directory of this microservice.
2. Type this command to build and execute the service:

mvn clean compile exec:java

3. The application will be running at the following URL: <http://localhost:8080/api/hello/AnyName>
3. The application will be running at the following URL: <http://localhost:8080/api/hello/AnyName>


### The following will show you how to deploy a 12factor-app using OpenShift.
**Codebase** – use version control, one codebase tracked in revision control for many deployments. For this lab we will use the repo below as the codebase.
````
https://github.com/tosin2013/12factor-app.git
````

**Dependencies** – use a package manager and don’t commit dependencies in the codebase repository. OpenShift allows for native language dependency management systems to be used in this lab we will be using maven. To Build the Java Application.
Exercises: 01_build.sh

**Config** – store the config in Environment Variable, if you have to repackage your application, you’re doing it wrong. OpenShift Allows this using features called ConfigMaps.
Exercises: 04_config.sh
[Using ConfigMaps](https://github.com/tosin2013/openshift-demos/blob/master/configmaps.md)

**Backing Services** – a deploy of the twelve-factor app should be able to swap out a local MySQL database with one managed by a third party (such as Amazon RDS ) without any changes to the app’s code. OpenShift allows for external databases to be used with your service.
Exercises: 05_backing.sh, 06_populatedb.sh

**Build, Release, Run** – the twelve-factor app uses strict separation between the build, release, and run stages. Every release should always have a unique release ID and releases should allow rollback.
Exercises: 01_build.sh, 02_release.sh, 03_run.sh

**Processes** – execute the app as one or more stateless processes, the Twelve-factor processes are stateless and share-nothing.
Exercises: 03_run.sh

**Port Binding** – export services via port binding, The twelve-factor app is completely self-contained.
Exercises: 07_port-binding.sh

**Concurrency** – scale out via the process model. Each process should be individually scaled, with Factor 6 (Stateless), it is easy to scale the services.
Exercises: 08_concurrency.sh

**Disposability** – maximize robustness with fast startup and graceful shutdown, we can achieve this with containers.
Exercises: 09_disposability.sh

**Dev/Prod Parity** – Keep development, staging, and production as similar as possible, the twelve-factor app is designed for continuous deployment by keeping the gap between development and production small.
Exercises: 10_deploy-staging.sh

**Logs** – treat logs as event streams, a twelve-factor app never concerns itself with routing or storage of its output stream.
Exercises: 11_logs.sh

**Admin Processes** – run admin/management tasks as one-off processes.
Exercises: 12_admin.sh
Loading