Skip to content

Instantly share code, notes, and snippets.

Dave James Miller davejamesmiller

View GitHub Profile
@davejamesmiller
davejamesmiller / ._ Loading variables from .env files in Ansible.md
Last active Apr 18, 2019
Loading variables from .env files in Ansible
View ._ Loading variables from .env files in Ansible.md

Loading variables from .env files in Ansible

Ansible has various ways of looking up data from outside sources, including plain text password files, CSV files and INI files. But it doesn't seem to have a lookup for .env files, as used in Laravel projects, also available for PHP, Ruby, Node.js, Python and others.

One option is to launch Ansible with the Ruby dotenv command line script... But that requires Ruby, which seems like overkill to me.

So here is a simpler solution that I use. It consists of:

  1. The .env file itself
  2. A small shell script that loads the .env file into environment variables - ansible-playbook.sh
@davejamesmiller
davejamesmiller / Laravel-Container.md
Last active Apr 19, 2019
Laravel's Dependency Injection Container in Depth
View Laravel-Container.md

Laravel's Dependency Injection Container in Depth

Translations: Korean (by Yongwoo Lee)

Laravel has a powerful Inversion of Control (IoC) / Dependency Injection (DI) Container. Unfortunately the official documentation doesn't cover all of the available functionality, so I decided to experiment with it and document it for myself. The following is based on Laravel 5.4.26 - other versions may vary.

Introduction to Dependency Injection

I won't attempt to explain the principles behind DI / IoC here - if you're not familiar with them you might want to read What is Dependency Injection? by Fabien Potencier (creator of the Symfony framework).

@davejamesmiller
davejamesmiller / BladeServiceProvider.php
Last active Oct 14, 2017
Laravel Blade @ifSection() tag
View BladeServiceProvider.php
<?php
namespace App\Providers;
use Blade;
use Illuminate\Support\ServiceProvider;
class BladeServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
@davejamesmiller
davejamesmiller / http-redirect-target.php
Last active Apr 9, 2019
Get HTTP redirect destination for a URL in PHP
View http-redirect-target.php
<?php
// FOLLOW A SINGLE REDIRECT:
// This makes a single request and reads the "Location" header to determine the
// destination. It doesn't check if that location is valid or not.
function get_redirect_target($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
@davejamesmiller
davejamesmiller / download-live-site.sh
Last active Dec 25, 2017
Script to download MySQL database & files from live to development server
View download-live-site.sh
#!/bin/bash
#===============================================================================
# This is a template for a script I use on a lot of sites to copy the database
# (MySQL) and any uploaded files to the development site, and modify the
# database as required.
#
# The script should be on the development server. The live site can either be on
# the same server, or a remote server connected via SSH.
#
@davejamesmiller
davejamesmiller / .htaccess
Last active Jan 14, 2019
Resize an image on the fly with PHP and cache it (like Drupal's ImageCache)
View .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+)/([^/]*)$ resize.php?size=$1&file=$2
@davejamesmiller
davejamesmiller / deploy.rb
Last active Oct 10, 2017
Fully restart Ruby on Rails app under Phusion Passenger during Capistrano (cap) deploy
View deploy.rb
# Most instructions for using Capistrano tell you how to make it restart Phusion
# Passenger by 'touch'ing the restart.txt file, but this doesn't immediately
# restart the app - instead the first person to try to use the application will
# cause it to be restarted, so they will see a delay of at least a few seconds.
# This shows how to add a post-deploy task to 'ping' the server, to cause it to
# restart immediately.
# First, (optionally) add a '/ping' route to config/routes.rb that immediately
# returns a blank page. This prevents any non-essential work being done, such as
@davejamesmiller
davejamesmiller / deploy.rb
Last active Mar 9, 2018
Automatically push changes to Git repository before deploying with Capistrano (cap deploy)
View deploy.rb
# I use this Capistrano task so I don't have manually run 'git push' before 'cap
# deploy'. It includes some error checking to make sure I'm on the right branch
# (master) and haven't got any uncommitted changes.
# Simply add the code below to config/deploy.rb, then run 'cap deploy:push' to
# test it, and 'cap deploy' to deploy as usual.
namespace :deploy do
desc "Push local changes to Git repository"
task :push do
@davejamesmiller
davejamesmiller / deploy.rb
Last active Oct 10, 2017
Download data from Ruby on Rails production database to development server using Replicate
View deploy.rb
# OVERVIEW:
# Replicate [1] is a Gem that lets you dump and load relational objects between
# Ruby/Ruby on Rails environments, e.g. dump data from your production database
# and load it in your development database.
# The examples given in the README [2] are of shell scripts being used to do
# this. I wanted to use a Capistrano task because it keeps everything related to
# the production site in one place (particularly the config).
@davejamesmiller
davejamesmiller / detect-input-type.sh
Last active Jun 8, 2018
Detect pipe/file input in Bash / shell script
View detect-input-type.sh
# How to detect whether input is from keyboard, a file, or another process.
# Useful for writing a script that can read from standard input, or prompt the
# user for input if there is none.
# Source: http://www.linuxquestions.org/questions/linux-software-2/bash-scripting-pipe-input-to-script-vs.-1-570945/
if readlink /proc/$$/fd/0 | grep -q "^pipe:"; then
# Pipe input (echo abc | myscript)
elif file $( readlink /proc/$$/fd/0 ) | grep -q "character special"; then
# Terminal input (keyboard)
You can’t perform that action at this time.