From f0b5d751d6c54da470d047d8b72126354efc7186 Mon Sep 17 00:00:00 2001 From: Ian Date: Thu, 26 Sep 2019 15:19:32 -0700 Subject: [PATCH 01/31] python 1.3.0 Ready for RC status --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index f96e2cd..6b8cb04 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ setup( name='iofog-python-sdk', - version='1.0.0', + version='1.3.0', project_urls={ 'Documentation': 'https://github.com/ioFog/iofog-python-sdk/blob/master/README.md', 'Source': 'https://github.com/ioFog/iofog-python-sdk.git', @@ -25,6 +25,6 @@ author='Eclipse ioFog', author_email='edgemaster@iofog.org', description='Native python SDK for Eclipse ioFog development.', - requires=['ws4py'], + requires=['ws4py, json, requests'], keywords='iofog IoT Eclipse fog computing edgeworx', ) From 050809717b4bb2e6fd347c3c3debd949f1b81bf8 Mon Sep 17 00:00:00 2001 From: Ian Date: Thu, 26 Sep 2019 15:19:32 -0700 Subject: [PATCH 02/31] python 1.3.0 Ready for RC status --- azure-pipelines.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 0b5042d..a4a8e09 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -17,7 +17,7 @@ jobs: pool: vmImage: 'ubuntu-16.04' steps: - - task: UsePythonVersion-3.x + - task: UsePythonVersion-3.x@1 inputs: versionSpec: '3.5' addToPath: true @@ -28,7 +28,7 @@ jobs: - script: | bash ./test.sh displayName: 'Testing-3.x' - - task: UsePythonVersion-2.7 + - task: UsePythonVersion-2.7@1 inputs: versionSpec: '2.7' addToPath: true From bf24d60cd081b7fef4e013f8d0f37ff61e174dff Mon Sep 17 00:00:00 2001 From: Ian Date: Thu, 26 Sep 2019 15:19:32 -0700 Subject: [PATCH 03/31] python 1.3.0 Ready for RC status --- azure-pipelines.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index a4a8e09..9d8d750 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -17,7 +17,7 @@ jobs: pool: vmImage: 'ubuntu-16.04' steps: - - task: UsePythonVersion-3.x@1 + - task: UsePythonVersion@0 inputs: versionSpec: '3.5' addToPath: true @@ -28,7 +28,7 @@ jobs: - script: | bash ./test.sh displayName: 'Testing-3.x' - - task: UsePythonVersion-2.7@1 + - task: UsePythonVersion@0 inputs: versionSpec: '2.7' addToPath: true From dfe7b3225cda7d2327174bfc47f2cba4aae0e438 Mon Sep 17 00:00:00 2001 From: Ian Date: Thu, 26 Sep 2019 15:19:32 -0700 Subject: [PATCH 04/31] python 1.3.0 Ready for RC status --- build.sh | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/build.sh b/build.sh index d995059..f04783f 100755 --- a/build.sh +++ b/build.sh @@ -7,10 +7,4 @@ else PYTHON_TAG="2.7-stretch" fi -# Build Python Images -docker build --build-arg TAG_NAME=${PYTHON_TAG} -t iofog/test-python-sdk-send:${PYTHON_VERSION} -f ./Docker/Dockerfile.send . -docker build --build-arg TAG_NAME=${PYTHON_TAG} -t iofog/test-python-sdk-recieve:${PYTHON_VERSION} -f ./Docker/Dockerfile.recieve . - -# Push Python Images -docker push iofog/test-python-sdk-send:${PYTHON_VERSION} -docker push iofog/test-python-sdk-recieve:${PYTHON_VERSION} \ No newline at end of file +echo "Works" \ No newline at end of file From 6449ad0c90d8d58a47b1579b1137949086c960e5 Mon Sep 17 00:00:00 2001 From: Ian Date: Thu, 26 Sep 2019 15:19:32 -0700 Subject: [PATCH 05/31] python 1.3.0 Ready for RC status --- build.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/build.sh b/build.sh index f04783f..20d1e2f 100755 --- a/build.sh +++ b/build.sh @@ -7,4 +7,6 @@ else PYTHON_TAG="2.7-stretch" fi -echo "Works" \ No newline at end of file +# Build Python Images +docker build --build-arg TAG_NAME=${PYTHON_TAG} -t iofog/test-python-sdk-send:${PYTHON_VERSION} -f ./Docker/Dockerfile.send . +docker build --build-arg TAG_NAME=${PYTHON_TAG} -t iofog/test-python-sdk-recieve:${PYTHON_VERSION} -f ./Docker/Dockerfile.recieve . From 7ceaa68ae456e7b5f311e2662dba3a832ce86e1a Mon Sep 17 00:00:00 2001 From: Ian Date: Thu, 26 Sep 2019 15:52:46 -0700 Subject: [PATCH 06/31] Changelog for 1.3.0 --- CHANGELOG-1.3.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 CHANGELOG-1.3.md diff --git a/CHANGELOG-1.3.md b/CHANGELOG-1.3.md new file mode 100644 index 0000000..bedf593 --- /dev/null +++ b/CHANGELOG-1.3.md @@ -0,0 +1,11 @@ +# Changes in 1.3.0 + +## Added + +* Ability to deploy ioFog microservices, and connections through SDK are now available. +* Added standards for rest calls made to ioFog under create_rest_call.py +* Fixed issue with python3 object handling + +## Removed + +## Known Issues \ No newline at end of file From 4106097d85427e01b479ff53a8adbf9becc06a89 Mon Sep 17 00:00:00 2001 From: Ian Date: Thu, 26 Sep 2019 15:52:46 -0700 Subject: [PATCH 07/31] Changelog for 1.3.0 --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index f703406..658cccb 100644 --- a/README.md +++ b/README.md @@ -146,3 +146,20 @@ Pack IoMessage into bytearray: ```python msg_bytes = io_msg_instance.to_bytearray() ``` + +#### Scheduling Microservices and adding to catalogs + +You can interact with any deployed ioFog you have now through some functions that will +help in deploying full ECN's and microservices, as well as let you edit and change what is currently running +as well as any yml deployment configurations you have. + + +ex: Creating a route within ioFog from python +```python +from iofog_python_sdk.create_rest_call import rest_call + +post_address = "{}/microservices/{}/routes/{}".format(controller_address, route["from"], route["to"]) +json_response = rest_call({}, post_address, auth_token).response +``` + +You can find relevant API endpoints at [the ioFog Website](http://iofog.org) under Documentation \ No newline at end of file From 764cfef158ad28b0aa7798682413f65e5bc2af3d Mon Sep 17 00:00:00 2001 From: Ian Date: Wed, 2 Oct 2019 13:18:34 -0700 Subject: [PATCH 08/31] Split client and deployment sections --- CHANGELOG-1.3.md | 5 +++-- README.md | 12 ++++++------ iofog_python_sdk/{ => client}/client.py | 10 +++++----- iofog_python_sdk/{ => client}/definitions.py | 0 iofog_python_sdk/{ => client}/exception.py | 0 iofog_python_sdk/{ => client}/httpclient.py | 8 ++++---- iofog_python_sdk/{ => client}/iomessage.py | 4 ++-- iofog_python_sdk/{ => client}/listener.py | 0 iofog_python_sdk/{ => client}/util.py | 2 +- iofog_python_sdk/{ => client}/wsclient.py | 6 +++--- iofog_python_sdk/{ => deploy}/agent_service.py | 2 +- iofog_python_sdk/{ => deploy}/catalog_service.py | 6 +++--- iofog_python_sdk/{ => deploy}/create_rest_call.py | 2 +- iofog_python_sdk/{ => deploy}/flow_service.py | 2 +- .../{ => deploy}/microservice_service.py | 6 +++--- iofog_python_sdk/{ => deploy}/pretty_print.py | 0 16 files changed, 33 insertions(+), 32 deletions(-) rename iofog_python_sdk/{ => client}/client.py (94%) rename iofog_python_sdk/{ => client}/definitions.py (100%) rename iofog_python_sdk/{ => client}/exception.py (100%) rename iofog_python_sdk/{ => client}/httpclient.py (92%) rename iofog_python_sdk/{ => client}/iomessage.py (99%) rename iofog_python_sdk/{ => client}/listener.py (100%) rename iofog_python_sdk/{ => client}/util.py (96%) rename iofog_python_sdk/{ => client}/wsclient.py (97%) rename iofog_python_sdk/{ => deploy}/agent_service.py (95%) rename iofog_python_sdk/{ => deploy}/catalog_service.py (96%) rename iofog_python_sdk/{ => deploy}/create_rest_call.py (96%) rename iofog_python_sdk/{ => deploy}/flow_service.py (97%) rename iofog_python_sdk/{ => deploy}/microservice_service.py (98%) rename iofog_python_sdk/{ => deploy}/pretty_print.py (100%) diff --git a/CHANGELOG-1.3.md b/CHANGELOG-1.3.md index bedf593..59ad5df 100644 --- a/CHANGELOG-1.3.md +++ b/CHANGELOG-1.3.md @@ -3,9 +3,10 @@ ## Added * Ability to deploy ioFog microservices, and connections through SDK are now available. -* Added standards for rest calls made to ioFog under create_rest_call.py + * This can be imported through iofog_python_sdk.deploy options. + * Previous standard iofog_python_sdk can be imported through iofog_python_sdk.client +* Added standards for rest calls made to ioFog under deploy.create_rest_call * Fixed issue with python3 object handling -## Removed ## Known Issues \ No newline at end of file diff --git a/README.md b/README.md index 658cccb..c31a385 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,10 @@ sudo python2 -m pip install iofog-python-sdk Import iofog client and additional classes to your project: ```python -from iofog_python_sdk.client import IoFogClient -from iofog_python_sdk.exception import IoFogException -from iofog_python_sdk.iomessage import IoMessage -from iofog_python_sdk.listener import * +from iofog_python_sdk.client.client import IoFogClient +from iofog_python_sdk.client.exception import IoFogException +from iofog_python_sdk.client.iomessage import IoMessage +from iofog_python_sdk.client.listener import * ``` Create IoFog client with default settings: @@ -156,9 +156,9 @@ as well as any yml deployment configurations you have. ex: Creating a route within ioFog from python ```python -from iofog_python_sdk.create_rest_call import rest_call +from iofog_python_sdk.deploy.create_rest_call import rest_call -post_address = "{}/microservices/{}/routes/{}".format(controller_address, route["from"], route["to"]) +post_address = "{}/microservices/{}/routes/{}".format(controller_ip_address, route["from"], route["to"]) json_response = rest_call({}, post_address, auth_token).response ``` diff --git a/iofog_python_sdk/client.py b/iofog_python_sdk/client/client.py similarity index 94% rename from iofog_python_sdk/client.py rename to iofog_python_sdk/client/client.py index 019b650..f661139 100644 --- a/iofog_python_sdk/client.py +++ b/iofog_python_sdk/client/client.py @@ -14,11 +14,11 @@ import subprocess -from iofog_python_sdk.httpclient import IoFogHttpClient -from iofog_python_sdk.definitions import * -from iofog_python_sdk.wsclient import IoFogControlWsClient, IoFogMessageWsClient -from iofog_python_sdk.listener import * -from iofog_python_sdk.exception import * +from iofog_python_sdk.client.httpclient import IoFogHttpClient +from iofog_python_sdk.client.definitions import * +from iofog_python_sdk.client.wsclient import IoFogControlWsClient, IoFogMessageWsClient +from iofog_python_sdk.client.listener import * +from iofog_python_sdk.client.exception import * parser = argparse.ArgumentParser() parser.add_argument("-l", "--log", dest="logLevel", choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], diff --git a/iofog_python_sdk/definitions.py b/iofog_python_sdk/client/definitions.py similarity index 100% rename from iofog_python_sdk/definitions.py rename to iofog_python_sdk/client/definitions.py diff --git a/iofog_python_sdk/exception.py b/iofog_python_sdk/client/exception.py similarity index 100% rename from iofog_python_sdk/exception.py rename to iofog_python_sdk/client/exception.py diff --git a/iofog_python_sdk/httpclient.py b/iofog_python_sdk/client/httpclient.py similarity index 92% rename from iofog_python_sdk/httpclient.py rename to iofog_python_sdk/client/httpclient.py index 68549e4..9f87540 100644 --- a/iofog_python_sdk/httpclient.py +++ b/iofog_python_sdk/client/httpclient.py @@ -15,10 +15,10 @@ except ImportError: from urllib2 import HTTPError #for python 2 -from iofog_python_sdk.definitions import * -from iofog_python_sdk.util import make_post_request -from iofog_python_sdk.iomessage import IoMessage -from iofog_python_sdk.exception import IoFogHttpException +from iofog_python_sdk.client.definitions import * +from iofog_python_sdk.client.util import make_post_request +from iofog_python_sdk.client.iomessage import IoMessage +from iofog_python_sdk.client.exception import IoFogHttpException class IoFogHttpClient: diff --git a/iofog_python_sdk/iomessage.py b/iofog_python_sdk/client/iomessage.py similarity index 99% rename from iofog_python_sdk/iomessage.py rename to iofog_python_sdk/client/iomessage.py index 31a6769..288a0a9 100644 --- a/iofog_python_sdk/iomessage.py +++ b/iofog_python_sdk/client/iomessage.py @@ -13,10 +13,10 @@ from struct import pack, unpack import sys -from iofog_python_sdk.util import * +from iofog_python_sdk.client.util import * import base64 -from iofog_python_sdk.definitions import * +from iofog_python_sdk.client.definitions import * # todo check fields == None? diff --git a/iofog_python_sdk/listener.py b/iofog_python_sdk/client/listener.py similarity index 100% rename from iofog_python_sdk/listener.py rename to iofog_python_sdk/client/listener.py diff --git a/iofog_python_sdk/util.py b/iofog_python_sdk/client/util.py similarity index 96% rename from iofog_python_sdk/util.py rename to iofog_python_sdk/client/util.py index 5cb31ea..034d1c4 100644 --- a/iofog_python_sdk/util.py +++ b/iofog_python_sdk/client/util.py @@ -17,7 +17,7 @@ import urllib2 as urllib_request # for python 2 from struct import pack -from iofog_python_sdk.definitions import CODE_MSG +from iofog_python_sdk.client.definitions import CODE_MSG def num_to_bytearray(num): diff --git a/iofog_python_sdk/wsclient.py b/iofog_python_sdk/client/wsclient.py similarity index 97% rename from iofog_python_sdk/wsclient.py rename to iofog_python_sdk/client/wsclient.py index 869b4a3..a669cf4 100644 --- a/iofog_python_sdk/wsclient.py +++ b/iofog_python_sdk/client/wsclient.py @@ -13,9 +13,9 @@ import time -import iofog_python_sdk.util as util -from iofog_python_sdk.iomessage import IoMessage -from iofog_python_sdk.definitions import * +import iofog_python_sdk.client.util as util +from iofog_python_sdk.client.iomessage import IoMessage +from iofog_python_sdk.client.definitions import * from ws4py.client.threadedclient import WebSocketClient from ws4py.framing import OPCODE_PONG diff --git a/iofog_python_sdk/agent_service.py b/iofog_python_sdk/deploy/agent_service.py similarity index 95% rename from iofog_python_sdk/agent_service.py rename to iofog_python_sdk/deploy/agent_service.py index 9207e49..50f5198 100644 --- a/iofog_python_sdk/agent_service.py +++ b/iofog_python_sdk/deploy/agent_service.py @@ -8,7 +8,7 @@ # SPDX-License-Identifier: EPL-2.0 #******************************************************************************** -from iofog_python_sdk.create_rest_call import rest_call +from iofog_python_sdk.deploy.create_rest_call import rest_call class agent_service: def get_agent_per_microservice(controller_address, auth_token, microservices): diff --git a/iofog_python_sdk/catalog_service.py b/iofog_python_sdk/deploy/catalog_service.py similarity index 96% rename from iofog_python_sdk/catalog_service.py rename to iofog_python_sdk/deploy/catalog_service.py index 293d180..4dd078a 100755 --- a/iofog_python_sdk/catalog_service.py +++ b/iofog_python_sdk/deploy/catalog_service.py @@ -1,6 +1,6 @@ -from iofog_python_sdk.create_rest_call import rest_call -from iofog_python_sdk.pretty_print import print_info -from iofog_python_sdk.microservice_service import microservices as msv +from iofog_python_sdk.deploy.create_rest_call import rest_call +from iofog_python_sdk.deploy.pretty_print import print_info +from iofog_python_sdk.deploy.microservice_service import microservices as msv class catalog_service: diff --git a/iofog_python_sdk/create_rest_call.py b/iofog_python_sdk/deploy/create_rest_call.py similarity index 96% rename from iofog_python_sdk/create_rest_call.py rename to iofog_python_sdk/deploy/create_rest_call.py index e1b672a..b6a4d21 100644 --- a/iofog_python_sdk/create_rest_call.py +++ b/iofog_python_sdk/deploy/create_rest_call.py @@ -2,7 +2,7 @@ import json from requests.exceptions import HTTPError -from iofog_python_sdk.pretty_print import * +from iofog_python_sdk.deploy.pretty_print import * class rest_call: # diff --git a/iofog_python_sdk/flow_service.py b/iofog_python_sdk/deploy/flow_service.py similarity index 97% rename from iofog_python_sdk/flow_service.py rename to iofog_python_sdk/deploy/flow_service.py index 2834ffd..cfb68d3 100644 --- a/iofog_python_sdk/flow_service.py +++ b/iofog_python_sdk/deploy/flow_service.py @@ -1,4 +1,4 @@ -from iofog_python_sdk.create_rest_call import rest_call +from iofog_python_sdk.deploy.create_rest_call import rest_call import time diff --git a/iofog_python_sdk/microservice_service.py b/iofog_python_sdk/deploy/microservice_service.py similarity index 98% rename from iofog_python_sdk/microservice_service.py rename to iofog_python_sdk/deploy/microservice_service.py index 4131eec..fb9414f 100644 --- a/iofog_python_sdk/microservice_service.py +++ b/iofog_python_sdk/deploy/microservice_service.py @@ -1,8 +1,8 @@ import json -from iofog_python_sdk.create_rest_call import rest_call -from iofog_python_sdk.pretty_print import print_info, print_error -from iofog_python_sdk.flow_service import Flow +from iofog_python_sdk.deploy.create_rest_call import rest_call +from iofog_python_sdk.deploy.pretty_print import print_info, print_error +from iofog_python_sdk.deploy.flow_service import Flow class microservices: def __init__(self): diff --git a/iofog_python_sdk/pretty_print.py b/iofog_python_sdk/deploy/pretty_print.py similarity index 100% rename from iofog_python_sdk/pretty_print.py rename to iofog_python_sdk/deploy/pretty_print.py From 8a2514733d3406bd9f26c0dab75bb62fe828c3c5 Mon Sep 17 00:00:00 2001 From: Pixcell Date: Thu, 3 Oct 2019 11:08:18 +1300 Subject: [PATCH 09/31] Update README.md --- README.md | 73 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index c31a385..534dd42 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,16 @@ # iofog-python-sdk +This SDK is divided in two parts: client and deploy. + +## Installation + +Install python package: +```bash +sudo python2 -m pip install iofog-python-sdk +``` + +## Client + This module lets you easily build an ioElement. It gives you all the functionality to interact with ioFog via Local API. It contains all necessary methods for IoMessage transformation as well. - send new message to ioFog (post_message) @@ -10,12 +21,7 @@ This module lets you easily build an ioElement. It gives you all the functionali - connect to ioFog Control Channel via WebSocket (establish_control_ws_connection) - connect to ioFog Message Channel via WebSocket (establish_message_ws_connection) and publish new message via this channel (post_message_via_socket) -## Code snippets: - -Install python package: -```bash -sudo python2 -m pip install iofog-python-sdk -``` +### Code snippets: Import iofog client and additional classes to your project: ```python @@ -41,7 +47,7 @@ except IoFogException as e: # client creation failed, e contains description ``` -#### REST calls +##### REST calls Get list of next unread IoMessages: ```python @@ -94,7 +100,7 @@ except IoFogException, ex: ``` -#### WebSocket calls +##### WebSocket calls To use websocket connections you should implement listeners as follows: ```python @@ -126,7 +132,7 @@ client.post_message_via_socket(io_msg_instance) ``` -#### Message utils +##### Message utils Construct IoMessage from JSON(both json string and python dictionary are acceptable): ```python msg = IoMessage.from_json(json_msg) @@ -146,20 +152,49 @@ Pack IoMessage into bytearray: ```python msg_bytes = io_msg_instance.to_bytearray() ``` +## Deploy -#### Scheduling Microservices and adding to catalogs +This module lets you easily communicate with the [Controller REST API](https://iofog.org/docs/1.3.0/controllers/rest-api.html). -You can interact with any deployed ioFog you have now through some functions that will -help in deploying full ECN's and microservices, as well as let you edit and change what is currently running -as well as any yml deployment configurations you have. - - -ex: Creating a route within ioFog from python + - Deploy flow, microservices, agents, etc. + - Edit microservice configuration + - Edit flow routing + + ### Code snippets + +Import iofog deploy client ```python from iofog_python_sdk.deploy.create_rest_call import rest_call +from iofog_python_sdk.deploy.microservice_service import microservices +``` + +Update microservice config +```python +controller_address = http://localhost:51121/api/v3 +microservice_service = microservices() + +def iofog_auth(controller_address, email, password): + data = {} + data["email"] = email + data["password"] = password + post_address = "{}/user/login".format(controller_address) + jsonResponse = rest_call(data, post_address) + auth_token = jsonResponse["accessToken"] + return auth_token + +auth_token = iofog_auth(controller_address, "user@domain.com", "myPassword") +flow_id = 1 +current_microservice = microservice_service.get_microservice_by_name(controller_address, "my_microservice", flow_id, auth_token) + +updated_microservice = current_microservice +updated_microservice.config = {"newKey": 42} + +microservice_service.update_microservice(controller_address, updated_microservice, current_microservice.iofogUuid, catalog_id, auth_token) -post_address = "{}/microservices/{}/routes/{}".format(controller_ip_address, route["from"], route["to"]) -json_response = rest_call({}, post_address, auth_token).response ``` -You can find relevant API endpoints at [the ioFog Website](http://iofog.org) under Documentation \ No newline at end of file +#### Disclaimer + +This module is very much a Work In Progress. It was first written as a set of helper functions used by a python script to deploy a set of microservices configured using yaml files. + +Our [golang SDK](https://github.com/eclipse-iofog/iofog-go-sdk) is more adapted and modular for communicating with the Controller REST API From 796f35c106458dc960723689ca234ecbdeed7422 Mon Sep 17 00:00:00 2001 From: Saeid Rezaei Baghbidi Date: Thu, 7 Nov 2019 19:46:50 -0800 Subject: [PATCH 10/31] Pipeline triggers --- azure-pipelines.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 9d8d750..cf4dc61 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -1,8 +1,11 @@ trigger: + tags: + include: + - v* branches: include: - - master - develop + - release* paths: exclude: - README.md From 20e5ec622fddaed3087608b439dc1dbb8b1ed07a Mon Sep 17 00:00:00 2001 From: Saeid Rezaei Baghbidi Date: Thu, 7 Nov 2019 19:55:24 -0800 Subject: [PATCH 11/31] Python2 docker image tag --- azure-pipelines.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index cf4dc61..9099a41 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -37,7 +37,7 @@ jobs: addToPath: true architecture: 'x64' - script: | - bash ./build.sh + bash ./build.sh 'python2' displayName: 'Building-2.7' - script: | bash ./test.sh From d327a42be5a6c57e3d84f5222cf26f3188953061 Mon Sep 17 00:00:00 2001 From: Saeid Rezaei Baghbidi Date: Thu, 7 Nov 2019 20:17:53 -0800 Subject: [PATCH 12/31] Test --- test.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test.sh b/test.sh index 95e8296..5336cbc 100644 --- a/test.sh +++ b/test.sh @@ -6,20 +6,20 @@ FAILURES=( 0 0 ) idx=0 for version in ${PYTHON_VERSIONS[@]}; do - docker pull iofog/test-python-sdk-send::${version} - docker pull iofog/test-python-sdk-recieve:${version} + docker run --name send -d iofog/test-python-sdk-send:$version 1>/dev/null + docker run --name recieve -d iofog/test-python-sdk-recieve:$version 1>/dev/null - docker run -d iofog/test-python-sdk-send:${version} - docker run -d iofog/test-python-sdk-recieve:${version} + SEND_CONTAINER_ID=$(docker ps -aqf "name=send") + RECIEVE_CONTAINER_ID=$(docker ps -aqf "name=recieve") - RECIEVE_CONTAINER_ID=$( docker ps | grep "python recieve.py" | awk '{print $1}' ) - SEND_CONTAINER_ID=$( docker ps | grep "python send.py" | awk '{print $1}' ) - - if [[ "${RECIEVE_CONTAINER_ID}" -eq "" || "${SEND_CONTAINER_ID}" -eq "" ]]; then + if [[ -z $RECIEVE_CONTAINER_ID || -z $SEND_CONTAINER_ID ]]; then echo "${version} has failed to send/recieve data" - ${FAILURES[idx]}= 1 + ${FAILURES[idx]} = 1 fi idx+=1 + + docker rm -f send 1>/dev/null + docker rm -f recieve 1>/dev/null done for failure in ${FAILURES[@]}; do From a34d1ba3d15efc249fe7499a21ca50b871cd9c7d Mon Sep 17 00:00:00 2001 From: Saeid Rezaei Baghbidi Date: Thu, 7 Nov 2019 20:21:40 -0800 Subject: [PATCH 13/31] Test --- azure-pipelines.yml | 4 ++-- test.sh | 33 ++++++++++----------------------- 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 9099a41..4104036 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -29,7 +29,7 @@ jobs: bash ./build.sh 'python3' displayName: 'Building-3.x' - script: | - bash ./test.sh + bash ./test.sh 'python3' displayName: 'Testing-3.x' - task: UsePythonVersion@0 inputs: @@ -40,5 +40,5 @@ jobs: bash ./build.sh 'python2' displayName: 'Building-2.7' - script: | - bash ./test.sh + bash ./test.sh 'python2' displayName: 'Testing-2.7' \ No newline at end of file diff --git a/test.sh b/test.sh index 5336cbc..faadf9f 100644 --- a/test.sh +++ b/test.sh @@ -1,31 +1,18 @@ #!/usr/bin/env bash -PYTHON_VERSIONS=( "python3" "python2" ) -FAILURES=( 0 0 ) +PYTHON_VERSION=$1 -idx=0 -for version in ${PYTHON_VERSIONS[@]}; do +docker run --name send -d iofog/test-python-sdk-send:$PYTHON_VERSION 1>/dev/null +docker run --name recieve -d iofog/test-python-sdk-recieve:$PYTHON_VERSION 1>/dev/null - docker run --name send -d iofog/test-python-sdk-send:$version 1>/dev/null - docker run --name recieve -d iofog/test-python-sdk-recieve:$version 1>/dev/null +SEND_CONTAINER_ID=$(docker ps -aqf "name=send") +RECIEVE_CONTAINER_ID=$(docker ps -aqf "name=recieve") - SEND_CONTAINER_ID=$(docker ps -aqf "name=send") - RECIEVE_CONTAINER_ID=$(docker ps -aqf "name=recieve") +if [[ -z $RECIEVE_CONTAINER_ID || -z $SEND_CONTAINER_ID ]]; then + exit 1 +fi - if [[ -z $RECIEVE_CONTAINER_ID || -z $SEND_CONTAINER_ID ]]; then - echo "${version} has failed to send/recieve data" - ${FAILURES[idx]} = 1 - fi - idx+=1 - - docker rm -f send 1>/dev/null - docker rm -f recieve 1>/dev/null -done - -for failure in ${FAILURES[@]}; do - if [[ ${failure} == 1 ]]; then - exit 1 - fi -done +docker rm -f send 1>/dev/null +docker rm -f recieve 1>/dev/null exit 0 From 809ac32a6a78fec11bb45e9897c6ceacfa6b87bc Mon Sep 17 00:00:00 2001 From: Saeid Rezaei Baghbidi Date: Thu, 7 Nov 2019 20:26:13 -0800 Subject: [PATCH 14/31] Artifacts --- azure-pipelines.yml | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 4104036..ec1841d 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -27,10 +27,10 @@ jobs: architecture: 'x64' - script: | bash ./build.sh 'python3' - displayName: 'Building-3.x' + displayName: 'Building 3.x' - script: | bash ./test.sh 'python3' - displayName: 'Testing-3.x' + displayName: 'Testing 3.x' - task: UsePythonVersion@0 inputs: versionSpec: '2.7' @@ -38,7 +38,17 @@ jobs: architecture: 'x64' - script: | bash ./build.sh 'python2' - displayName: 'Building-2.7' + displayName: 'Building 2.7' - script: | bash ./test.sh 'python2' - displayName: 'Testing-2.7' \ No newline at end of file + displayName: 'Testing 2.7' + - task: CopyFiles@2 + inputs: + SourceFolder: $(System.DefaultWorkingDirectory) + TargetFolder: $(Build.ArtifactStagingDirectory) + Contents: | + setup.py + requirements.txt + iofog_python_sdk + OverWrite: true + displayName: 'Publish artifacts' From 2c2e82d149c96557d75d9018376cb87a6fdf6064 Mon Sep 17 00:00:00 2001 From: Saeid Rezaei Baghbidi Date: Thu, 7 Nov 2019 20:30:06 -0800 Subject: [PATCH 15/31] Artifacts --- azure-pipelines.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index ec1841d..c93bf24 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -49,6 +49,6 @@ jobs: Contents: | setup.py requirements.txt - iofog_python_sdk + 'iofog_python_sdk/**' OverWrite: true displayName: 'Publish artifacts' From 8f880bb91a9c9a2bd9f769e9ffdef53a8b5fc667 Mon Sep 17 00:00:00 2001 From: Saeid Rezaei Baghbidi Date: Thu, 7 Nov 2019 20:34:27 -0800 Subject: [PATCH 16/31] Artifacts --- azure-pipelines.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index c93bf24..5882d4b 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -49,6 +49,6 @@ jobs: Contents: | setup.py requirements.txt - 'iofog_python_sdk/**' + iofog_python_sdk/**/* OverWrite: true displayName: 'Publish artifacts' From 2e9cb9811c431e74c7d78feba77ddd7e8e74f016 Mon Sep 17 00:00:00 2001 From: Saeid Rezaei Baghbidi Date: Thu, 7 Nov 2019 20:47:36 -0800 Subject: [PATCH 17/31] Artifacts --- azure-pipelines.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 5882d4b..249bc99 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -51,4 +51,10 @@ jobs: requirements.txt iofog_python_sdk/**/* OverWrite: true - displayName: 'Publish artifacts' + displayName: 'Copy artifacts' + - task: PublishBuildArtifacts@1 + inputs: + PathtoPublish: '$(Build.ArtifactStagingDirectory)' + ArtifactName: 'sdk' + displayName: 'Publish artefacts' + From 2c85072896705b38e520522fad77c01985a185b1 Mon Sep 17 00:00:00 2001 From: Saeid Rezaei Baghbidi Date: Thu, 7 Nov 2019 21:39:30 -0800 Subject: [PATCH 18/31] Setup tools --- azure-pipelines.yml | 1 + setup.py | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 249bc99..de2dbaf 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -49,6 +49,7 @@ jobs: Contents: | setup.py requirements.txt + README.md iofog_python_sdk/**/* OverWrite: true displayName: 'Copy artifacts' diff --git a/setup.py b/setup.py index 6b8cb04..a229ea6 100644 --- a/setup.py +++ b/setup.py @@ -8,23 +8,27 @@ # SPDX-License-Identifier: EPL-2.0 #******************************************************************************** -from distutils.core import setup +import setuptools -setup( +with open("README.md", "r") as fh: + long_description = fh.read() + +setuptools.setup( name='iofog-python-sdk', version='1.3.0', project_urls={ - 'Documentation': 'https://github.com/ioFog/iofog-python-sdk/blob/master/README.md', - 'Source': 'https://github.com/ioFog/iofog-python-sdk.git', - 'Tracker': 'https://github.com/ioFog/iofog-python-sdk/issues', + 'Documentation': 'https://github.com/eclipse-iofog/iofog-python-sdk/blob/master/README.md', + 'Source': 'https://github.com/eclipse-iofog/iofog-python-sdk.git', + 'Tracker': 'https://github.com/eclipse-iofog/iofog-python-sdk/issues', 'Eclipse ioFog': 'http://iofog.org' }, packages=['iofog_python_sdk'], - url='https://github.com/ioFog/iofog-python-sdk', + url='https://github.com/eclipse-iofog/iofog-python-sdk', license='EPL-2.0', author='Eclipse ioFog', author_email='edgemaster@iofog.org', description='Native python SDK for Eclipse ioFog development.', - requires=['ws4py, json, requests'], - keywords='iofog IoT Eclipse fog computing edgeworx', + long_description=long_description, + requires=['ws4py', 'json', 'requests'], + keywords='ioFog IoT Eclipse fog computing edgeworx', ) From e29cd4bfc2d2a3ebc4fa77fa9f1cb83eb0391c7c Mon Sep 17 00:00:00 2001 From: Saeid Rezaei Baghbidi Date: Thu, 7 Nov 2019 22:12:14 -0800 Subject: [PATCH 19/31] Description content type --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index a229ea6..b670509 100644 --- a/setup.py +++ b/setup.py @@ -29,6 +29,7 @@ author_email='edgemaster@iofog.org', description='Native python SDK for Eclipse ioFog development.', long_description=long_description, + long_description_content_type="text/markdown", requires=['ws4py', 'json', 'requests'], keywords='ioFog IoT Eclipse fog computing edgeworx', ) From f3058f8701b458cf476c9d899f3cfee2a1192e28 Mon Sep 17 00:00:00 2001 From: SergeRadinovich Date: Fri, 14 Aug 2020 15:08:28 +1200 Subject: [PATCH 20/31] Add rest pkg --- iofog_python_sdk/rest/controller/client.py | 57 +++++++++++++++++++++ iofog_python_sdk/rest/controller/request.py | 28 ++++++++++ test/rest.py | 25 +++++++++ 3 files changed, 110 insertions(+) create mode 100644 iofog_python_sdk/rest/controller/client.py create mode 100644 iofog_python_sdk/rest/controller/request.py create mode 100644 test/rest.py diff --git a/iofog_python_sdk/rest/controller/client.py b/iofog_python_sdk/rest/controller/client.py new file mode 100644 index 0000000..28efa80 --- /dev/null +++ b/iofog_python_sdk/rest/controller/client.py @@ -0,0 +1,57 @@ +from iofog_python_sdk.rest.controller.request import * + + +class Client: + """ + Iofog Controller REST API client. + """ + + def __init__(self, host, port, email, password): + self.base_path = "http://" + host + ":" + str(port) + "/api/v3" + self._login(email, password) + + def _login(self, email, password): + url = "{}/user/login".format(self.base_path) + body = { + "email": email, + "password": password + } + self.token = request("POST", url, "", body)["accessToken"] + + def get_status(self): + url = "{}/status".format(self.base_path) + return request("GET", url) + + def create_agent(self, name, host): + url = "{}/iofog".format(self.base_path) + body = { + "name": name, + "fogType": 0, + "host": host + } + return request("POST", url, self.token, body)["uuid"] + + def _get_provision_key(self, agent_id): + url = "{}/iofog/{}/provisioning-key".format(self.base_path, agent_id) + return request("GET", url, self.token)["key"] + + def _delete_agent(self, agent_id): + url = "{}/iofog/{}".format(self.base_path, agent_id) + request("DELETE", url, self.token) + + def _get_agent_id(self, name): + url = "{}/iofog-list".format(self.base_path) + resp = request("GET", url, self.token) + for fog in resp["fogs"]: + if fog["name"] == name: + return fog["uuid"] + + def delete_agent(self, name): + id = self._get_agent_id(name) + if id is not None: + self._delete_agent(id) + + def get_provision_key(self, agent_name): + id = self._get_agent_id(agent_name) + if id is not None: + return self._get_provision_key(id) \ No newline at end of file diff --git a/iofog_python_sdk/rest/controller/request.py b/iofog_python_sdk/rest/controller/request.py new file mode 100644 index 0000000..ffffd66 --- /dev/null +++ b/iofog_python_sdk/rest/controller/request.py @@ -0,0 +1,28 @@ +import requests +import json + + +def request(method, address, auth_token="", body={}): + switch = { + "POST": requests.post, + "GET": requests.get, + "DELETE": requests.delete, + "PATCH": requests.patch, + } + headers = {} + data = {} + if body: + headers["Content-Type"] = 'application/json' + data = json.dumps(body, indent=4) + + if auth_token: + headers["Authorization"] = auth_token + else: + headers["cache-control"] = "no-cache" + + response = switch[method](address, data=data, headers=headers, timeout=30) + response.raise_for_status() + responseDict = {} + if response.content: + responseDict = json.loads(response.content) + return responseDict \ No newline at end of file diff --git a/test/rest.py b/test/rest.py new file mode 100644 index 0000000..f2e41ad --- /dev/null +++ b/test/rest.py @@ -0,0 +1,25 @@ +from iofog_python_sdk.rest.controller.client import * +import sys + + +def main(): + client = Client(host=sys.argv[1], + port=sys.argv[2], + email=sys.argv[3], + password=sys.argv[4]) + + status = client.get_status() + print(status) + + name = "name" + client.delete_agent(name) + + client.create_agent(name, "localhost") + + key = client.get_provision_key(name) + print(key) + + client.delete_agent(name) + +if __name__ == "__main__": + main() \ No newline at end of file From fd349f77c2f3f79a1952a4e87c61685fc7f80be4 Mon Sep 17 00:00:00 2001 From: SergeRadinovich Date: Thu, 10 Sep 2020 17:48:30 +1200 Subject: [PATCH 21/31] Add upgrade_agent to rest client --- iofog_python_sdk/rest/controller/client.py | 19 +++++++++++++------ iofog_python_sdk/rest/controller/request.py | 6 +++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/iofog_python_sdk/rest/controller/client.py b/iofog_python_sdk/rest/controller/client.py index 28efa80..f766f12 100644 --- a/iofog_python_sdk/rest/controller/client.py +++ b/iofog_python_sdk/rest/controller/client.py @@ -47,11 +47,18 @@ def _get_agent_id(self, name): return fog["uuid"] def delete_agent(self, name): - id = self._get_agent_id(name) - if id is not None: - self._delete_agent(id) + uuid = self._get_agent_id(name) + if uuid is not None: + self._delete_agent(uuid) def get_provision_key(self, agent_name): - id = self._get_agent_id(agent_name) - if id is not None: - return self._get_provision_key(id) \ No newline at end of file + uuid = self._get_agent_id(agent_name) + if uuid is not None: + return self._get_provision_key(uuid) + + def upgrade_agent(self, agent_name, version): + uuid = self._get_agent_id(agent_name) + if uuid is None: + raise Exception("Could not get Agent UUID") + url = "{}/iofog/{}/version/upgrade".format(self.base_path, uuid) + request("POST", url, self.token) diff --git a/iofog_python_sdk/rest/controller/request.py b/iofog_python_sdk/rest/controller/request.py index ffffd66..bdc6d8f 100644 --- a/iofog_python_sdk/rest/controller/request.py +++ b/iofog_python_sdk/rest/controller/request.py @@ -21,7 +21,11 @@ def request(method, address, auth_token="", body={}): headers["cache-control"] = "no-cache" response = switch[method](address, data=data, headers=headers, timeout=30) - response.raise_for_status() + try: + response.raise_for_status() + except requests.HTTPError as e: + print(e.response.content) + raise e responseDict = {} if response.content: responseDict = json.loads(response.content) From 11960e606328a3c6efc99a0cd540a08d23a4120b Mon Sep 17 00:00:00 2001 From: SergeRadinovich Date: Thu, 10 Sep 2020 18:06:18 +1200 Subject: [PATCH 22/31] Fix upgrade version func --- iofog_python_sdk/rest/controller/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iofog_python_sdk/rest/controller/client.py b/iofog_python_sdk/rest/controller/client.py index f766f12..709df11 100644 --- a/iofog_python_sdk/rest/controller/client.py +++ b/iofog_python_sdk/rest/controller/client.py @@ -56,7 +56,7 @@ def get_provision_key(self, agent_name): if uuid is not None: return self._get_provision_key(uuid) - def upgrade_agent(self, agent_name, version): + def upgrade_agent(self, agent_name): uuid = self._get_agent_id(agent_name) if uuid is None: raise Exception("Could not get Agent UUID") From c7c4844981a1354542eb509851af11101d9c125b Mon Sep 17 00:00:00 2001 From: SergeRadinovich Date: Thu, 10 Sep 2020 18:57:16 +1200 Subject: [PATCH 23/31] Add patch agent function --- iofog_python_sdk/rest/controller/client.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/iofog_python_sdk/rest/controller/client.py b/iofog_python_sdk/rest/controller/client.py index 709df11..bfc051f 100644 --- a/iofog_python_sdk/rest/controller/client.py +++ b/iofog_python_sdk/rest/controller/client.py @@ -1,6 +1,5 @@ from iofog_python_sdk.rest.controller.request import * - class Client: """ Iofog Controller REST API client. @@ -53,12 +52,16 @@ def delete_agent(self, name): def get_provision_key(self, agent_name): uuid = self._get_agent_id(agent_name) - if uuid is not None: - return self._get_provision_key(uuid) + if uuid is None: + raise Exception("Could not get Agent UUID") + return self._get_provision_key(uuid) def upgrade_agent(self, agent_name): uuid = self._get_agent_id(agent_name) - if uuid is None: - raise Exception("Could not get Agent UUID") url = "{}/iofog/{}/version/upgrade".format(self.base_path, uuid) - request("POST", url, self.token) + return request("POST", url, self.token) + + def patch_agent(self, agent_name, config): + uuid = self._get_agent_id(agent_name) + url = "{}/iofog/{}".format(self.base_path, uuid) + return request("PATCH", url, self.token, config) From 5110be67ce22e17c44fa6bcbde6b81fa3f75df73 Mon Sep 17 00:00:00 2001 From: SergeRadinovich Date: Thu, 10 Sep 2020 20:07:57 +1200 Subject: [PATCH 24/31] Specify py 3.7 in pipeline --- azure-pipelines.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index de2dbaf..bb0d84d 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -22,7 +22,7 @@ jobs: steps: - task: UsePythonVersion@0 inputs: - versionSpec: '3.5' + versionSpec: '3.7' addToPath: true architecture: 'x64' - script: | From 14031294a8b987dea7f250ae8e8f6b0a41b2717f Mon Sep 17 00:00:00 2001 From: SergeRadinovich Date: Thu, 17 Sep 2020 11:49:06 +1200 Subject: [PATCH 25/31] Remove python 2.7 --- README.md | 2 +- azure-pipelines.yml | 16 ++-------------- test/rest.py | 25 ------------------------- 3 files changed, 3 insertions(+), 40 deletions(-) delete mode 100644 test/rest.py diff --git a/README.md b/README.md index 534dd42..9740f02 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This SDK is divided in two parts: client and deploy. Install python package: ```bash -sudo python2 -m pip install iofog-python-sdk +sudo python3 -m pip install iofog-python-sdk ``` ## Client diff --git a/azure-pipelines.yml b/azure-pipelines.yml index bb0d84d..e16ff19 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -27,21 +27,10 @@ jobs: architecture: 'x64' - script: | bash ./build.sh 'python3' - displayName: 'Building 3.x' + displayName: 'Building 3.7' - script: | bash ./test.sh 'python3' - displayName: 'Testing 3.x' - - task: UsePythonVersion@0 - inputs: - versionSpec: '2.7' - addToPath: true - architecture: 'x64' - - script: | - bash ./build.sh 'python2' - displayName: 'Building 2.7' - - script: | - bash ./test.sh 'python2' - displayName: 'Testing 2.7' + displayName: 'Testing 3.7' - task: CopyFiles@2 inputs: SourceFolder: $(System.DefaultWorkingDirectory) @@ -58,4 +47,3 @@ jobs: PathtoPublish: '$(Build.ArtifactStagingDirectory)' ArtifactName: 'sdk' displayName: 'Publish artefacts' - diff --git a/test/rest.py b/test/rest.py deleted file mode 100644 index f2e41ad..0000000 --- a/test/rest.py +++ /dev/null @@ -1,25 +0,0 @@ -from iofog_python_sdk.rest.controller.client import * -import sys - - -def main(): - client = Client(host=sys.argv[1], - port=sys.argv[2], - email=sys.argv[3], - password=sys.argv[4]) - - status = client.get_status() - print(status) - - name = "name" - client.delete_agent(name) - - client.create_agent(name, "localhost") - - key = client.get_provision_key(name) - print(key) - - client.delete_agent(name) - -if __name__ == "__main__": - main() \ No newline at end of file From 3bbb81383c0c12f7d54390b0aed7c4b91829264b Mon Sep 17 00:00:00 2001 From: SergeRadinovich Date: Thu, 17 Sep 2020 11:57:54 +1200 Subject: [PATCH 26/31] Remove deploy module and rename client module to microservices --- CHANGELOG-1.3.md | 2 +- README.md | 49 +--- iofog_python_sdk/deploy/agent_service.py | 33 --- iofog_python_sdk/deploy/catalog_service.py | 102 -------- iofog_python_sdk/deploy/create_rest_call.py | 47 ---- iofog_python_sdk/deploy/flow_service.py | 69 ------ .../deploy/microservice_service.py | 220 ------------------ iofog_python_sdk/deploy/pretty_print.py | 54 ----- .../{client => microservices}/client.py | 10 +- .../{client => microservices}/definitions.py | 0 .../{client => microservices}/exception.py | 0 .../{client => microservices}/httpclient.py | 8 +- .../{client => microservices}/iomessage.py | 4 +- .../{client => microservices}/listener.py | 0 .../{client => microservices}/util.py | 2 +- .../{client => microservices}/wsclient.py | 6 +- test/recieve.py | 2 +- test/send.py | 2 +- 18 files changed, 27 insertions(+), 583 deletions(-) delete mode 100644 iofog_python_sdk/deploy/agent_service.py delete mode 100755 iofog_python_sdk/deploy/catalog_service.py delete mode 100644 iofog_python_sdk/deploy/create_rest_call.py delete mode 100644 iofog_python_sdk/deploy/flow_service.py delete mode 100644 iofog_python_sdk/deploy/microservice_service.py delete mode 100644 iofog_python_sdk/deploy/pretty_print.py rename iofog_python_sdk/{client => microservices}/client.py (93%) rename iofog_python_sdk/{client => microservices}/definitions.py (100%) rename iofog_python_sdk/{client => microservices}/exception.py (100%) rename iofog_python_sdk/{client => microservices}/httpclient.py (91%) rename iofog_python_sdk/{client => microservices}/iomessage.py (98%) rename iofog_python_sdk/{client => microservices}/listener.py (100%) rename iofog_python_sdk/{client => microservices}/util.py (95%) rename iofog_python_sdk/{client => microservices}/wsclient.py (97%) diff --git a/CHANGELOG-1.3.md b/CHANGELOG-1.3.md index 59ad5df..9988c88 100644 --- a/CHANGELOG-1.3.md +++ b/CHANGELOG-1.3.md @@ -4,7 +4,7 @@ * Ability to deploy ioFog microservices, and connections through SDK are now available. * This can be imported through iofog_python_sdk.deploy options. - * Previous standard iofog_python_sdk can be imported through iofog_python_sdk.client + * Previous standard iofog_python_sdk can be imported through iofog_python_sdk.microservices * Added standards for rest calls made to ioFog under deploy.create_rest_call * Fixed issue with python3 object handling diff --git a/README.md b/README.md index 9740f02..2c34142 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # iofog-python-sdk -This SDK is divided in two parts: client and deploy. +This SDK provides the following modules: +* microservices: Clients for Microservices to talk to ioFog (e.g. ioMessage and Web Socket clients) +* rest.controller: Client for Controller REST API ## Installation @@ -25,10 +27,10 @@ This module lets you easily build an ioElement. It gives you all the functionali Import iofog client and additional classes to your project: ```python -from iofog_python_sdk.client.client import IoFogClient -from iofog_python_sdk.client.exception import IoFogException -from iofog_python_sdk.client.iomessage import IoMessage -from iofog_python_sdk.client.listener import * +from iofog_python_sdk.microservices.client import IoFogClient +from iofog_python_sdk.microservices.exception import IoFogException +from iofog_python_sdk.microservices.iomessage import IoMessage +from iofog_python_sdk.microservices.listener import * ``` Create IoFog client with default settings: @@ -160,41 +162,8 @@ This module lets you easily communicate with the [Controller REST API](https://i - Edit microservice configuration - Edit flow routing - ### Code snippets - -Import iofog deploy client -```python -from iofog_python_sdk.deploy.create_rest_call import rest_call -from iofog_python_sdk.deploy.microservice_service import microservices -``` - -Update microservice config -```python -controller_address = http://localhost:51121/api/v3 -microservice_service = microservices() - -def iofog_auth(controller_address, email, password): - data = {} - data["email"] = email - data["password"] = password - post_address = "{}/user/login".format(controller_address) - jsonResponse = rest_call(data, post_address) - auth_token = jsonResponse["accessToken"] - return auth_token - -auth_token = iofog_auth(controller_address, "user@domain.com", "myPassword") -flow_id = 1 -current_microservice = microservice_service.get_microservice_by_name(controller_address, "my_microservice", flow_id, auth_token) - -updated_microservice = current_microservice -updated_microservice.config = {"newKey": 42} - -microservice_service.update_microservice(controller_address, updated_microservice, current_microservice.iofogUuid, catalog_id, auth_token) - -``` - #### Disclaimer -This module is very much a Work In Progress. It was first written as a set of helper functions used by a python script to deploy a set of microservices configured using yaml files. +These modules are a Work In Progress. It was first written as a set of helper functions used by a python script to deploy a set of microservices configured using yaml files. -Our [golang SDK](https://github.com/eclipse-iofog/iofog-go-sdk) is more adapted and modular for communicating with the Controller REST API +Our [golang SDK](https://github.com/eclipse-iofog/iofog-go-sdk) is highly recommended as Controller REST API client. diff --git a/iofog_python_sdk/deploy/agent_service.py b/iofog_python_sdk/deploy/agent_service.py deleted file mode 100644 index 50f5198..0000000 --- a/iofog_python_sdk/deploy/agent_service.py +++ /dev/null @@ -1,33 +0,0 @@ -#******************************************************************************** -# Copyright (c) 2018 Edgeworx, Inc. -# -# This program and the accompanying materials are made available under the -# terms of the Eclipse Public License v. 2.0 which is available at -# http://www.eclipse.org/legal/epl-2.0 -# -# SPDX-License-Identifier: EPL-2.0 -#******************************************************************************** - -from iofog_python_sdk.deploy.create_rest_call import rest_call - -class agent_service: - def get_agent_per_microservice(controller_address, auth_token, microservices): - data = {} - agent_uuids = {} - post_address = "{}/iofog-list".format(controller_address) - json_response = rest_call(data, post_address, auth_token, method="GET") - for microserviceKey in microservices: - microservice = microservices[microserviceKey] - agent_uuids[microserviceKey] = next(x for x in json_response["fogs"] if x["name"] == microservice["agent-name"]) - return agent_uuids - - - def get_agent_info(agent): - config = {} - config.update(agent) - return config - - - def update_agent(controller_address, uuid, fog_info, auth_token): - url = "{}/iofog/{}".format(controller_address, uuid) - return rest_call(fog_info, url, auth_token, method="PATCH").response diff --git a/iofog_python_sdk/deploy/catalog_service.py b/iofog_python_sdk/deploy/catalog_service.py deleted file mode 100755 index 4dd078a..0000000 --- a/iofog_python_sdk/deploy/catalog_service.py +++ /dev/null @@ -1,102 +0,0 @@ -from iofog_python_sdk.deploy.create_rest_call import rest_call -from iofog_python_sdk.deploy.pretty_print import print_info -from iofog_python_sdk.deploy.microservice_service import microservices as msv - - -class catalog_service: - def create_catalog_curl_data(self, catalog_item): - data = {} - data["name"] = catalog_item["name"] - data["images"] = [] - data["images"].append({'containerImage': '{}'.format(catalog_item["images"]["arm"]), 'fogTypeId': 2}) - data["images"].append({'containerImage': '{}'.format(catalog_item["images"]["x86"]), 'fogTypeId': 1}) - data["registryId"] = 1 - - return data - - def update_catalog(self, controller_address, auth_token, catalog_item): - # Delete all microservices that uses this specific catalog item - print_info("====> Deleting all microservices currently using this catalog item") - microself = msv() - msv.delete_all_by_catalog_id(microself, controller_address, auth_token, catalog_item["id"]) - # Update catalog item - data = catalog_service.create_catalog_curl_data(self, catalog_item) - if data == {}: - # If data has failed to be created, exit here, and echo which service failed - return "{} failed to create curl data".format(catalog_item) - post_address = "{}/catalog/microservices/{}".format(controller_address, catalog_item["id"]) - json_response = rest_call(data, post_address, auth_token, method="PATCH").response - - def add_to_catalog(self, controller_address, auth_token, catalog_item): - data = catalog_service.create_catalog_curl_data(self, catalog_item) - if data == {}: - # If data has failed to be created, exit here, and echo which service failed - return "{} failed to create curl data".format(catalog_item) - post_address = "{}/catalog/microservices".format(controller_address) - return rest_call(data, post_address, auth_token).response["id"] - - - def delete_by_id(self, controller_address, catalog_id, auth_token): - post_address = "{}/catalog/microservices/{}".format(controller_address, catalog_id) - return rest_call({}, post_address, auth_token, method="DELETE").response - - - def delete_items(self, controller_address, catalog_items, auth_token): - for catalog_id in catalog_items: - catalog_service.delete_by_id(self, controller_address, catalog_id, auth_token) - - def get_catalog(self, controller_address, auth_token): - post_address = "{}/catalog/microservices".format(controller_address) - return rest_call({}, post_address, auth_token, method="GET").response["catalogItems"] - - def get_catalog_item_by_name(self, controller_address, name, auth_token): - catalog_items = catalog_service.get_catalog(self, controller_address, auth_token) - return next((x for x in catalog_items if x["name"] == name), None) - - def is_same(self, yaml_item, existing_item): - if len(yaml_item["images"]) != len(existing_item["images"]): - return False - for image_type in yaml_item["images"]: - image = yaml_item["images"][image_type] - same = next((x for x in existing_item["images"] if (image_type == "x86" and x["fogTypeId"] == 1 and x["containerImage"] == image) or (image_type == "arm" and x["fogTypeId"] == 2 and x["containerImage"] == image)), False) - if same == False: - return False - return True - - def setup(self, controller_address, auth_token, microservices): - # For each microservice check if catalog item exists - print_info("====> Reading the catalog") - updated = False - existing_catalog_items = catalog_service.get_catalog(self, controller_address, auth_token) - catalog_ids = {} - for microserviceKey in microservices: - microservice = microservices[microserviceKey] - catalog_item = { - "images": microservice["images"], - "name": microservice["microservice"]["name"] + "_catalog" - } - catalog_id = "" - existing_catalog_item = next((x for x in existing_catalog_items if x["name"] == catalog_item["name"]), None) - # If it does not exists yet, create - if existing_catalog_item == None: - print_info("====> Adding to the catalog") - updated = True - catalog_id = catalog_service.add_to_catalog(self, controller_address, auth_token, catalog_item) - # Otherwise, patch to update images - else: - catalog_item["id"] = existing_catalog_item["id"] - # Check if images have changed - if catalog_service.is_same(self, catalog_item, existing_catalog_item) == False: - print_info("====> Updating a catalog item (Delete / Recreate)") - updated = True - # update_catalog(controller_address, auth_token, catalog_item) - catalog_service.delete_by_id(self, controller_address, existing_catalog_item["id"], auth_token) - catalog_id = catalog_service.add_to_catalog(self, controller_address, auth_token, catalog_item) - else: - catalog_id = catalog_item["id"] - catalog_ids[microserviceKey] = catalog_id - if updated == False: - print_info("====> Catalog is up-to-date") - else: - print_info("====> Catalog updated") - return catalog_ids diff --git a/iofog_python_sdk/deploy/create_rest_call.py b/iofog_python_sdk/deploy/create_rest_call.py deleted file mode 100644 index b6a4d21..0000000 --- a/iofog_python_sdk/deploy/create_rest_call.py +++ /dev/null @@ -1,47 +0,0 @@ -import requests -import json - -from requests.exceptions import HTTPError -from iofog_python_sdk.deploy.pretty_print import * - -class rest_call: - # - # create a rest call, with two optional variables, auth_token for before you can retrieve it, and - # get if this needs to be a GET call instead of POST - # - def __init__(self, data, address, auth_token="none", method="POST"): - switch = { - "POST": requests.post, - "GET": requests.get, - "DELETE": requests.delete, - "PATCH": requests.patch, - } - headers = {} - headers["Content-Type"] = 'application/json' - - # Dump the data to Json so the curl call can take it in. - data = json.dumps(data, indent=4) - - if auth_token == "none": - headers["cache-control"] = "no-cache" - else: - headers["Authorization"] = auth_token - - print("==== " + method + " CALL ====") - print("Sending data: " + data) - print("To addr: " + address) - - try: - r = switch[method](address, data=data, headers=headers, timeout=30) - r.raise_for_status() - except HTTPError as http_err: - print_error("HTTP error occurred: " + str(http_err)) - except Exception as err: - print_error("Other error occurred: " + str(err)) - else: - jsonResponse = "" - if r.text: - jsonResponse = json.loads(r.text) - print("Response: " + str(jsonResponse)) - print('=====') - self.response = jsonResponse diff --git a/iofog_python_sdk/deploy/flow_service.py b/iofog_python_sdk/deploy/flow_service.py deleted file mode 100644 index cfb68d3..0000000 --- a/iofog_python_sdk/deploy/flow_service.py +++ /dev/null @@ -1,69 +0,0 @@ -from iofog_python_sdk.deploy.create_rest_call import rest_call -import time - - -class Flow: - - def __init__(self): - self.id = "" - - def create_flow(self, controller_address, auth_token, flow): - data = {} - - data["name"] = flow - post_address = "{}/flow".format(controller_address) - jsonResponse = rest_call(data, post_address, auth_token).response - self.id = jsonResponse["id"] - - def create_flow_if_not_exist(self, controller_address, auth_token, flow): - try: - id = Flow.get_id(self, controller_address, flow, auth_token) - return id - except StopIteration: - return Flow.create_flow(self, controller_address, auth_token, flow) - - def restart_flow(self, controller_address, flow_id, flow_name, auth_token): - flow = Flow.get_flow_by_id(self, controller_address, flow_id, auth_token) - if flow.get("isActivated", False) == True: - Flow.stop_flow(self, controller_address, flow_id, flow_name, auth_token) - time.sleep(3) - Flow.start_flow(self, controller_address, flow_id, flow_name, auth_token) - - def start_flow(self, controller_address, flow_id, flow_name, auth_token): - data = {} - data["name"] = flow_name - data["isActivated"] = True - post_address = "{}/flow/{}".format(controller_address, flow_id) - jsonRespone = rest_call(data, post_address, auth_token, method="PATCH").response - - def stop_flow(self, controller_address, flow_id, flow_name, auth_token): - data = {} - data["name"] = flow_name - data["isActivated"] = False - post_address = "{}/flow/{}".format(controller_address, flow_id) - jsonRespone = rest_call(data, post_address, auth_token, method="PATCH").response - - def delete_flow(self, controller_address, flow_id, auth_token): - data = {} - post_address = "{}/flow/{}".format(controller_address, flow_id) - jsonRespone = rest_call(data, post_address, auth_token, method="DELETE").response - - def get_flow_by_name(self, controller_address, flow_name, auth_token): - flows = Flow.get_all(self, controller_address, auth_token) - return next(x for x in flows if x["name"] == flow_name) - - def get_flow_by_id(self, controller_address, flow_id, auth_token): - data = {} - post_address = "{}/flow/{}".format(controller_address, flow_id) - json_response = rest_call(data, post_address, auth_token, method="GET").response - return json_response - - def get_id(self, controller_address, flow_name, auth_token): - self.id = Flow.get_flow_by_name(self, controller_address, flow_name, auth_token)["id"] - - def get_all(self, controller_address, auth_token): - data = {} - flow_ids = {} - post_address = "{}/flow".format(controller_address) - json_response = rest_call(data, post_address, auth_token, method="GET").response - return json_response["flows"] diff --git a/iofog_python_sdk/deploy/microservice_service.py b/iofog_python_sdk/deploy/microservice_service.py deleted file mode 100644 index fb9414f..0000000 --- a/iofog_python_sdk/deploy/microservice_service.py +++ /dev/null @@ -1,220 +0,0 @@ -import json - -from iofog_python_sdk.deploy.create_rest_call import rest_call -from iofog_python_sdk.deploy.pretty_print import print_info, print_error -from iofog_python_sdk.deploy.flow_service import Flow - -class microservices: - def __init__(self): - self.data = {} - - def create_microservice_curl_data(self, microservice, flow_id, fog_uuid, catalog_id): - self.data["name"] = microservice["name"] - - if len(microservice.get("volumes", [])) > 0: - self.data["volumeMappings"] = microservice["volumes"] - - if len(microservice.get("env", [])) > 0: - self.data["env"] = microservice["env"] - - if len(microservice.get("ports", [])) > 0: - self.data["ports"] = microservice["ports"] - - if "config" in microservice: - self.data["config"] = json.dumps(microservice.get("config", {})) - - self.data["rootHostAccess"] = microservice.get("root-host", False) - self.data["flowId"] = flow_id - self.data["iofogUuid"] = fog_uuid - self.data["catalogItemId"] = catalog_id - - return self.data - - def update_microservice_curl_data(self, microservice, fog_uuid, catalog_id): - self.data["name"] = microservice["name"] - - if len(microservice.get("volumes", [])) > 0: - self.data["volumeMappings"] = microservice["volumes"] - - if len(microservice.get("env", [])) > 0: - self.data["env"] = microservice["env"] - - # if len(microservice.get("ports", [])) > 0: - # data["ports"] = microservice["ports"] - - if "config" in microservice: - self.data["config"] = json.dumps(microservice.get("config", {})) - - self.data["rootHostAccess"] = microservice.get("root-host", False) - self.data["iofogUuid"] = fog_uuid - # data["catalogItemId"] = catalog_id - - return self.data - - def create_route(controller_address, auth_token, route): - post_address = "{}/microservices/{}/routes/{}".format(controller_address, route["from"], route["to"]) - json_response = rest_call({}, post_address, auth_token).response - - def delete_route(controller_address, auth_token, route): - post_address = "{}/microservices/{}/routes/{}".format(controller_address, route["from"], route["to"]) - json_response = rest_call({}, post_address, auth_token, method="DELETE").response - - def update_routing(controller_address, microservices_per_name, auth_token, routes): - print_info("====> Update routing") - updated = False - for route in routes: - # Check if route exists - msvc = microservices_per_name.get(route["from"], None) - dest_msvc = microservices_per_name.get(route["to"], None) - if msvc == None: - print_error("No source microservice for route: {} - Microservices: {}".format(route, microservices_per_name)) - continue - if dest_msvc == None: - print_error("No destination microservice for route: {} - Microservices: {}".format(route, microservices_per_name)) - continue - route_exists = msvc != None and next((x for x in msvc.get("routes", []) if x == dest_msvc["uuid"]), False) - # Create missing route - if route_exists == False: - print_info("====> Create new route") - updated = True - microservices.create_route(controller_address, auth_token, - { - "from": microservices_per_name[route["from"]]["uuid"], - "to": microservices_per_name[route["to"]]["uuid"] - }) - # Delete unecessary routes - for microservice_name in microservices_per_name: - microservice = microservices_per_name[microservice_name] - microservice_routes = microservice.get("routes", []) - for route in microservice_routes: - route_needed = next((x for x in routes if x["from"] == microservice["name"] and microservices_per_name.get(x["to"], {"uuid": None})["uuid"] == route), False) - if route_needed == False: - print_info("====> Delete outdated route") - updated = True - microservices.delete_route(controller_address, auth_token, - { - "from": microservice["uuid"], - "to": route - }) - if updated == False: - print_info("====> Routing is up-to-date.") - else: - print_info("====> Routing updated.") - - def create_microservice(self, controller_address, microservice, fog_uuid, catalog_id, flow_id, auth_token): - data = microservices.create_microservice_curl_data(microservice, flow_id, fog_uuid, catalog_id) - post_address = "{}/microservices".format(controller_address) - json_response = rest_call(data, post_address, auth_token).response - return json_response - - def get_microservice_port_mapping(self, controller_address, microservice_uuid, auth_token): - data = microservices().data - post_address = "{}/microservices/{}/port-mapping".format(controller_address, microservice_uuid) - json_response = rest_call(data, post_address, auth_token, method="GET").response - return json_response["ports"] - - def delete_microservice_port_mapping(self, controller_address, microservice_uuid, mapping, auth_token): - post_address = "{}/microservices/{}/port-mapping/{}".format(controller_address, microservice_uuid, mapping["internal"]) - json_response = rest_call(self.data, post_address, auth_token, method="DELETE").response - return json_response - - def create_microservice_port_mapping(self, controller_address, microservice_uuid, mapping, auth_token): - data = microservices().data - data = mapping - post_address = "{}/microservices/{}/port-mapping".format(controller_address, microservice_uuid) - json_response = rest_call(data, post_address, auth_token).response - return json_response - - def update_ports(self, controller_address, microservice, auth_token): - print_info("====> Getting current port mapping") - updated = False - ports = microservice.get("ports", []) - existing_port_mappings = microservices.get_microservice_port_mapping(controller_address, microservice["uuid"], auth_token) - # Remove false port mapping - for existing_mapping in existing_port_mappings: - valid = next((x for x in ports if x["internal"] == existing_mapping["internal"] and x["external"] == existing_mapping["external"]), False) - if valid == False: - print_info("====> Remove outdated port mapping") - updated = True - microservices.delete_microservice_port_mapping(controller_address, microservice["uuid"], existing_mapping, auth_token) - # Create missing port mapping - for new_mapping in ports: - exists = next((x for x in existing_port_mappings if x["internal"] == new_mapping["internal"] and x["external"] == new_mapping["external"]), False) - if exists == False: - print_info("====> Create new port mapping") - updated = True - microservices.create_microservice_port_mapping(controller_address, microservice["uuid"], new_mapping, auth_token) - if updated == False: - print_info("====> Port mapping is up-to-date") - else: - print_info("====> Port mapping updated") - - - def update_microservice(self, controller_address, microservice, fog_uuid, catalog_id, auth_token): - self.data = microservices.update_microservice_curl_data(self, microservice, fog_uuid, catalog_id) - post_address = "{}/microservices/{}".format(controller_address, microservice["uuid"]) - json_response = rest_call(self.data, post_address, auth_token, method="PATCH").response - # Update port mapping - microservices.update_ports(controller_address, microservice, auth_token) - return json_response - - def get_microservices_by_flow_id(self, controller_address, flow_id, auth_token): - data = microservices() - post_address = "{}/microservices?flowId={}".format(controller_address, flow_id) - json_response = rest_call(data, post_address, auth_token, method="GET").response - return json_response["microservices"] - - def get_all_microservices(self, controller_address, auth_token): - # Get all flows - flow_self = Flow() - flows = Flow.get_all(flow_self, controller_address, auth_token) - all_msvcs = [] - for flow in flows: - all_msvcs.extend(microservices.get_microservices_by_flow_id(self, controller_address, flow["id"], auth_token)) - return all_msvcs - - def get_microservice_by_name(self, controller_address, microservice_name, flow_id, auth_token): - msvcs = microservices.get_microservices_by_flow_id(self, controller_address, flow_id, auth_token) - return next(x for x in msvcs if x["name"] == microservice_name) - - def setup(self, controller_address, flow_id, fog_per_microservice, catalog_ids, auth_token, microservices, routes): - route = "" - microservices_per_name = {} - # Get exisiting microservices - for microserviceKey in microservices: - microservice = microservices[microserviceKey] - name = microservice["microservice"]["name"] - try: - msvc = microservices.get_microservice_by_name(controller_address, name, flow_id, auth_token) - microservices_per_name[name] = msvc - except StopIteration: - microservices_per_name[name] = None - continue - # Create missing microservices - for microserviceKey in microservices: - microservice = microservices[microserviceKey]["microservice"] - name = microservice["name"] - fog_uuid = fog_per_microservice[microserviceKey]["uuid"] - catalog_id = catalog_ids[microserviceKey] - msvc = None - if microservices_per_name[name] == None: - msvc = microservices.create_microservice(controller_address, microservice, fog_uuid, catalog_id, flow_id, auth_token) - else: - microservice["uuid"] = microservices_per_name[name]["uuid"] - microservices.update_microservice(controller_address, microservice, fog_uuid, catalog_id, auth_token) - msvc = {**microservice, **microservices_per_name[name]} - microservices_per_name[microservice["name"]] = msvc - # Update routing - microservices.update_routing(controller_address, microservices_per_name, auth_token, routes) - - def delete_microservice(self, controller_address, microservice, auth_token): - post_address = "{}/microservices/{}".format(controller_address, microservice["uuid"]) - json_response = rest_call({}, post_address, auth_token, method="DELETE").response - return json_response - - def delete_all_by_catalog_id(self, controller_address, auth_token, catalog_id): - msvcs = microservices.get_all_microservices(self, controller_address, auth_token) - print("All microservices: {}".format(msvcs)) - for msvc in msvcs: - if msvc["catalogItemId"] == catalog_id: - microservices.delete_microservice(self, controller_address, msvc, auth_token) \ No newline at end of file diff --git a/iofog_python_sdk/deploy/pretty_print.py b/iofog_python_sdk/deploy/pretty_print.py deleted file mode 100644 index 40b183b..0000000 --- a/iofog_python_sdk/deploy/pretty_print.py +++ /dev/null @@ -1,54 +0,0 @@ -from colorama import init - -# -# Pretty Print is a rather simple helper utility that exposes some helper methods to make printing logging -# information more colorful and easier to understand. -# -# Initialize colorama -init(autoreset=True) - -# These are the colors that we'll use in our output -NO_FORMAT = '\033[0m' -C_SKYBLUE1 = '\033[38;5;117m' -C_DEEPSKYBLUE4 = '\033[48;5;25m' -RED = '\033[38;5;1m' -GREEN = '\033[38;5;28m' - - -# -# Display a nice title line for any output. You can optionally populate it with a string -# -# Usage: pretty_title("Bootstrapping ioFog") -# -def pretty_title(title): - print_info("## " + title + " ####################################################") - - -# -# Display a nice header for any command line script. You can optionally populate it with a string -# -# Usage: pretty_header("Bootstrapping ioFog") -# -def pretty_header(title): - print_info("## " + title + " ####################################################") - print_info("## Copyright (C) 2019, Edgeworx, Inc.\n") - - -# Basic subtle output -def print_info(message): - print(C_SKYBLUE1 + message + NO_FORMAT) - - -# Highlighted output with a background -def print_notify(message): - print(C_DEEPSKYBLUE4 + message + NO_FORMAT) - - -# Hurrah! -def print_success(message): - print(GREEN + message + NO_FORMAT) - - -# Houston, we have a problem! -def print_error(message): - print(RED + str(message) + NO_FORMAT) diff --git a/iofog_python_sdk/client/client.py b/iofog_python_sdk/microservices/client.py similarity index 93% rename from iofog_python_sdk/client/client.py rename to iofog_python_sdk/microservices/client.py index f661139..a68572c 100644 --- a/iofog_python_sdk/client/client.py +++ b/iofog_python_sdk/microservices/client.py @@ -14,11 +14,11 @@ import subprocess -from iofog_python_sdk.client.httpclient import IoFogHttpClient -from iofog_python_sdk.client.definitions import * -from iofog_python_sdk.client.wsclient import IoFogControlWsClient, IoFogMessageWsClient -from iofog_python_sdk.client.listener import * -from iofog_python_sdk.client.exception import * +from iofog_python_sdk.microservices.httpclient import IoFogHttpClient +from iofog_python_sdk.microservices.definitions import * +from iofog_python_sdk.microservices.wsclient import IoFogControlWsClient, IoFogMessageWsClient +from iofog_python_sdk.microservices.listener import * +from iofog_python_sdk.microservices.exception import * parser = argparse.ArgumentParser() parser.add_argument("-l", "--log", dest="logLevel", choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], diff --git a/iofog_python_sdk/client/definitions.py b/iofog_python_sdk/microservices/definitions.py similarity index 100% rename from iofog_python_sdk/client/definitions.py rename to iofog_python_sdk/microservices/definitions.py diff --git a/iofog_python_sdk/client/exception.py b/iofog_python_sdk/microservices/exception.py similarity index 100% rename from iofog_python_sdk/client/exception.py rename to iofog_python_sdk/microservices/exception.py diff --git a/iofog_python_sdk/client/httpclient.py b/iofog_python_sdk/microservices/httpclient.py similarity index 91% rename from iofog_python_sdk/client/httpclient.py rename to iofog_python_sdk/microservices/httpclient.py index 9f87540..93ae5b2 100644 --- a/iofog_python_sdk/client/httpclient.py +++ b/iofog_python_sdk/microservices/httpclient.py @@ -15,10 +15,10 @@ except ImportError: from urllib2 import HTTPError #for python 2 -from iofog_python_sdk.client.definitions import * -from iofog_python_sdk.client.util import make_post_request -from iofog_python_sdk.client.iomessage import IoMessage -from iofog_python_sdk.client.exception import IoFogHttpException +from iofog_python_sdk.microservices.definitions import * +from iofog_python_sdk.microservices.util import make_post_request +from iofog_python_sdk.microservices.iomessage import IoMessage +from iofog_python_sdk.microservices.exception import IoFogHttpException class IoFogHttpClient: diff --git a/iofog_python_sdk/client/iomessage.py b/iofog_python_sdk/microservices/iomessage.py similarity index 98% rename from iofog_python_sdk/client/iomessage.py rename to iofog_python_sdk/microservices/iomessage.py index 288a0a9..282ce92 100644 --- a/iofog_python_sdk/client/iomessage.py +++ b/iofog_python_sdk/microservices/iomessage.py @@ -13,10 +13,10 @@ from struct import pack, unpack import sys -from iofog_python_sdk.client.util import * +from iofog_python_sdk.microservices.util import * import base64 -from iofog_python_sdk.client.definitions import * +from iofog_python_sdk.microservices.definitions import * # todo check fields == None? diff --git a/iofog_python_sdk/client/listener.py b/iofog_python_sdk/microservices/listener.py similarity index 100% rename from iofog_python_sdk/client/listener.py rename to iofog_python_sdk/microservices/listener.py diff --git a/iofog_python_sdk/client/util.py b/iofog_python_sdk/microservices/util.py similarity index 95% rename from iofog_python_sdk/client/util.py rename to iofog_python_sdk/microservices/util.py index 034d1c4..09434ae 100644 --- a/iofog_python_sdk/client/util.py +++ b/iofog_python_sdk/microservices/util.py @@ -17,7 +17,7 @@ import urllib2 as urllib_request # for python 2 from struct import pack -from iofog_python_sdk.client.definitions import CODE_MSG +from iofog_python_sdk.microservices.definitions import CODE_MSG def num_to_bytearray(num): diff --git a/iofog_python_sdk/client/wsclient.py b/iofog_python_sdk/microservices/wsclient.py similarity index 97% rename from iofog_python_sdk/client/wsclient.py rename to iofog_python_sdk/microservices/wsclient.py index a669cf4..9ca08ad 100644 --- a/iofog_python_sdk/client/wsclient.py +++ b/iofog_python_sdk/microservices/wsclient.py @@ -13,9 +13,9 @@ import time -import iofog_python_sdk.client.util as util -from iofog_python_sdk.client.iomessage import IoMessage -from iofog_python_sdk.client.definitions import * +import iofog_python_sdk.microservices.util as util +from iofog_python_sdk.microservices.iomessage import IoMessage +from iofog_python_sdk.microservices.definitions import * from ws4py.client.threadedclient import WebSocketClient from ws4py.framing import OPCODE_PONG diff --git a/test/recieve.py b/test/recieve.py index 6e3b512..3caa4ec 100644 --- a/test/recieve.py +++ b/test/recieve.py @@ -1,4 +1,4 @@ -from iofog_python_sdk.client import IoFogClient +from iofog_python_sdk.microservices import IoFogClient from iofog_python_sdk.exception import IoFogException from iofog_python_sdk.iomessage import IoMessage from iofog_python_sdk.listener import * diff --git a/test/send.py b/test/send.py index 4ce9fb2..55ff69e 100644 --- a/test/send.py +++ b/test/send.py @@ -1,4 +1,4 @@ -from iofog_python_sdk.client import IoFogClient +from iofog_python_sdk.microservices import IoFogClient from iofog_python_sdk.exception import IoFogException from iofog_python_sdk.iomessage import IoMessage from iofog_python_sdk.listener import * From 4a628799f24f80ea79ae5c3e47ff25c5ef75dd73 Mon Sep 17 00:00:00 2001 From: SergeRadinovich Date: Thu, 17 Sep 2020 11:58:18 +1200 Subject: [PATCH 27/31] Rename and update changelog --- CHANGELOG-1.3.md => CHANGELOG.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) rename CHANGELOG-1.3.md => CHANGELOG.md (72%) diff --git a/CHANGELOG-1.3.md b/CHANGELOG.md similarity index 72% rename from CHANGELOG-1.3.md rename to CHANGELOG.md index 9988c88..0a22dd6 100644 --- a/CHANGELOG-1.3.md +++ b/CHANGELOG.md @@ -1,12 +1,15 @@ -# Changes in 1.3.0 +# Changelog -## Added +## [v2.0.0] - unreleased + +* Remove deploy module +* Add rest.controller module +* Rename client module to microservices + +## [v1.3.0] * Ability to deploy ioFog microservices, and connections through SDK are now available. * This can be imported through iofog_python_sdk.deploy options. * Previous standard iofog_python_sdk can be imported through iofog_python_sdk.microservices * Added standards for rest calls made to ioFog under deploy.create_rest_call * Fixed issue with python3 object handling - - -## Known Issues \ No newline at end of file From a4097d7a31ddb4edc90955d8d4dab23674cb048f Mon Sep 17 00:00:00 2001 From: SergeRadinovich Date: Thu, 17 Sep 2020 12:00:46 +1200 Subject: [PATCH 28/31] Update version to 2.0.0 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index b670509..0b28a66 100644 --- a/setup.py +++ b/setup.py @@ -15,7 +15,7 @@ setuptools.setup( name='iofog-python-sdk', - version='1.3.0', + version='2.0.0', project_urls={ 'Documentation': 'https://github.com/eclipse-iofog/iofog-python-sdk/blob/master/README.md', 'Source': 'https://github.com/eclipse-iofog/iofog-python-sdk.git', From ae7436194cecde5a67cc6057c98777036c3885b3 Mon Sep 17 00:00:00 2001 From: SergeRadinovich Date: Thu, 17 Sep 2020 12:40:40 +1200 Subject: [PATCH 29/31] Fix packaging --- iofog_python_sdk/microservices/__init__.py | 0 iofog_python_sdk/rest/__init__.py | 0 iofog_python_sdk/rest/controller/__init__.py | 0 setup.py | 2 +- 4 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 iofog_python_sdk/microservices/__init__.py create mode 100644 iofog_python_sdk/rest/__init__.py create mode 100644 iofog_python_sdk/rest/controller/__init__.py diff --git a/iofog_python_sdk/microservices/__init__.py b/iofog_python_sdk/microservices/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/iofog_python_sdk/rest/__init__.py b/iofog_python_sdk/rest/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/iofog_python_sdk/rest/controller/__init__.py b/iofog_python_sdk/rest/controller/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/setup.py b/setup.py index 0b28a66..a0125ba 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ 'Tracker': 'https://github.com/eclipse-iofog/iofog-python-sdk/issues', 'Eclipse ioFog': 'http://iofog.org' }, - packages=['iofog_python_sdk'], + packages=setuptools.find_packages(), url='https://github.com/eclipse-iofog/iofog-python-sdk', license='EPL-2.0', author='Eclipse ioFog', From e5ee08163e3c5adcdd33c24cdbf4e200d6e6d93e Mon Sep 17 00:00:00 2001 From: SergeRadinovich Date: Thu, 17 Sep 2020 12:51:56 +1200 Subject: [PATCH 30/31] 2.0.1 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index a0125ba..833afc0 100644 --- a/setup.py +++ b/setup.py @@ -15,7 +15,7 @@ setuptools.setup( name='iofog-python-sdk', - version='2.0.0', + version='2.0.1', project_urls={ 'Documentation': 'https://github.com/eclipse-iofog/iofog-python-sdk/blob/master/README.md', 'Source': 'https://github.com/eclipse-iofog/iofog-python-sdk.git', From 297741d4ecec7c5ac9265a9657c0e872d1ec61e6 Mon Sep 17 00:00:00 2001 From: Xiang Zhou Date: Thu, 29 Apr 2021 16:30:32 -0700 Subject: [PATCH 31/31] fix issue in make_post_request and update tests to get it running --- Docker/Dockerfile.recieve | 4 +- Docker/Dockerfile.send | 5 ++- iofog_python_sdk/microservices/iomessage.py | 8 ++-- iofog_python_sdk/microservices/util.py | 3 ++ test.sh | 10 ++++- test/recieve.py | 33 +++++++++++--- test/send.py | 50 ++++++++++++++++----- 7 files changed, 84 insertions(+), 29 deletions(-) mode change 100644 => 100755 test.sh diff --git a/Docker/Dockerfile.recieve b/Docker/Dockerfile.recieve index 59febc5..8b545d8 100644 --- a/Docker/Dockerfile.recieve +++ b/Docker/Dockerfile.recieve @@ -5,6 +5,6 @@ COPY test/recieve.py recieve.py RUN pip install ws4py -RUN python -m pip install iofog-python-sdk +ADD iofog_python_sdk iofog_python_sdk -ENTRYPOINT ["python", "recieve.py"] \ No newline at end of file +CMD export SELFNAME=iofog; python recieve.py \ No newline at end of file diff --git a/Docker/Dockerfile.send b/Docker/Dockerfile.send index 41381ea..6b55ace 100644 --- a/Docker/Dockerfile.send +++ b/Docker/Dockerfile.send @@ -5,6 +5,7 @@ COPY test/send.py send.py RUN pip install ws4py -RUN python -m pip install iofog-python-sdk +ADD iofog_python_sdk iofog_python_sdk + +CMD export SELFNAME=iofog; python send.py -ENTRYPOINT ["python", "send.py"] \ No newline at end of file diff --git a/iofog_python_sdk/microservices/iomessage.py b/iofog_python_sdk/microservices/iomessage.py index 282ce92..dc866ca 100644 --- a/iofog_python_sdk/microservices/iomessage.py +++ b/iofog_python_sdk/microservices/iomessage.py @@ -40,8 +40,8 @@ def __init__(self): self.difficultytarget = 0 self.infotype = '' self.infoformat = '' - self.contextdata = bytearray() - self.contentdata = bytearray() + self.contextdata = '' + self.contentdata = '' def to_bytearray(self): if self.version != IO_MESSAGE_VERSION: @@ -253,7 +253,7 @@ def to_json(self): DIFFICULTY_TARGET: self.difficultytarget, INFO_TYPE: self.infotype, INFO_FORMAT: self.infoformat, - CONTEXT_DATA: base64.b64encode(self.contextdata), - CONTENT_DATA: base64.b64encode(self.contentdata) + CONTEXT_DATA: self.contextdata, + CONTENT_DATA: self.contentdata } return json.dumps(json_msg) diff --git a/iofog_python_sdk/microservices/util.py b/iofog_python_sdk/microservices/util.py index 09434ae..c77d03f 100644 --- a/iofog_python_sdk/microservices/util.py +++ b/iofog_python_sdk/microservices/util.py @@ -44,6 +44,9 @@ def bytearray_to_num(arr): def make_post_request(url, body_type, body): + # urllib.request.Request(url, data ...) + # 'data' should be encoded to bytes before being used as the data parameter. + body = body.encode("utf-8") req = urllib_request.Request(url, body, {'Content-Type': body_type}) response = urllib_request.urlopen(req) return json.loads(response.read()) diff --git a/test.sh b/test.sh old mode 100644 new mode 100755 index faadf9f..08813ae --- a/test.sh +++ b/test.sh @@ -2,17 +2,23 @@ PYTHON_VERSION=$1 -docker run --name send -d iofog/test-python-sdk-send:$PYTHON_VERSION 1>/dev/null -docker run --name recieve -d iofog/test-python-sdk-recieve:$PYTHON_VERSION 1>/dev/null +docker run --name send --network host -d iofog/test-python-sdk-send:$PYTHON_VERSION 1>/dev/null +docker run --name recieve --network host -d iofog/test-python-sdk-recieve:$PYTHON_VERSION 1>/dev/null + +echo 'wait for 5 secs to check result....' +sleep 5s SEND_CONTAINER_ID=$(docker ps -aqf "name=send") RECIEVE_CONTAINER_ID=$(docker ps -aqf "name=recieve") if [[ -z $RECIEVE_CONTAINER_ID || -z $SEND_CONTAINER_ID ]]; then + echo 'Failed: either send container or recieve container is dead, please check' exit 1 fi docker rm -f send 1>/dev/null docker rm -f recieve 1>/dev/null + +echo 'Success! ' exit 0 diff --git a/test/recieve.py b/test/recieve.py index 3caa4ec..bcd4a86 100644 --- a/test/recieve.py +++ b/test/recieve.py @@ -1,23 +1,42 @@ -from iofog_python_sdk.microservices import IoFogClient -from iofog_python_sdk.exception import IoFogException -from iofog_python_sdk.iomessage import IoMessage -from iofog_python_sdk.listener import * +from iofog_python_sdk.microservices.client import IoFogClient +from iofog_python_sdk.microservices.exception import IoFogException +from iofog_python_sdk.microservices.iomessage import IoMessage +from iofog_python_sdk.microservices.listener import * +import logging + +def init_logger(): + logger = logging.getLogger(__name__) + logger.setLevel('DEBUG') + ch = logging.StreamHandler() + ch.setLevel('DEBUG') + formatter = logging.Formatter( + '%(levelname)5s [%(asctime)-15s] %(module)10s - - %(message)s') + ch.setFormatter(formatter) + logger.addHandler(ch) def main(): + init_logger() + logger = logging.getLogger(__name__) try: client = IoFogClient() except IoFogException as e: - print("Failed to start client") + logger.error("Failed to start client") + exit(e) while True: try: messages = client.get_next_messages() for message in messages: if message.contentdata == "python2" or message.contentdata == "python3": - print("It worked") + logger.info("It worked") else: exit(1) except IoFogException as e: - print("Could not get next message") + logger.error("Could not get next message") exit(e) + + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/test/send.py b/test/send.py index 55ff69e..b1a169f 100644 --- a/test/send.py +++ b/test/send.py @@ -1,9 +1,23 @@ -from iofog_python_sdk.microservices import IoFogClient -from iofog_python_sdk.exception import IoFogException -from iofog_python_sdk.iomessage import IoMessage -from iofog_python_sdk.listener import * +from iofog_python_sdk.microservices.client import IoFogClient +from iofog_python_sdk.microservices.exception import IoFogException +from iofog_python_sdk.microservices.iomessage import IoMessage +from iofog_python_sdk.microservices.listener import * +from iofog_python_sdk.microservices.definitions import * import sys +import time +import logging + +def init_logger(): + logger = logging.getLogger(__name__) + logger.setLevel('DEBUG') + ch = logging.StreamHandler() + ch.setLevel('DEBUG') + formatter = logging.Formatter( + '%(levelname)5s [%(asctime)-15s] %(module)10s - - %(message)s') + ch.setFormatter(formatter) + logger.addHandler(ch) + def build_message(): msg = IoMessage() @@ -21,10 +35,11 @@ def build_message(): return msg def python3(): + logger = logging.getLogger(__name__) try: client = IoFogClient() except IoFogException as e: - print("Client Failed on Python3") + logger.error("Client Failed on Python3") exit(e) msg = build_message() @@ -33,17 +48,18 @@ def python3(): try: receipt = client.post_message(msg) except IoFogException as e: - print("Message Failed on Python3") + logger.error("Message Failed on Python3") exit(e) - print("Python3 working with SDK") + logger.info("Python3 working with SDK") def python2(): + logger = logging.getLogger(__name__) try: client = IoFogClient() except IoFogException as e: - print("Client Failed on Python2") + logger.error("Client Failed on Python2") exit(e) msg = build_message() @@ -52,17 +68,27 @@ def python2(): try: receipt = client.post_message(msg) except IoFogException as e: - print("Message Failed on Python2") + logger.error("Message Failed on Python2") exit(e) - print("Python2 working with SDK") + logger.info("Python2 working with SDK") + + def main(): - if sys.version_info[0] > 3: + init_logger() + if sys.version_info[0] >= 3: print("test python3") while True: + time.sleep(2) python3() else: print("test python2") while True: - python2() \ No newline at end of file + time.sleep(2) + python2() + + + +if __name__ == "__main__": + main() \ No newline at end of file