Skip to content

Instantly share code, notes, and snippets.

@AdityaRanjanSingh
Last active March 12, 2020 16:37
Show Gist options
  • Save AdityaRanjanSingh/f4bdf36351b1bac50a816e056bb0be31 to your computer and use it in GitHub Desktop.
Save AdityaRanjanSingh/f4bdf36351b1bac50a816e056bb0be31 to your computer and use it in GitHub Desktop.
Merge separate files for the intent, slot type and bot into one before start import aws lex-model start-import cli
#!/usr/bin/env sh
STATUS_COMPLETED="COMPLETE"
STATUS_IN_PROGRESS="IN_PROGRESS"
IMPORT_STATUS="IN_PROGRESS"
#Clear the distribution directory
rm -rf dis
mkdir dist && mkdir dist/input && mkdir dist/output
#Move to the new lex directory and merge all intents and slot types into single file
gomplate --input-dir=src/lex/slottypes/ --output-dir=dist/input/slottypes --datasource config=iac/resources/env/$ENVIRONMENT/config.json
gomplate --input-dir=src/lex/intents/ --output-dir=dist/input/intents --datasource config=iac/resources/env/$ENVIRONMENT/config.json
gomplate --input-dir=src/lex/bot/ --output-dir=dist/input/bot --datasource config=iac/resources/env/$ENVIRONMENT/config.json
#Merge all the slottypes and intents into 1
jq --slurp '.' dist/input/slottypes/*.json > "dist/output/slotTypes.json"
jq --slurp '.' dist/input/intents/*.json > "dist/output/intents.json"
#Merge the slot types and intents with the bot json and save in export file
jq --slurp '.[0].resource.intents=.[1]' './dist/input/bot/bot.json' './dist/output/intents.json' | jq '.[0]' > ./dist/output/bot.json
jq --slurp '.[0].resource.slotTypes=.[1]' './dist/output/bot.json' './dist/output/slotTypes.json' | jq '.[0]' > ./dist/Export_bot.json
cat ./dist/Export_bot.json
#Zipping the file
npm run zip
aws lex-models start-import --payload fileb://./dist/askwilbur.zip --resource-type BOT --merge-strategy OVERWRITE_LATEST --region $BOT_REGION > dist/start-import-response.json || { echo 'Start import failed' ; exit 1; }
IMPORT_ID=$( jq -r '.importId' dist/start-import-response.json )
echo "Checking if the import is completed ${IMPORT_ID}"
while [ "$IMPORT_STATUS" == "$STATUS_IN_PROGRESS" ]
do
aws lex-models get-import --import-id $IMPORT_ID > dist/get-import.json || { echo 'Get import failed' ; exit 1; }
IMPORT_STATUS=$( jq -r '.importStatus' dist/get-import.json )
BOT_NAME=$( jq -r '.name' dist/get-import.json )
echo "IMPORT_STATUS = ${IMPORT_STATUS}"
done
if [ $IMPORT_STATUS == $STATUS_COMPLETED ]
then
echo "Bot imported successfully"
else
echo "Bot import failed"
exit 1
fi
#Getting the latest version of the bot
aws lex-models get-bot --region $BOT_REGION --name $BOT_NAME --version-or-alias "\$LATEST" > dist/get-bot.json || { echo 'Get bot failed' ; exit 1; }
jq 'del(.createdDate)' dist/get-bot.json | jq 'del(.lastUpdatedDate)' | jq 'del(.status)' | jq 'del(.version)' | jq '.processBehavior = "BUILD"' > dist/put-bot.json
aws lex-models put-bot --region $BOT_REGION --name $BOT_NAME --cli-input-json file://dist/put-bot.json > dist/put-bot-response.json || { echo 'Put bot failed' ; exit 1; }
aws lex-models get-bot --name $BOT_NAME --version-or-alias "\$LATEST" --region $BOT_REGION > dist/get-bot.json || { echo 'Get bot failed' ; exit 1; }
BOT_CHECKSUM_VERSION=$( jq -r '.checksum' dist/get-bot.json )
LATEST_BOT_VERSION=$( jq -r '.version' dist/get-bot.json )
#Not creating a new version of the bot
aws lex-models get-bot-alias --name $BOT_ALIAS --bot-name $BOT_NAME -- region $BOT_REGION || { echo 'Alias does not exist setting checksum to blank'; BOT_CHECKSUM_VERSION=""; }
echo "Only create a new alias if bot already there, checksum = ${BOT_CHECKSUM_VERSION} and latest bot version ${LATEST_BOT_VERSION}"
if [ $BOT_CHECKSUM_VERSION =="" ]
then
aws lex-models put-bot-alias --region $BOT_REGION --name $BOT_ALIAS --bot-name $BOT_NAME --bot-version $LATEST_BOT_VERSION > dist/put-bot-alias.json || { echo 'Publish version failed' ; exit 1; }
else
aws lex-models put-bot-alias --region $BOT_REGION --name $BOT_ALIAS --bot-name $BOT_NAME --bot-version $LATEST_BOT_VERSION --checksum $BOT_CHECKSUM_VERSION > dist/put-bot-alias.json || { echo 'Publish version failed' ; exit 1; }
fi
echo "Done deploying the bot"
cat dist/put-bot-alias.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment