Skip to content

Commit

Permalink
Replace Vagrant with Docker Compose
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbrictson committed Jun 15, 2024
1 parent 830751c commit 8a66c94
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 197 deletions.
6 changes: 6 additions & 0 deletions .docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM ubuntu:22.04
WORKDIR /provision
COPY ./ubuntu_setup.sh ./
RUN ./ubuntu_setup.sh
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
22 changes: 22 additions & 0 deletions .docker/ubuntu_setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -e

export DEBIAN_FRONTEND=noninteractive
apt -y update

# Create `deployer` user that can sudo without a password
apt-get -y install sudo
adduser --disabled-password deployer < /dev/null
echo "deployer:topsecret" | chpasswd
echo "deployer ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

# Install and configure sshd
apt-get -y install openssh-server
{
echo "Port 22"
echo "PasswordAuthentication yes"
echo "ChallengeResponseAuthentication no"
} >> /etc/ssh/sshd_config
mkdir /var/run/sshd
chmod 0755 /var/run/sshd
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
bin/rake
.bundle
.yardoc
.vagrant*
test/tmp
Gemfile.lock
7 changes: 0 additions & 7 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ Layout/IndentHash:
Exclude:
- 'test/functional/backends/test_local.rb'
- 'test/functional/backends/test_netssh.rb'
- 'test/support/vagrant_wrapper.rb'
- 'test/unit/formatters/test_custom.rb'
- 'test/unit/formatters/test_pretty.rb'
- 'test/unit/test_mapping_interaction_handler.rb'
Expand Down Expand Up @@ -445,12 +444,6 @@ Style/MethodName:
Exclude:
- 'test/unit/test_color.rb'

# Offense count: 1
# Cop supports --auto-correct.
Style/MutableConstant:
Exclude:
- 'Vagrantfile'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: Strict.
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ using unsupported features.

## Tests

SSHKit has a unit test suite and a functional test suite. Some functional tests run against
[Vagrant](https://www.vagrantup.com/) VMs. If possible, you should make sure that the
SSHKit has a unit test suite and a functional test suite. Some functional tests run using
[Docker](https://docs.docker.com/get-docker/). If possible, you should make sure that the
tests pass for each commit by running `rake` in the sshkit directory. This is in case we
need to cherry pick commits or rebase. You should ensure the tests pass, (preferably on
the minimum and maximum ruby version), before creating a PR.
Expand Down
2 changes: 1 addition & 1 deletion RELEASING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
## How to release

1. Run `bundle install` to make sure that you have all the gems necessary for testing and releasing.
2. **Ensure the tests are passing by running `rake test`.** If functional tests fail, ensure you have [Vagrant](https://www.vagrantup.com) installed and have started it with `vagrant up`.
2. **Ensure the tests are passing by running `rake test`.** If functional tests fail, ensure you have [Docker installed](https://docs.docker.com/get-docker/) and running.
3. Determine which would be the correct next version number according to [semver](http://semver.org/).
4. Update the version in `./lib/sshkit/version.rb`.
5. Commit the `version.rb` change with a message like "Preparing vX.Y.Z"
Expand Down
4 changes: 0 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ namespace :test do

end

Rake::Task["test:functional"].enhance do
warn "Remember there are still some VMs running, kill them with `vagrant halt` if you are finished using them."
end

desc 'Run RuboCop lint checks'
RuboCop::RakeTask.new(:lint) do |task|
task.options = ['--lint']
Expand Down
24 changes: 0 additions & 24 deletions Vagrantfile

This file was deleted.

8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: sshkit

services:
ssh_server:
build:
context: .docker
ports:
- "2122:22"
17 changes: 0 additions & 17 deletions test/boxes.json

This file was deleted.

2 changes: 1 addition & 1 deletion test/functional/backends/netssh_transfer_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def setup
end

def a_host
VagrantWrapper.hosts['one']
DockerWrapper.host
end

def test_upload_and_then_capture_file_contents
Expand Down
2 changes: 1 addition & 1 deletion test/functional/backends/test_netssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def setup
end

def a_host
VagrantWrapper.hosts['one']
DockerWrapper.host
end

def test_simple_netssh
Expand Down
24 changes: 0 additions & 24 deletions test/functional/test_ssh_server_comes_up_for_functional_tests.rb

This file was deleted.

45 changes: 2 additions & 43 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,51 +28,10 @@ def flush_connections
end

class FunctionalTest < Minitest::Test

def setup
unless VagrantWrapper.running?
warn "Vagrant VMs are not running. Please, start it manually with `vagrant up`"
end
require_relative "support/docker_wrapper"
DockerWrapper.start unless DockerWrapper.running?
end

private

def create_user_with_key(username, password = :secret)
username, password = username.to_s, password.to_s

keys = VagrantWrapper.hosts.collect do |_name, host|
Net::SSH.start(host.hostname, host.user, port: host.port, password: host.password) do |ssh|

# Remove the user, make it again, force-generate a key for him
# short keys save us a few microseconds
ssh.exec!("sudo userdel -rf #{username}; true") # The `rescue nil` of the shell world
ssh.exec!("sudo useradd -m #{username}")
ssh.exec!("sudo echo y | ssh-keygen -b 1024 -f #{username} -N ''")
ssh.exec!("sudo chown vagrant:vagrant #{username}*")
ssh.exec!("sudo echo #{username}:#{password} | chpasswd")

# Make the .ssh directory, change the ownership and the
ssh.exec!("sudo mkdir -p /home/#{username}/.ssh")
ssh.exec!("sudo chown #{username}:#{username} /home/#{username}/.ssh")
ssh.exec!("sudo chmod 700 /home/#{username}/.ssh")

# Move the key to authorized keys and chown and chmod it
ssh.exec!("sudo cat #{username}.pub > /home/#{username}/.ssh/authorized_keys")
ssh.exec!("sudo chown #{username}:#{username} /home/#{username}/.ssh/authorized_keys")
ssh.exec!("sudo chmod 600 /home/#{username}/.ssh/authorized_keys")

key = ssh.exec!("cat /home/vagrant/#{username}")

# Clean Up Files
ssh.exec!("sudo rm #{username} #{username}.pub")

key
end
end

Hash[VagrantWrapper.hosts.collect { |n, _h| n.to_sym }.zip(keys)]
end

end

#
Expand Down
50 changes: 50 additions & 0 deletions test/support/docker_wrapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Minitest.after_run do
DockerWrapper.stop if DockerWrapper.running?
end

module DockerWrapper
class << self
def host
SSHKit::Host.new(
user: "deployer",
hostname: "localhost",
port: "2122",
password: "topsecret",
ssh_options: host_verify_options
)
end

def running?
out, status = run_compose_command("ps --status running", false)
status.success? && out.include?("ssh_server")
end

def start
run_compose_command("up -d")
end

def stop
run_compose_command("down")
end

private

def run_compose_command(command, echo=true)
$stderr.puts "[docker compose] #{command}" if echo
stdout, stderr, status = Open3.capture3("docker compose #{command}")

output = stdout + stderr
output.each_line { |line| $stderr.puts "[docker compose] #{line}" } if echo

[output, status]
end

def host_verify_options
if Net::SSH::Version::MAJOR >= 5
{ verify_host_key: :never }
else
{ paranoid: false }
end
end
end
end
64 changes: 0 additions & 64 deletions test/support/vagrant_wrapper.rb

This file was deleted.

16 changes: 8 additions & 8 deletions test/unit/test_command_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ def test_setter_procs

def test_prefix
map = CommandMap.new
map.prefix[:rake].push("/home/vagrant/.rbenv/bin/rbenv exec")
map.prefix[:rake].push("/home/deployer/.rbenv/bin/rbenv exec")
map.prefix[:rake].push("bundle exec")

assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec rake"
assert_equal map[:rake], "/home/deployer/.rbenv/bin/rbenv exec bundle exec rake"
end

def test_prefix_procs
map = CommandMap.new
map.prefix[:rake].push("/home/vagrant/.rbenv/bin/rbenv exec")
map.prefix[:rake].push("/home/deployer/.rbenv/bin/rbenv exec")
map.prefix[:rake].push(proc{ "bundle exec" })

assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec rake"
assert_equal map[:rake], "/home/deployer/.rbenv/bin/rbenv exec bundle exec rake"
end

def test_prefix_unshift
map = CommandMap.new
map.prefix[:rake].push("bundle exec")
map.prefix[:rake].unshift("/home/vagrant/.rbenv/bin/rbenv exec")
map.prefix[:rake].unshift("/home/deployer/.rbenv/bin/rbenv exec")

assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec rake"
assert_equal map[:rake], "/home/deployer/.rbenv/bin/rbenv exec bundle exec rake"
end

def test_indifferent_setter
Expand All @@ -59,10 +59,10 @@ def test_indifferent_setter

def test_indifferent_prefix
map = CommandMap.new
map.prefix[:rake].push("/home/vagrant/.rbenv/bin/rbenv exec")
map.prefix[:rake].push("/home/deployer/.rbenv/bin/rbenv exec")
map.prefix["rake"].push("bundle exec")

assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec rake"
assert_equal map[:rake], "/home/deployer/.rbenv/bin/rbenv exec bundle exec rake"
end

def test_prefix_initialization_is_thread_safe
Expand Down

0 comments on commit 8a66c94

Please sign in to comment.