Skip to content

Commit

Permalink
Added skill.json, config and hook scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashanth Bheemagani committed Nov 21, 2018
1 parent 0c11686 commit 40eed56
Show file tree
Hide file tree
Showing 6 changed files with 366 additions and 26 deletions.
20 changes: 20 additions & 0 deletions .ask/config
@@ -0,0 +1,20 @@
{
"deploy_settings": {
"default": {
"skill_id": "",
"resources": {
"lambda": [
{
"alexaUsage": [
"custom/default"
],
"handler": "lambda_function.lambda_handler",
"runtime": "python3.6"
}
]
},
"was_cloned": false,
"merge": {}
}
}
}
95 changes: 95 additions & 0 deletions hooks/post_new_hook.ps1
@@ -0,0 +1,95 @@
# Powershell script for ask-cli post-new hook for Python
# Script Usage: post_new_hook.ps1 <SKILL_NAME> <DO_DEBUG>

# SKILL_NAME is the preformatted name passed from the CLI, after removing special characters.
# DO_DEBUG is boolean value for debug logging

# Run this script one level outside of the skill root folder

# The script does the following:
# - Create a '.venv' directory under <SKILL_NAME> folder
# - Find if python3 is installed.
# - If yes, try creating virtual environment using built-in venv
# - If that fails, install virtualenv and create virtualenv
# - If no, install virtualenv and create virtualenv
# - If virtual environment is created, use container pip to install dependencies from ${SOURCE_DIR}/requirements.txt
# - Provide message on activation script location and additional dependencies

param(
[string] $SKILL_NAME,
[bool] $DO_DEBUG = $False
)

if ($DO_DEBUG) {
Write-Output "###########################"
Write-Output "###### post-new hook ######"
Write-Output "###########################"
}

function create_env () {
# Check for Python3 installation
python -V | Select-String -Pattern "Python 3." 2>&1 | Out-Null
if ($?) {
python -m venv $ENV_LOC 2>&1 | Out-Null
if ($?) {
return $true
}
}
return create_using_virtualenv
}

function create_using_virtualenv() {
# Check for virtualenv installation or install
python -m pip install virtualenv 2>&1 | Out-Null
if ($?) {
python -m virtualenv $ENV_LOC 2>&1 | Out-Null
if ($?) {
return $true
}
}
if ($DO_DEBUG) {
Write-Output "There was a problem installing virtualenv"
}
return $false
}

function install_dependencies($PARAM_SOURCE_DIR) {
# Install dependencies at lambda/py/requirements.txt
$PYTHON_PATH = $ENV_LOC + "\Scripts\python"
$REQUIREMENTS_PATH = $SKILL_NAME + "\" + $PARAM_SOURCE_DIR + "\requirements.txt"
$CMD = "$PYTHON_PATH -m pip -q install -r $REQUIREMENTS_PATH"
return Invoke-Expression $CMD 2>&1 | Out-Null
}


$SKILL_ENV_NAME = "skill_env"
$ENV_LOC = $SKILL_NAME + "\.venv\" + $SKILL_ENV_NAME
if (create_env) {
$SKILL_FILE_PATH = $SKILL_NAME + "\skill.json"
$ALL_SOURCE_DIRS = Get-Content -Path $SKILL_FILE_PATH | select-string -Pattern "sourceDir" -CaseSensitive
if ($DO_DEBUG) {
Write-Output "Created $SKILL_ENV_NAME virtualenv at $ENV_LOC"
Write-Output "###########################"
Write-Output "Installing dependencies based on sourceDir"
}
Foreach ($SOURCE_DIR in $ALL_SOURCE_DIRS) {
$FILTER_SOURCE_DIR = $SOURCE_DIR -replace "`"", "" -replace "\s", "" -replace ",","" -replace "sourceDir:", ""
if (-Not (install_dependencies $FILTER_SOURCE_DIR)) {
if ($DO_DEBUG) {
Write-Output "Codebase ($FILTER_SOURCE_DIR) built successfully."
}
} else {
if ($DO_DEBUG) {
Write-Output "There was a problem installing dependencies for ($FILTER_SOURCE_DIR)."
}
exit 1
}
}
if ($DO_DEBUG) {
Write-Output "###########################"
Write-Output "Activate the environment before installing any other dependencies by running 'source $ENV_LOC/bin/activate'"
}
exit 0
} else {
exit 1
}
93 changes: 93 additions & 0 deletions hooks/post_new_hook.sh
@@ -0,0 +1,93 @@
#!/bin/bash
# Shell script for ask-cli post-new hook for Python
# Script Usage: post_new_hook.sh <SKILL_NAME> <DO_DEBUG>

# SKILL_NAME is the preformatted name passed from the CLI, after removing special characters.
# DO_DEBUG is boolean value for debug logging

# Run this script one level outside of the skill root folder

# The script does the following:
# - Create a '.venv' directory under <SKILL_NAME> folder
# - Find if python3 is installed.
# - If yes, try creating virtual environment using built-in venv
# - If that fails, install virtualenv and create virtualenv
# - If no, install virtualenv and create virtualenv
# - If virtual environment is created, use container pip to install dependencies from ${SOURCE_DIR}/requirements.txt
# - Provide message on activation script location and additional dependencies

create_env () {
# Check for Python3 installation
if command -v python3 &> /dev/null; then
PYTHON=python3
# Use Python3's venv script to create virtualenv.
if $PYTHON -m venv "$ENV_LOC"; then
echo "Using Python3's venv script"
return 0
else
# No venv script present (< Py 3.3). Install using virtualenv
return create_using_virtualenv $PYTHON
fi
else
# Python2 environment. Install using virtualenv
PYTHON=python
return create_using_virtualenv $PYTHON
fi
return 1
}

create_using_virtualenv () {
# Check for virtualenv installation or install
if $1 -m pip install virtualenv; then
echo "Using virtualenv library"
# Try creating env
if $1 -m virtualenv "$ENV_LOC"; then
return 0
else
echo "There was a problem creating virtualenv"
return 1
fi
else
echo "There was a problem installing virtualenv"
return 1
fi
}

install_dependencies() {
# Install dependencies at lambda/py/requirements.txt
return $("$ENV_LOC"/bin/python -m pip -q install -r "$SKILL_DIR"/"$1"/requirements.txt)
}

SKILL_NAME=$1
DO_DEBUG=${2:-false}
SKILL_DIR=$SKILL_NAME
SKILL_ENV_NAME="skill_env"
ENV_LOC="$SKILL_DIR/.venv/$SKILL_ENV_NAME"

if ! $DO_DEBUG ; then
exec > /dev/null 2>&1
fi

echo "###########################"
echo "###### post-new hook ######"
echo "###########################"
echo "Creating virtualenv for $SKILL_NAME"
mkdir "$SKILL_NAME/.venv"
if create_env; then
echo "Created $SKILL_ENV_NAME virtualenv at $ENV_LOC"
echo "###########################"
echo "Installing dependencies based on sourceDir"
grep "sourceDir" "$SKILL_NAME/skill.json" | cut -d: -f2 | sed 's/"//g' | sed 's/,//g' | while read -r SOURCE_DIR; do
if install_dependencies $SOURCE_DIR; then
echo "Codebase ($SOURCE_DIR) built successfully."
else
echo "There was a problem installing dependencies for ($SOURCE_DIR)."
exit 1
fi
done
echo "###########################"
echo "Activate the environment before installing any other dependencies by running 'source $ENV_LOC/bin/activate'"
exit 0
else
exit 1
fi
65 changes: 65 additions & 0 deletions hooks/pre_deploy_hook.ps1
@@ -0,0 +1,65 @@
# Powershell script for ask-cli pre-deploy hook for Python
# Script Usage: pre_deploy_hook.ps1 <SKILL_NAME> <DO_DEBUG> <TARGET>

# SKILL_NAME is the preformatted name passed from the CLI, after removing special characters.
# DO_DEBUG is boolean value for debug logging
# TARGET is the deploy TARGET provided to the CLI. (eg: all, skill, lambda etc.)

# Run this script under the skill root folder

# The script does the following:
# - Create a temporary 'lambda_upload' directories under each SOURCE_DIR folder
# - Copy the contents of '<SKILL_NAME>/SOURCE_DIR' folder into '<SKILL_NAME>/SOURCE_DIR/lambda_upload'
# - Copy the contents of site packages in $VIRTUALENV created in <SKILL_NAME>/.venv/ folder
# - Update the location of this 'lambda_upload' folder to skill.json for zip and upload

param(
[string] $SKILL_NAME,
[bool] $DO_DEBUG = $False,
[string] $TARGET = "all"
)

if ($DO_DEBUG) {
Write-Output "###########################"
Write-Output "##### pre-deploy hook #####"
Write-Output "###########################"
}

if ($TARGET -eq "all" -Or $TARGET -eq "lambda") {
$ALL_SOURCE_DIRS = Get-Content -Path "skill.json" | select-string -Pattern "sourceDir" -CaseSensitive
Foreach ($SOURCE_DIR in $ALL_SOURCE_DIRS) {
# Step 1: Decide source path and upload path
$FILTER_SOURCE_DIR = $SOURCE_DIR -replace "`"", "" -replace "\s", "" -replace ",","" -replace "sourceDir:", ""
if ($FILTER_SOURCE_DIR.endsWith("/lambda_upload")) {
$UPLOAD_DIR_PATH = $FILTER_SOURCE_DIR
$CODE_PATH = $FILTER_SOURCE_DIR.replace("/lambda_upload", "")
} else {
$UPLOAD_DIR_PATH = $FILTER_SOURCE_DIR + "/lambda_upload"
$CODE_PATH = $FILTER_SOURCE_DIR
}
# Step 2: Create empty lambda_upload folder
Remove-Item -Recurse -Force $UPLOAD_DIR_PATH -ErrorAction Ignore
New-Item -Force $UPLOAD_DIR_PATH -ItemType "directory" 2>&1 | Out-Null

# Step 3: Copy source code in sourceDir to lambda_upload
$EXCLUDE_PATH = Resolve-Path -Path ((pwd).Path + "/" + $UPLOAD_DIR_PATH)
robocopy $CODE_PATH $UPLOAD_DIR_PATH /s /e /ndl /XD $EXCLUDE_PATH 2>&1 | Out-Null

# Step 4: Find virtual environment site packages, copy contents to lambda_upload
$SITE = $(.venv\skill_env\Scripts\python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
Copy-Item "$SITE\*" -Destination $UPLOAD_DIR_PATH -Recurse

# Step 5: Update the "manifest.apis.custom.endpoint.sourceDir" value in skill.json if necessary
if (!$FILTER_SOURCE_DIR.endsWith("/lambda_upload")) {
$RAW_SOURCE_DIR_LINE = "`"sourceDir`": `"$FILTER_SOURCE_DIR`""
$NEW_SOURCE_DIR_LINE = "`"sourceDir`": `"$UPLOAD_DIR_PATH`""
(Get-Content "skill.json").replace($RAW_SOURCE_DIR_LINE, $NEW_SOURCE_DIR_LINE) | Set-Content "skill.json"
}
}

if ($DO_DEBUG) {
Write-Output "###########################"
}

exit 0
}
66 changes: 66 additions & 0 deletions hooks/pre_deploy_hook.sh
@@ -0,0 +1,66 @@
#!/bin/bash
# Shell script for ask-cli pre-deploy hook for Python
# Script Usage: pre_deploy_hook.sh <SKILL_NAME> <DO_DEBUG> <TARGET>

# SKILL_NAME is the preformatted name passed from the CLI, after removing special characters.
# DO_DEBUG is boolean value for debug logging
# TARGET is the deploy TARGET provided to the CLI. (eg: all, skill, lambda etc.)

# Run this script under skill root folder

# The script does the following:
# - Create a temporary 'lambda_upload' directories under each SOURCE_DIR folder
# - Copy the contents of '<SKILL_NAME>/SOURCE_DIR' folder into '<SKILL_NAME>/SOURCE_DIR/lambda_upload'
# - Copy the contents of site packages in $VIRTUALENV created in <SKILL_NAME>/.venv/ folder
# - Update the location of this 'lambda_upload' folder to skill.json for zip and upload

SKILL_NAME=$1
DO_DEBUG=${2:-false}
TARGET=${3:-"all"}
SKILL_ENV_NAME="skill_env"

if ! $DO_DEBUG ; then
exec > /dev/null 2>&1
fi

echo "###########################"
echo "##### pre-deploy hook #####"
echo "###########################"

if [[ $TARGET == "all" || $TARGET == "lambda" ]]; then
grep "sourceDir" ./skill.json | cut -d: -f2 | sed 's/"//g' | sed 's/,//g' | while read -r SOURCE_DIR; do
# Step 1: Decide source path and upload path
if [[ $SOURCE_DIR == */lambda_upload ]]; then
ADJUSTED_SOURCE_DIR=${SOURCE_DIR%"/lambda_upload"}
UPLOAD_DIR=$SOURCE_DIR
else
ADJUSTED_SOURCE_DIR=$SOURCE_DIR
UPLOAD_DIR="$SOURCE_DIR/lambda_upload"
fi

# Step 2: Create empty lambda_upload folder
echo "Checking for lambda_upload folder existence in sourceDir $ADJUSTED_SOURCE_DIR"
rm -rf $UPLOAD_DIR
mkdir $UPLOAD_DIR

# Step 3: Copy source code in sourceDir to lambda_upload
echo "Copying source code in $SKILL_NAME/$ADJUSTED_SOURCE_DIR folder to $SKILL_NAME/$UPLOAD_DIR"
rsync -avzq --exclude '*lambda_upload' $ADJUSTED_SOURCE_DIR/* $UPLOAD_DIR

# Step 4: Find virtual environment site packages, copy contents to lambda_upload
echo "Copying dependencies installed in $SKILL_NAME/.venv/$SKILL_ENV_NAME to $SKILL_NAME/$UPLOAD_DIR"
SITE=$(.venv/$SKILL_ENV_NAME/bin/python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')
cp -r $SITE/* $UPLOAD_DIR

# Step 4: Update the "manifest.apis.custom.endpoint.sourceDir" value in skill.json if necessary
if ! [[ $SOURCE_DIR == */lambda_upload ]]; then
echo "Updating sourceDir to point to lambda_upload folder in skill.json"
RAW_SOURCE_DIR_LINE="\"sourceDir\": \"$SOURCE_DIR\""
NEW_SOURCE_DIR_LINE="\"sourceDir\": \"$UPLOAD_DIR\""
sed -in "s#$RAW_SOURCE_DIR_LINE#$NEW_SOURCE_DIR_LINE#g" ./skill.json
fi
done
echo "###########################"
fi

exit 0

0 comments on commit 40eed56

Please sign in to comment.