From 0179afdcde793edbb98a6b71dcac95404e41f9d6 Mon Sep 17 00:00:00 2001 From: Bruce Potter Date: Fri, 19 Jun 2020 15:16:35 -0400 Subject: [PATCH] initial files for mgmt hub deployment --- LICENSE.txt | 175 +++++++++++++++++++++++++++++++ mgmt-hub/agbot-tmpl.json | 48 +++++++++ mgmt-hub/css-tmpl.conf | 13 +++ mgmt-hub/deploy-mgmt-hub.sh | 164 +++++++++++++++++++++++++++++ mgmt-hub/docker-compose-tmpl.yml | 91 ++++++++++++++++ mgmt-hub/exchange-tmpl.json | 15 +++ 6 files changed, 506 insertions(+) create mode 100644 LICENSE.txt create mode 100644 mgmt-hub/agbot-tmpl.json create mode 100644 mgmt-hub/css-tmpl.conf create mode 100755 mgmt-hub/deploy-mgmt-hub.sh create mode 100644 mgmt-hub/docker-compose-tmpl.yml create mode 100644 mgmt-hub/exchange-tmpl.json diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..19dc35b --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,175 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. \ No newline at end of file diff --git a/mgmt-hub/agbot-tmpl.json b/mgmt-hub/agbot-tmpl.json new file mode 100644 index 0000000..ed406b7 --- /dev/null +++ b/mgmt-hub/agbot-tmpl.json @@ -0,0 +1,48 @@ +{ + "AgreementBot": { + "DBPath": "", + "Postgresql": { + "Host": "postgres", + "Port": "${POSTGRES_PORT}", + "User": "${POSTGRES_USER}", + "Password": "", + "DBName": "${AGBOT_DATABASE}", + "SSLMode": "disable", + "MaxOpenConnections": 20 + }, + "Partitions": "part0", + "TxLostDelayTolerationSeconds": 120, + "AgreementWorkers": 5, + "ProtocolTimeoutS": 120, + "AgreementTimeoutS": 360, + "NoDataIntervalS": 300, + "ActiveAgreementsURL": "", + "ActiveAgreementsUser": "", + "ActiveAgreementsPW": "", + "PolicyPath": "/home/agbotuser/policy.d", + "NewContractIntervalS": 10, + "ProcessGovernanceIntervalS": 10, + "IgnoreContractWithAttribs": "ethereum_account", + "ExchangeURL": "${HZN_TRANSPORT}://exhange-api:8080/v1", + "ExchangeId": "${EXCHANGE_SYSTEM_ORG}/${AGBOT_ID}", + "ExchangeToken":"${AGBOT_TOKEN}", + "ExchangeVersionCheckIntervalM": 1, + "ExchangeHeartbeat": 10, + "ExchangeMessageTTL": 1800, + "ActiveDeviceTimeoutS": 180, + "DefaultWorkloadPW": "", + "MessageKeyPath": "msgKey", + "APIListen": "0.0.0.0:${AGBOT_PORT}", + "PurgeArchivedAgreementHours": 1, + "CheckUpdatedPolicyS": 15, + "CSSURL": "${HZN_TRANSPORT}://css-api:${CSS_PORT}", + "SecureAPIListenHost": "0.0.0.0", + "SecureAPIListenPort": "8083", + "AgreementBatchSize": 1300 + }, + "ArchSynonyms": { + "x86_64": "amd64", + "armhf": "arm", + "aarch64": "arm64" + } +} diff --git a/mgmt-hub/css-tmpl.conf b/mgmt-hub/css-tmpl.conf new file mode 100644 index 0000000..d7fdfde --- /dev/null +++ b/mgmt-hub/css-tmpl.conf @@ -0,0 +1,13 @@ +TraceRootPath /tmp/trace +TraceLevel TRACE +UnsecureListeningPort ${CSS_PORT} +PersistenceRootPath /var/wiotp-edge/persist/ +NodeType CSS +MongoAuthDbName d_edge +MongoAddressCsv mongo:${MONGO_PORT} +LogTraceDestination stdout,glog +LogRootPath /tmp +LogLevel TRACE +ListeningType unsecure +CommunicationProtocol ${HZN_TRANSPORT} +OrgID ${EXCHANGE_USER_ORG} diff --git a/mgmt-hub/deploy-mgmt-hub.sh b/mgmt-hub/deploy-mgmt-hub.sh new file mode 100755 index 0000000..ecf3abe --- /dev/null +++ b/mgmt-hub/deploy-mgmt-hub.sh @@ -0,0 +1,164 @@ +#!/bin/bash + +# Deploy the management hub components (agbot, exchange, css, postgre, mongo), the agent, and the CLI + +# Default environment variables +#HZN_LISTEN_IP # the host IP address the hub components should listen on. Can be set to 0.0.0.0 to mean all interfaces. Defaults to the private IP address +HZN_TRANSPORT=${HZN_TRANSPORT:-http} + +EXCHANGE_IMAGE_TAG=${EXCHANGE_IMAGE_TAG:latest} # or can be set to stable or a specific version +EXCHANGE_PORT=${EXCHANGE_PORT:3090} +EXCHANGE_LOG_LEVEL=${EXCHANGE_LOG_LEVEL:-INFO} +EXCHANGE_SYSTEM_ORG=${EXCHANGE_SYSTEM_ORG:-IBM} +EXCHANGE_USER_ORG=${EXCHANGE_USER_ORG:-myorg} + + +AGBOT_IMAGE_TAG=${AGBOT_IMAGE_TAG:latest} # or can be set to stable or a specific version +AGBOT_PORT=${AGBOT_PORT:3091} +AGBOT_ID=${AGBOT_ID:agbot} # its agbot id in the exchange +AGBOT_TOKEN=${AGBOT_TOKEN:$(cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 30 | head -n 1)} # its agbot token in the exchange + +CSS_IMAGE_TAG=${CSS_IMAGE_TAG:latest} # or can be set to stable or a specific version +CSS_PORT=${CSS_PORT:9443} + +POSTGRES_IMAGE_TAG=${POSTGRES_IMAGE_TAG:latest} # or can be set to stable or a specific version +POSTGRES_PORT=${POSTGRES_PORT:5432} +POSTGRES_USER=${POSTGRES_USER:-admin} +EXCHANGE_DATABASE=${EXCHANGE_DATABASE:-exchange} # the db the exchange uses in the postgres instance +AGBOT_DATABASE=${AGBOT_DATABASE:-agbot} # the db the agbot uses in the postgres instance + +MONGO_IMAGE_TAG=${MONGO_IMAGE_TAG:latest} # or can be set to stable or a specific version +MONGO_PORT=${MONGO_PORT:27017} + +usage() { + exitCode=${1:-0} + cat << EndOfMessage +Usage: ${0##*/} [-h] + +Deploys the Open Horizon management hub components, agent, and CLI on this host. + +Flags: + -h Show this usage. + +Required Environment Variables: + EXCHANGE_ROOT_PW_BCRYPTED: the bcrypted exchange root pw to be put in the exchange config file. Can be the clear pw, but that is not recommended. + EXCHANGE_ROOT_PW: the clear exchange root pw to use temporarily to prime the exchange. + +Optional Environment Variables: + For a list of optional environment variables, their defaults and descriptions, see the beginning of this script. +EndOfMessage + exit $exitCode +} + +# Only echo this if VERBOSE is 1 or true +verbose() { + if [[ "$VERBOSE" == "1" || "$VERBOSE" == "true" ]]; then + echo 'verbose:' $* + fi +} + +# Echo message and exit +fatal() { + local exitCode=$1 + # the rest of the args are the message + echo "Error:" ${@:2} + exit $exitCode +} + +# Check the exit code passed in and exit if non-zero +chk() { + local exitCode=$1 + local task=$2 + local dontExit=$3 # set to 'continue' to not exit for this error + if [[ $exitCode == 0 ]]; then return; fi + echo "Error: exit code $exitCode from: $task" + if [[ $dontExit != 'continue' ]]; then + exit $exitCode + fi +} + +# Check both the exit code and http code passed in and exit if non-zero +chkHttp() { + local exitCode=$1 + local httpCode=$2 + local task=$3 + local dontExit=$4 # set to 'continue' to not exit for this error + chk $exitCode $task + if [[ $httpCode == 200 ]]; then return; fi + echo "Error: http code $httpCode from: $task" + if [[ $dontExit != 'continue' ]]; then + exit $httpCode + fi +} + +# Returns exit code 0 if the specified cmd is in the path +isCmdInstalled() { + local cmd=$1 + command -v $cmd >/dev/null 2>&1 +} + +# Verify that the prereq commands we need are installed +confirmCmds() { + for c in $*; do + #echo "checking $c..." + if ! isCmdInstalled $c; then + fatal 2 "$c is not installed but required, exiting" + fi + done +} + +ensureWeAreRoot() { + if [[ $(whoami) != 'root' ]]; then + fatal 2 "must be root to run ${0##*/} with these options." + fi +} + +# Download a file via a URL +getUrlFile() { + local url="$1" + echo "Downloading $url ..." + httpCode=$(curl -sS -w "%{http_code}" -L -O $url) + chkHttp $? $httpCode "downloading $url" +} + +getPrivateIp() { + ip address | grep -m 1 -o -E " inet (172|10|192.168)[^/]*" | awk '{ print $2 }' +} + +# Parse cmd line +while getopts ":h" opt; do + case $opt in + h) usage + ;; + \?) echo "Error: invalid option: -$OPTARG"; usage 1 + ;; + :) echo "Error: option -$OPTARG requires an argument"; usage 1 + ;; + esac +done + +# Initial checking of the OS +ensureWeAreRoot +confirmCmds grep awk curl + +# Get private IP to listen on, if they did not specify it otherwise +if [[ -z $HZN_LISTEN_IP ]]; then + HZN_LISTEN_IP=getPrivateIp + chk $? 'getting private IP' + if [[ -z $HZN_LISTEN_IP ]]; then fatal 2 "Could not get the private IP address"; fi +fi +echo "Manaagement hub components will listen on $HZN_LISTEN_IP" + +# Install jq envsubst (gettext-base) docker docker-compose +apt install -y jq gettext-base docker-compose +chk $? 'installing required software' + +# Download and process templates from open-horizon/devops + +# Start mgmt hub components + +# Prime exchange with the user org and admin, and horizon examples + +# Install agent and CLI + + diff --git a/mgmt-hub/docker-compose-tmpl.yml b/mgmt-hub/docker-compose-tmpl.yml new file mode 100644 index 0000000..3bf2bdc --- /dev/null +++ b/mgmt-hub/docker-compose-tmpl.yml @@ -0,0 +1,91 @@ +version: '3' + +volumes: + exchangedb: + mongovol: + +networks: + horizonnet: + driver: bridge + +services: + + agbot: + image: openhorizon/amd64_agbot:${AGBOT_IMAGE_TAG} + container_name: agbot + privileged: true + networks: horizonnet + restart: always + ports: + - ${AGBOT_PORT}:${AGBOT_PORT} + volumes: + - ${PWD}/agbot.json:/etc/horizon/anax.json + environment: + - HZN_VAR_BASE=/home/agbotuser + - HZN_DISABLE_IPTABLES_MANIPULATION=true + #- AGBOT_NAME=${AGBOT_ID} + #- CSS_URL=${CSS_URL} + #- EXCH_APP_HOST=${EXCHANGE_URL} + #- EXCH_ROOTPW=${EXCHANGE_ROOT_PW} + #- HZN_AGBOT_API=${AGBOT_URL} + depends_on: + exchange-api: + condition: service_healthy + + exchange-api: + image: openhorizon/amd64_exchange-api:${EXCHANGE_IMAGE_TAG} + container_name: exchange-api + ports: + - ${EXCHANGE_PORT}:8080 + networks: horizonnet + volumes: + - ${PWD}/exchange.json:/etc/horizon/exchange/config.json + depends_on: + postgres: + condition: service_healthy + healthcheck: + test: test $(curl -sS -w "%{http_code}" -o /dev/null http://localhost:8080/v1/admin/version) == 201 + interval: 2s + timeout: 2s + retries: 10 + + postgres: + image: postgres:${POSTGRES_IMAGE_TAG} + container_name: postgres + restart: always + volumes: + - exchangedb:/var/lib/postgresql/data + networks: horizonnet + environment: + - POSTGRES_DB=${EXCHANGE_DATABASE} + - POSTGRES_USER=${POSTGRES_USER} + - POSTGRES_HOST_AUTH_METHOD=trust + healthcheck: + test: pg_isready -U ${POSTGRES_USER} + interval: 2s + timeout: 2s + retries: 10 + + css-api: + image: openhorizon/amd64_cloud-sync-service:${CSS_IMAGE_TAG} + container_name: css-api + restart: always + ports: + - ${CSS_PORT}:${CSS_PORT} + networks: horizonnet + volumes: + - ${PWD}/css:/etc/edge-sync-service + - mongovol:/var/wiotp-edge/persist + environment: + - HZN_EXCHANGE_URL=${HZN_TRANSPORT}://exhange-api:8080/v1 + depends_on: + mongo: + condition: service_healthy + + mongo: + image: mongo:${MONGO_IMAGE_TAG} + container_name: mongo + restart: always + volumes: + - mongovol:/var/wiotp-edge/persist + networks: horizonnet diff --git a/mgmt-hub/exchange-tmpl.json b/mgmt-hub/exchange-tmpl.json new file mode 100644 index 0000000..eb24d57 --- /dev/null +++ b/mgmt-hub/exchange-tmpl.json @@ -0,0 +1,15 @@ +{ + "api": { + "db": { + "jdbcUrl": "jdbc:postgresql://postgres:${POSTGRES_PORT}/${EXCHANGE_DATABASE}", + "user": "${POSTGRES_USER}", + "password": "" + }, + "logging": { + "level": "${EXCHANGE_LOG_LEVEL}" + }, + "root": { + "password": "${EXCHANGE_ROOT_PW_BCRYPTED}" + } + } +}