This repository was archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVagrantfile
97 lines (84 loc) · 2.87 KB
/
Vagrantfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- mode: ruby -*-
# # vi: set ft=ruby :
require 'fileutils'
require 'yaml'
Vagrant.require_version ">= 1.6.0"
ENV['VAGRANT_DEFAULT_PROVIDER'] = "docker"
$config_path = File.expand_path(
"./config.rb",
File.dirname(__FILE__)
)
$containers_config_path = File.expand_path(
"./containers.yml",
File.dirname(__FILE__)
)
# Defalut configuration options
$docker_host_vm_name = "localhost"
if File.exist?($config_path)
require $config_path
end
if !File.exist?($containers_config_path)
abort("Cannot find path: %s" % $containers_config_path)
end
$docker_host_vm_vagrantfile = ""
if $docker_host_vm_name != "localhost"
$docker_host_vm_vagrantfile = File.expand_path(
"./%s/Vagrantfile" % $docker_host_vm_name,
File.dirname(__FILE__)
)
if !File.exist?($docker_host_vm_vagrantfile)
abort("Cannot find path: %s" % $docker_host_vm_vagrantfile)
end
end
Vagrant.configure("2") do |config|
YAML.load_file($containers_config_path).each do |containers|
config.vm.define containers["name"] do |container|
container.vm.synced_folder ".", "/vagrant", disabled: true
container.ssh.port = 22
container.ssh.username = containers["ssh_username"]
container.vm.provider "docker" do |docker, override|
if $docker_host_vm_name != "localhost"
docker.force_host_vm = true
docker.vagrant_vagrantfile = $docker_host_vm_vagrantfile
end
docker.vagrant_machine = $docker_host_vm_name
docker.name = containers["name"]
docker.image = containers["image"]
if !containers["has_ssh"].nil?
docker.has_ssh = containers["has_ssh"]
end
if !containers["ports"].nil?
docker.ports = containers["ports"]
end
if !containers["remains_running"].nil?
docker.remains_running = containers["remains_running"]
end
docker.env = {
SSH_SUDO: containers["ssh_sudo"],
SSH_USER: containers["ssh_username"],
SSH_USER_PASSWORD: containers["ssh_password"],
SSH_USER_HOME: "/home/%s" % containers["ssh_username"]
}
# Work-around for endless "Warning: Connection refused. Retrying..."
# Attribution:
# - https://objectpartners.com/2017/08/03/test-vagrant-boxes-using-docker/
# - https://gist.github.com/double16/81572c74684c18ace981c21042fbc397#file-vagrantfile-rb-L18
override.ssh.proxy_command = "docker run \
--rm \
--interactive \
--link %s \
alpine/socat \
- TCP:%s:22,retry=10,interval=1
" % [
containers["name"],
containers["name"]
]
end
container.vm.post_up_message = "
Usage:
vagrant docker-logs %s - Inspect the container logs. Useful to find the SSH credentials for sudo.
vagrant ssh %s - SSH to the container.
" % [containers["name"], containers["name"]]
end
end
end