Resolving xip.io DNS with OpenWRT

I use MAMP for local PHP development server and it has a very nice option to automatically set up ServerAlias using xip.io so that the virtual host will become accessible from LAN. This is especially nice to allow communication between tablet and my virtual host.

Screen Shot 2017-07-13 at 11.25.37.png

This worked well in the office, but my home OpenWRT router seemed to block such usage. Turns out that it has a security feature to filter out DNS results that point to private networks, otherwise known as RFC1918 responses.

Allow RFC1918 Private Network DNS responses

To allow OpenWRT to forward such responses, open the web interface, and navigate to DNS settings.

openwrt_dns_settings.png

In the General Settings tab, scroll down to find Rebind Protection and add xip.io as whitelisted exception.openwrt_rfc1918_exception.png

Now try to access your xip.io domain from your LAN and it should work as intended.

Google Translator Service

MacOS has very handy shortcut ⌃⌘D to look up translations in the built-in dictionary. Unfortunately this does not always help if the word in question is either not in English or the definition is equally puzzling.

Automator to the rescue

Combining sources like this question in apple.stackexchange and this blog post in blog.fosketts I created an Automator Service that takes selected text from any application and then opens Google Translate with Auto->ET translation of the selection. Also added a shortcut for it: ⌃⇧⌘D

The AppleScript

Open Automator and create a New > Service, then search for Run AppleScript and drop that into the workflow. Then paste in this AppleScript from below.

on run {input, parameters}
	set phrase to input as string
	set myBrowser to "Google Chrome"
	set toLang to "et"

	tell application myBrowser
		open location "https://translate.google.com/#auto/" & toLang & "/" & phrase
	end tell
end run

Change myBrowser to your preferred browser and also toLang to desired destination language and then save the service. I chose name Translate to Estonian for obvious reasons.

The global shortcut

img-alternative-text

Next open System Preferences > Keyboard > Shortcuts and scroll down to your new service. Then add a convinient shortcut. I chose ⌃⇧⌘D because that is similar to the official Look up shortcut.

Now go forth and translate! 🖖

MacOS Estonian Development keyboard layout

My previous post about creating custom Estonian layout using Ukelele has gone seriously outdated.

Nowadays customising layouts is a lot simpler, as is sharing your custom layouts.

My new customised EstonianDev layout has the following modifications compared to stock layout:

  • Option+ä will result in ^ (caret symbol)
  • Key next to Backspace is now correctly producing   acute accent (´) and grave accent/backtick (`)

 

Installing

  1. Download  the EstonianDev.dmg.zip file (sorry for the .zip – WP does not allow .dmg files to be uploaded directly)
  2. Unzip and mount the image
  3. Drag the EstonianDev.bundle to the Keyboard Layouts folder linked into the DMG
  4. Add the new layout from Input Sources
  5. Enjoy!

Atom New Window (via AppleScript)

Again, building on my previous scripts it was dead to make a script that creates new window for Atom editor.

This is best used with Spotlight or Alfred to quickly create new window in current space (even if another window was open in some other space it will not switch there).

Alfred activating Atom New Window script
Alfred activating Atom New Window script

 

Download: Atom New Window v1

 

Installation

  1. Unzip the archive and place it in Applications folder. Right-click the Atom New Window icon and select Open and then allow OS X to open this (as it is downloaded from internet).
  2. Newer OS X versions require you to enable Assistive Access for the app. Go to System Preferences -> Security -> Accessibility and then enable this app.
Go to System Preferences > Security > Accessibility to enable Atom New Window to control application menus.
Go to System Preferences > Security > Accessibility to enable Atom New Window to control application menus.

Of course, you are advised to explore the script yourself: right click the app icon and select Show Package Contents and then open the file Contents/Resources/Scripts/main.scpt with Apple Script Editor to view it’s contents.

PS. In related posts section you can find similar scripts for other apps.

Faster RubyMine RSpec with Vagrant

I use RubyMine and Vagrant for my Rails development and run RSpec specs from RubyMine for convenience. The problem is that every time I start rspec, it takes a few seconds to connect to the box, before actually doing anything. This is especially annoying when running single spec which should be fast.

vagrant ssh is slow to connect

% time vagrant ssh -c whoami
vagrant
Connection to 10.211.55.45 closed.
2,57s user 0,73s system 82% cpu 3,984 total

That’s probably because vagrant ssh-config takes most of that time to sort things out:

% time vagrant ssh-config > /dev/null
2,53s user 0,79s system 77% cpu 4,269 total

When researching this issue, some people mention DNS or other Vagrant provider specific issues, but they complain of an order of magnitude slower (30-40 sec) connection. Might just be that I have everything set up correctly and it just takes that much time to get the configuration.

But what this means is that if you run RSpecs from RubyMine, they are slow to start, because RubyMine executes vagrant ssh-config every time (RUBY-16186).

Direct SSH to the rescue

Fortunately, direct SSH with keypair authentication is blazing fast:

time ssh vagrant@10.211.55.45 -i ~/.ssh/parallels_key whoami
vagrant
0,01s user 0,01s system 21% cpu 0,114 total

So, to take advantage of that you need to:

  1. set up SSH configuration
  2. configure RubyMine remote ruby over SSH, not Vagrant
  3. Add RubyMine helpers to RUBYLIB to be able to start persistent spring instance.

Set up SSH configuration

Vagrant might be using their vagrant_insecure_private_key for the SSH or, in my case, Parallels custom keypair, as I don’t use VirtualBox. So run this command to find out which key is in use:

% vagrant ssh-config
Host default
  HostName 10.211.55.45
  User vagrant
  Port 22
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/laas/project/.vagrant/machines/default/parallels/private_key
  IdentitiesOnly yes
  LogLevel FATAL

Now we know the IP address of the box and what keyfile is used. For easier access, I symlinked the key file to my .ssh folder:

ln -s IdentityFile /Users/laas/project/.vagrant/machines/default/parallels/private_key ~/.ssh/parallels_key

You can try out SSH connection:

ssh vagrant@10.211.55.45 -i ~/.ssh/parallels_key whoami
vagrant

Configure RubyMine to use remote ruby over SSH

Next step is to configure new Ruby SDK for the project. Open Preferences and navigate Language & Frameworks > Ruby SDK and Gems, click + button and pick New remote…. Then fill in the data as shown in the screenshot and when closing dialog, ensure that the newly added ruby is selected as active.

Configure Remote Ruby Interpreter

Now specs should still run (but take several seconds every time to boot up Rails, because spring is shut down after every connection).

Note: I actually use landrush to manage development boxes names and so I can configure remote ruby with a domain name and not worry about IP address changes when recreating the devbox.

Add RubyMine helpers to RUBYLIB

The problem above is that spring closes itself down every time SSH connection is closed. One could start a long-running spring from terminal SSH, but that would result in RubyMine complaining that it can not load teamcity formatter:

cannot load such file -- teamcity/spec/runner/formatter/teamcity/formatter (LoadError)

This is because RubyMine injects a special formatter into rspec so that it can parse the output better. Thanks to Oleg at Jetbrains (RUBY-16324) I discovered that the required formatters are present in the vagrant box and all I need is to add them to ruby load path, before starting spring.

So, I added following lines to my /home/vagrant/.bashrc:

# RubyMine-RUBYLIB
if [[ -d "$HOME/.rubymine_helpers" ]]
then
  export RUBYLIB=$HOME/.rubymine_helpers/rb/testing/patch/common/:$HOME/.rubymine_helpers/rb/testing/patch/bdd/:$RUBYLIB
fi

This sets up RUBYLIB variable with RubyMine helpers. I skipped over testunit folder, as I do not use that, but feel free to add that also if you need it.

Let the specs fly

Now, just SSH into your Vagrant box, start Spring, e.g. by running:

bin/rspec --help

And then RubyMine can re-use the already running Spring server to speed itself up.

GIT merge-svn

How to use GIT to merge two SVN branches

TL;DR: Download the git-merge-svn script here

I’ve been using git for years now but had to start using SVN for some projects. I found that GIT is good enough Subversion client too, especially as I retain the ability to commit often and rebase my work on top of commits from other devs (on the SVN side).

The only question arose – can I merge two SVN branches so that GIT log will show the merge?

The git-svn manual states that one should avoid all git clone|merge|pull|push activity when using git-svn.

But git log does show merge history that was created in Subversion – how does it do that?

svn:mergeinfo

Subversion does not support actual merge of branches (more like cherry-picking), but since version 1.5 Subversion supports the svn:mergeinfo property that is used to track what has been merged into this folder previously.

Digging some more into the matter, I found out that GIT supports setting svn:mergeinfo property on the SVN branch when dcommit‘ing:

git svn dcommit --mergeinfo "/branches/somebranch:1-3"

NB! the svn:mergeinfo is overwritten with whatever is given on the command-line, so be careful to list previous merges too.

While more recent git version added the config parameter to automatically set this property:

config key: svn.pushmergeinfo

I had some troubles with the automatic mergeinfo – for one reason or the other GIT calculated it wrong and I couldn’t get it to work.

SOLUTION: git-merge-svn

To automate the process, I wrote a shell script git merge-svn which can be used to merge two SVN branches with correct svn:mergeinfo set on the dcommit.

The script handles both situations:

  • the branch is not merged in git – will do git merge beforehand
  • the branches have been already merged in git (but not in SVN) – will traverse until previous ancestor for the merged commit revisions.

UPDATE: Thanks theantway and haraldreingruber for patches – the script now:

  • always does full merge (no fast-forward) so that SVN can fully understand and
  • does not die on first merge (no previous mergeinfo)

Download the git-merge-svn script here

Example usage

With this script I was able to produce these merges solely on git-side and retain the merge info so that GIT graph shows the log nicely:

git-merge-svn result

  1. Make some commits on devel6
  2. dcommit devel6 to SVN (required to get SVN revision numbers for the commits)
  3. check out testtunk6 – yes, I know I made a typo in the name 😉
  4. git merge-svn devel6

The last commant outputs:

% git merge-svn devel6
About to do an SVN merge: devel6 -> testtunk6

* NEW MERGE COMMIT
|
| * devel6 [7b71187] (r102)
* | testtunk6 [0682a45] (r101)
 |
  * [273d6d6] (r100)


STEP 1: GIT merge
Executing:
  git merge --no-ff devel6

Continue? (y/n) [n]: y
Merge made by the 'recursive' strategy.
 testfile | 1 +
 1 file changed, 1 insertion(+)

STEP 2: SVN dcommit

executing:
git svn dcommit --mergeinfo
/idp/branches/devel:9-32,35-41 /idp/branches/devel6:89 /idp/branches/devel6:94 /idp/branches/devel6:93 /idp/branches/devel6:96 /idp/branches/devel6:97 /idp/branches/devel6:99 /idp/branches/devel6:100 /idp/branches/devel6:102

Continue? (y/n) [n]: y
Committing to https://my.svn.host/svn/auth/idp/branches/testtunk6 ...
  M testfile
Committed r103
  M testfile
Found merge parent (svn:mergeinfo prop): 7b71187fc371d3f86658c5850009e63be88157ac
r103 = 87759323cbadd38bac78087d57b6870a926287e7 (refs/remotes/svn/testtunk6)
No changes between 3fb2168cfbbe605fbd810d76513443203a85a549 and refs/remotes/svn/testtunk6
Resetting to the latest refs/remotes/svn/testtunk6

Safari New Window

Safari New Window icon

Download: Safari New Window

Previously I have written AppleScripts to open new windows for TextMate, Sublime Text 2 and also Safari. But the latter was somehow corrupt when using with Mac OS X 10.8.3, so I rewrote it based on previous two, adding features in the process:

  1. Checks if Safari is already running and creates new window (without switching spaces)
  2. Activates Safari if it’s not running yet.

The full source code is here:

-- quickly check if app is running.
-- Thanks https://discussions.apple.com/message/13184433#13184433
on appIsRunning(appName)
    tell application "System Events" to (name of processes) contains appName
end appIsRunning

property appName : "Safari"

-- Open new windows or If app is not already running, start it
if appIsRunning(appName) then
    tell application appName
        make new document
        activate
    end tell
else
    tell application appName to activate
end if

 

Rails redirect_back_or_default

In a recent project I found myself writing recirect_to :back alot, and then found myself worrying that what if for some reason there is no :back.

Drawing inspiration from this blog, I wrote two helpers in my application_controller.rb.

The store_location stores current URI (or referer URI in case of non-GET request) into session[:return_to] for later usage:

#!ruby
def store_location
  session[:return_to] = if request.get?
    request.request_uri
  else
    request.referer
  end
end

And redirect_back_or_default tries its best to redirect the user to somewhere, in the following order:

  1. previously stored session[:return_to]
  2. Referer URI
  3. Given default URI
  4. or root_url if all else fails

The code itself

#!ruby
def redirect_back_or_default(default = root_url, options)
  redirect_to(session.delete(:return_to) || request.referer || default, options)
end

I’ve found that when rewriting redirect_to :back, notice: 'something' into redirect_back_or_default-call, adding this alias helps:

#!ruby
alias_method :redirect_to_back_or_default, :redirect_back_or_default

But of course, if you are testing your code (and you should be), it’s better to stick to one variant of above and use tests to catch all erroneous incarnations.

Rails 3: Merge scopes

I run into a case where I had User.search method and I wanted the GroupMember model be searchable by the user’s attributes. The most DRY way to accomplish this in Rails 3 is to merge scopes. In the User model:

#!ruby
# user.rb
class User < ActiveRecord::Base
  has_many :memberships, :class_name => "GroupMember", :foreign_key => "user_id"

  def self.search(search)
    if search.present?
      query = []
      params = []
      %w(uid email name).each do |field|
        # The field name must be fully qualified to merge scopes
        query << "#{self.table_name}.#{field} LIKE ?"
        params << "%#{search}%"
      end
      query = query.join(" OR ")
      where(query, *params)
    else
      scoped
    end
  end
end

NB! It’s important to have the User’s field names fully qualified so that they won’t be applied to the GroupMember table. And in the GroupMember model:

#!ruby
# group_member.rb
class GroupMember < ActiveRecord::Base
  belongs_to :user
  belongs_to :group

  def self.search(search)
    if search.present?
      # We search GroupMembers by the user attributes
      scoped.joins(:user).merge(User.search(search))
    else
      scoped
    end
  end
end

Now it’s possible to search for GroupMembers by the User attributes:

#!ruby
group = Group.find 1
group.group_members.search('david')

This results in SQL query:

#!sql
SELECT "group_members".* FROM "group_members" INNER JOIN "users"
ON "users"."id" = "group_members"."user_id" WHERE "group_members"."group_id" = 1
AND (users.uid LIKE '%david%' OR users.email LIKE '%david%'
OR users.name LIKE '%david%')

Sublime Text 2 New Window (via AppleScript)

Download: SublNewWindow

Building upon my older post on opening new window in Text Mate 2, I now threw together small script to do the same in Sublime Text 2.

While Sublime Text 2 is superb text editor, it is not Mac native, thus it’s even less probable that a new Mac-style Dock icon menu will appear in near future, that allows opening new window quickly. Thus a helper application is in order.

SublNewWindow

Based on the original TMNewWindow code, only small modifications were needed. One unexpected behavior was with detecting working state of the application. For whatever reason the original script gave errors when run against Sublime Text 2.

Again, the AppleScript application is linked below and feel free to send me updates and patches if needed.