Develop & Publish Sample Edge Application

Sample Edge Application Introduction

Network Architecture

In below is the step by step guidance to develop and publish the Sample Edge Application. After completing the below steps, you will be able to:

Prerequisites

Develop the Sample Edge Application

  1. Log in to your edge node with the user id ibm-workshop created during IEAM agent deployment. Pull the sample Github code. Create the horizon private signing key.

    Note: For edge node only Linux operating system is supported.

     cd /home/ibm-workshop/workspace
     git clone https://github.com/gargpriyank/edge-app-skeleton.git
     mv edge-app-skeleton sample-edge-app
     cd sample-edge-app
     hzn key create IBM <your_email_id>
    
  2. Look at the below sample code. HZN_ORG_ID is the IEAM organization id where the edge service will be published. DOCKER_IMAGE_BASE is your docker repo name. SERVICE_NAME is the edge service name with which the service will be published. SERVICE_VERSION is the version of edge service and the docker image. Copy and paste this code in edge-app/horizon/hzn.json file and update the values as per your requirement.

     {
       "HZN_ORG_ID": "$HZN_ORG_ID",
       "MetadataVars": {
         "DOCKER_IMAGE_BASE": "sandboxedgeworkshop/edge-app",
         "SERVICE_NAME": "edge-app",
         "SERVICE_VERSION": "0.0.1"
       }
     }
    
  3. Look at the below sample code. ARCH is the hardware architecture of the edge node where the edge service will be deployed. requiredServices section shows the required services that should start first to run this service. userInput section lists all the input variables required by this service. deployment section lists all the services that will be published and deployed. Copy and paste this code in edge-app/horizon/service.definition.json file and update the values as per your requirement.

     {
       "org": "$HZN_ORG_ID",
       "label": "$SERVICE_NAME for $ARCH",
       "description": "Edge Application service",
       "public": true,
       "documentation": "",
       "url": "$SERVICE_NAME",
       "version": "$SERVICE_VERSION",
       "arch": "$ARCH",
       "sharable": "singleton",
       "requiredServices": [
         {
           "url": "esf-ieam",
           "org": "$HZN_ORG_ID",
           "versionRange": "[0.0.0,INFINITY)",
           "arch": "$ARCH"
         }
       ],
       "userInput": [
         {
           "name": "EVENTSTREAMS_TOPIC_NAME",
           "label": "The Topic name of the instance of IBM Event Streams",
           "type": "string",
           "defaultValue": ""
         },
         {
           "name": "EVENTSTREAMS_BROKER_URL",
           "label": "The Broker URL of the instance of IBM Event Streams",
           "type": "string",
           "defaultValue": ""
         },
         {
           "name": "EVENTSTREAMS_API_KEY",
           "label": "The API Key of the instance of IBM Event Streams",
           "type": "string",
           "defaultValue": ""
         }
       ],
       "deployment": {
         "services": {
           "$SERVICE_NAME": {
             "image": "${DOCKER_IMAGE_BASE}:$SERVICE_VERSION",
             "network": "edge_workshop",
             "privileged": true
           }
         }
       }
     }
    
  4. Look at the below sample code. constraints section lists all the deployment constraints that should match with the node policy properties while registering. Copy and paste this code in edge-app/horizon/deployment.policy.json file and update the values as per your requirement.

     {
       "label": "$SERVICE_NAME Deployment Policy",
       "description": "A Horizon Deployment Policy example to run edge-app",
       "service": {
         "name": "$SERVICE_NAME",
         "org": "$HZN_ORG_ID",
         "arch": "$ARCH",
         "serviceVersions": [
           {
             "version": "$SERVICE_VERSION",
             "priority":{}
           }
         ]
       },
       "properties": [
       ],
       "constraints": [
         "deploy-edge-app == true"
       ],
       "userInput": [
         {
           "serviceOrgid": "$HZN_ORG_ID",
           "serviceUrl": "$SERVICE_NAME",
           "serviceVersionRange": "[0.0.0,INFINITY)",
           "inputs": [
             {
               "name": "EVENTSTREAMS_TOPIC_NAME",
               "value": "$EVENTSTREAMS_TOPIC_NAME"
             },
             {
               "name": "EVENTSTREAMS_BROKER_URL",
               "value": "$EVENTSTREAMS_BROKER_URL"
             },
             {
               "name": "EVENTSTREAMS_API_KEY",
               "value": "$EVENTSTREAMS_API_KEY"
             }
           ]
         }
       ]
     }
    
  5. Look at the below sample code. constraints section lists all the service policy constraints that uses the Open Horizon inbuild variable openhorizon.memory that should match with the node policy properties while registering. Copy and paste this code in edge-app/horizon/service.policy.json file and update the values as per your requirement.

     {
       "properties": [],
       "constraints": [
         "openhorizon.memory >= 100"
       ]
     }
    
  6. Look at the content in below. Copy and paste it into edge-app/Makefile to build and publish the edge-app service.

     -include horizon/.hzn.json.tmp.mk
     export ARCH ?= $(shell hzn architecture)
        
     build:
         docker build -t $(DOCKER_IMAGE_BASE):$(SERVICE_VERSION) .
        
     publish-service:
         hzn exchange service publish -O -f horizon/service.definition.json
        
     publish-service-policy:
         hzn exchange service addpolicy -f horizon/service.policy.json $(HZN_ORG_ID)/$(SERVICE_NAME)_$(SERVICE_VERSION)_$(ARCH)
        
     publish-deployment-policy:
         hzn exchange deployment addpolicy -f horizon/deployment.policy.json $(HZN_ORG_ID)/policy-$(SERVICE_NAME)_$(SERVICE_VERSION)_$(ARCH)
        
     build-publish:
         $(MAKE) build
         $(MAKE) publish-service
         $(MAKE) publish-service-policy
         $(MAKE) publish-deployment-policy
        
     horizon/.hzn.json.tmp.mk: horizon/hzn.json
         @ hzn util configconv -f $< | sed 's/=/?=/' > $@
    
  7. Look at the content in below. Copy and paste it into Makefile to build and publish the edge-app and esf services.

     export ARCH ?= $(shell hzn architecture)
        
     publish-esf-ieam:
         cd esf-ieam && $(MAKE) publish-service
        
     publish-edge-app:
         cd edge-app && $(MAKE) build-publish
        
     publish:
         $(MAKE) publish-esf-ieam
         $(MAKE) publish-edge-app
    

Publish the Sample Edge Application

Run the below command and that will build and publish the edge-app and esf services.

make publish

Next Step