diff --git a/.distignore b/.distignore index 7b0d49bbf783899d0bb28db9824caf83da2a519b..95930c62eb37c889aa32baeacaa614396262b73c 100644 --- a/.distignore +++ b/.distignore @@ -11,3 +11,4 @@ update-version.js .gitignore .gitattributes package +scripts/ diff --git a/.gitattributes b/.gitattributes index d561145139ae38a50800e9fc82ceb30a265a459f..25e3431e5c95c452d807cf61c100ccc2f2e5dc97 100644 --- a/.gitattributes +++ b/.gitattributes @@ -5,4 +5,5 @@ /.gitlab-ci.yml export-ignore /make-zip.sh export-ignore /update-version.js export-ignore -/.gitattributes export-ignore \ No newline at end of file +/.gitattributes export-ignore +/scripts export-ignore diff --git a/composer.json b/composer.json index ee70cf6bb37378d8e92e04c118a1557857fb698b..692f662c3a3722350be93c4d8d52416d5d138db3 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "bobgo/bobgo-magento-extension", "description": "Smart shipping and order management solution in South Africa", "type": "magento2-module", - "version": "1.0.58", + "version": "1.0.59", "authors": [ { "name": "Bob Go", diff --git a/etc/module.xml b/etc/module.xml index c6eb73e5896716b41734bbf63e6d3cd4dfe1b434..cd44f78d9d642f13fa8c034f28a89fe0154177c3 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -7,7 +7,7 @@ */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> - <module name="BobGroup_BobGo" setup_version="1.0.58"> + <module name="BobGroup_BobGo" setup_version="1.0.59"> <sequence> <module name="Magento_Webapi"/> <module name="Magento_Catalog"/> diff --git a/package.json b/package.json index 361e426ca801ef3692f69bfe2217af722e2c8ca5..8ac8c83971c3e70d26218912b1b5f4266f08f0ea 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "bobgo-magento-plugin", "description": "Bob Go magento plugin", - "version": "1.0.58", + "version": "1.0.59", "license": "GPL-2.0-or-later", "scripts": { "prepare": "husky install", diff --git a/scripts/config.cfg b/scripts/config.cfg new file mode 100644 index 0000000000000000000000000000000000000000..03acf59efeb9800edb442a95f4cf8dc541712024 --- /dev/null +++ b/scripts/config.cfg @@ -0,0 +1,15 @@ +# S3 Configuration +S3_PREFIX="/" +#S3_BUCKET="bobgo-s3-magento-plugin-dev" +S3_BUCKET="magento-plugin.dev.bobgo.co.za" +S3_REGION="af-south-1" + +# Local Configuration +LOCAL_DIR="/home/bitnami/scripts/releases" +LOG_FILE="/home/bitnami/scripts/release_script.log" + +# Magento Configuration +PLUGIN_DIR="/opt/bitnami/magento/app/code/BobGroup/BobGo" +MAGENTO_CLI="/opt/bitnami/magento/bin/magento-cli" +MAGENTO_CACHE="/opt/bitnami/magento/var/cache/" +PHP_BIN="/opt/bitnami/php/bin/php" diff --git a/scripts/release_script.sh b/scripts/release_script.sh new file mode 100644 index 0000000000000000000000000000000000000000..dc3ac90d8243f8fb54de16b7309550eaec0690c1 --- /dev/null +++ b/scripts/release_script.sh @@ -0,0 +1,159 @@ +#!/bin/bash + +# Configuration +WORKING_DIR="/home/bitnami/scripts" +CONFIG_FILE="$WORKING_DIR/config.cfg" +LOCK_FILE="/tmp/release_script.lock" +LOG_FILE="$WORKING_DIR/release_script.log" + +# Ensure the script runs from the specified working directory +cd "$WORKING_DIR" || exit 1 + +# Load configuration +if [[ -f "$CONFIG_FILE" ]]; then + source "$CONFIG_FILE" +else + echo "Config file not found: $CONFIG_FILE" | tee -a "$LOG_FILE" + exit 1 +fi + +# Functions +log() { + local message=$1 + echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" | tee -a "$LOG_FILE" +} + +acquire_lock() { + if [[ -e "$LOCK_FILE" ]]; then + log "Script is already running." + exit 1 + fi + touch "$LOCK_FILE" +} + +release_lock() { + rm -f "$LOCK_FILE" +} + +fetch_latest_version() { + aws s3 ls "s3://$S3_BUCKET/" --region=$S3_REGION| sort | tail -n 1 | awk '{print $4}' +} + +get_md5() { + local file=$1 + md5sum "$file" | awk '{print $1}' +} + +check_dependencies() { + if ! command -v aws &>/dev/null; then + log "AWS CLI is not installed. Please install it and configure it." + exit 1 + fi + if ! command -v jq &>/dev/null; then + log "jq is not installed. Please install it." + exit 1 + fi + + if ! command sudo $MAGENTO_CLI &>/dev/null; then + log "$MAGENTO_CLI not found. Update the binary path in $CONFIG_FILE" + exit 1 + fi +} + +extract_and_apply_release() { + local release_file=$1 + + sudo rm -rf $PLUGIN_DIR/* + sudo rm -rf /bitnami/magento/generated/* + + # Ensure the plugin directory exists + if [[ ! -d "$PLUGIN_DIR" ]]; then + mkdir -p "$PLUGIN_DIR" + fi + + # Extract the new version to the plugin directory + log "Extracting $release_file to $PLUGIN_DIR." + + if sudo unzip -o "$release_file" -d "$PLUGIN_DIR"; then + log "Extraction successful." + else + log "Extraction failed." + release_lock + exit 1 + fi + + log "Clearing Plugin $PLUGIN_DIR directory" + log "Backup Magento code." + sudo $MAGENTO_CLI setup:backup --code + + log "Executing Magento commands." + apply_release +} + +apply_release() { + sudo chown -R bitnami: $PLUGIN_DIR + sudo $MAGENTO_CLI module:enable BobGroup_BobGo --clear-static-content + sudo $MAGENTO_CLI cache:clean + sudo $MAGENTO_CLI cache:flush + sudo $MAGENTO_CLI setup:upgrade + sudo $MAGENTO_CLI setup:di:compile + sudo rm -rf "$MAGENTO_CACHE/*" +} + +main() { + # Check dependencies + check_dependencies + + # Acquire lock + acquire_lock + + # Ensure the local directory exists + mkdir -p "$LOCAL_DIR" + + # Determine the latest version on S3 + latest_s3_file=$(fetch_latest_version) + if [[ -z "$latest_s3_file" ]]; then + log "No files found in the S3 bucket with the $S3_PREFIX naming convention." + release_lock + exit 1 + fi + latest_s3_file_path="s3://$S3_BUCKET/$latest_s3_file" + + # Determine the local version + local_file="$LOCAL_DIR/$latest_s3_file" + if [[ ! -f "$local_file" ]]; then + # Download the file + log "Fetching $latest_s3_file from S3." + if aws s3 cp "$latest_s3_file_path" "$LOCAL_DIR/"; then + log "File downloaded successfully." + extract_and_apply_release "$local_file" + else + log "Failed to download the file from S3." + fi + else + # Compare the MD5 hashes + local_md5=$(get_md5 "$local_file") + s3_md5=$(aws s3api head-object --bucket "$S3_BUCKET" --key "$latest_s3_file" --query "ETag" --output text | tr -d '"') + + if [[ "$local_md5" != "$s3_md5" ]]; then + log "New version found, removing old file and fetching $latest_s3_file from S3." + rm -f "$local_file" + if aws s3 cp "$latest_s3_file_path" "$LOCAL_DIR/"; then + log "New version downloaded successfully." + extract_and_apply_release "$local_file" + else + log "Failed to download the new version from S3." + fi + else + log "No new version found. Local file is up to date." + fi + fi + + # Release lock + release_lock +} + +# Run the main function +main +# fetch_latest_version +#apply_release