Browse Source

bootstrap containers from metadata info / added a sid example box

master
Georg Hopp 9 years ago
parent
commit
78524563b8
  1. 2
      gentoo.json
  2. BIN
      gentoo_001_lxd.box
  3. 13
      gentoo_box/metadata.json
  4. 9
      lib/vagrant/lxd/action.rb
  5. 6
      lib/vagrant/lxd/action/bootstrap.rb
  6. 4
      lib/vagrant/lxd/action/ensure_ssh.rb
  7. 39
      lib/vagrant/lxd/driver.rb
  8. 17
      sid.json
  9. BIN
      sid_001_lxd.box
  10. 16
      sid_box/README.md
  11. 33
      sid_box/metadata.json
  12. 1
      sid_box/vagrant.pub

2
gentoo.json

@ -9,7 +9,7 @@
"name": "lxd", "name": "lxd",
"url": "file:///data/ghopp/projects/vagrant/vagrant-lxd/gentoo_001_lxd.box", "url": "file:///data/ghopp/projects/vagrant/vagrant-lxd/gentoo_001_lxd.box",
"checksum_type": "sha1", "checksum_type": "sha1",
"checksum": "9cf9ffd2c840680672a329a87abcd056b021d130"
"checksum": "303573ec033c0eb1939df486f444c5bcc3f82d1e"
} }
] ]
} }

BIN
gentoo_001_lxd.box

13
gentoo_box/metadata.json

@ -16,7 +16,18 @@
}, },
"sudo": { "sudo": {
"exec": [ "exec": [
["echo", "vagrant ALL=(ALL) NOPASSWD: ALL", ">>/etc/sudoers"]
[
"sed",
"-i",
"/vagrant/b;$a\\\nvagrant ALL=(ALL) NOPASSWD: ALL",
"/etc/sudoers"
]
]
},
"sshd_service": {
"exec": [
["rc-update", "add", "sshd", "default"],
["/etc/init.d/sshd", "start"]
] ]
} }
} }

9
lib/vagrant/lxd/action.rb

@ -28,17 +28,18 @@ module Vagrant
b2.use EnsureImage b2.use EnsureImage
b2.use Network b2.use Network
b2.use Create b2.use Create
b2.use action_start
b2.use Bootstrap
b2.use EnsureSsh
else
b2.use action_start
end end
end end
b.use action_start
b.use EnsureSsh
b.use Bootstrap
end end
end end
def self.action_start def self.action_start
Vagrant::Action::Builder.new.tap do |b| Vagrant::Action::Builder.new.tap do |b|
b.use Bootstrap
b.use EnsureStarted b.use EnsureStarted
end end
end end

6
lib/vagrant/lxd/action/bootstrap.rb

@ -24,11 +24,13 @@ module Vagrant
hostname = env[:machine].name hostname = env[:machine].name
data.each do |d| data.each do |d|
d.collect! { |element| ERB.new(element).result(binding) } d.collect! { |element| ERB.new(element).result(binding) }
driver.exec(*d)
env[:ui].info "--- #{d.inspect} ---", :prefix => false
env[:ui].info "--- #{action}: #{d.inspect} ---",
:prefix => false
driver.exec(*d, :retryable => true)
end end
end end
end end
driver.restart
@app.call(env) @app.call(env)
end end

4
lib/vagrant/lxd/action/ensure_ssh.rb

@ -10,8 +10,8 @@ module Vagrant
def call(env) def call(env)
driver = env[:machine].provider.driver driver = env[:machine].provider.driver
env[:ui].info "--- #{env[:machine].box.directory} ---",
:prefix => false
# Currently I suppose this is the same on all linux distributions
# so it is not configured in the metadata of the box.
driver.vagrant_user driver.vagrant_user
driver.enable_ssh driver.enable_ssh

39
lib/vagrant/lxd/driver.rb

@ -72,12 +72,12 @@ module Vagrant
def initialize(machine) def initialize(machine)
@machine = machine @machine = machine
@name = "vagrant-#{machine.name}"
@name = "vagrant-#{machine.config.vm.hostname}"
@name = "vagrant-#{machine.name}" unless @name
@logger = Log4r::Logger.new("vagrant::provider::lxd::driver") @logger = Log4r::Logger.new("vagrant::provider::lxd::driver")
# This flag is used to keep track of interrupted state (SIGINT) # This flag is used to keep track of interrupted state (SIGINT)
@interrupted = false @interrupted = false
@image = machine.box.name.split("/")[1] if machine.box
bridge bridge
end end
@ -89,8 +89,12 @@ module Vagrant
end.flatten(1)] end.flatten(1)]
end end
def image
@machine.box.name.split("/")[1..-1].join("/") if @machine.box
end
def image? def image?
images.key? @image
images.key? image
end end
# Get infos about all existing containers # Get infos about all existing containers
@ -130,7 +134,7 @@ module Vagrant
args = [ args = [
"image", "image",
"copy", "copy",
"#{remote}:#{@image}",
"#{remote}:#{image}",
"local:", "local:",
"--copy-aliases" "--copy-aliases"
] ]
@ -141,7 +145,7 @@ module Vagrant
def create def create
# network could be also attached right here if it turns out to be # network could be also attached right here if it turns out to be
# a good idea. # a good idea.
execute("init", @image, @name, "-n", @bridge["name"])
execute("init", image, @name, "-n", @bridge["name"])
end end
def start def start
@ -161,15 +165,18 @@ module Vagrant
@bridge @bridge
end end
def restart
execute("stop", @name)
execute("start", @name)
end
def vagrant_user def vagrant_user
pwent = [] pwent = []
while pwent.empty? do while pwent.empty? do
begin begin
pwent = execute(
"exec", @name, "getent", "passwd", "vagrant"
).split(":")
pwent = exec("getent", "passwd", "vagrant").split(":")
rescue rescue
execute("exec", @name, "--", "useradd", "-m", "vagrant")
exec("useradd", "-m", "-s", "/bin/bash", "vagrant")
end end
end end
execute( execute(
@ -178,17 +185,19 @@ module Vagrant
"--uid=#{pwent[2]}", "--uid=#{pwent[2]}",
"--gid=#{pwent[3]}", "--gid=#{pwent[3]}",
"--mode=0400", "--mode=0400",
"-p",
"#{@machine.box.directory}/vagrant.pub", "#{@machine.box.directory}/vagrant.pub",
"#{@name}/#{pwent[5]}/.ssh/authorized_keys"
"#{@name}#{pwent[5]}/.ssh/authorized_keys"
) )
exec("chmod", "700", "#{pwent[5]}/.ssh")
end end
def enable_ssh def enable_ssh
begin
execute("exec", @name, "--", "rc-update", "add", "sshd", "default")
execute("exec", @name, "--", "/etc/init.d/sshd", "start")
rescue
end
#begin
service = @machine.box.metadata["bootstrap"]["sshd_service"]
service["exec"].each { |command| exec(*command) }
#rescue
#end
end end
def exec(*command) def exec(*command)

17
sid.json

@ -0,0 +1,17 @@
{
"name": "lxd/debian/sid",
"description": "The latest debian/sid LXD image.",
"versions": [
{
"version": "0.0.1",
"providers": [
{
"name": "lxd",
"url": "file:///data/ghopp/projects/vagrant/vagrant-lxd/sid_001_lxd.box",
"checksum_type": "sha1",
"checksum": "a533654e7577931659c1cb457b13e6b184d4013d"
}
]
}
]
}

BIN
sid_001_lxd.box

16
sid_box/README.md

@ -0,0 +1,16 @@
# Vagrant LXD Example Box
Vagrant providers each require a custom provider-specific box format.
This folder shows the example contents of a box for the `lxd` provider.
To turn this into a box:
```
$ tar cvzf lxd.box ./metadata.json ./vagrant.pub
```
The `lxd` provider right now just uses the default lxd images provided
by the lxd images: remote. Upon start these will be provisioned with an
vagrant ssh user and and the unsafe common pubkey of vagrant and
sshd will be enabled.
Well, at least thats the idea for now.

33
sid_box/metadata.json

@ -0,0 +1,33 @@
{
"provider": "lxd",
"bootstrap": {
"hostname": {
"exec": [
["sed", "-i", "s/<%= container %>/<%= hostname %>/", "/etc/hostname"],
["sed", "-i", "s/<%= container %>/<%= hostname %>/", "/etc/hosts"]
]
},
"packages": {
"exec": [
["apt-get", "install", "-y", "sudo"],
["apt-get", "install", "-y", "openssh-server"]
]
},
"sudo": {
"exec": [
[
"sed",
"-i",
"/vagrant/b;$a\\\nvagrant ALL=(ALL) NOPASSWD: ALL",
"/etc/sudoers"
]
]
},
"sshd_service": {
"exec": [
["systemctl", "enable", "ssh"],
["systemctl", "start", "ssh"]
]
}
}
}

1
sid_box/vagrant.pub

@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key
Loading…
Cancel
Save