diff --git a/gentoo.json b/gentoo.json index f443ae4..48df0db 100644 --- a/gentoo.json +++ b/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" } ] } diff --git a/gentoo_001_lxd.box b/gentoo_001_lxd.box index a9d38b9..336e0fe 100644 Binary files a/gentoo_001_lxd.box and b/gentoo_001_lxd.box differ diff --git a/gentoo_box/metadata.json b/gentoo_box/metadata.json index 59dd199..b10263b 100644 --- a/gentoo_box/metadata.json +++ b/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"] ] } } diff --git a/lib/vagrant/lxd/action.rb b/lib/vagrant/lxd/action.rb index f1fba9e..6326c3c 100644 --- a/lib/vagrant/lxd/action.rb +++ b/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 diff --git a/lib/vagrant/lxd/action/bootstrap.rb b/lib/vagrant/lxd/action/bootstrap.rb index 51caa33..3020921 100644 --- a/lib/vagrant/lxd/action/bootstrap.rb +++ b/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 diff --git a/lib/vagrant/lxd/action/ensure_ssh.rb b/lib/vagrant/lxd/action/ensure_ssh.rb index 2f88826..e0457c4 100644 --- a/lib/vagrant/lxd/action/ensure_ssh.rb +++ b/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 diff --git a/lib/vagrant/lxd/driver.rb b/lib/vagrant/lxd/driver.rb index 9e72c43..0dcdd7e 100644 --- a/lib/vagrant/lxd/driver.rb +++ b/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) diff --git a/sid.json b/sid.json new file mode 100644 index 0000000..2c98206 --- /dev/null +++ b/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" + } + ] + } + ] +} diff --git a/sid_001_lxd.box b/sid_001_lxd.box new file mode 100644 index 0000000..8e2e25d Binary files /dev/null and b/sid_001_lxd.box differ diff --git a/sid_box/README.md b/sid_box/README.md new file mode 100644 index 0000000..11de021 --- /dev/null +++ b/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. diff --git a/sid_box/metadata.json b/sid_box/metadata.json new file mode 100644 index 0000000..afe625c --- /dev/null +++ b/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"] + ] + } + } +} diff --git a/sid_box/vagrant.pub b/sid_box/vagrant.pub new file mode 100644 index 0000000..18a9c00 --- /dev/null +++ b/sid_box/vagrant.pub @@ -0,0 +1 @@ +ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key