Skip to content

Commit

Permalink
Add Vagrant ssh-config to ~/.ssh/config on vagrant up
Browse files Browse the repository at this point in the history
  • Loading branch information
retlehs committed Dec 13, 2018
1 parent 8641ca9 commit 4587eca
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
### HEAD
* Add Vagrant `ssh-config` to `~/.ssh/config` on `vagrant up` ([#1042](https://github.com/roots/trellis/pull/1042))
* [BREAKING] Add Ubuntu 18.04 support and default to it ([#992](https://github.com/roots/trellis/pull/992))
* Python 3 support ([#1031](https://github.com/roots/trellis/pull/1031))
* Allow customizing Nginx `worker_connections` ([#1021](https://github.com/roots/trellis/pull/1021))
Expand Down
15 changes: 11 additions & 4 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

ANSIBLE_PATH = __dir__ # absolute path to Ansible directory on host machine
ANSIBLE_PATH_ON_VM = '/home/vagrant/trellis'.freeze # absolute path to Ansible directory on virtual machine

Expand Down Expand Up @@ -135,9 +132,19 @@ Vagrant.configure('2') do |config|
extra_vars = Hash[vars.split(',').map { |pair| pair.split('=') }]
ansible.extra_vars.merge!(extra_vars)
end

if !Vagrant::Util::Platform.windows?
config.trigger.after :up do |trigger|
# Add Vagrant ssh-config to ~/.ssh/config
trigger.run = {
path: File.join(provisioning_path, 'bin/ssh-vagrant-config.sh'),
args: [main_hostname]
}
end
end
end

# Virtualbox settings
# VirtualBox settings
config.vm.provider 'virtualbox' do |vb|
vb.name = config.vm.hostname
vb.customize ['modifyvm', :id, '--cpus', vconfig.fetch('vagrant_cpus')]
Expand Down
8 changes: 8 additions & 0 deletions bin/ssh-vagrant-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
vagrant_host=$1

# Add Vagrant ssh-config to ~/.ssh/config
sed "/^$/d;s/Host /$NL&/" ~/.ssh/config | sed '/^Host '"$vagrant_host"'$/,/^$/d;' > config &&
cat config > ~/.ssh/config &&
rm config &&
vagrant ssh-config --host ${vagrant_host} >> ~/.ssh/config
3 changes: 1 addition & 2 deletions bin/xdebug-tunnel.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env bash

#!/bin/bash
show_usage() {
echo "
Usage: bin/xdebug-tunnel.sh <action> <host>
Expand Down

2 comments on commit 4587eca

@mockey
Copy link
Contributor

@mockey mockey commented on 4587eca Dec 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sed-script sort of messes up my ssh-config:

  • It removes all empty lines (why?)
  • it replaces an already existing entry for the vagrant-server (hm, OK)
  • but it also it removes all existing enttries after it (not good!)

I don't quite understand the sed syntax but the command seems to work better without the first part:
sed "/^$/d;s/Host /$NL&/"
What is that supposed to do?
sed '/^Host '"$vagrant_host"'$/,/^$/d;'
seems to remove an entry for $vagrant_host without it (and it doesn't remove the following entries).

Also it might be better to only replace the entry when it's not already existing.

@retlehs
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in #1053

Please sign in to comment.