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. 15
      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",
"url": "file:///data/ghopp/projects/vagrant/vagrant-lxd/gentoo_001_lxd.box",
"checksum_type": "sha1",
"checksum": "9cf9ffd2c840680672a329a87abcd056b021d130"
"checksum": "303573ec033c0eb1939df486f444c5bcc3f82d1e"
}
]
}

BIN
gentoo_001_lxd.box

15
gentoo_box/metadata.json

@ -5,7 +5,7 @@
"exec": [
["sed", "-i", "s/-lxc //", "/etc/init.d/hostname"],
["sed", "-i", "s/LXC_NAME/<%= hostname %>/", "/etc/conf.d/hostname"],
["sed", "-i", "s/<%= container %>/<%= hostname %>/", "/etc/hostname"],
["sed", "-i", "s/<%= container %>/<%= hostname %>/", "/etc/hostname"],
["sed", "-i", "s/<%= container %>/<%= hostname %>/", "/etc/hosts"]
]
},
@ -16,7 +16,18 @@
},
"sudo": {
"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 Network
b2.use Create
b2.use action_start
b2.use Bootstrap
b2.use EnsureSsh
else
b2.use action_start
end
end
b.use action_start
b.use EnsureSsh
b.use Bootstrap
end
end
def self.action_start
Vagrant::Action::Builder.new.tap do |b|
b.use Bootstrap
b.use EnsureStarted
end
end

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

@ -24,11 +24,13 @@ module Vagrant
hostname = env[:machine].name
data.each do |d|
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
driver.restart
@app.call(env)
end

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

@ -10,8 +10,8 @@ module Vagrant
def call(env)
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.enable_ssh

39
lib/vagrant/lxd/driver.rb

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