Follow the steps in this page to create your own CPU To IBM Event Streams Edge Service.
- If you have not already done so, complete the steps in these sections:
- Preconditions for Using the CPU To IBM Event Streams Example Edge Service
- Using the CPU To IBM Event Streams Example Edge Service with Deployment Pattern
- If you are using macOS as your development host, configure Docker to store credentials in
~/.docker
:
- Open the Docker Preferences dialog
- Uncheck Securely store Docker logins in macOS keychain
- If you do not already have a docker ID, obtain one at https://hub.docker.com/ . Log in to Docker Hub using your Docker Hub ID:
export DOCKER_HUB_ID="<dockerhubid>"
echo "<dockerhubpassword>" | docker login -u $DOCKER_HUB_ID --password-stdin
Output example:
WARNING! Your password will be stored unencrypted in /home/pi/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
- Create a cryptographic signing key pair. This enables you to sign services when publishing them to the exchange. This step only needs to be done once.
hzn key create "<x509-org>" "<x509-cn>"
where <x509-org>
is your company name, and <x509-cn>
is typically set to your email address.
- Install
git
andjq
:
On Linux:
sudo apt install -y git jq
On macOS:
brew install git jq
- Clone this git repo:
cd ~ # or wherever you want
git clone [email protected]:open-horizon/examples.git
cd examples/
- Check your Horizon CLI version:
hzn version
- Starting with Horizon version
v2.29.0-595
you cancheckout
to a version of the example services that directly corresponds to your Horizon CLI version with these commands:
export EXAMPLES_REPO_TAG="v$(hzn version 2>/dev/null | grep 'Horizon CLI' | awk '{print $4}')"
git checkout tags/$EXAMPLES_REPO_TAG -b $EXAMPLES_REPO_TAG
Note: if you are using an older version of the hzn
CLI you can checkout to the branch that corresponds to the major version you are using. For example Horizon CLI version: 2.27.0-173
can run git checkout v2.27
- Copy the
cpu2evtstreams
dir to where you will start development of your new service:
cp -a edge/evtstreams/cpu2evtstreams ~/myservice # or wherever
cd ~/myservice
- Set the values in
horizon/hzn.json
to your own values. After editinghorizon/hzn.json
, set the variables in your environment:
eval $(hzn util configconv -f horizon/hzn.json)
-
Edit
service.sh
however you want.- Note: this service is a shell script simply for brevity, but you can write your service in any language.
-
Build the cpu2evtstreams docker image:
make
- Test the service by having Horizon start it locally:
hzn dev service start -S
- Check that the container is running:
sudo docker ps
- See the cpu2evtstreams service output:
hzn dev service log -f $SERVICE_NAME
- See the environment variables Horizon passes into your service container:
docker inspect $(docker ps -q --filter name=$SERVICE_NAME) | jq '.[0].Config.Env'
- Stop the service:
hzn dev service stop
- Have Horizon push your docker image to your registry and publish your service in the Horizon Exchange and see it there:
hzn exchange service publish -f horizon/service.definition.json
hzn exchange service list
- Set the required environment variables:
eval $(hzn util configconv -f horizon/hzn.json)
export ARCH=$(hzn architecture)
- Publish and view your service policy in the Horizon Exchange:
hzn exchange service addpolicy -f policy/service.policy.json ${HZN_ORG_ID}/${SERVICE_NAME}_${SERVICE_VERSION}_${ARCH}
hzn exchange service listpolicy ${HZN_ORG_ID}/${SERVICE_NAME}_${SERVICE_VERSION}_${ARCH}
- Publish and view your deployment policy in the Horizon Exchange:
hzn exchange deployment addpolicy -f policy/deployment.policy.json policy-${HZN_ORG_ID}/${SERVICE_NAME}_${SERVICE_VERSION}
hzn exchange deployment listpolicy policy-${HZN_ORG_ID}/${SERVICE_NAME}_${SERVICE_VERSION}
- Register your edge device with the node policy:
hzn register --policy policy/node.policy.json
- The edge device will make an agreement with one of the Horizon agreement bots (this typically takes about 15 seconds). Repeatedly query the agreements of this device until the
agreement_finalized_time
andagreement_execution_start_time
fields are filled in:
hzn agreement list
- After the agreement is made, list the docker container edge service that has been started as a result:
sudo docker ps
- See the cpu2evtstreams service output:
hzn service log -f ibm.cpu2evtstreams
- Unregister your edge device (which will also stop the myhelloworld service):
hzn unregister -f
- Publish and view your edge node deployment pattern in the Horizon Exchange:
hzn exchange pattern publish -f horizon/pattern.json
hzn exchange pattern list
- Register your edge node with Horizon to use your deployment pattern (substitute
<service-name>
for theSERVICE_NAME
you specified inhorizon/hzn.json
):
hzn register -p pattern-<service-name>-$(hzn architecture) -f horizon/userinput.json
- The edge device will make an agreement with one of the Horizon agreement bots (this typically takes about 15 seconds). Repeatedly query the agreements of this device until the
agreement_finalized_time
andagreement_execution_start_time
fields are filled in:
hzn agreement list
- Once the agreement is made, list the docker container edge service that has been started as a result:
sudo docker ps
- On any machine, subscribe to the Event Streams topic to see the json data that cpu2evtstreams is sending:
kafkacat -C -q -o end -f "%t/%p/%o/%k: %s\n" -b $EVTSTREAMS_BROKER_URL -X api.version.request=true -X security.protocol=sasl_ssl -X sasl.mechanisms=PLAIN -X sasl.username=token -X sasl.password=$EVTSTREAMS_API_KEY -X ssl.ca.location=$EVTSTREAMS_CERT_FILE -t cpu2evtstreams
- See the cpu2evtstreams service output:
hzn service log -f ibm.cpu2evtstreams
- Unregister your edge node, stopping the cpu2evtstreams service:
hzn unregister -f