rebasing changes from a development branch onto another branch

If you have changes on a branch which you want to apply to another branch which has a different history you might not want to merge or rebase. This method allows you to identify a series of commits and replay them onto your destination branch. For instance:

  • You have your master branch which you want to apply some commits from another branch - menu-test
  • master and menu-test have diverged so you just want a portion of the commits on that branch
  1. Checkout a new temporary branch from the top of the destination branch master
    a...

Restoring Tarsnap backups

Note: If you're logged on to the server as root you shouldn't need to sudo these commands, but in many cases you'll be logged on as another user (such as a deploy user or your own user) and not using the root account.

To See a list of archives

sudo tarsnap --list-archives | sort

To restore one of those archives:

sudo tarsnap -x -f [name_of_archive]

Check that the files and directories are what you expect.

If you have access to the tarsnap key that was used for creating the backup you can also do the above from your local computer, ...

Connecting to MySQL running in a vagrant box

If you want to use something like Sequel Pro to connect to a database running within a vagrant vm you need to edit the /etc/mysql/my.cnf file and change the bind-address setting from 127.0.0.1 to 0.0.0.0 and restart mysql. sudo service mysql restart.

Then you will be able to set up the connection. With Sequel Pro you'll want to set the private key using the ~/.vagrant.d/insecure_private_key option in the ssh connection settings. Then typically it's user vagrant password also vagrant

AWS CLI on Mac

There's a handy (and official) command line interface tool for dealing with AWS. It's great for managing files with S3.

Here's a short guide, somewhat from memory. Please improve this if you encounter any issues:

  1. Install AWS cli via homebrew
  2. Configure the AWS client (you need to add credentials for accessing the service)

Refer to this post for information on how to do this.

After that, you can do things like:

`aws s3 sync s3://source_bucket ...

Let's Encrypt certificate install/renew issues

There are various reasons why a Let's Encrypt certificate can't be installed or renewed. Here are some I've come across:


Error:

Not documented, add it if you have it

Issue:

The stack is behind a load balancer and traffic is being shared between multiple web servers. The verification check isn't getting to the server where the temporary authorisation file is located

Solution:

The certificate needs to be added to the land balancer rather than the web server(s). This probably means that you don't want to use Let's Encrypt ...

Wrong Ruby version in development

You try to do some work on an old project which you haven't worked on for a while. You start up your development environment and try running the specs to make sure everything's in order, but you get the error:

Your Ruby version is 2.1.8, but your Gemfile specified 2.3.3

Looks like things have moved on since you last worked on the project...

The surest way to solve this issue is to destroy your development environment and provision it again from scratch, but before you do that you can try:

vagrant provision

Which will re-provisio...

Download database for use in development

When you first check out a development project (or come back to it after some time away) you may want to use a copy of the production site database as a basis for your development database so that your data reflects real data.

C66 with managed backups:

Databases are backed up automatically and can be viewed and downloaded using the cx backups cli:

  1. cx backups list -s [Stack Name] -e production to list backups
  2. cx backups download -s [Stack Name] -e production [Backup ID]

C66 with unmanaged backups

Database are backed up ...

Accepting a self-signed SSL certificate (OS X)

If you want to get your development site secured with an SSL certificate you might want to accept the certificate so that you don't get errors.

Safari

  1. Click on the padlock
  2. View the certificate trust details
  3. trust the certificate

Chrome

This was tricky for me on Chrome 58+, but it basically comes down to the following (if I recall correctly)

  1. You probably need to make sure your certificate is trusted via the keychain. The easiest way to do this is to accept it via Safari :/
  2. You possibly need to copy the certific...

Getting started with Git Log

The easy option:

git log --graph --decorate --oneline

  • --graph draws a graph indicating commits (implies --topo-order)
  • --decorate shows any ref names (tags and branches)
  • --oneline shows the start of the commit sha and the commit title

The more complicated option:

git log --graph --pretty=format:'%C(cyan)%an (%cr) %C(yellow)%h%Cred%d%Creset %s'

It's much like the easy option, but with author and time-since-commit

You might also want to mix these in sometimes:

  • --all shows all commits (across other branches)
  • `--autho...

Backup and Restore PostgreSQL Tables from the Command Line

If you need to backup and restore data for a single table in PostgreSQL you can use the following methods:

Backup table data

pg_dump -Fc --data-only -h<host> -p<port> -U<username> -W -d<database> -t<table> > ~/Downloads/<filename.dump>

Restore table data

Before you restore you need to connect to the database and truncate the table to clear all the data:

  1. psql -h<host> -p<port> -U<username> -W -d<database>
  2. TRUNCATE <table> RESTART IDENTITY;
  3. \q

That will connect to the database, truncate the table and then disconnec...

Cloud66 tunnelling for database access

You might be interested in connecting to a Cloud66 hosted database from your local conmputer, either via command console or database client (such as SequelPro).

To do that you first need to temporarily open a port so that your traffic can get to the server. One way to do that it to set firewall rules on the stack, but that's undesirable. If you have the Cloud66 toolbelt you can use that to temporarily open a connection as follows:

`cx tunnel -s -e --server --remote --local ...

Example of creating seed data with seed-fu

Using an existing table to programmatically create initial seed fu data definition

locations = CountryLocation.all
SeedFu::Writer.write("db/fixtures/country_locations.rb", class_name: "CountryLocation", :constraints => [:id]) do |writer|
  locations.each do |l|
    writer.add(id: l.id, iso: l.iso, name: l.name, printable_name: l.printable_name, iso3: l.iso3, numcode: l.numcode, email: l.email)
  end
end

Unable to `bundle install` on Debian Wheezy

When running bundle install you get the following error:

Could not verify the SSL certificate for https://rubygems.org/.
There is a chance you are experiencing a man-in-the-middle attack, but most likely your system doesn't have the CA certificates needed for verification. For information about
OpenSSL certificates, see http://bit.ly/ruby-ssl. To connect without using SSL, edit your Gemfile sources and change 'https' to 'http'.

The proper way to resolve this seems to be to update Rubygems with gem update --system, but on a Debi...

Nginx rewrite rules for redirecting

​Here's an example of how you can use match replacements in nginx redirect rewrite rules that can be used within a server block:

Here's a complete example using the last one in the list on the staging site. So where you had:

​rewrite (?i)^/en/pages/137-test-page$ $scheme://$host/en/pages/the-new-place permanent;

​I've changed it to be:

​rewrite (?i)^/(de/|en/|es/|fr/|it/|.?)pages/137-test-page$ $scheme://$host/$1pages/the-new-place redirect;

​Hopefully you can see the regular expression part which is matching on any of the c...

OpenSSL versions under Apt package manager

It's not as simple as you might think checking that you've got an up-to-date version of OpenSSL installed, especially if you install it via a package manager, such as Apt. The reason for this is that the package installed versions don't always match up to the OpenSSL release versions.

For example, I want to make sure I have at least 1.0.1t installed, but ssh-ing onto the server and doing openssl version tells me it's 1.0.1f. No problem, I just sudo apt-get update and then sudo apt-get install openssl to upgrade to the latest versi...

NFS is reporting that your exports file is invalid (Issue starting vagrant box)

when trying to vagrant up I got the following error:

NFS is reporting that your exports file is invalid. Vagrant does
this check before making any changes to the file. Please correct
the issues below and execute "vagrant reload":

exports:5: path contains non-directory or non-existent components: /Volumes/imac_work/projects/sumeru
exports:5: no usable directories in export entry
exports:5: using fallback (marked offline): /Volumes/imac_work

This issue was probably caused because I moved the sumeru directory, but the directories ...

Super simple ruby webserver

If you have a need to quickly serve a static site locally you can just run the following:

ruby -rwebrick -e'WEBrick::HTTPServer.new(:Port => 3000, :DocumentRoot => Dir.pwd).start'

render_template errors in Rails 2.3.18.16

If you're seeing spec failures, e.g.

FooController PUT update when unsuccessful renders edit' FAILED expected "edit", got nil

It's likely due to rspec-rails and how it's isolating the templates in order to test them. The following monkey patch resolves this: (courtesy of the Rails LTS maintainers.) You need to ensure this is included in your controller specs

module Spec
  module Rails
    module Example
      class ControllerExampleGroup
        module TemplateIsolationExtensions
        
          private

          def record_ren...

RubyMine Debugging with Vagrant

This took lots of figuring out, so I'm going to document it here.

Install debugging gems on vagrant server (optionally add them to the Gemfile)

Apparently they need to be the pre-release versions, but I'll experiment with this as the opportunity presents itself.

gem install ruby-debug-base19x --pre
gem install ruby-debug-ide --pre

Add remote SDK in RubyMine

[Set-up remote debug confi...