Skip to content

Instantly share code, notes, and snippets.

View erikhansen's full-sized avatar

Erik Hansen erikhansen

View GitHub Profile
@erikhansen
erikhansen / warmer-cron.sh
Created August 4, 2020 16:54 — forked from davidalger/warmer-cron.sh
Script to crawl and warm the cache two levels deep on Magento demo Pods via CronJob spec
#!/bin/bash
set -euo pipefail
FRONT_URL="${FRONT_URL:-https://app.exampleproject.test/}"
echo "==> [$(date +%H:%M:%S)] waiting on readiness"
ELAPSED_SECONDS=0
while : ; do
ELAPSED_SECONDS=$(echo ${ELAPSED_SECONDS}+2 | bc)
RESPONSE_CODE=$(curl -sI "${FRONT_URL}" 2>/dev/null | head -n1 | awk '{print $2}')
# clear the entire cache
varnishadm "ban req.url ~ /"
# monitor varnish log with multiple patterns
varnishlog | grep 'eqURL\|Age:\|VCL_call\|TTL\|Expires:\|Cache-Control:'
# Use a query with varnishlog to watch requests from a specific IP
varnishlog -q 'ReqHeader:X-User-IP eq "1.2.3.4" or BereqHeader:X-User-IP eq "1.2.3.4"'
# Monitor PURGE requests hitting Varnish

Opening and closing an SSH tunnel in a shell script the smart way

I recently had the following problem:

  • From an unattended shell script (called by Jenkins), run a command-line tool that accesses the MySQL database on another host.
  • That tool doesn't know that the database is on another host, plus the MySQL port on that host is firewalled and not accessible from other machines.

We didn't want to open the MySQL port to the network, but it's possible to SSH from the Jenkins machine to the MySQL machine. So, basically you would do something like

ssh -L 3306:localhost:3306 remotehost

@erikhansen
erikhansen / createUsefulAssetManifest.js
Last active June 25, 2019 14:44 — forked from mdecorte/createUsefulAssetManifest.js
Script to create a useful asset-manifest file for CRA-3 that only contains paths to assets needed on page load
const fs = require('fs');
const path = require('path');
const assetManifestPath = path.join(__dirname, 'build/asset-manifest.json');
let assetManifest = fs.readFileSync(assetManifestPath);
assetManifest = JSON.parse(assetManifest)['files'];
const indexFilePath = path.join(__dirname, 'build/index.html');
const BUILD_PATH = path.join(__dirname, 'build/useful-asset-manifest.json');
// Filter paths that exists in the create-react-app generated `build/asset-manifest.json` and use the path from that file because that can contain the "homepage" prefix from package.json.
# Magento 2 Sandbox Snippet - You can copy and paste into any shell in a Magento root directory
# Extended: https://docs.classyllama.net/disciplines/engineering/magento/sandbox-file-m2
set +H # disable history expansion
PHP_CODE=$(cat <<'PHP_CODE'
<?php
header('Content-type: text/plain');
require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
@erikhansen
erikhansen / rsync_examples.sh
Last active March 1, 2019 19:00 — forked from mttjohnson/rsync_examples.sh
rsync examples (two remotes)
# rsync with sudo
rsync -av \
--rsync-path="sudo -u www-data rsync" \
-e 'ssh -o StrictHostKeyChecking=no'
path_to_local_data/ user_name@example.com:/var/www/
# rsync media between two remotes that can not talk to each other
# this creates a port (50000) on the origin server that establishes a tunnel between the origin and destination through
# your computer, as long as your computer can access both servers you can rsync the files between the two
@erikhansen
erikhansen / brew_vagrant_version.sh
Created February 8, 2019 18:22 — forked from mttjohnson/brew_vagrant_version.sh
Installing older version of vagrant/ansible with brew
# You can find the current list of casks on github, and the one for vagrant here:
# https://github.com/Homebrew/homebrew-cask/blob/master/Casks/vagrant.rb
# From there you may be able to load the history of that file and lookup previous versions of
# the cask which would be related to previous versions of vagrant as indicated in the cask definition file.
# If the github history of the file fails you can search through recent commit history and find a commit prior
# to the version change you are trying to avoid and get the commit hash before the change and use it to locate
# a previous version of the cask file.
# current (master branch) casks:
SET @utc_offset = 6;
-- Orders Per Year --
SELECT period_date, CONCAT("UTC-", @utc_offset) AS utc_offset, order_count, gross_revenue, ROUND(gross_revenue / order_count, 2) AS gross_aov
FROM (
SELECT
COUNT(*) AS order_count,
ROUND(SUM(base_grand_total), 2) AS gross_revenue,
date_format(date_sub(o.created_at, INTERVAL @utc_offset HOUR), "%Y") AS period_date
FROM sales_order o
@erikhansen
erikhansen / composer_private_repos.sh
Created November 7, 2018 17:47 — forked from mttjohnson/composer_private_repos.sh
Composer Notes and Private Repositories
# List the composer home directory
# Typically /Users/<user>/.composer or /home/<user>/.composer or C:\Users\<user>\AppData\Roaming\Composer
echo $COMPOSER_HOME
# List files in the composer home
ls -la $COMPOSER_HOME
# View auth.json in composer home used when no local ./auth.json exists in the directory executed from
cat $COMPOSER_HOME/auth.json
@erikhansen
erikhansen / mysql_size_info.sql
Created June 21, 2018 15:58 — forked from mttjohnson/mysql_size_info.sql
MySQL data usage queries
/* find the largest tables on the server */
SELECT CONCAT(table_schema, '.', table_name) 'db.table',
CONCAT(ROUND(table_rows / 1000000, 2), 'M') rows,
CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G') DATA,
CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G') idx,
CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') total_size,
ROUND(index_length / data_length, 2) idxfrac
FROM information_schema.TABLES
ORDER BY data_length + index_length DESC
LIMIT 10;