Skip to content

Commit

Permalink
Merge pull request #27 from IBM-Swift/script
Browse files Browse the repository at this point in the history
Finished helper script
  • Loading branch information
rfdickerson authored Jan 16, 2017
2 parents 30ae34a + 3af97b2 commit b84ff7e
Show file tree
Hide file tree
Showing 2 changed files with 238 additions and 34 deletions.
72 changes: 38 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,73 +74,77 @@ You can use this button to deploy TodoList to your Bluemix account, all from the

### Deploying Docker to IBM Bluemix Container

1. Download and install the CloudFoundry CLI [here](https://github.com/cloudfoundry/cli/releases).
For the following instructions, we will be using our [Bash Script](config.sh) located in the root directory.
You can attempt to complete the whole process with the following command:

2. Install the IBM Containers plugin for CF:
```
./config.sh all <imageName>
```

[Directions are here](https://console.ng.bluemix.net/docs/containers/container_cli_cfic_install.html) for different operating systems.

Or, you can follow the step-by-step instructions below.

1. Install the Cloud Foundry CLI tool and the IBM Containers plugin for CF with the following

```
cf install-plugin https://static-ice.ng.bluemix.net/ibm-containers-mac
cf api https://api.ng.bluemix.net
cf login
cf ic login
./config.sh install-tools
```
Note the namespace you see:

2. Ensure you are logged in with

```
Authenticating with the IBM Containers registry host registry.ng.bluemix.net...
OK
You are authenticated with the IBM Containers registry.
Your organization's private Bluemix registry: registry.ng.bluemix.net/<your namespace>
./config.sh login
```

5. Build a Docker image
3. Build and run a Docker container with the following

```
docker build -t todolist-couchdb .
./config.sh build <imageName>
```

6. Tag the Docker image:
To test out created Docker image, use

```
docker tag todolist-couchdb registry.ng.bluemix.net/<your namespace>/todolist-couchdb
./config.sh run <imageName>
./config.sh stop <imageName>
```

7. Push the Docker image:
4. Push created Docker container to Bluemix

```
docker push registry.ng.bluemix.net/<your namespace>/todolist-couchdb
./config.sh push-docker <imageName>
```
8. Create Cloudant service:

5. Create a bridge CF application to later bind to your container

```
cf create-service cloudantNoSQLDB Lite TodoListCloudantDatabase
./config.sh create-bridge
```

8. Create a new local directory with an `empty.txt` file, then navigate into that directory.

8. Create a bridge application:
6. Create the Cloudant service and bind to your bridge CF application.

```
cf push containerbridge -p . -i 1 -d mybluemix.net -k 1M -m 64M --no-hostname --no-manifest --no-route --no-start
./config.sh create-db
```

8. Bind service to bridge app:
7. Create a Bluemix container group where your app will live, binding it to your bridge CF application in the process

```
cf bind-service containerbridge TodoListCloudantDatabase
./config.sh deploy <imageName>
```

Afterwards, you can ensure Cloudant was bound correctly by viewing all credentials for your group

```
cf ic group inspect <imageName>
```

8. Create the Docker group:
8. Optionally, if you want to populate your database with some sample data, run the following command with your image name:

```
cf ic group create --anti --auto --name todolist-couchdb -n <hostname you want> -d mybluemix.net -p 8090 -m 128 -e "CCS_BIND_APP=containerbridge" registry.ng.bluemix.net/<your namespace>/todolist-couchdb
./config.sh populate-db <imageName>
```

At this point, your app should be deployed! Accessing your apps route should return your todos, which should be `[]` if you did not populate the database.

### Manually

Bluemix is a hosting platform from IBM that makes it easy to deploy your app to the cloud. Bluemix also provides various popular databases. [Cloudant](https://cloudant.com/) is an offering that is compatible with the CouchDB database, but provides additional features. You can use Cloudant with your deployed TodoList-CouchDB application.
Expand Down
200 changes: 200 additions & 0 deletions config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
#!/bin/bash
#------------------------------------------------------------
# Script: config.sh
# Author: Swift@IBM
# -----------------------------------------------------------

VERSION="1.0"
BUILD_DIR=".build-linux"
BRIDGE_APP_NAME="containerbridge"
DATABASE_NAME="TodoListCloudantDatabase"
REGISTRY_URL="registry.ng.bluemix.net"
DATABASE_TYPE="cloudantNoSQLDB"
DATABASE_LEVEL="Lite"

function help {
cat <<-!!EOF
Usage: $CMD [ build | run | push-docker ] [arguments...]
Where:
install-tools Installs necessary tools for config, like Cloud Foundry CLI
login Logs into Bluemix and Container APIs
build <imageName> Builds Docker container from Dockerfile
run <imageName> Runs Docker container, ensuring it was built properly
stop <imageName> Stops Docker container, if running
push-docker <imageName> Tags and pushes Docker container to Bluemix
create-bridge Creates empty bridge application
create-db Creates database service and binds to bridge
deploy <imageName> Binds everything together (app, db, container) through container group
populate-db <imageName> Populates database with initial data
delete <imageName> Delete the group container and deletes created service if possible
all <imageName> Combines all necessary commands to deploy an app to Bluemix in a Docker container.
!!EOF
}

install-tools () {
brew tap cloudfoundry/tap
brew install cf-cli
cf install-plugin https://static-ice.ng.bluemix.net/ibm-containers-mac
}

login () {
echo "Setting api and login tools."
cf api https://api.ng.bluemix.net
cf login
cf ic login
}

buildDocker () {
if [ -z "$1" ]
then
echo "Error: build failed, docker name not provided."
return
fi
docker build -t $1 --force-rm .
}

runDocker () {
if [ -z "$1" ]
then
echo "Error: run failed, docker name not provided."
return
fi
docker run --name $1 -d -p 8090:8090 $1
}

stopDocker () {
if [ -z "$1" ]
then
echo "Error: clean failed, docker name not provided."
return
fi
docker rm -fv $1 || true
}

pushDocker () {
if [ -z "$1" ] || [ -z $REGISTRY_URL ]
then
echo "Error: Pushing Docker container to Bluemix failed, missing variables."
return
fi
echo "Tagging and pushing docker container..."
namespace=$(cf ic namespace get)
docker tag $1 $REGISTRY_URL/$namespace/$1
docker push $REGISTRY_URL/$namespace/$1
}

createBridge () {
if [ -z $BRIDGE_APP_NAME ]
then
echo "Error: Creating bridge application failed, missing BRIDGE_APP_NAME."
return
fi
mkdir $BRIDGE_APP_NAME
cd $BRIDGE_APP_NAME
touch empty.txt
cf push $BRIDGE_APP_NAME -p . -i 1 -d mybluemix.net -k 1M -m 64M --no-hostname --no-manifest --no-route --no-start
rm empty.txt
cd ..
rm -rf $BRIDGE_APP_NAME
}

createDatabase () {
if [ -z $DATABASE_TYPE ] || [ -z $DATABASE_LEVEL ] || [ -z $DATABASE_NAME ] || [ -z $BRIDGE_APP_NAME ]
then
echo "Error: Creating bridge application failed, missing variables."
return
fi
cf create-service $DATABASE_TYPE $DATABASE_LEVEL $DATABASE_NAME
cf bind-service $BRIDGE_APP_NAME $DATABASE_NAME
cf restage $BRIDGE_APP_NAME
}

deployContainer () {
if [ -z "$1" ] || [ -z $REGISTRY_URL ] || [ -z $BRIDGE_APP_NAME ]
then
echo "Error: Could not deploy container to Bluemix, missing variables."
return
fi

namespace=$(cf ic namespace get)
hostname=$1"-app"

cf ic group create \
--anti \
--auto \
-m 128 \
--name $1 \
-p 8090 \
-n $hostname \
-e "CCS_BIND_APP="$BRIDGE_APP_NAME \
-d mybluemix.net $REGISTRY_URL/$namespace/$1
}

populateDB () {
if [ -z "$1" ]
then
echo "Error: Could not populate db with sample data, missing imageName."
return
fi

appURL="https://"$1"-app.mybluemix.net"
eval $(curl -X POST -H "Content-Type: application/json" -d '{ "title": "Wash the car", "order": 0, "completed": false }' $appURL)
eval $(curl -X POST -H "Content-Type: application/json" -d '{ "title": "Walk the dog", "order": 2, "completed": true }' $appURL)
eval $(curl -X POST -H "Content-Type: application/json" -d '{ "title": "Clean the gutters", "order": 1, "completed": false }' $appURL)
}

delete () {
if [ -z "$1" ] || [ -z $DATABASE_NAME ] || [ -z $BRIDGE_APP_NAME ]
then
echo "Error: Could not delete container group and service, missing variables."
return
fi

cf ic group rm $1
cf unbind-service $BRIDGE_APP_NAME $DATABASE_NAME
cf delete-service $DATABASE_NAME
}

all () {
if [ -z "$1" ]
then
echo "Error: Could not complete entire deployment process, missing variables."
return
fi

login
buildDocker $1
pushDocker $1
createBridge
createDatabase
deployContainer $1
}

#----------------------------------------------------------
# MAIN
# ---------------------------------------------------------

ACTION="$1"

[[ -z $ACTION ]] && help && exit 0

# Initialize the SwiftEnv project environment
eval "$(swiftenv init -)"


case $ACTION in
"install-tools") install-tools;;
"login") login;;
"build") buildDocker "$2";;
"run") runDocker "$2";;
"stop") stopDocker "$2";;
"push-docker") pushDocker "$2";;
"create-bridge") createBridge;;
"create-db") createDatabase;;
"deploy") deployContainer "$2";;
"populate-db") populateDB "$2";;
"delete") delete "$2";;
"all") all "$2";;
*) help;;
esac

0 comments on commit b84ff7e

Please sign in to comment.