#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params

# Solution: devicesimulation-nohub

# Note: this script is invoked by setup-wrapper.sh and errors are stored in /app/setup.log
set -ex

APP_PATH="/app"
WEBUICONFIG="${APP_PATH}/webui-config.js"
WEBUICONFIG_SAFE="${APP_PATH}/webui-config.js.safe"
WEBUICONFIG_UNSAFE="${APP_PATH}/webui-config.js.unsafe"
ENVVARS="${APP_PATH}/env-vars"
DOCKERCOMPOSE="${APP_PATH}/docker-compose.yml"
CERTS="${APP_PATH}/certs"
CERT="${CERTS}/tls.crt"
PKEY="${CERTS}/tls.key"

# ========================================================================

# Default values
export HOST_NAME="localhost"
export PCS_LOG_LEVEL="Info"
export PCS_WEBUI_AUTH_TYPE="aad"
export PCS_IOTHUB_CONNSTRING=""

while [ "$#" -gt 0 ]; do
    case "$1" in
        --solution-setup-url)      PCS_SOLUTION_SETUP_URL="$2" ;; # e.g. https://raw.githubusercontent.com/Azure/azure-iot-pcs-device-simulation/DS-1.0.0/arm-deployment/devicesimulation-nohub
        --release-version)         PCS_RELEASE_VERSION="$2" ;;
        --docker-tag)              PCS_DOCKER_TAG="$2" ;;
        --solution-type)           PCS_SOLUTION_TYPE="$2" ;;
        --solution-name)           PCS_SOLUTION_NAME="$2" ;;
        --subscription-domain)     PCS_SUBSCRIPTION_DOMAIN="$2" ;;
        --subscription-id)         PCS_SUBSCRIPTION_ID="$2" ;;
        --hostname)                HOST_NAME="$2" ;;
        --log-level)               PCS_LOG_LEVEL="$2" ;;
        --resource-group)          PCS_RESOURCE_GROUP="$2" ;;
        --docdb-name)              PCS_DOCDB_NAME="$2" ;;
        --docdb-connstring)        PCS_STORAGEADAPTER_DOCUMENTDB_CONNSTRING="$2" ;;
        --ssl-certificate)         PCS_CERTIFICATE="$2" ;;
        --ssl-certificate-key)     PCS_CERTIFICATE_KEY="$2" ;;
        --auth-audience)           PCS_AUTH_AUDIENCE="$2" ;;
        --auth-issuer)             PCS_AUTH_ISSUER="$2" ;;
        --auth-type)               PCS_WEBUI_AUTH_TYPE="$2" ;;
        --aad-appid)               PCS_WEBUI_AUTH_AAD_APPID="$2" ;;
        --aad-sp-client-id)        PCS_AAD_CLIENT_SP_ID="$2" ;;
        --aad-app-secret)          PCS_AAD_SECRET="$2" ;;
        --aad-tenant)              PCS_WEBUI_AUTH_AAD_TENANT="$2" ;;
        --aad-instance)            PCS_WEBUI_AUTH_AAD_INSTANCE="$2" ;;
        --cloud-type)              PCS_CLOUD_TYPE="$2" ;;
        --deployment-id)           PCS_DEPLOYMENT_ID="$2" ;;
        --resource-group-location) PCS_RESOURCE_GROUP_LOCATION="$2" ;;
        --vmss-name)               PCS_VMSS_NAME="$2" ;;
        --storage-connstring)      PCS_AZURE_STORAGE_ACCOUNT="$2" ;;
        --app-insights-ikey)       PCS_APPINSIGHTS_INSTRUMENTATIONKEY="$2" ;;
    esac
    shift
done

if [ -z "$PCS_SOLUTION_SETUP_URL" ]; then
    echo "Setup URL not specified (see --solution-setup-url)"
    exit 1
fi

if [ -z "$PCS_RELEASE_VERSION" ]; then
    echo "Release version not specified (see --release-version)"
    exit 1
fi

# Note: Solution = devicesimulation-nohub
REPOSITORY="https://raw.githubusercontent.com/Azure/azure-iot-pcs-device-simulation/${PCS_RELEASE_VERSION}/arm-deployment/devicesimulation-nohub/single-vm"
SCRIPTS_URL="${REPOSITORY}/scripts/"
SETUP_URL="${REPOSITORY}/setup/"

# ========================================================================

### Install Docker and Docker Compose

install_docker_ce() {
    apt-get update -o Acquire::CompressionTypes::Order::=gz \
        && DEBIAN_FRONTEND=noninteractive apt-get upgrade -y \
        && apt-get update \
        && apt-get remove docker docker-engine docker.io \
        && apt-get -y --allow-downgrades --allow-remove-essential --allow-change-held-packages --no-install-recommends install apt-transport-https ca-certificates curl gnupg2 software-properties-common \
        && curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg | sudo apt-key add - \
        && add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") $(lsb_release -cs) stable" \
        && apt-get update \
        && DEBIAN_FRONTEND=noninteractive apt-get -y --allow-downgrades install docker-ce docker-compose \
        && docker run --rm hello-world && docker rmi hello-world

    local RESULT=$?
    if [ $RESULT -ne 0 ]; then
        INSTALL_DOCKER_RESULT="FAIL"
    else
        INSTALL_DOCKER_RESULT="OK"
    fi
}

set +e
INSTALL_DOCKER_RESULT="OK"
install_docker_ce
if [ "$INSTALL_DOCKER_RESULT" != "OK" ]; then
    set -e
    echo "Error: first attempt to install Docker failed, retrying..."
    # Retry once, in case apt wasn't ready
    sleep 30
    install_docker_ce
    if [ "$INSTALL_DOCKER_RESULT" != "OK" ]; then
        echo "Error: Docker installation failed"
        exit 1
    fi
fi
set -e

# ========================================================================

# Configure Docker registry based on host name
# ToDo: we may need to add similar parameter to AzureGermanCloud and AzureUSGovernment
config_for_azure_china() {
    set +e
    local host_name=$1
    if (echo $host_name | grep -c  "\.cn$") ; then
        # If the host name has .cn suffix, dockerhub in China will be used to avoid slow network traffic failure.
        local config_file='/etc/docker/daemon.json'
        echo "{\"registry-mirrors\": [\"https://registry.docker-cn.com\"]}" > ${config_file}
        service docker restart

        # Rewrite the AAD issuer in Azure China environment
        export PCS_AUTH_ISSUER="https://sts.chinacloudapi.cn/$2/"
    fi
    set -e
}

config_for_azure_china $HOST_NAME $PCS_WEBUI_AUTH_AAD_TENANT

# ========================================================================

# Note: the directory might already exists, with "-p" there is no error
mkdir -p ${APP_PATH}
cd ${APP_PATH}

# ========================================================================

# Docker compose file

DOCKERCOMPOSE_SOURCE="${PCS_SOLUTION_SETUP_URL}/single-vm/docker-compose.yml"
wget $DOCKERCOMPOSE_SOURCE -O ${DOCKERCOMPOSE}
sed -i 's/${PCS_DOCKER_TAG}/'${PCS_DOCKER_TAG}'/g' ${DOCKERCOMPOSE}

# ========================================================================

# HTTPS certificates
mkdir -p ${CERTS}
touch ${CERT} && chmod 550 ${CERT}
touch ${PKEY} && chmod 550 ${PKEY}
# Always have quotes around the certificate and key value to preserve the formatting
echo "${PCS_CERTIFICATE}"      > ${CERT}
echo "${PCS_CERTIFICATE_KEY}"  > ${PKEY}

# ========================================================================

# Download scripts
wget $SCRIPTS_URL/logs.sh     -O /app/logs.sh     && chmod 750 /app/logs.sh
wget $SCRIPTS_URL/start.sh    -O /app/start.sh    && chmod 750 /app/start.sh
wget $SCRIPTS_URL/stats.sh    -O /app/stats.sh    && chmod 750 /app/stats.sh
wget $SCRIPTS_URL/status.sh   -O /app/status.sh   && chmod 750 /app/status.sh
wget $SCRIPTS_URL/stop.sh     -O /app/stop.sh     && chmod 750 /app/stop.sh

# ========================================================================

# Web App configuration
touch ${WEBUICONFIG} && chmod 444 ${WEBUICONFIG}
touch ${WEBUICONFIG_SAFE} && chmod 444 ${WEBUICONFIG_SAFE}
touch ${WEBUICONFIG_UNSAFE} && chmod 444 ${WEBUICONFIG_UNSAFE}

echo "var DeploymentConfig = {"                        > ${WEBUICONFIG_SAFE}
echo "  solutionName: '${PCS_SOLUTION_NAME}',"        >> ${WEBUICONFIG_SAFE}
echo "  authEnabled: true,"                           >> ${WEBUICONFIG_SAFE}
echo "  authType: '${PCS_WEBUI_AUTH_TYPE}',"          >> ${WEBUICONFIG_SAFE}
echo "  aad : {"                                      >> ${WEBUICONFIG_SAFE}
echo "    tenant: '${PCS_WEBUI_AUTH_AAD_TENANT}',"    >> ${WEBUICONFIG_SAFE}
echo "    appId: '${PCS_WEBUI_AUTH_AAD_APPID}',"      >> ${WEBUICONFIG_SAFE}
echo "    instance: '${PCS_WEBUI_AUTH_AAD_INSTANCE}'" >> ${WEBUICONFIG_SAFE}
echo "  },"                                           >> ${WEBUICONFIG_SAFE}
echo "  maxDevicesPerSimulation: 20000,"              >> ${WEBUICONFIG_SAFE}
echo "  minTelemetryInterval: 10000"                  >> ${WEBUICONFIG_SAFE}
echo "}"                                              >> ${WEBUICONFIG_SAFE}

echo "var DeploymentConfig = {"                        > ${WEBUICONFIG_UNSAFE}
echo "  solutionName: '${PCS_SOLUTION_NAME}',"        >> ${WEBUICONFIG_UNSAFE}
echo "  authEnabled: false,"                          >> ${WEBUICONFIG_UNSAFE}
echo "  authType: '${PCS_WEBUI_AUTH_TYPE}',"          >> ${WEBUICONFIG_UNSAFE}
echo "  aad : {"                                      >> ${WEBUICONFIG_UNSAFE}
echo "    tenant: '${PCS_WEBUI_AUTH_AAD_TENANT}',"    >> ${WEBUICONFIG_UNSAFE}
echo "    appId: '${PCS_WEBUI_AUTH_AAD_APPID}',"      >> ${WEBUICONFIG_UNSAFE}
echo "    instance: '${PCS_WEBUI_AUTH_AAD_INSTANCE}'" >> ${WEBUICONFIG_UNSAFE}
echo "  },"                                           >> ${WEBUICONFIG_UNSAFE}
echo "  maxDevicesPerSimulation: 20000,"              >> ${WEBUICONFIG_UNSAFE}
echo "  minTelemetryInterval: 10000"                  >> ${WEBUICONFIG_UNSAFE}
echo "}"                                              >> ${WEBUICONFIG_UNSAFE}

cp -p ${WEBUICONFIG_SAFE} ${WEBUICONFIG}

# ========================================================================

# Environment variables
touch ${ENVVARS} && chmod 440 ${ENVVARS}

echo "# Valid values: Debug, Info, Warn, Error"                                                           > ${ENVVARS}
echo "export PCS_LOG_LEVEL=\"${PCS_LOG_LEVEL}\""                                                         >> ${ENVVARS}
echo ""                                                                                >> ${ENVVARS}
echo "export HOST_NAME=\"${HOST_NAME}\""                                                                 >> ${ENVVARS}
echo "export PCS_AUTH_ISSUER=\"${PCS_AUTH_ISSUER}\""                                                     >> ${ENVVARS}
echo "export PCS_AUTH_AUDIENCE=\"${PCS_AUTH_AUDIENCE}\""                                                 >> ${ENVVARS}
echo "export PCS_STORAGEADAPTER_DOCUMENTDB_CONNSTRING=\"${PCS_STORAGEADAPTER_DOCUMENTDB_CONNSTRING}\""   >> ${ENVVARS}
echo "export PCS_SUBSCRIPTION_DOMAIN=\"${PCS_SUBSCRIPTION_DOMAIN}\""                                     >> ${ENVVARS}
echo "export PCS_SUBSCRIPTION_ID=\"${PCS_SUBSCRIPTION_ID}\""                                             >> ${ENVVARS}
echo "export PCS_WEBUI_AUTH_AAD_APPID=\"${PCS_WEBUI_AUTH_AAD_APPID}\""                                   >> ${ENVVARS}
echo "export PCS_WEBUI_AUTH_AAD_TENANT=\"${PCS_WEBUI_AUTH_AAD_TENANT}\""                                 >> ${ENVVARS}
echo "export PCS_AAD_CLIENT_SP_ID=\"${PCS_AAD_CLIENT_SP_ID}\""                                           >> ${ENVVARS}
echo "export PCS_AAD_SECRET=\"${PCS_AAD_SECRET}\""                                                       >> ${ENVVARS}
echo "export PCS_RESOURCE_GROUP=\"${PCS_RESOURCE_GROUP}\""                                               >> ${ENVVARS}
echo "export PCS_SOLUTION_TYPE=\"${PCS_SOLUTION_TYPE}\""                                                 >> ${ENVVARS}
echo "export PCS_SOLUTION_NAME=\"${PCS_SOLUTION_NAME}\""                                                 >> ${ENVVARS}
echo "export PCS_SEED_TEMPLATE=\"multiple-simulations-template\""                                        >> ${ENVVARS}
echo "export PCS_CLOUD_TYPE=\"${PCS_CLOUD_TYPE}\""                                                       >> ${ENVVARS}
echo "export PCS_DEPLOYMENT_ID=\"${PCS_DEPLOYMENT_ID}\""                                                 >> ${ENVVARS}
echo "export PCS_AZURE_STORAGE_ACCOUNT=\"${PCS_AZURE_STORAGE_ACCOUNT}\""                                 >> ${ENVVARS}
echo "export PCS_RESOURCE_GROUP_LOCATION=\"${PCS_RESOURCE_GROUP_LOCATION}\""                             >> ${ENVVARS}
echo "export PCS_VMSS_NAME=\"${PCS_VMSS_NAME}\""                                                         >> ${ENVVARS}
echo "export PCS_APPINSIGHTS_INSTRUMENTATIONKEY=\"${PCS_APPINSIGHTS_INSTRUMENTATIONKEY}\""               >> ${ENVVARS}

# Setting some empty vars as these are required vars by Config service
echo "export PCS_DEVICESIMULATION_WEBSERVICE_URL=\"\""                                                   >> ${ENVVARS}
echo "export PCS_TELEMETRY_WEBSERVICE_URL=\"\""                                                          >> ${ENVVARS}
echo "export PCS_IOTHUBMANAGER_WEBSERVICE_URL=\"\""                                                      >> ${ENVVARS}
echo "export PCS_BINGMAP_KEY=\"\""                                                                       >> ${ENVVARS}

echo ""                                                                                >> ${ENVVARS}
echo "##########################################################################################"        >> ${ENVVARS}
echo "# Development settings, don't change these in Production"                                          >> ${ENVVARS}
echo "# You can run 'start.sh --unsafe' to temporarily disable Auth and Cross-Origin protections"        >> ${ENVVARS}
echo ""                                                                                >> ${ENVVARS}
echo "# Format: true | false"                                                                            >> ${ENVVARS}
echo "# empty => Auth required"                                                                          >> ${ENVVARS}
echo "export PCS_AUTH_REQUIRED=\"\""                                                                     >> ${ENVVARS}
echo ""                                                                                >> ${ENVVARS}
echo "# Format: { 'origins': ['*'], 'methods': ['*'], 'headers': ['*'] }"                                >> ${ENVVARS}
echo "# empty => CORS support disabled"                                                                  >> ${ENVVARS}
echo "export PCS_CORS_WHITELIST=\"\""                                                                    >> ${ENVVARS}

# ========================================================================

# Shell environment enhancements
echo "### CUSTOMIZATIONS ###" >> /etc/nanorc
wget $SETUP_URL/nanorc -O /tmp/nanorc && cat /tmp/nanorc >> /etc/nanorc

echo "### CUSTOMIZATIONS ###" >> /etc/bash.bashrc
wget $SETUP_URL/bashrc -O /tmp/bashrc && cat /tmp/bashrc >> /etc/bash.bashrc

# Optional script to customize Bash shell, to be executed manually by the user
wget https://aka.ms/bashir-setup -O /etc/bashir-script \
    && chmod 444 /etc/bashir-script \
    && echo 'cat /etc/bashir-script | bash && . ~/.bashir && echo "Restart Bash to see the changes (e.g. exit and login again)."' > /usr/local/bin/bashir-setup \
    && chmod 755 /usr/local/bin/bashir-setup

# ========================================================================

# Auto-start after reboots
wget $SETUP_URL/init -O /etc/init.d/azure-iot-solution \
    && chmod 755 /etc/init.d/azure-iot-solution \
    && update-rc.d azure-iot-solution defaults

# ========================================================================

nohup /app/start.sh > /dev/null 2>&1&


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

cd /app

if [[ "$1" == "" ]]; then
  docker-compose logs
else
  docker logs -f --tail 100 $1
fi


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

# Network settings
sysctl -w net.ipv4.ip_local_port_range="1024 65535"
sysctl -w net.ipv4.tcp_max_syn_backlog=4096
sysctl -w net.core.somaxconn=1024

cd /app

source "env-vars"

COL_NO="\033[0m" # no color
COL_WARN="\033[1;33m" # light yellow
COL_ERR="\033[1;31m" # light red

APP_PATH="/app"
WEBUICONFIG="${APP_PATH}/webui-config.js"
WEBUICONFIG_SAFE="${APP_PATH}/webui-config.js.safe"
WEBUICONFIG_UNSAFE="${APP_PATH}/webui-config.js.unsafe"

rm -f ${WEBUICONFIG}
cp -p ${WEBUICONFIG_SAFE} ${WEBUICONFIG}

if [[ "$1" == "--unsafe" || "$2" == "--unsafe" ]]; then
  echo -e "${COL_ERR}WARNING! Starting services in UNSAFE mode!${COL_NO}"
  # Disable Auth
  export PCS_AUTH_REQUIRED="false"
  # Allow cross-origin requests from anywhere
  export PCS_CORS_WHITELIST="{ 'origins': ['*'], 'methods': ['*'], 'headers': ['*'] }"

  rm -f ${WEBUICONFIG}
  cp -p ${WEBUICONFIG_UNSAFE} ${WEBUICONFIG}
fi

list=$(docker ps -aq)
if [ -n "$list" ]; then
    echo -e "${COL_WARN}Stopping existing services...${COL_NO}"
    docker rm -f $list
fi

if [[ "$1" == "--debug" || "$2" == "--debug" ]]; then
  echo -e "${COL_WARN}Starting services... Press CTRL+C to exit${COL_NO}"
  docker-compose up
  exit 0
fi

echo -e "${COL_WARN}Starting services...${COL_NO}"
nohup docker-compose up > /dev/null 2>&1&

ISUP=$(curl -ks https://localhost/ | grep -i "html" | wc -l)
while [[ "$ISUP" == "0" ]]; do
  echo "Waiting for web site to start..."
  sleep 3
  ISUP=$(curl -ks https://localhost/ | grep -i "html" | wc -l)
done


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

cd /app

docker stats -a


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

cd /app

echo

docker-compose ps

COL_NO="\033[0m" # no color
COL_MARK="\033[1;33m" # yellow

echo -e "\n${COL_MARK}Run ./logs.sh <service name> to see the logs of a service.${COL_NO}"


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

list=$(docker ps -aq)

if [ -n "$list" ]; then
    docker rm -f $list
fi


#!/usr/bin/env bash

set -e -x

mkdir -p /app/logs
nginx -c /app/nginx.conf


#!/usr/bin/env bash

set -e

SIM_ID="$1"
DEVICE_COUNT="$2"

if [ -z "$SIM_ID" ]; then
    echo "Simulation ID not specified. Usage: <script> <SIM_ID> <COUNT>"
    exit 1
fi

if [ -z "$DEVICE_COUNT" ]; then
    echo "Simulation ID not specified. Usage: <script> <SIM_ID> <COUNT>"
    exit 1
fi

CONN_STRING="default"
SIM_ENABLED="true"
DELETE_DEVICES="true"

JSON='{'
JSON=$JSON'"Name": "-",'
JSON=$JSON'"Description": "-",'
#JSON=$JSON'"StartTime": "...",'
#JSON=$JSON'"EndTime": "...",'
JSON=$JSON'"Enabled": '$SIM_ENABLED','
JSON=$JSON'"DeleteDevicesWhenSimulationEnds": '$DELETE_DEVICES','
JSON=$JSON'"IoTHubs": [{ "ConnectionString": "'$CONN_STRING'" }],'
JSON=$JSON'"DeviceModels": [{"Id": "truck-01","Count": '$DEVICE_COUNT'}],'
JSON=$JSON'"RateLimits": {'
JSON=$JSON'"RegistryOperationsPerMinute": 100,'
JSON=$JSON'"TwinReadsPerSecond": 10,'
JSON=$JSON'"TwinWritesPerSecond": 10,'
JSON=$JSON'"ConnectionsPerSecond": 120,'
JSON=$JSON'"DeviceMessagesPerSecond": 120'
JSON=$JSON'}'
JSON=$JSON'}'

curl -v -X PUT 'http://localhost:9003/v1/simulations/'$SIM_ID \
    -H 'Content-Type: application/json' \
    -d "${JSON}"


#!/usr/bin/env bash

set -e

SIM_ID="$1"

if [ -z "$SIM_ID" ]; then
    echo "Simulation ID not specified. Usage: <script> <SIM_ID>"
    exit 1
fi

curl -v -X DELETE 'http://localhost:9003/v1/simulations/'$SIM_ID


#!/usr/bin/env bash

# You can uncomment and set your connection string here
# BECAREFUL not to commit this file into the repository

# PCS_STORAGEADAPTER_DOCUMENTDB_CONNSTRING="AccountEndpoint=https://_______.documents.azure.com:443/;AccountKey=_______;"

if [ ! -z "$1" ]; then
    PCS_STORAGEADAPTER_DOCUMENTDB_CONNSTRING="$1"
fi

docker run -itd \
    -p 9022:9022 \
    -e PCS_STORAGEADAPTER_DOCUMENTDB_CONNSTRING \
    --name pcs-storage-adapter \
    azureiotpcs/pcs-storage-adapter-dotnet:testing


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.

cd /app/

cd webservice && dotnet Microsoft.Azure.IoTSolutions.DeviceSimulation.WebService.dll


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

# Sometimes when creating bash scripts in Windows, bash scripts will not have
# the +x flag carried over to Linux/MacOS. This script should help setting the
# permission flags right.

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
cd $APP_HOME

chmod ugo+x ./scripts/build
chmod ugo+x ./scripts/compile
chmod ugo+x ./scripts/run
chmod ugo+x ./scripts/clean-up
chmod ugo+x ./scripts/env-vars-check
chmod ugo+x ./scripts/env-vars-setup
chmod ugo+x ./scripts/travis
chmod ugo+x ./scripts/docker/build
chmod ugo+x ./scripts/docker/run
chmod ugo+x ./scripts/docker/publish
chmod ugo+x ./scripts/docker/content/*.sh 2> /dev/null
chmod ugo+x ./scripts/git/setup
chmod ugo+x ./scripts/git/*.sh

git update-index --chmod=+x ./scripts/build
git update-index --chmod=+x ./scripts/compile
git update-index --chmod=+x ./scripts/run
git update-index --chmod=+x ./scripts/clean-up
git update-index --chmod=+x ./scripts/env-vars-check
git update-index --chmod=+x ./scripts/env-vars-setup
git update-index --chmod=+x ./scripts/travis
git update-index --chmod=+x ./scripts/docker/build
git update-index --chmod=+x ./scripts/docker/run
git update-index --chmod=+x ./scripts/docker/publish
git update-index --chmod=+x ./scripts/docker/content/*.sh
git update-index --chmod=+x ./scripts/git/setup
git update-index --chmod=+x ./scripts/git/*.sh

set +e


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

# Path relative to .git/hooks/
APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
cd $APP_HOME

./scripts/git/pre-commit.sh --no-sandbox

set +e


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

# Path relative to .git/hooks/
APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
cd $APP_HOME

./scripts/git/pre-commit.sh --with-sandbox

set +e


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
source "$APP_HOME/scripts/git/.functions.sh"

SANDBOX_MODE="$1"

cd $APP_HOME
echo "Current folder: `pwd`"

if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

check_filenames() {
    header "Checking filenames..."

    # Redirect output to stderr.
    exec 1>&2

    # Cross platform projects tend to avoid non-ASCII filenames; prevent
    # them from being added to the repository. We exploit the fact that the
    # printable range starts at the space character and ends with tilde.
    # Note that the use of brackets around a tr range is ok here, (it's
    # even required, for portability to Solaris 10's /usr/bin/tr), since
    # the square bracket bytes happen to fall in the designated range.
    set +e
    if test $(git diff --cached --name-only --diff-filter=A -z $against | LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 ; then
        error "Attempt to add a non-ASCII file name. This can cause problems on other platforms."
        exit 1
    fi
    set -e
}

check_whitespaces() {
    header "Checking white spaces and line separators..."
    git diff-index --check --cached $against --
}

check_do_not_commit() {
    PATTERN1="DONOT"
    PATTERN1="${PATTERN1}COMMIT"
    PATTERN2="DO NOT"
    PATTERN2="${PATTERN2} COMMIT"
    PATTERN3="DONT"
    PATTERN3="${PATTERN3}COMMIT"
    PATTERN4="DONT"
    PATTERN4="${PATTERN4} COMMIT"
    PATTERN5="DON'T"
    PATTERN5="${PATTERN5} COMMIT"

    header "Checking diff for comments containing '${PATTERN1}'..."

    set +e

    PATT="^\+.*${PATTERN1}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN1}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN2}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN2}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN3}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN3}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN4}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN4}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN5}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN5}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    set -e
}

verify_build() {
    header "Verifying build..."

    cd $APP_HOME/scripts
    if [[ "$SANDBOX_MODE" == "--with-sandbox" ]]; then
        ./build --in-sandbox
    else
        ./build
    fi

    if [ $? -ne 0 ]; then
        error "Some tests failed."
        exit 1
    else
        header "All tests passed"
    fi
}

check_filenames
check_whitespaces
check_do_not_commit
verify_build

set +e


#!/usr/bin/env bash

cd /app/

cd webservice

dotnet Microsoft.Azure.IoTSolutions.UIConfig.WebService.dll


#!/usr/bin/env bash -e

# Sometimes when creating bash scripts in Windows, bash scripts will not have
# the +x flag carried over to Linux/MacOS. This script should help setting the
# permission flags right.

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
cd $APP_HOME

set +e

chmod ugo+x ./scripts/build               2> /dev/null
chmod ugo+x ./scripts/compile             2> /dev/null
chmod ugo+x ./scripts/run                 2> /dev/null
chmod ugo+x ./scripts/clean-up            2> /dev/null
chmod ugo+x ./scripts/env-vars-check      2> /dev/null
chmod ugo+x ./scripts/env-vars-setup      2> /dev/null
chmod ugo+x ./scripts/travis              2> /dev/null
chmod ugo+x ./scripts/docker/build        2> /dev/null
chmod ugo+x ./scripts/docker/run          2> /dev/null
chmod ugo+x ./scripts/docker/publish      2> /dev/null
chmod ugo+x ./scripts/docker/content/*.sh 2> /dev/null
chmod ugo+x ./scripts/git/setup           2> /dev/null
chmod ugo+x ./scripts/git/*.sh            2> /dev/null

if [ -d ./scripts/iothub ]; then
  chmod ugo+x ./scripts/iothub/*.sh       2> /dev/null
fi

git update-index --chmod=+x ./scripts/build
git update-index --chmod=+x ./scripts/compile
git update-index --chmod=+x ./scripts/run
git update-index --chmod=+x ./scripts/clean-up
git update-index --chmod=+x ./scripts/env-vars-check
git update-index --chmod=+x ./scripts/env-vars-setup
git update-index --chmod=+x ./scripts/travis
git update-index --chmod=+x ./scripts/docker/build
git update-index --chmod=+x ./scripts/docker/run
git update-index --chmod=+x ./scripts/docker/publish
git update-index --chmod=+x ./scripts/docker/content/*.sh
git update-index --chmod=+x ./scripts/git/setup
git update-index --chmod=+x ./scripts/git/*.sh

if [ -d ./scripts/iothub ]; then
  git update-index --chmod=+x ./scripts/iothub/*.sh
fi


#!/usr/bin/env bash

set -e

# Path relative to .git/hooks/
APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
cd $APP_HOME

./scripts/git/pre-commit.sh --no-sandbox

set +e


#!/usr/bin/env bash

set -e

# Path relative to .git/hooks/
APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
cd $APP_HOME

./scripts/git/pre-commit.sh --with-sandbox

set +e


#!/usr/bin/env bash

set -e
APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
source "$APP_HOME/scripts/git/.functions.sh"

SANDBOX_MODE="$1"

cd $APP_HOME
echo "Current folder: `pwd`"

if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

check_filenames() {
    header "Checking filenames..."

    # Redirect output to stderr.
    exec 1>&2

    # Cross platform projects tend to avoid non-ASCII filenames; prevent
    # them from being added to the repository. We exploit the fact that the
    # printable range starts at the space character and ends with tilde.
    # Note that the use of brackets around a tr range is ok here, (it's
    # even required, for portability to Solaris 10's /usr/bin/tr), since
    # the square bracket bytes happen to fall in the designated range.
    set +e
    if test $(git diff --cached --name-only --diff-filter=A -z $against | LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 ; then
        error "Attempt to add a non-ASCII file name. This can cause problems on other platforms."
        exit 1
    fi
    set -e
}

check_whitespaces() {
    header "Checking white spaces and line separators..."
    git diff-index --check --cached $against --
}

check_do_not_commit() {
    PATTERN1="DONOT"
    PATTERN1="${PATTERN1}COMMIT"
    PATTERN2="DO NOT"
    PATTERN2="${PATTERN2} COMMIT"
    PATTERN3="DONT"
    PATTERN3="${PATTERN3}COMMIT"
    PATTERN4="DONT"
    PATTERN4="${PATTERN4} COMMIT"
    PATTERN5="DON'T"
    PATTERN5="${PATTERN5} COMMIT"

    header "Checking diff for comments containing '${PATTERN1}'..."

    set +e

    PATT="^\+.*${PATTERN1}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN1}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN2}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN2}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN3}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN3}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN4}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN4}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN5}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN5}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    set -e
}

verify_build() {
    header "Verifying build..."

    cd $APP_HOME/scripts
    if [[ "$SANDBOX_MODE" == "--with-sandbox" ]]; then
        ./build --in-sandbox
    else
        ./build
    fi

    if [ $? -ne 0 ]; then
        error "Some tests failed."
        exit 1
    else
        header "All tests passed"
    fi
}

check_filenames
check_whitespaces
check_do_not_commit
verify_build

set +e


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.

cd /app/

cd webservice

dotnet Microsoft.Azure.IoTSolutions.Diagnostics.WebService.dll


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

# Sometimes when creating bash scripts in Windows, bash scripts will not have
# the +x flag carried over to Linux/MacOS. This script should help setting the
# permission flags right.

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
cd $APP_HOME

set +e

chmod ugo+x ./scripts/build               2> /dev/null
chmod ugo+x ./scripts/compile             2> /dev/null
chmod ugo+x ./scripts/run                 2> /dev/null
chmod ugo+x ./scripts/clean-up            2> /dev/null
chmod ugo+x ./scripts/env-vars-check      2> /dev/null
chmod ugo+x ./scripts/env-vars-setup      2> /dev/null
chmod ugo+x ./scripts/travis              2> /dev/null
chmod ugo+x ./scripts/docker/build        2> /dev/null
chmod ugo+x ./scripts/docker/run          2> /dev/null
chmod ugo+x ./scripts/docker/publish      2> /dev/null
chmod ugo+x ./scripts/docker/content/*.sh 2> /dev/null
chmod ugo+x ./scripts/git/setup           2> /dev/null
chmod ugo+x ./scripts/git/*.sh            2> /dev/null

git update-index --chmod=+x ./scripts/build
git update-index --chmod=+x ./scripts/compile
git update-index --chmod=+x ./scripts/run
git update-index --chmod=+x ./scripts/clean-up
git update-index --chmod=+x ./scripts/env-vars-check
git update-index --chmod=+x ./scripts/env-vars-setup
git update-index --chmod=+x ./scripts/travis
git update-index --chmod=+x ./scripts/docker/build
git update-index --chmod=+x ./scripts/docker/run
git update-index --chmod=+x ./scripts/docker/publish
git update-index --chmod=+x ./scripts/docker/content/*.sh
git update-index --chmod=+x ./scripts/git/setup
git update-index --chmod=+x ./scripts/git/*.sh


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.

set -e

# Path relative to .git/hooks/
APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
cd $APP_HOME

./scripts/git/pre-commit.sh --no-sandbox

set +e


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.

set -e

# Path relative to .git/hooks/
APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
cd $APP_HOME

./scripts/git/pre-commit.sh --with-sandbox

set +e


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.

set -e
APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
source "$APP_HOME/scripts/git/.functions.sh"

SANDBOX_MODE="$1"

cd $APP_HOME
echo "Current folder: `pwd`"

if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

check_filenames() {
    header "Checking filenames..."

    # Redirect output to stderr.
    exec 1>&2

    # Cross platform projects tend to avoid non-ASCII filenames; prevent
    # them from being added to the repository. We exploit the fact that the
    # printable range starts at the space character and ends with tilde.
    # Note that the use of brackets around a tr range is ok here, (it's
    # even required, for portability to Solaris 10's /usr/bin/tr), since
    # the square bracket bytes happen to fall in the designated range.
    set +e
    if test $(git diff --cached --name-only --diff-filter=A -z $against | LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 ; then
        error "Attempt to add a non-ASCII file name. This can cause problems on other platforms."
        exit 1
    fi
    set -e
}

check_whitespaces() {
    header "Checking white spaces and line separators..."
    git diff-index --check --cached $against --
}

check_do_not_commit() {
    PATTERN1="DONOT"
    PATTERN1="${PATTERN1}COMMIT"
    PATTERN2="DO NOT"
    PATTERN2="${PATTERN2} COMMIT"
    PATTERN3="DONT"
    PATTERN3="${PATTERN3}COMMIT"
    PATTERN4="DONT"
    PATTERN4="${PATTERN4} COMMIT"
    PATTERN5="DON'T"
    PATTERN5="${PATTERN5} COMMIT"

    header "Checking diff for comments containing '${PATTERN1}'..."

    set +e

    PATT="^\+.*${PATTERN1}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN1}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN2}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN2}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN3}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN3}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN4}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN4}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN5}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN5}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    set -e
}

verify_build() {
    header "Verifying build..."

    cd $APP_HOME/scripts
    if [[ "$SANDBOX_MODE" == "--with-sandbox" ]]; then
        ./build.cmd --in-sandbox
    else
        ./build.cmd
    fi

    if [ $? -ne 0 ]; then
        error "Some tests failed."
        exit 1
    else
        header "All tests passed"
    fi
}

check_filenames
check_whitespaces
check_do_not_commit
verify_build

set +e


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -ex

# serve the app via nginx
mkdir -p /app/logs
nginx -g 'daemon off;' -c /app/nginx.conf


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

# Sometimes when creating bash scripts in Windows, bash scripts will not have
# the +x flag carried over to Linux/MacOS. This script should help setting the
# permission flags right.

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
cd $APP_HOME

set +e

chmod ugo+x ./scripts/build               2> /dev/null
chmod ugo+x ./scripts/git/setup           2> /dev/null
chmod ugo+x ./scripts/git/*.sh            2> /dev/null

if [ -d ./scripts/docker ]; then
  chmod ugo+x ./scripts/docker/build        2> /dev/null
  chmod ugo+x ./scripts/docker/run          2> /dev/null
  chmod ugo+x ./scripts/docker/publish      2> /dev/null
  chmod ugo+x ./scripts/docker/content/*.sh 2> /dev/null
fi

if [ -d ./scripts/iothub ]; then
  chmod ugo+x ./scripts/iothub/*.sh       2> /dev/null
fi

git update-index --chmod=+x ./scripts/build
git update-index --chmod=+x ./scripts/git/setup
git update-index --chmod=+x ./scripts/git/*.sh

if [ -d ./scripts/docker ]; then
  git update-index --chmod=+x ./scripts/docker/build
  git update-index --chmod=+x ./scripts/docker/run
  git update-index --chmod=+x ./scripts/docker/publish
  git update-index --chmod=+x ./scripts/docker/content/*.sh
fi

if [ -d ./scripts/iothub ]; then
  git update-index --chmod=+x ./scripts/iothub/*.sh
fi


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

# Path relative to .git/hooks/
APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
cd $APP_HOME

./scripts/git/pre-commit.sh

set +e


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
source "$APP_HOME/scripts/git/.functions.sh"

cd $APP_HOME
echo "Current folder: `pwd`"

if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

check_filenames() {
    header "Checking filenames..."

    # Redirect output to stderr.
    exec 1>&2

    # Cross platform projects tend to avoid non-ASCII filenames; prevent
    # them from being added to the repository. We exploit the fact that the
    # printable range starts at the space character and ends with tilde.
    # Note that the use of brackets around a tr range is ok here, (it's
    # even required, for portability to Solaris 10's /usr/bin/tr), since
    # the square bracket bytes happen to fall in the designated range.
    set +e
    if test $(git diff --cached --name-only --diff-filter=A -z $against | LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 ; then
        error "Attempt to add a non-ASCII file name. This can cause problems on other platforms."
        exit 1
    fi
    set -e
}

check_whitespaces() {
    header "Checking white spaces and line separators..."
    git diff-index --check --cached $against --
}

check_do_not_commit() {
    PATTERN1="DONOT"
    PATTERN1="${PATTERN1}COMMIT"
    PATTERN2="DO NOT"
    PATTERN2="${PATTERN2} COMMIT"
    PATTERN3="DONT"
    PATTERN3="${PATTERN3}COMMIT"
    PATTERN4="DONT"
    PATTERN4="${PATTERN4} COMMIT"
    PATTERN5="DON'T"
    PATTERN5="${PATTERN5} COMMIT"

    header "Checking diff for comments containing '${PATTERN1}'..."

    set +e

    PATT="^\+.*${PATTERN1}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN1}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN2}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN2}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN3}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN3}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN4}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN4}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN5}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN5}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    set -e
}

verify_build() {
    header "Verifying build..."

    cd $APP_HOME/scripts
    ./build

    if [ $? -ne 0 ]; then
        error "Some tests failed."
        exit 1
    else
        header "All tests passed"
    fi
}

check_filenames
check_whitespaces
check_do_not_commit
verify_build

set +e


#!/usr/bin/env bash

cd /app/

cd webservice

dotnet Microsoft.Azure.IoTSolutions.StorageAdapter.WebService.dll


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

# Sometimes when creating bash scripts in Windows, bash scripts will not have
# the +x flag carried over to Linux/MacOS. This script should help setting the
# permission flags right.

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
cd $APP_HOME

set +e

chmod ugo+x ./scripts/build               2> /dev/null
chmod ugo+x ./scripts/compile             2> /dev/null
chmod ugo+x ./scripts/run                 2> /dev/null
chmod ugo+x ./scripts/clean-up            2> /dev/null
chmod ugo+x ./scripts/env-vars-check      2> /dev/null
chmod ugo+x ./scripts/env-vars-setup      2> /dev/null
chmod ugo+x ./scripts/travis              2> /dev/null
chmod ugo+x ./scripts/docker/build        2> /dev/null
chmod ugo+x ./scripts/docker/run          2> /dev/null
chmod ugo+x ./scripts/docker/publish      2> /dev/null
chmod ugo+x ./scripts/docker/content/*.sh 2> /dev/null
chmod ugo+x ./scripts/git/setup           2> /dev/null
chmod ugo+x ./scripts/git/*.sh            2> /dev/null

if [ -d ./scripts/iothub ]; then
  chmod ugo+x ./scripts/iothub/*.sh       2> /dev/null
fi

git update-index --chmod=+x ./scripts/build
git update-index --chmod=+x ./scripts/compile
git update-index --chmod=+x ./scripts/run
git update-index --chmod=+x ./scripts/clean-up
git update-index --chmod=+x ./scripts/env-vars-check
git update-index --chmod=+x ./scripts/env-vars-setup
git update-index --chmod=+x ./scripts/travis
git update-index --chmod=+x ./scripts/docker/build
git update-index --chmod=+x ./scripts/docker/run
git update-index --chmod=+x ./scripts/docker/publish
git update-index --chmod=+x ./scripts/docker/content/*.sh
git update-index --chmod=+x ./scripts/git/setup
git update-index --chmod=+x ./scripts/git/*.sh

if [ -d ./scripts/iothub ]; then
  git update-index --chmod=+x ./scripts/iothub/*.sh
fi


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

# Path relative to .git/hooks/
APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
cd $APP_HOME

./scripts/git/pre-commit.sh --no-sandbox

set +e


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

# Path relative to .git/hooks/
APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
cd $APP_HOME

./scripts/git/pre-commit.sh --with-sandbox

set +e


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
source "$APP_HOME/scripts/git/.functions.sh"

SANDBOX_MODE="$1"

cd $APP_HOME
echo "Current folder: `pwd`"

if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

check_filenames() {
    header "Checking filenames..."

    # Redirect output to stderr.
    exec 1>&2

    # Cross platform projects tend to avoid non-ASCII filenames; prevent
    # them from being added to the repository. We exploit the fact that the
    # printable range starts at the space character and ends with tilde.
    # Note that the use of brackets around a tr range is ok here, (it's
    # even required, for portability to Solaris 10's /usr/bin/tr), since
    # the square bracket bytes happen to fall in the designated range.
    set +e
    if test $(git diff --cached --name-only --diff-filter=A -z $against | LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 ; then
        error "Attempt to add a non-ASCII file name. This can cause problems on other platforms."
        exit 1
    fi
    set -e
}

check_whitespaces() {
    header "Checking white spaces and line separators..."
    git diff-index --check --cached $against --
}

check_do_not_commit() {
    PATTERN1="DONOT"
    PATTERN1="${PATTERN1}COMMIT"
    PATTERN2="DO NOT"
    PATTERN2="${PATTERN2} COMMIT"
    PATTERN3="DONT"
    PATTERN3="${PATTERN3}COMMIT"
    PATTERN4="DONT"
    PATTERN4="${PATTERN4} COMMIT"
    PATTERN5="DON'T"
    PATTERN5="${PATTERN5} COMMIT"

    header "Checking diff for comments containing '${PATTERN1}'..."

    set +e

    PATT="^\+.*${PATTERN1}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN1}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN2}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN2}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN3}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN3}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN4}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN4}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN5}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN5}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    set -e
}

verify_build() {
    header "Verifying build..."

    cd $APP_HOME/scripts
    if [[ "$SANDBOX_MODE" == "--with-sandbox" ]]; then
        ./build --in-sandbox
    else
        ./build
    fi

    if [ $? -ne 0 ]; then
        error "Some tests failed."
        exit 1
    else
        header "All tests passed"
    fi
}

check_filenames
check_whitespaces
check_do_not_commit
verify_build

set +e


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

while [ "$#" -gt 0 ]; do
    case "$1" in
        --release_version)              RELEASE_VERSION="$2" ;;
        --git_access_token)             GIT_ACCESS_TOKEN="$2" ;;
        --docker_user)                  DOCKER_USER="$2" ;;
        --docker_pwd)                   DOCKER_PWD="$2" ;;
        --from_docker_namespace)        FROM_DOCKER_NAMESPACE="$2" ;;
        --to_docker_namespace)          TO_DOCKER_NAMESPACE="$2" ;;
        --docker_tag)                   DOCKER_TAG="$2" ;;
        --description)                  DESCRIPTION="$2" ;;
        --pre_release)                  PRE_RELEASE="$2" ;;
        --local)                        LOCAL="$2" ;;
    esac
    shift
done

# Set default values for optional parameters
FROM_DOCKER_NAMESPACE=${FROM_DOCKER_NAMESPACE:-azureiotpcs}
TO_DOCKER_NAMESPACE=${TO_DOCKER_NAMESPACE:-azureiotpcs}
DOCKER_TAG=${DOCKER_TAG:-testing}
DESCRIPTION=${DESCRIPTION:-''}
PRE_RELEASE=${PRE_RELEASE:-false}
LOCAL=${LOCAL:-''}

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"

NC="\033[0m" # no color
CYAN="\033[1;36m" # light cyan
YELLOW="\033[1;33m" # yellow
RED="\033[1;31m" # light red

failed() {
    SUB_MODULE=$1
    echo -e "${RED}Cannot find directory $SUB_MODULE${NC}"
    exit 1
}

usage() {
    echo -e "${RED}ERROR: $1 is a required option${NC}"
    echo "Usage: ./release"
    echo -e 'Options:
        --release_version       Version of this release (required)
        --git_access_token      Git access token to push tag (required)
        --docker_user           Username to login docker hub (required)
        --docker_pwd            Password to login docker hub (required)
        --from_docker_namespace Source namespace of docker image (default:azureiotpcs)
        --to_docker_namespace   Target namespace of docker image (default:azureiotpcs)
        --docker_tag            Source tag of docker image (default:testing)
        --description           Description of this release (default:empty)
        --pre_release           Publish as non-production release on github (default:false)
        --local                 Clean up the local repo at first (default:empty)
        '
    exit 1
}

check_input() {
    if [ ! -n "$RELEASE_VERSION" ]; then
        usage "release_version"
    fi
    if [ ! -n "$DOCKER_USER" ]; then
        usage "docker_user"
    fi
    echo $DOCKER_PWD | docker login -u $DOCKER_USER --password-stdin
}

tag_build_publish_repo() {
    SUB_MODULE=$1
    REPO_NAME=$2
    DOCKER_CONTAINER_NAME=${3:-$2}
    DESCRIPTION=$4

    echo
    echo -e "${CYAN}====================================     Start: Tagging the $REPO_NAME repo     ====================================${NC}"
    echo
    echo -e "Current working directory ${CYAN}$APP_HOME$SUB_MODULE${NC}"
    echo
    cd $APP_HOME$SUB_MODULE || failed $SUB_MODULE

    if [ -n "$LOCAL" ]; then
        echo "Cleaning the repo"
        git reset --hard origin/master
        git clean -xdf
    fi

    if [ "$REPO_NAME" == "pcs-config-dotnet" ]; then
        git checkout RemoveServiceDependency
    else
        git checkout master
    fi

    echo "set url"
    git remote set-url origin https://$GIT_ACCESS_TOKEN@github.com/Azure/$REPO_NAME.git

    echo "git pull"
    git pull --all --prune

    echo "git tag"
    git tag --force $RELEASE_VERSION

    echo "git push"
    git push https://$GIT_ACCESS_TOKEN@github.com/Azure/$REPO_NAME.git $RELEASE_VERSION

    echo
    echo -e "${CYAN}====================================     End: Tagging $REPO_NAME repo     ====================================${NC}"
    echo

    echo
    echo -e "${CYAN}====================================     Start: Release for $REPO_NAME     ====================================${NC}"
    echo

    # For documentation https://help.github.com/articles/creating-releases/
    DATA="{
        \"tag_name\": \"$RELEASE_VERSION\",
        \"target_commitish\": \"master\",
        \"name\": \"$RELEASE_VERSION\",
        \"body\": \"$DESCRIPTION\",
        \"draft\": false,
        \"prerelease\": $PRE_RELEASE
    }"

    curl -X POST --data "$DATA" https://api.github.com/repos/Azure/$REPO_NAME/releases?access_token=$GIT_ACCESS_TOKEN
    echo
    echo -e "${CYAN}====================================     End: Release for $REPO_NAME     ====================================${NC}"
    echo

    if [ -n "$SUB_MODULE" ] && [ "$REPO_NAME" != "pcs-cli" ]; then
        echo
        echo -e "${CYAN}====================================     Start: Building $REPO_NAME     ====================================${NC}"
        echo

        BUILD_PATH="scripts/docker/build"
        if [ "$SUB_MODULE" == "api-gateway" ]; then
            BUILD_PATH="build"
        fi

        # Building docker containers
        echo "Building docker containers"
        echo $APP_HOME$SUB_MODULE/$BUILD_PATH
        /bin/bash $APP_HOME$SUB_MODULE/$BUILD_PATH

        echo
        echo -e "${CYAN}====================================     End: Building $REPO_NAME     ====================================${NC}"
        echo

        # Tag containers
        echo -e "${CYAN}Tagging $FROM_DOCKER_NAMESPACE/$DOCKER_CONTAINER_NAME:$DOCKER_TAG ==> $TO_DOCKER_NAMESPACE/$DOCKER_CONTAINER_NAME:$RELEASE_VERSION${NC}"
        echo
        docker tag $FROM_DOCKER_NAMESPACE/$DOCKER_CONTAINER_NAME:$DOCKER_TAG  $TO_DOCKER_NAMESPACE/$DOCKER_CONTAINER_NAME:$RELEASE_VERSION

        # Push containers
        echo -e "${CYAN}Pusing container $TO_DOCKER_NAMESPACE/$DOCKER_CONTAINER_NAME:$RELEASE_VERSION${NC}"
        docker push $TO_DOCKER_NAMESPACE/$DOCKER_CONTAINER_NAME:$RELEASE_VERSION
    fi
}

check_input

# DOTNET Microservices
# As of DS-2.0.6, only the Web UI and the Simulation service containers are
# refrenced by a new docker tag for each release (in docker-compse.yaml in the
# deployed VMs)
tag_build_publish_repo simulation-service     device-simulation-dotnet
tag_build_publish_repo webui                  pcs-simulation-webui              device-simulation-webui
tag_build_publish_repo pcs-diagnostics-dotnet pcs-diagnostics-dotnet
tag_build_publish_repo storage-service        pcs-storage-adapter-dotnet
tag_build_publish_repo pcs-config-dotnet      pcs-config-dotnet
#tag_build_publish_repo api-gateway            azure-iot-pcs-device-simulation   simulation-api-gateway

set +e


#!/usr/bin/env bash
set -e
set -x

mkdir -p /app/logs
nginx -c /app/config/nginx.conf


#!/usr/bin/env bash

set -e

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/"

cd $APP_HOME

git checkout master
git pull

COL="\e[93m\e[1m"
NOCOL="\e[0m"

echo -e "${COL}### Auth${NOCOL}"
git submodule update auth              && cd auth              && git checkout master && git pull && cd ..
echo -e "${COL}### CLI${NOCOL}"
git submodule update cli               && cd cli               && git checkout master && git pull && cd ..
echo -e "${COL}### Config${NOCOL}"
git submodule update config            && cd config            && git checkout master && git pull && cd ..
echo -e "${COL}### Device Simulation${NOCOL}"
git submodule update device-simulation && cd device-simulation && git checkout master && git pull && cd ..
echo -e "${COL}### IoT Hub Manager${NOCOL}"
git submodule update iothub-manager    && cd iothub-manager    && git checkout master && git pull && cd ..
echo -e "${COL}### Storage Adapter${NOCOL}"
git submodule update storage-adapter   && cd storage-adapter   && git checkout master && git pull && cd ..
echo -e "${COL}### Telemetry${NOCOL}"
git submodule update telemetry         && cd telemetry         && git checkout master && git pull && cd ..
echo -e "${COL}### ASA Manager${NOCOL}"
git submodule update asa-manager       && cd asa-manager       && git checkout master && git pull && cd ..
echo -e "${COL}### Web UI${NOCOL}"
git submodule update webui             && cd webui             && git checkout master && git pull && cd ..


#!/usr/bin/env bash -e

# Path relative to .git/hooks/
APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
source "$APP_HOME/scripts/git/pre-commit.sh"


#!/usr/bin/env bash

set -e
APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"
source "$APP_HOME/scripts/git/.functions.sh"

cd $APP_HOME
echo "Current folder: `pwd`"

if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

check_filenames() {
    header "Checking filenames..."

    # Redirect output to stderr.
    exec 1>&2

    # Cross platform projects tend to avoid non-ASCII filenames; prevent
    # them from being added to the repository. We exploit the fact that the
    # printable range starts at the space character and ends with tilde.
    # Note that the use of brackets around a tr range is ok here, (it's
    # even required, for portability to Solaris 10's /usr/bin/tr), since
    # the square bracket bytes happen to fall in the designated range.
    set +e
    if test $(git diff --cached --name-only --diff-filter=A -z $against | LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 ; then
        error "Attempt to add a non-ASCII file name. This can cause problems on other platforms."
        exit 1
    fi
    set -e
}

check_whitespaces() {
    header "Checking white spaces and line separators..."
    git diff-index --check --cached $against --
}

check_do_not_commit() {
    PATTERN1="DONOT"
    PATTERN1="${PATTERN1}COMMIT"
    PATTERN2="DO NOT"
    PATTERN2="${PATTERN2} COMMIT"
    PATTERN3="DONT"
    PATTERN3="${PATTERN3}COMMIT"
    PATTERN4="DONT"
    PATTERN4="${PATTERN4} COMMIT"
    PATTERN5="DON'T"
    PATTERN5="${PATTERN5} COMMIT"

    header "Checking diff for comments containing '${PATTERN1}'..."

    set +e

    PATT="^\+.*${PATTERN1}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN1}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN2}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN2}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN3}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN3}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN4}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN4}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    PATT="^\+.*${PATTERN5}.*$"
    diffstr=`git diff --cached $against | grep -ie "$PATT"`
    if [[ -n "$diffstr" ]]; then
        error "You have left '${PATTERN5}' in your changes, you can't commit until it has been removed."
        exit 1
    fi

    set -e
}

verify_build() {
    header "Verifying build..."

    cd $APP_HOME/scripts
    ./build

    if [ $? -ne 0 ]; then
        error "Some tests failed."
        exit 1
    fi
}

check_filenames
check_whitespaces
check_do_not_commit
verify_build


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

while [ "$#" -gt 0 ]; do
    case "$1" in
        --version)                      VERSION="$2" ;;
        --git_access_token)             GIT_ACCESS_TOKEN="$2" ;;
        --docker_user)                  DOCKER_USER="$2" ;;
        --docker_pwd)                   DOCKER_PWD="$2" ;;
        --from_docker_namespace)        FROM_DOCKER_NAMESPACE="$2" ;;
        --to_docker_namespace)          TO_DOCKER_NAMESPACE="$2" ;;
        --docker_tag)                   DOCKER_TAG="$2" ;;
        --description)                  DESCRIPTION="$2" ;;
        --pre_release)                  PRE_RELEASE="$2" ;;
        --local)                        LOCAL="$2" ;;
    esac
    shift
done

# Set default values for optional parameters
FROM_DOCKER_NAMESPACE=${FROM_DOCKER_NAMESPACE:-azureiotpcs}
TO_DOCKER_NAMESPACE=${TO_DOCKER_NAMESPACE:-azureiotpcs}
DOCKER_TAG=${DOCKER_TAG:-testing}
DESCRIPTION=${DESCRIPTION:-''}
PRE_RELEASE=${PRE_RELEASE:-false}
LOCAL=${LOCAL:-''}

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && cd .. && pwd )/"

NC="\033[0m" # no color
CYAN="\033[1;36m" # light cyan
YELLOW="\033[1;33m" # yellow
RED="\033[1;31m" # light red

failed() {
    SUB_MODULE=$1
    echo -e "${RED}Cannot find directory $SUB_MODULE${NC}"
    exit 1
}

usage() {
    echo -e "${RED}ERROR: $1 is a required option${NC}"
    echo "Usage: ./release"
    echo -e 'Options:
        --version               Version of this release (required)
        --git_access_token      Git access token to push tag (required)
        --docker_user           Username to login docker hub (required)
        --docker_pwd            Password to login docker hub (required)
        --from_docker_namespace Source namespace of docker image (default:azureiotpcs)
        --to_docker_namespace   Target namespace of docker image (default:azureiotpcs)
        --docker_tag            Source tag of docker image (default:testing)
        --description           Description of this release (default:empty)
        --pre_release           Publish as non-production release on github (default:false)
        --local                 Clean up the local repo at first (default:empty)
        '
    exit 1
}

check_input() {
    if [ ! -n "$VERSION" ]; then
        usage "version"
    fi
    if [ ! -n "$GIT_ACCESS_TOKEN" ]; then
        usage "git_access_token"
    fi
    if [ ! -n "$DOCKER_USER" ]; then
        usage "docker_user"
    fi
    if [ ! -n "$DOCKER_PWD" ]; then
        usage "docker_pwd"
    fi
    echo $DOCKER_PWD | docker login -u $DOCKER_USER --password-stdin
}

tag_release_repo() {
    SUB_MODULE=$1
    REPO_NAME=$2
    DESCRIPTION=$3

    echo
    echo -e "${CYAN}====================================     Start: Tagging the $REPO_NAME repo     ====================================${NC}"
    echo
    echo -e "Current working directory ${CYAN}$APP_HOME$SUB_MODULE${NC}"
    echo
    cd $APP_HOME$SUB_MODULE || failed $SUB_MODULE

    if [ -n "$LOCAL" ]; then
        echo "Cleaning the repo"
        git reset --hard origin/master
        git clean -xdf
    fi
    git checkout master
    git pull --all --prune
    git fetch --tags

    git tag --force $VERSION
    git push https://$GIT_ACCESS_TOKEN@github.com/Azure/$REPO_NAME.git $VERSION

    echo
    echo -e "${CYAN}====================================     End: Tagging $REPO_NAME repo     ====================================${NC}"
    echo

    echo
    echo -e "${CYAN}====================================     Start: Release for $REPO_NAME     ====================================${NC}"
    echo

    # For documentation https://help.github.com/articles/creating-releases/
    DATA="{
        \"tag_name\": \"$VERSION\",
        \"target_commitish\": \"master\",
        \"name\": \"$VERSION\",
        \"body\": \"$DESCRIPTION\",
        \"draft\": false,
        \"prerelease\": $PRE_RELEASE
    }"

    curl -X POST --data "$DATA" https://api.github.com/repos/Azure/$REPO_NAME/releases?access_token=$GIT_ACCESS_TOKEN
    echo
    echo -e "${CYAN}====================================     End: Release for $REPO_NAME     ====================================${NC}"
    echo
}

publish_docker_containers()
{
    DOCKER_CONTAINER_NAME=$1
    SUB_MODULE=$2

    if [ "$SUB_MODULE" == "reverse-proxy" ]; then
        cd $APP_HOME$SUB_MODULE || failed $SUB_MODULE
        if [ -n "$LOCAL" ]; then
            echo "Cleaning the repo"
            git reset --hard origin/master
            git clean -xdf
        fi
        git checkout master
        git pull --all --prune

        echo
        echo -e "${CYAN}====================================     Start: Building $DOCKER_CONTAINER_NAME     ====================================${NC}"
        echo

        BUILD_PATH="build"
        # Building docker containers
        /bin/bash $APP_HOME$SUB_MODULE/$BUILD_PATH

        echo
        echo -e "${CYAN}====================================     End: Building $DOCKER_CONTAINER_NAME     ====================================${NC}"
        echo
    else
        # Pull containers
        echo -e "${CYAN}Pulling $FROM_DOCKER_NAMESPACE/$DOCKER_CONTAINER_NAME:$DOCKER_TAG${NC}"
        echo
        docker pull $FROM_DOCKER_NAMESPACE/$DOCKER_CONTAINER_NAME:$DOCKER_TAG
    fi

    # Tag containers
    echo -e "${CYAN}Tagging $FROM_DOCKER_NAMESPACE/$DOCKER_CONTAINER_NAME:$DOCKER_TAG ==> $TO_DOCKER_NAMESPACE/$DOCKER_CONTAINER_NAME:$VERSION${NC}"
    echo
    docker tag $FROM_DOCKER_NAMESPACE/$DOCKER_CONTAINER_NAME:$DOCKER_TAG  $TO_DOCKER_NAMESPACE/$DOCKER_CONTAINER_NAME:$VERSION

    # Push containers
    echo -e "${CYAN}Pusing container $TO_DOCKER_NAMESPACE/$DOCKER_CONTAINER_NAME:$VERSION${NC}"
    docker push $TO_DOCKER_NAMESPACE/$DOCKER_CONTAINER_NAME:$VERSION
}

check_input

# Publish DOTNET Microservices docker containers
publish_docker_containers pcs-auth-dotnet
publish_docker_containers pcs-config-dotnet
publish_docker_containers device-simulation-dotnet
publish_docker_containers iothub-manager-dotnet
publish_docker_containers pcs-storage-adapter-dotnet
publish_docker_containers telemetry-dotnet
publish_docker_containers asa-manager-dotnet
publish_docker_containers pcs-remote-monitoring-webui
publish_docker_containers pcs-diagnostics-dotnet
# Top Level repo
publish_docker_containers remote-monitoring-nginx       reverse-proxy

# # Release and Tag all the repositories with latest version
tag_release_repo          webui                           pcs-remote-monitoring-webui
tag_release_repo          services                        remote-monitoring-services-dotnet
# PCS CLI
tag_release_repo          cli                             pcs-cli
# Top Level repo
tag_release_repo          ""                              azure-iot-pcs-remote-monitoring-dotnet $DESCRIPTION

set +e


#!/usr/bin/env bash

set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/"
source "$DIR/.functions.sh"

if [[ "$1" == "-a" || "$1" == "--all-options" ]]; then
    ADVANCED=true
else
    ADVANCED=false
fi

SUBSCRIPTION=""
HUB_NAME=""
RESOURCE_GROUP=""
NEW_RESOURCE_GROUP="false"
LOCATION=""
SKU=""
MAXUNITS=""
UNITS=""
D2C_PARTITIONS=""
D2C_TTL=""
C2D_ATTEMPTS=""
C2D_TTL=""
C2D_FEEDBACK_LOCK=""
C2D_FEEDBACK_TTL=""
C2D_FEEDBACK_ATTEMPTS=""

ask_hub_name() {
    header "New Azure IoT Hub"
    HUB_NAME=""

    while [[ -z "$HUB_NAME" ]]; do
        echo -n "Enter IoT Hub name: "
        read HUB_NAME
    done
}

select_resource_group() {
    header "Resource group"

    echo "Existing resource groups"

    azure group list

    echo
    announce "You can re-use an existing group or enter a new name to create a new resource group."
    echo
    RESOURCE_GROUP=""
    while [[ -z "$RESOURCE_GROUP" ]]; do
        echo -n "Enter resource group name: "
        read RESOURCE_GROUP
    done

    NEW_RESOURCE_GROUP="false"
    TEST=$(azure group list | grep -i "$RESOURCE_GROUP")
    if [[ -z "$TEST" ]]; then
        NEW_RESOURCE_GROUP="true"
    fi
}

select_location() {
    header "Azure region"

    # TODO: az account list-locations + filter for IoT Hub
    echo -e "Select the location where to create the hub:"
    PS3=">> "
    select LOCATION in eastus westus westus2 westcentralus northeurope westeurope eastasia southeastasia japaneast japanwest australiaeast australiasoutheast; do
        break;
    done
}

select_sku_and_units() {
    header "Pricing and Scale Tier"

    echo "F1: 8k messages/unit/day (free)"
    echo "S1: 400k messages/unit/day"
    echo "S2: 6M messages/unit/day"
    echo "S3: 300M messages/unit/day"

    echo -e "\nSelect pricing and scale:"
    PS3=">> "
    select SKU in F1 S1 S2 S3; do
        case $SKU in
            F1 ) MAXUNITS=1; break;;
            S1 ) MAXUNITS=200; break;;
            S2 ) MAXUNITS=200; break;;
            S3 ) MAXUNITS=10; break;;
        esac
    done

    UNITS="1"
    if [[ "$SKU" != "F1" ]]; then
        echo -n -e "\nHow many units? (1..${MAXUNITS}) [${UNITS}]: "
        read TEMP
        if [[ ! -z "$TEMP" ]]; then UNITS="$TEMP"; fi
    fi
}

ask_d2c_partitions() {
    D2C_PARTITIONS="2" # 2..128 (2 for F1)
    if [[ "$ADVANCED" == "false" ]]; then return 0; fi

    if [[ "$SKU" != "F1" ]]; then
        echo -n "How many partitions? (1..128) [${D2C_PARTITIONS}]: "
        read TEMP
        if [[ ! -z "$TEMP" ]]; then D2C_PARTITIONS="$TEMP"; fi
    fi
}

ask_d2c_retention() {
    D2C_TTL="1" # 1..7 days
    if [[ "$ADVANCED" == "false" ]]; then return 0; fi

    echo -n "How many days before device-to-cloud messages expire? (1..7) [${D2C_TTL}]: "
    read TEMP
    if [[ ! -z "$TEMP" ]]; then D2C_TTL="$TEMP"; fi
}

ask_c2d_attempts() {
    C2D_ATTEMPTS="10" # 1..100 count
    if [[ "$ADVANCED" == "false" ]]; then return 0; fi

    echo -n "How many attempts to deliver a cloud-to-device message? (1..100) [${C2D_ATTEMPTS}]: "
    read TEMP
    if [[ ! -z "$TEMP" ]]; then C2D_ATTEMPTS="$TEMP"; fi
}

ask_c2d_ttl() {
    # PnYnMnDTnHnMnS
    C2D_TTL="PT1H" # 1 min .. 2 days
    if [[ "$ADVANCED" == "false" ]]; then return 0; fi

    echo -n "How long before cloud-to-device messages expire? (ISO8601: 1 min .. 2 days) [${C2D_TTL}]: "
    read TEMP
    if [[ ! -z "$TEMP" ]]; then C2D_TTL="$TEMP"; fi
}

ask_c2d_feedback_lock() {
    # PnYnMnDTnHnMnS
    C2D_FEEDBACK_LOCK="PT1M" # 5 secs .. 5 mins
    if [[ "$ADVANCED" == "false" ]]; then return 0; fi

    echo -n "How long before cloud-to-device feedback messages lock expire? (ISO8601: 5 secs .. 5 mins) [${C2D_FEEDBACK_LOCK}]: "
    read TEMP
    if [[ ! -z "$TEMP" ]]; then C2D_FEEDBACK_LOCK="$TEMP"; fi
}

ask_c2d_feedback_ttl() {
    # PnYnMnDTnHnMnS
    C2D_FEEDBACK_TTL="PT1M" # 1 min .. 2days
    if [[ "$ADVANCED" == "false" ]]; then return 0; fi

    echo -n "How long before cloud-to-device feedback messages expire? (ISO8601: 1 min .. 2days) [${C2D_FEEDBACK_TTL}]: "
    read TEMP
    if [[ ! -z "$TEMP" ]]; then C2D_FEEDBACK_TTL="$TEMP"; fi
}

ask_c2d_feedback_attempts() {
    C2D_FEEDBACK_ATTEMPTS="1" # 1..100 count
    if [[ "$ADVANCED" == "false" ]]; then return 0; fi

    echo -n "How many attempts to deliver a cloud-to-device feedback message? (1..100) [${C2D_FEEDBACK_ATTEMPTS}]: "
    read TEMP
    if [[ ! -z "$TEMP" ]]; then C2D_FEEDBACK_ATTEMPTS="$TEMP"; fi
}

advanced() {
    if [[ "$ADVANCED" == "true" ]]; then
        header "Other settings";
    else
        announce "Using default settings. Use '--all-options' to manually set all the hub options."
    fi

    ask_d2c_partitions
    ask_d2c_retention
    ask_c2d_attempts
    ask_c2d_ttl
    ask_c2d_feedback_lock
    ask_c2d_feedback_ttl
    ask_c2d_feedback_attempts
}

create_hub() {
    header "Create '$HUB_NAME' in '$LOCATION'"

    if [[ $NEW_RESOURCE_GROUP == "true" ]]; then
        azure group create "$RESOURCE_GROUP" "$LOCATION" || error_and_exit "Unable to create resource group."
    fi

    azure iothub create "$RESOURCE_GROUP" "$HUB_NAME" "$LOCATION" "$SKU" "$UNITS" \
        --enable-dm \
        --d2c-partitions "$D2C_PARTITIONS" \
        --d2c-retention-time-in-days "$D2C_TTL" \
        --c2d-max-delivery-count "$C2D_ATTEMPTS" \
        --c2d-ttl "$C2D_TTL" \
        --feedback-lock-duration "$C2D_FEEDBACK_LOCK" \
        --feedback-ttl "$C2D_FEEDBACK_TTL" \
        --feedback-max-delivery-count "$C2D_FEEDBACK_ATTEMPTS" \
        || error_and_exit "Unable to create IoT Hub"
}

confirm() {
    header "Summary"

    echo "Azure IoT Hub name:             ${HUB_NAME}"
    echo "Resource group name:            ${RESOURCE_GROUP} (new: ${NEW_RESOURCE_GROUP})"
    echo "Azure region:                   ${LOCATION}"
    echo "Pricing and Scaling SKU         ${SKU}"
    echo "Capacity units:                 ${UNITS}"
    echo "D2C events partitions:          ${D2C_PARTITIONS}"
    echo "D2C events TTL:                 ${D2C_TTL} days"
    echo "C2D messages TTL:               ${C2D_TTL}"
    echo "C2D message delivery attempts:  ${C2D_ATTEMPTS}"
    echo "C2D feedback TTL:               ${C2D_FEEDBACK_TTL}"
    echo "C2D feedback delivery attempts: ${C2D_FEEDBACK_ATTEMPTS}"
    echo "C2D feedback lock TTL:          ${C2D_FEEDBACK_LOCK}"
    echo

    echo "Proceed?"
    PS3=">> "
    select SKU in Yes No; do
        case $SKU in
            No ) user_exit;;
            Yes ) break;;
        esac
    done
}

main() {
    check_dependencies
    login

    ask_hub_name
    select_subscription
    select_resource_group
    select_location
    select_sku_and_units
    advanced
    confirm
    create_hub
    show_hub_details "$RESOURCE_GROUP" "$HUB_NAME"

    announce "Tip: to show the hub details, run: ./show-hub.sh $RESOURCE_GROUP $HUB_NAME"
}

main


#!/usr/bin/env bash

set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/"
source "$DIR/.functions.sh"

main() {
    check_dependencies
    login

    header "Azure IoT Hubs in the current subscription"
    az iot hub list | jq '.[] | {name: .name, resourcegroup: .resourcegroup, location: .location, subscription: .subscriptionid}' | jq .
}

main


#!/usr/bin/env bash

set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/"
source "$DIR/.functions.sh"

main() {
    check_dependencies
    login

    header "Azure subscriptions"
    azure account list
    #az account list | jq '.[] | {name: .name, id: .id, current: .isDefault}' | jq .
}

main


#!/usr/bin/env bash

set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/"
source "$DIR/.functions.sh"

SUBSCRIPTION_ID="$1"

check_input() {
    if [[ -z "$SUBSCRIPTION_ID" ]]; then
        announce "Usage: ./select-subscription.sh SUBSCRIPTION_ID"
        echo "Use './list-subscriptions.sh' to see the list of subscriptions."
    fi
}

main() {
    check_input
    check_dependencies
    login

    if [[ ! -z "$SUBSCRIPTION_ID" ]]; then
        header "Changing current subscription"
        #az account set --subscription "$SUBSCRIPTION_ID"
        azure account set "$SUBSCRIPTION_ID"
    else
        select_subscription
    fi
}

main


#!/usr/bin/env bash

set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/"
source "$DIR/.functions.sh"

RESOURCE_GROUP="$1"
HUB_NAME="$2"

check_input() {
    if [[ -z "$RESOURCE_GROUP" || -z "$HUB_NAME" ]]; then
        announce "Usage: ./show-hub.sh RESOURCE_GROUP HUB_NAME"
        echo "Use './list-hubs.sh' to see the list of hubs."
        exit -1
    fi
}

main() {
    check_input
    check_dependencies
    login
    show_hub_details "$RESOURCE_GROUP" "$HUB_NAME"
}

main


ï»¿#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

TEST_SUITE="ASA Manager Tests"

TEST_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/"
APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/"

source "$APP_HOME/scripts/.functions.sh"

start_containers() {
    header2 "$TEST_SUITE - Starting services..."

    cd $APP_HOME
    ./scripts/storageadapter.sh start
    ./scripts/telemetry.sh start
    ./scripts/devicesimulation.sh start
    ./scripts/config.sh start
    ./scripts/iothubmanager.sh start
    ./scripts/asamanager.sh start
    docker ps -a
}

stop_containers() {
    header2 "$TEST_SUITE - Stopping services..."

    cd $APP_HOME
    docker ps -a
    ./scripts/storageadapter.sh stop
    ./scripts/telemetry.sh stop
    ./scripts/devicesimulation.sh stop
    ./scripts/config.sh stop
    ./scripts/iothubmanager.sh stop
    ./scripts/asamanager.sh stop
}

run_tests() {
    header2 "$TEST_SUITE - Downloading dependencies..."

    cd $APP_HOME
    dotnet restore

    header2 "$TEST_SUITE - Compiling tests..."
    dotnet build --configuration Release

    header2 "$TEST_SUITE - Running tests..."
    cd $TEST_HOME
    find . -name *.csproj | xargs dotnet test --configuration Release

    docker logs "asa-manager"
}

#Script options
repo=""
tag="testing"
dockeraccount="azureiotpcs"

set_up() {
    export DOCKER_TAG=$tag
    export REPO=dotnet
    export DOCKER_ACCOUNT=$dockeraccount
    check_dependency_docker
    check_dependency_dotnet
}

run() {
    start_containers
    run_tests
    stop_containers
}

tear_down() {
    unset DOCKER_TAG
    unset DOCKER_ACCOUNT
}

while [[ $# -gt 0 ]] ;
do
    opt=$1;
    shift;
    case $opt in
        -t|--tag) tag=$1; shift;;
        -da|--docker-account) dockeraccount=$1; shift;;
        *) shift;;
    esac
done

header "Running $TEST_SUITE"

set_up
run
tear_down

set +e


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

TEST_SUITE="PCS Config Tests"

TEST_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/"
APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/"

source "$APP_HOME/scripts/.functions.sh"

start_containers() {
    header2 "$TEST_SUITE - Starting services..."

    cd $APP_HOME
    ./scripts/storageadapter.sh start
    ./scripts/telemetry.sh start
    ./scripts/devicesimulation.sh start
    ./scripts/config.sh start
    docker ps -a
}

stop_containers() {
    header2 "$TEST_SUITE - Stopping services..."

    cd $APP_HOME
    docker ps -a
    ./scripts/storageadapter.sh stop
    ./scripts/telemetry.sh stop
    ./scripts/devicesimulation.sh stop
    ./scripts/config.sh stop
}

run_tests() {
    header2 "$TEST_SUITE - Downloading dependencies..."

    cd $APP_HOME
    dotnet restore

    header2 "$TEST_SUITE - Compiling tests..."
    dotnet build --configuration Release

    header2 "$TEST_SUITE - Running tests..."
    cd $TEST_HOME
    find . -name *.csproj | xargs dotnet test --configuration Release
}

#Script options
repo=""
tag="testing"
dockeraccount="azureiotpcs"

set_up() {
    export DOCKER_TAG=$tag
    export DOCKER_ACCOUNT=$dockeraccount
    check_dependency_docker
    check_dependency_dotnet
}

run() {
    start_containers
    run_tests
    stop_containers
}

tear_down() {
    unset DOCKER_TAG
    unset DOCKER_ACCOUNT
}

#### Parse script arguments
while [[ $# -gt 0 ]] ;
do
    opt=$1;
    shift;
    case $opt in
        -t|--tag) tag=$1; shift;;
        -re|--repo) repo=$1; shift;;
        -da|--docker-account) dockeraccount=$1; shift;;
        *)  shift;;
    esac
done

header "Running $TEST_SUITE"

#### Set Up Tests
set_up

#### Run tests based on repo
if [ "$repo" == "both" ] || [ "$repo" == "" ]; then
    export REPO=dotnet
    run
    export REPO=java
    run
elif [[ "$repo" == "dotnet" ]]; then
    export REPO=dotnet
    run
elif [[ "$repo" == "java" ]]; then
    export REPO=java
    run
else
    error "No build type specified, pass either 'dotnet' or 'java'."
    echo "eg:- sh containers.sh -re java"
    exit 1
fi

#### Tear down test
tear_down

set +e


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

TEST_SUITE="IOTHub Manager Tests"

TEST_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/"
APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/"

source "$APP_HOME/scripts/.functions.sh"

start_containers() {
    header2 "$TEST_SUITE - Starting services..."

    cd $APP_HOME
    ./scripts/storageadapter.sh start
    ./scripts/iothubmanager.sh start
    ./scripts/devicesimulation.sh start
    docker ps -a
}

stop_containers() {
    header2 "$TEST_SUITE - Stopping services..."

    cd $APP_HOME
    docker ps -a
    ./scripts/storageadapter.sh stop
    ./scripts/iothubmanager.sh stop
    ./scripts/devicesimulation.sh stop
}

run_tests() {
    header2 "$TEST_SUITE - Downloading dependencies..."

    cd $APP_HOME
    dotnet restore

    header2 "$TEST_SUITE - Compiling tests..."
    dotnet build --configuration Release

    header2 "$TEST_SUITE - Running tests..."
    cd $TEST_HOME
    find . -name *.csproj | xargs dotnet test --configuration Release
}

#Script options
repo=""
tag="testing"
dockeraccount="azureiotpcs"

set_up() {
    export DOCKER_TAG=$tag
    export DOCKER_ACCOUNT=$dockeraccount
    check_dependency_docker
    check_dependency_dotnet
}

run() {
    start_containers
    run_tests
    stop_containers
}

tear_down() {
    unset DOCKER_TAG
    unset DOCKER_ACCOUNT
}

#### Parse script arguments
while [[ $# -gt 0 ]] ;
do
    opt=$1;
    shift;
    case $opt in
        -t|--tag) tag=$1; shift;;
        -re|--repo) repo=$1; shift;;
        -da|--docker-account) dockeraccount=$1; shift;;
        *)  shift;;
    esac
done

header "Running $TEST_SUITE"

#### Set Up Tests
set_up

#### Run tests based on repo
if [ "$repo" == "both" ] || [ "$repo" == "" ]; then
    export REPO=dotnet
    run
    export REPO=java
    run
elif [[ "$repo" == "dotnet" ]]; then
    export REPO=dotnet
    run
elif [[ "$repo" == "java" ]]; then
    export REPO=java
    run
else
    error "No build type specified, pass either 'dotnet' or 'java'."
    echo "eg:- sh containers.sh -re java"
    exit 1
fi

#### Tear down test
tear_down

set +e


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

if [[ "$DOCKER_TAG" == "" ]]; then
    export DOCKER_TAG=testing
fi

DOCKER_IMAGE="$DOCKER_ACCOUNT/asa-manager-dotnet:$DOCKER_TAG"
DOCKER_PORT=9024
DOCKER_NAME="asa-manager"
DOCKER_NETWOK="integrationtests"

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/"
cd $APP_HOME

source "$APP_HOME/scripts/.functions.sh"

check_env_variables()
{
    variables_not_found=0
    if [[ ! -n "${PCS_ASA_DATA_AZUREBLOB_ACCOUNT}" ]]; then
        error "PCS_ASA_DATA_AZUREBLOB_ACCOUNT is not set"
        variables_not_found=1
    fi
    if [[ ! -n "${PCS_ASA_DATA_AZUREBLOB_KEY}" ]]; then
        error "PCS_ASA_DATA_AZUREBLOB_KEY is not set"
        variables_not_found=1
    fi
    if [[ ! -n "${PCS_ASA_DATA_AZUREBLOB_ENDPOINT_SUFFIX}" ]]; then
        error "PCS_ASA_DATA_AZUREBLOB_ENDPOINT_SUFFIX is not set"
        variables_not_found=1
    fi
    if [[ ! -n "${PCS_EVENTHUB_CONNSTRING}" ]]; then
        error "PCS_EVENTHUB_CONNSTRING is not set"
        variables_not_found=1
    fi
    if [[ ! -n "${PCS_EVENTHUB_NAME}" ]]; then
        error "PCS_EVENTHUB_NAME is not set"
        variables_not_found=1
	fi

    if [[ ! -n "${PCS_TELEMETRY_DOCUMENTDB_CONNSTRING}" ]]; then
        error "PCS_TELEMETRY_DOCUMENTDB_CONNSTRING is not set"
        variables_not_found=1
    fi

    if [ "$variables_not_found" == 1 ]; then
        error "Environment variable(s) not found, exiting"
        exit -1
    fi

    header3 "Connection strings set in environment variables"
}

exit_if_running() {
    ISUP=$(curl -ks http://127.0.0.1:$DOCKER_PORT/v1/status | grep -i "{" | wc -l | tr -d '[:space:]')
    if [[ "$ISUP" != "0" ]]; then
        error "'$DOCKER_IMAGE' is already running"
        exit 0
    fi
}

fail_if_not_running() {
    sleep 1
    ISUP=$(docker ps | grep $DOCKER_NAME | wc -l | tr -d '[:space:]')
    if [[ "$ISUP" == "0" ]]; then
        error "'$DOCKER_IMAGE' failed to start or crashed"
        exit -1
    fi
}

create_network() {
    set +e
    docker network create $DOCKER_NETWOK 2> /dev/null
    set -e
}

start() {
    docker pull $DOCKER_IMAGE
    header3 "Starting '$DOCKER_IMAGE'"
    docker run --detach --network=$DOCKER_NETWOK -p 127.0.0.1:$DOCKER_PORT:$DOCKER_PORT \
        --env-file $APP_HOME/scripts/env.list --rm --name $DOCKER_NAME $DOCKER_IMAGE
    sleep 1
    fail_if_not_running
}

stop() {
    set +e
    header3 "Stopping '$DOCKER_IMAGE'"
    docker rm -f $DOCKER_NAME
    set -e
}

if [[ "$1" == "start" ]]; then
    exit_if_running
    check_env_variables
    create_network
    start
    exit 0
fi

if [[ "$1" == "stop" ]]; then
    stop
    exit 0
fi

error "No command specified, pass either 'start' or 'stop'."
exit 1


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

if [[ "$DOCKER_TAG" == "" ]]; then
    export DOCKER_TAG=testing
fi

DOCKER_IMAGE="$DOCKER_ACCOUNT/pcs-config-$REPO:$DOCKER_TAG"
DOCKER_PORT=9005
DOCKER_NAME="pcs-config"
DOCKER_NETWOK="integrationtests"

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/"
cd $APP_HOME

source "$APP_HOME/scripts/.functions.sh"

check_env_variables()
{
    if [[ ! -n "${PCS_IOTHUB_CONNSTRING}" ]]; then
        error "PCS_IOTHUB_CONNSTRING is not set"
        exit -1
    fi

    header3 "Connection strings set in environment variables"
}

exit_if_running() {
    ISUP=$(curl -ks http://127.0.0.1:$DOCKER_PORT/v1/status | grep -i "{" | wc -l | tr -d '[:space:]')
    if [[ "$ISUP" != "0" ]]; then
        error "'$DOCKER_IMAGE' is already running"
        exit 0
    fi
}

fail_if_not_running() {
    sleep 1
    ISUP=$(docker ps | grep $DOCKER_NAME | wc -l | tr -d '[:space:]')
    if [[ "$ISUP" == "0" ]]; then
        error "'$DOCKER_IMAGE' failed to start or crashed"
        exit -1
    fi
}

create_network() {
    set +e
    docker network create $DOCKER_NETWOK 2> /dev/null
    set -e
}

start() {
    docker pull $DOCKER_IMAGE
    header3 "Starting '$DOCKER_IMAGE'"
    docker run --detach --network=$DOCKER_NETWOK -p 127.0.0.1:$DOCKER_PORT:$DOCKER_PORT \
        --env-file $APP_HOME/scripts/env.list --rm --name $DOCKER_NAME $DOCKER_IMAGE
    sleep 1
    fail_if_not_running
}

stop() {
    set +e
    header3 "Stopping '$DOCKER_IMAGE'"
    docker rm -f $DOCKER_NAME
    set -e
}

if [[ "$1" == "start" ]]; then
    exit_if_running
    check_env_variables
    create_network
    start
    exit 0
fi

if [[ "$1" == "stop" ]]; then
    stop
    exit 0
fi

error "No command specified, pass either 'start' or 'stop'."
exit 1


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/"
cd $APP_HOME
source "$APP_HOME/scripts/.functions.sh"

start() {
    ./scripts/storageadapter.sh start
    ./scripts/devicesimulation.sh start
    ./scripts/telemetry.sh start
    ./scripts/config.sh start
    ./scripts/iothubmanager.sh start
    ./scripts/asamanager.sh start
}

stop() {
    ./scripts/storageadapter.sh stop
    ./scripts/devicesimulation.sh stop
    ./scripts/telemetry.sh stop
    ./scripts/config.sh stop
    ./scripts/iothubmanager.sh stop
    ./scripts/asamanager.sh stop
}

#Script Args
repo=""
tag="testing"
act="start"
dockeraccount="azureiotpcs"

set_up_env() {
    if [[ "$repo" == "dotnet" ]]; then
        export REPO="dotnet"
    elif [[ "$repo" == "java" ]]; then
        export REPO="java"
    else
        error "No microservice type specified, pass either 'dotnet' or 'java'."
        echo "eg:- sh containers.sh -re java"
        exit 1
    fi
	export DOCKER_TAG=$tag
    export DOCKER_ACCOUNT=$dockeraccount
}

tear_down() {
    unset DOCKER_TAG
    unset DOCKER_ACCOUNT
}

while [[ $# -gt 0 ]] ;
do
    opt=$1;
    shift;
    case $opt in
        -dt|--dockertag) tag=$1; shift;;
        -re|--repo) repo=$1; shift;;
        -act|--action) act=$1; shift;;
        -da|--docker-account) dockeraccount=$1; shift;;
        *) shift;
    esac
done

set_up_env

if [[ "$act" == "start" ]]; then
    start
    exit 0
fi

if [[ "$act" == "stop" ]]; then
    stop
    exit 0
fi

tear_down

error "No command specified, pass either 'start' or 'stop'."
exit 1


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

export DOCKER_TAG=DS-1.0.3

DOCKER_IMAGE="$DOCKER_ACCOUNT/device-simulation-dotnet:$DOCKER_TAG"
DOCKER_PORT=9003
DOCKER_NAME="device-simulation"
DOCKER_NETWOK="integrationtests"

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/"
cd $APP_HOME

source "$APP_HOME/scripts/.functions.sh"

check_env_variables()
{
    if [[ ! -n "${PCS_IOTHUB_CONNSTRING}" ]]; then
        error "PCS_IOTHUB_CONNSTRING is not set"
        exit -1
    fi

    header3 "Connection strings set in environment variables"
}

exit_if_running() {
    ISUP=$(curl -ks http://127.0.0.1:$DOCKER_PORT/v1/status | grep -i "{" | wc -l | tr -d '[:space:]')
    if [[ "$ISUP" != "0" ]]; then
        error "'$DOCKER_IMAGE' is already running"
        exit 0
    fi
}

fail_if_not_running() {
    sleep 1
    ISUP=$(docker ps | grep $DOCKER_NAME | wc -l | tr -d '[:space:]')
    if [[ "$ISUP" == "0" ]]; then
        error "'$DOCKER_IMAGE' failed to start or crashed"
        exit -1
    fi
}

create_network() {
    set +e
    docker network create $DOCKER_NETWOK 2> /dev/null
    set -e
}

start() {
    docker pull $DOCKER_IMAGE
    header3 "Starting '$DOCKER_IMAGE'"
    docker run --detach --network=$DOCKER_NETWOK -p 127.0.0.1:$DOCKER_PORT:$DOCKER_PORT \
        --env-file $APP_HOME/scripts/env.list --rm --name $DOCKER_NAME $DOCKER_IMAGE
    sleep 1
    fail_if_not_running
}

stop() {
    set +e
    header3 "Stopping '$DOCKER_IMAGE'"
    docker rm -f $DOCKER_NAME
    set -e
}

if [[ "$1" == "start" ]]; then
    exit_if_running
    check_env_variables
    create_network
    start
    exit 0
fi

if [[ "$1" == "stop" ]]; then
    stop
    exit 0
fi

error "No command specified, pass either 'start' or 'stop'."
exit 1


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

if [[ "$DOCKER_TAG" == "" ]]; then
    export DOCKER_TAG=testing
fi

DOCKER_IMAGE="$DOCKER_ACCOUNT/iothub-manager-$REPO:$DOCKER_TAG"
DOCKER_PORT=9002
DOCKER_NAME="iothub-manager"
DOCKER_NETWOK="integrationtests"

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/"
cd $APP_HOME

source "$APP_HOME/scripts/.functions.sh"

check_env_variables()
{
    if [[ ! -n "${PCS_IOTHUB_CONNSTRING}" ]]; then
        error "PCS_IOTHUB_CONNSTRING is not set"
        exit -1
    fi

    header3 "Connection strings set in environment variables"
}

exit_if_running() {
    ISUP=$(curl -ks http://127.0.0.1:$DOCKER_PORT/v1/status | grep -i "{" | wc -l | tr -d '[:space:]')
    if [[ "$ISUP" != "0" ]]; then
        error "'$DOCKER_IMAGE' is already running"
        exit 0
    fi
}

fail_if_not_running() {
    sleep 1
    ISUP=$(docker ps | grep $DOCKER_NAME | wc -l | tr -d '[:space:]')
    if [[ "$ISUP" == "0" ]]; then
        error "'$DOCKER_IMAGE' failed to start or crashed"
        exit -1
    fi
}

create_network() {
    set +e
    docker network create $DOCKER_NETWOK 2> /dev/null
    set -e
}

start() {
    docker pull $DOCKER_IMAGE
    header3 "Starting '$DOCKER_IMAGE'"
    docker run --detach --network=$DOCKER_NETWOK -p 127.0.0.1:$DOCKER_PORT:$DOCKER_PORT \
        --env-file $APP_HOME/scripts/env.list --rm --name $DOCKER_NAME $DOCKER_IMAGE
    sleep 1
    fail_if_not_running
}

stop() {
    set +e
    header3 "Stopping '$DOCKER_IMAGE'"
    docker rm -f $DOCKER_NAME
    set -e
}

if [[ "$1" == "start" ]]; then
    exit_if_running
    check_env_variables
    create_network
    start
    exit 0
fi

if [[ "$1" == "stop" ]]; then
    stop
    exit 0
fi

error "No command specified, pass either 'start' or 'stop'."
exit 1


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

if [[ "$DOCKER_TAG" == "" ]]; then
    export DOCKER_TAG=testing
fi

DOCKER_IMAGE="$DOCKER_ACCOUNT/pcs-storage-adapter-$REPO:$DOCKER_TAG"
DOCKER_PORT=9022
DOCKER_NAME="storage-adapter"
DOCKER_NETWOK="integrationtests"

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/"
cd $APP_HOME

source "$APP_HOME/scripts/.functions.sh"

check_env_variables()
{
    if [[ ! -n "${PCS_STORAGEADAPTER_DOCUMENTDB_CONNSTRING}" ]]; then
        error "PCS_STORAGEADAPTER_DOCUMENTDB_CONNSTRING is not set"
        exit -1
    fi

    header3 "Connection strings set in environment variables"
}

exit_if_running() {
    ISUP=$(curl -ks http://127.0.0.1:$DOCKER_PORT/v1/status | grep -i "{" | wc -l | tr -d '[:space:]')
    if [[ "$ISUP" != "0" ]]; then
        error "'$DOCKER_IMAGE' is already running"
        exit 0
    fi
}

fail_if_not_running() {
    sleep 1
    ISUP=$(docker ps | grep $DOCKER_NAME | wc -l | tr -d '[:space:]')
    if [[ "$ISUP" == "0" ]]; then
        error "'$DOCKER_IMAGE' failed to start or crashed"
        exit -1
    fi
}

create_network() {
    set +e
    docker network create $DOCKER_NETWOK 2> /dev/null
    set -e
}

start() {
    docker pull $DOCKER_IMAGE
    header3 "Starting '$DOCKER_IMAGE'"
    docker run --detach --network=$DOCKER_NETWOK -p 127.0.0.1:$DOCKER_PORT:$DOCKER_PORT \
        --env-file $APP_HOME/scripts/env.list --rm --name $DOCKER_NAME $DOCKER_IMAGE
    sleep 1
    fail_if_not_running
}

stop() {
    set +e
    header3 "Stopping '$DOCKER_IMAGE'"
    docker rm -f $DOCKER_NAME
    set -e
}

if [[ "$1" == "start" ]]; then
    exit_if_running
    check_env_variables
    create_network
    start
    exit 0
fi

if [[ "$1" == "stop" ]]; then
    stop
    exit 0
fi

error "No command specified, pass either 'start' or 'stop'."
exit 1


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

if [[ "$DOCKER_TAG" == "" ]]; then
    export DOCKER_TAG=testing
fi

DOCKER_IMAGE="$DOCKER_ACCOUNT/telemetry-agent-$REPO:$DOCKER_TAG"
DOCKER_PORT=9023
DOCKER_NAME="telemetry-agent"
DOCKER_NETWOK="integrationtests"

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/"
cd $APP_HOME

source "$APP_HOME/scripts/.functions.sh"

check_env_variables()
{
    if [[ ! -n "${PCS_TELEMETRY_DOCUMENTDB_CONNSTRING}" ]]; then
        error "PCS_TELEMETRY_DOCUMENTDB_CONNSTRING is not set"
        exit -1
    fi

    header3 "Connection strings set in environment variables"
}

exit_if_running() {
    ISUP=$(curl -ks http://127.0.0.1:$DOCKER_PORT/v1/status | grep -i "{" | wc -l | tr -d '[:space:]')
    if [[ "$ISUP" != "0" ]]; then
        error "'$DOCKER_IMAGE' is already running"
        exit 0
    fi
}

fail_if_not_running() {
    sleep 1
    ISUP=$(docker ps | grep $DOCKER_NAME | wc -l | tr -d '[:space:]')
    if [[ "$ISUP" == "0" ]]; then
        error "'$DOCKER_IMAGE' failed to start or crashed"
        docker logs $DOCKER_NAME
        exit -1
    fi
}

create_network() {
    set +e
    docker network create $DOCKER_NETWOK 2> /dev/null
    set -e
}

start() {
    docker pull $DOCKER_IMAGE
    header3 "Starting '$DOCKER_IMAGE'"
    docker run --detach --network=$DOCKER_NETWOK -p 127.0.0.1:$DOCKER_PORT:$DOCKER_PORT \
        --env-file $APP_HOME/scripts/env.list --rm --name $DOCKER_NAME $DOCKER_IMAGE
    sleep 1
    fail_if_not_running
}

stop() {
    set +e
    header3 "Stopping '$DOCKER_IMAGE'"
    docker rm -f $DOCKER_NAME
    set -e
}

if [[ "$1" == "start" ]]; then
    exit_if_running
    check_env_variables
    create_network
    start
    exit 0
fi

if [[ "$1" == "stop" ]]; then
    stop
    exit 0
fi

error "No command specified, pass either 'start' or 'stop'."
exit 1


#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.
# Note: Windows Bash doesn't support shebang extra params
set -e

if [[ "$DOCKER_TAG" == "" ]]; then
    export DOCKER_TAG=testing
fi

DOCKER_IMAGE="$DOCKER_ACCOUNT/telemetry-$REPO:$DOCKER_TAG"
DOCKER_PORT=9004
DOCKER_NAME="telemetry"
DOCKER_NETWOK="integrationtests"

APP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/"
cd $APP_HOME

source "$APP_HOME/scripts/.functions.sh"

check_env_variables()
{
    if [[ ! -n "${PCS_TELEMETRY_DOCUMENTDB_CONNSTRING}" ]]; then
        error "PCS_TELEMETRY_DOCUMENTDB_CONNSTRING is not set"
        exit -1
    fi
    if [[ -z "$PCS_AUTH_ISSUER" ]]; then
    echo "Error: the PCS_AUTH_ISSUER environment variable is not defined."
    exit -1
    fi

    if [[ -z "$PCS_AUTH_AUDIENCE" ]]; then
        echo "Error: the PCS_AUTH_AUDIENCE environment variable is not defined."
        exit -1
    fi

    if [[ -z "$PCS_TELEMETRY_STORAGE_TYPE" ]]; then
        echo "Error: the PCS_TELEMETRY_STORAGE_TYPE environment variable is not defined."
        exit -1
    fi

    # The settings below are for Time Series Insights. If your deployment does not use
    # Time Series Insights they are safe to remove.

    if [[ -z "$PCS_TSI_FQDN" ]]; then
        echo "Error: the PCS_TSI_FQDN environment variable is not defined."
        exit -1
    fi

    # Settings for actions
    if [[ -z "$PCS_ACTION_EVENTHUB_NAME" ]]; then
        echo "Error: the PCS_ACTION_EVENTHUB_NAME environment variable is not defined."
        exit -1
    fi

    if [[ -z "$PCS_ACTION_EVENTHUB_CONNSTRING" ]]; then
        echo "Error: the PCS_ACTION_EVENTHUB_CONNSTRING environment variable is not defined."
        exit -1
    fi

    if [[ -z "$PCS_LOGICAPP_ENDPOINT_URL" ]]; then
        echo "Error: the PCS_LOGICAPP_ENDPOINT_URL environment variable is not defined."
        exit -1
    fi

    if [[ -z "$PCS_AZUREBLOB_CONNSTRING" ]]; then
        echo "Error: the PCS_AZUREBLOB_CONNSTRING environment variable is not defined."
        exit -1
    fi

    if [[ -z "$PCS_SOLUTION_WEBSITE_URL" ]]; then
        echo "Error: the PCS_SOLUTION_WEBSITE_URL environment variable is not defined."
        exit -1
    fi

    header3 "Connection strings set in environment variables"
}

exit_if_running() {
    ISUP=$(curl -ks http://127.0.0.1:$DOCKER_PORT/v1/status | grep -i "{" | wc -l | tr -d '[:space:]')
    if [[ "$ISUP" != "0" ]]; then
        error "'$DOCKER_IMAGE' is already running"
        exit 0
    fi
}

fail_if_not_running() {
    sleep 1
    ISUP=$(docker ps | grep $DOCKER_NAME | wc -l | tr -d '[:space:]')
    if [[ "$ISUP" == "0" ]]; then
        error "'$DOCKER_IMAGE' failed to start or crashed"
        docker logs $DOCKER_NAME
        exit -1
    fi
}

create_network() {
    set +e
    docker network create $DOCKER_NETWOK 2> /dev/null
    set -e
}

start() {
    docker pull $DOCKER_IMAGE
    header3 "Starting '$DOCKER_IMAGE'"
    docker run --detach --network=$DOCKER_NETWOK -p 127.0.0.1:$DOCKER_PORT:$DOCKER_PORT \
        --env-file $APP_HOME/scripts/env.list --rm --name $DOCKER_NAME $DOCKER_IMAGE
    sleep 1
    fail_if_not_running
}

stop() {
    set +e
    header3 "Stopping '$DOCKER_IMAGE'"
    docker rm -f $DOCKER_NAME
    set -e
}

if [[ "$1" == "start" ]]; then
    exit_if_running
    check_env_variables
    create_network
    start
    exit 0
fi

if [[ "$1" == "stop" ]]; then
    stop
    exit 0
fi

error "No command specified, pass either 'start' or 'stop'."
exit 1