Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Fix Device Simulation setup script (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
dluc authored and hathind-ms committed Nov 15, 2018
1 parent d7cf07f commit 0a87ed0
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 53 deletions.
6 changes: 3 additions & 3 deletions solutions/devicesimulation-nohub/armtemplate/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,8 @@
"fileUris": [
"[parameters('vmSetupScriptUri')]"
],

"commandToExecute": "[concat('sh setup-wrapper.sh --log-level Info ', ' --hostname ', concat('\"', variables('vmFQDN'), '\"'), ' --resource-group-location ', '\"', variables('location'), '\"', ' --vmss-name ', '\"', parameters('vmssName'), '\"', ' --docdb-connstring ', concat('\"', 'AccountEndpoint=', reference(variables('cosmosDBResourceId')).documentEndpoint, ';AccountKey=', listkeys(variables('cosmosDBResourceId'), variables('cosmosDBApiVersion')).primaryMasterKey, ';', '\"'), ' --ssl-certificate ', concat('\"', parameters('remoteEndpointCertificate'), '\"'), ' --ssl-certificate-key ', concat('\"', parameters('remoteEndpointCertificateKey'), '\"'), ' --auth-type aad ', ' --auth-audience ', concat('\"', parameters('aadClientId'), '\"'), ' --aad-appid ', concat('\"', parameters('aadClientId'), '\"'), ' --aad-tenant ', concat('\"', parameters('aadTenantId'), '\"'), ' --auth-issuer ', concat('\"https://sts.windows.net/', parameters('aadTenantId'), '/\"'), ' --aad-instance ', concat('\"', parameters('aadInstance'), '\"'), ' --resource-group ', concat('\"', parameters('solutionName'), '\"'), ' --solution-type ', concat('\"', parameters('solutionType'), '\"'), ' --release-version ', concat('\"', parameters('pcsReleaseVersion'), '\"'), ' --deployment-id ', concat('\"', parameters('deploymentId'), '\"'), ' --diagnostics-url ', concat('\"', parameters('diagnosticsEndpointUrl'), '\"'), ' --storage-connstring ', concat('\"', 'DefaultEndpointsProtocol=https;AccountName=', parameters('storageName'), ';AccountKey=', listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageName')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value, '\"'), ' --docdb-name ', concat('\"', parameters('documentDBName'), '\"'), ' --solution-name ', concat('\"', parameters('solutionName'), '\"'), ' --solution-setup-url ', concat('\"', parameters('solutionTemplateRepository'), '\"'), ' --docker-tag ', concat('\"', parameters('pcsDockerTag'), '\"'),' --subscription-domain ', concat('\"', parameters('domain'), '\"'), ' --aad-sp-client-id ', concat('\"', parameters('aadClientServicePrincipalId'), '\"'), ' --cloud-type ', concat('\"', parameters('cloudType'), '\"'), ' --aad-app-secret ', concat('\"', parameters('aadClientSecret'), '\"'), ' --subscription-id ', concat('\"', subscription().subscriptionId, '\"' ))]"
"commandToExecute": "[concat('bash setup-wrapper.sh --log-level Info ', ' --hostname ', concat('\"', variables('vmFQDN'), '\"'), ' --resource-group-location ', '\"', variables('location'), '\"', ' --vmss-name ', '\"', parameters('vmssName'), '\"', ' --docdb-connstring ', concat('\"', 'AccountEndpoint=', reference(variables('cosmosDBResourceId')).documentEndpoint, ';AccountKey=', listkeys(variables('cosmosDBResourceId'), variables('cosmosDBApiVersion')).primaryMasterKey, ';', '\"'), ' --ssl-certificate ', concat('\"', parameters('remoteEndpointCertificate'), '\"'), ' --ssl-certificate-key ', concat('\"', parameters('remoteEndpointCertificateKey'), '\"'), ' --auth-type aad ', ' --auth-audience ', concat('\"', parameters('aadClientId'), '\"'), ' --aad-appid ', concat('\"', parameters('aadClientId'), '\"'), ' --aad-tenant ', concat('\"', parameters('aadTenantId'), '\"'), ' --auth-issuer ', concat('\"https://sts.windows.net/', parameters('aadTenantId'), '/\"'), ' --aad-instance ', concat('\"', parameters('aadInstance'), '\"'), ' --resource-group ', concat('\"', parameters('solutionName'), '\"'), ' --solution-type ', concat('\"', parameters('solutionType'), '\"'), ' --release-version ', concat('\"', parameters('pcsReleaseVersion'), '\"'), ' --deployment-id ', concat('\"', parameters('deploymentId'), '\"'), ' --diagnostics-url ', concat('\"', parameters('diagnosticsEndpointUrl'), '\"'), ' --storage-connstring ', concat('\"', 'DefaultEndpointsProtocol=https;AccountName=', parameters('storageName'), ';AccountKey=', listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageName')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value, '\"'), ' --docdb-name ', concat('\"', parameters('documentDBName'), '\"'), ' --solution-name ', concat('\"', parameters('solutionName'), '\"'), ' --solution-setup-url ', concat('\"', parameters('solutionTemplateRepository'), '\"'), ' --docker-tag ', concat('\"', parameters('pcsDockerTag'), '\"'),' --subscription-domain ', concat('\"', parameters('domain'), '\"'), ' --aad-sp-client-id ', concat('\"', parameters('aadClientServicePrincipalId'), '\"'), ' --cloud-type ', concat('\"', parameters('cloudType'), '\"'), ' --aad-app-secret ', concat('\"', parameters('aadClientSecret'), '\"'), ' --subscription-id ', concat('\"', subscription().subscriptionId, '\"' ))]",
"skipDos2Unix": true
}
}
}
Expand Down Expand Up @@ -647,4 +647,4 @@
"value": "[parameters('adminUsername')]"
}
}
}
}
30 changes: 17 additions & 13 deletions solutions/devicesimulation-nohub/single-vm/setup-wrapper.sh
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.

# Solution: devicesimulation-nohub

# Important:
# 1. The script is designed NOT to throw errors, to avoid secrets ending in azureiotsolutions.com logs
# 2. In case of errors, the script terminates with exit code "1" which must be caught by the deployment service to inform the user.
# 3. The script invokes setup.sh script and checks for errors returned by the script, logging to a file in the VM.
# 1. The script runs in an environment with old/strict shell support, so don't rely on Bash niceties
# 2. The script is designed NOT to throw errors, to avoid secrets ending in azureiotsolutions.com logs
# 3. In case of errors, the script terminates with exit code "1" which must be caught by the deployment service to inform the user.
# 4. The script invokes setup.sh script and checks for errors returned by the script, logging to a file in the VM.

# Enable this for debugging only
##set -ex

APP_PATH="/app"
SETUP_LOG="${APP_PATH}/setup.log"

# Copy all params before shifting the original ones
PARAMS_COPY="$@"

while [ "$#" -gt 0 ]; do
case "$1" in
--release-version) PCS_RELEASE_VERSION="$2" ;;
# Loop through arguments and extract some parameters, without modifying $@ needed later
for X in "$@"; do
case "$PREVIOUS" in
--release-version) PCS_RELEASE_VERSION="$X" ;;
esac
shift
PREVIOUS="$X"
done

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

Expand Down Expand Up @@ -48,10 +52,10 @@ if [ $? -ne 0 ]; then
fi

# Invoke setup script
./setup.sh "${PARAMS_COPY}" >> ${SETUP_LOG} 2>&1
./setup.sh "${@}" >> ${SETUP_LOG} 2>&1
RESULT=$?
echo "Exit code: $RESULT"
if [[ $RESULT -ne 0 ]]; then
if [ $RESULT -ne 0 ]; then
echo "Setup failed, please see log file '${SETUP_LOG}' for more information"
cat ${SETUP_LOG}
exit 1
Expand Down
27 changes: 21 additions & 6 deletions solutions/devicesimulation-nohub/single-vm/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# 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

Expand All @@ -26,14 +28,15 @@ 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/pcs-cli/DS-1.0.0/solutions/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" ;;
--solution-type) PCS_SOLUTION_TYPE="$2" ;;
--solution-name) PCS_SOLUTION_NAME="$2" ;;
--resource-group) PCS_RESOURCE_GROUP="$2" ;;
--storage-connstring) PCS_AZURE_STORAGE_ACCOUNT="$2" ;;
--docdb-name) PCS_DOCDB_NAME="$2" ;;
--docdb-connstring) PCS_STORAGEADAPTER_DOCUMENTDB_CONNSTRING="$2" ;;
--ssl-certificate) PCS_CERTIFICATE="$2" ;;
Expand All @@ -49,14 +52,23 @@ while [ "$#" -gt 0 ]; do
--cloud-type) PCS_CLOUD_TYPE="$2" ;;
--deployment-id) PCS_DEPLOYMENT_ID="$2" ;;
--diagnostics-url) PCS_DIAGNOSTICS_ENDPOINT_URL="$2" ;;
--docker-tag) PCS_DOCKER_TAG="$2" ;;
--release-version) PCS_RELEASE_VERSION="$2" ;;
--resource-group-location) PCS_RESOURCE_GROUP_LOCATION="$2" ;;
--vmss-name) PCS_VMSS_NAME="$2" ;;
--storage-connstring) PCS_AZURE_STORAGE_ACCOUNT="$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/pcs-cli/${PCS_RELEASE_VERSION}/solutions/devicesimulation-nohub/single-vm"
SCRIPTS_URL="${REPOSITORY}/scripts/"
Expand Down Expand Up @@ -89,11 +101,12 @@ INSTALL_DOCKER_RESULT="OK"
install_docker_ce
if [ "$INSTALL_DOCKER_RESULT" != "OK" ]; then
set -e
echo "First attempt to install Docker failed, retrying..."
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
Expand Down Expand Up @@ -216,11 +229,13 @@ echo "export PCS_DIAGNOSTICS_ENDPOINT_URL=\"${PCS_DIAGNOSTICS_ENDPOINT_URL}\""
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}

# 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}
Expand Down
6 changes: 3 additions & 3 deletions solutions/devicesimulation/armtemplate/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,8 @@
"fileUris": [
"[parameters('vmSetupScriptUri')]"
],

"commandToExecute": "[concat('sh setup-wrapper.sh --log-level Info --hostname ', '\"', variables('vmFQDN'), '\"', ' --resource-group-location ', '\"', variables('location'), '\"', ' --vmss-name ', '\"', parameters('vmssName'), '\"', ' --docdb-connstring ', '\"', 'AccountEndpoint=', reference(variables('cosmosDBResourceId')).documentEndpoint, ';AccountKey=', listkeys(variables('cosmosDBResourceId'), variables('cosmosDBApiVersion')).primaryMasterKey, ';', '\"', ' --ssl-certificate ', '\"', parameters('remoteEndpointCertificate'), '\"', ' --ssl-certificate-key ', '\"', parameters('remoteEndpointCertificateKey'), '\"', ' --auth-type aad ', ' --auth-audience ', '\"', parameters('aadClientId'), '\"', ' --aad-appid ', '\"', parameters('aadClientId'), '\"', ' --aad-tenant ', '\"', parameters('aadTenantId'), '\"', ' --auth-issuer ', '\"', 'https://sts.windows.net/', parameters('aadTenantId'), '/', '\"', ' --aad-instance ', '\"', parameters('aadInstance'), '\"', ' --resource-group ', '\"', parameters('solutionName'), '\"', ' --release-version ', concat('\"', parameters('pcsReleaseVersion'), '\"'), ' --solution-type ', '\"', parameters('solutionType'), '\"', ' --deployment-id ', '\"', parameters('deploymentId'), '\"', ' --diagnostics-url ', '\"', parameters('diagnosticsEndpointUrl'), '\"', ' --storage-connstring ', '\"', 'DefaultEndpointsProtocol=https;AccountName=', parameters('storageName'), ';AccountKey=', listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageName')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value, '\"', ' --docdb-name ', '\"', parameters('documentDBName'), '\"', ' --solution-name ', '\"', parameters('solutionName'), '\"', ' --solution-setup-url ', '\"', parameters('solutionTemplateRepository'), '\"', ' --docker-tag ', '\"', parameters('pcsDockerTag'), '\"', ' --subscription-domain ', '\"', parameters('domain'), '\"', ' --subscription-id ', concat('\"', subscription().subscriptionId, '\"'),' --aad-sp-client-id ', concat('\"', parameters('aadClientServicePrincipalId'), '\"'), ' --aad-app-secret ', concat('\"', parameters('aadClientSecret'), '\"'), ' --iothub-name ', concat('\"', parameters('iotHubName'), '\"'), ' --iothub-sku ', parameters('iotHubSku'), ' --iothub-tier ', parameters('iotHubTier'), ' --iothub-units ', parameters('iotHubUnits') ,' --iothub-connstring ', concat('\"', 'HostName=', reference(variables('iotHubResourceId')).hostName, ';SharedAccessKeyName=', variables('iotHubKeyName'), ';SharedAccessKey=', listkeys(variables('iotHubKeyResource'), variables('iotHubApiVersion')).primaryKey, '\"'), ' --cloud-type ', concat('\"', parameters('cloudType'), '\"'))]"
"commandToExecute": "[concat('bash setup-wrapper.sh --log-level Info --hostname ', '\"', variables('vmFQDN'), '\"', ' --resource-group-location ', '\"', variables('location'), '\"', ' --vmss-name ', '\"', parameters('vmssName'), '\"', ' --docdb-connstring ', '\"', 'AccountEndpoint=', reference(variables('cosmosDBResourceId')).documentEndpoint, ';AccountKey=', listkeys(variables('cosmosDBResourceId'), variables('cosmosDBApiVersion')).primaryMasterKey, ';', '\"', ' --ssl-certificate ', '\"', parameters('remoteEndpointCertificate'), '\"', ' --ssl-certificate-key ', '\"', parameters('remoteEndpointCertificateKey'), '\"', ' --auth-type aad ', ' --auth-audience ', '\"', parameters('aadClientId'), '\"', ' --aad-appid ', '\"', parameters('aadClientId'), '\"', ' --aad-tenant ', '\"', parameters('aadTenantId'), '\"', ' --auth-issuer ', '\"', 'https://sts.windows.net/', parameters('aadTenantId'), '/', '\"', ' --aad-instance ', '\"', parameters('aadInstance'), '\"', ' --resource-group ', '\"', parameters('solutionName'), '\"', ' --release-version ', concat('\"', parameters('pcsReleaseVersion'), '\"'), ' --solution-type ', '\"', parameters('solutionType'), '\"', ' --deployment-id ', '\"', parameters('deploymentId'), '\"', ' --diagnostics-url ', '\"', parameters('diagnosticsEndpointUrl'), '\"', ' --storage-connstring ', '\"', 'DefaultEndpointsProtocol=https;AccountName=', parameters('storageName'), ';AccountKey=', listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageName')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value, '\"', ' --docdb-name ', '\"', parameters('documentDBName'), '\"', ' --solution-name ', '\"', parameters('solutionName'), '\"', ' --solution-setup-url ', '\"', parameters('solutionTemplateRepository'), '\"', ' --docker-tag ', '\"', parameters('pcsDockerTag'), '\"', ' --subscription-domain ', '\"', parameters('domain'), '\"', ' --subscription-id ', concat('\"', subscription().subscriptionId, '\"'),' --aad-sp-client-id ', concat('\"', parameters('aadClientServicePrincipalId'), '\"'), ' --aad-app-secret ', concat('\"', parameters('aadClientSecret'), '\"'), ' --iothub-name ', concat('\"', parameters('iotHubName'), '\"'), ' --iothub-sku ', parameters('iotHubSku'), ' --iothub-tier ', parameters('iotHubTier'), ' --iothub-units ', parameters('iotHubUnits') ,' --iothub-connstring ', concat('\"', 'HostName=', reference(variables('iotHubResourceId')).hostName, ';SharedAccessKeyName=', variables('iotHubKeyName'), ';SharedAccessKey=', listkeys(variables('iotHubKeyResource'), variables('iotHubApiVersion')).primaryKey, '\"'), ' --cloud-type ', concat('\"', parameters('cloudType'), '\"'))]",
"skipDos2Unix": true
}
}
}
Expand Down Expand Up @@ -707,4 +707,4 @@
"value": "[parameters('adminUsername')]"
}
}
}
}
30 changes: 17 additions & 13 deletions solutions/devicesimulation/single-vm/setup-wrapper.sh
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.

# Solution: devicesimulation

# Important:
# 1. The script is designed NOT to throw errors, to avoid secrets ending in azureiotsolutions.com logs
# 2. In case of errors, the script terminates with exit code "1" which must be caught by the deployment service to inform the user.
# 3. The script invokes setup.sh script and checks for errors returned by the script, logging to a file in the VM.
# 1. The script runs in an environment with old/strict shell support, so don't rely on Bash niceties
# 2. The script is designed NOT to throw errors, to avoid secrets ending in azureiotsolutions.com logs
# 3. In case of errors, the script terminates with exit code "1" which must be caught by the deployment service to inform the user.
# 4. The script invokes setup.sh script and checks for errors returned by the script, logging to a file in the VM.

# Enable this for debugging only
##set -ex

APP_PATH="/app"
SETUP_LOG="${APP_PATH}/setup.log"

# Copy all params before shifting the original ones
PARAMS_COPY="$@"

while [ "$#" -gt 0 ]; do
case "$1" in
--release-version) PCS_RELEASE_VERSION="$2" ;;
# Loop through arguments and extract some parameters, without modifying $@ needed later
for X in "$@"; do
case "$PREVIOUS" in
--release-version) PCS_RELEASE_VERSION="$X" ;;
esac
shift
PREVIOUS="$X"
done

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

Expand Down Expand Up @@ -48,10 +52,10 @@ if [ $? -ne 0 ]; then
fi

# Invoke setup script
./setup.sh "${PARAMS_COPY}" >> ${SETUP_LOG} 2>&1
./setup.sh "${@}" >> ${SETUP_LOG} 2>&1
RESULT=$?
echo "Exit code: $RESULT"
if [[ $RESULT -ne 0 ]]; then
if [ $RESULT -ne 0 ]; then
echo "Setup failed, please see log file '${SETUP_LOG}' for more information"
cat ${SETUP_LOG}
exit 1
Expand Down
Loading

0 comments on commit 0a87ed0

Please sign in to comment.