From c60e1bf0730634c943e67ab55f2f4c2ccc4eb6f2 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Sat, 6 May 2017 22:09:33 +0200 Subject: [PATCH] add box specific bootstrap options in metadata. --- example_box/metadata.json | 3 -- gentoo.json | 2 +- gentoo_001_lxd.box | Bin 535 -> 742 bytes {example_box => gentoo_box}/README.md | 0 gentoo_box/metadata.json | 23 ++++++++++++++ {example_box => gentoo_box}/vagrant.pub | 0 lib/vagrant/lxd/action.rb | 3 ++ lib/vagrant/lxd/action/bootstrap.rb | 40 ++++++++++++++++++++++++ lib/vagrant/lxd/driver.rb | 6 +++- 9 files changed, 72 insertions(+), 5 deletions(-) delete mode 100644 example_box/metadata.json rename {example_box => gentoo_box}/README.md (100%) create mode 100644 gentoo_box/metadata.json rename {example_box => gentoo_box}/vagrant.pub (100%) create mode 100644 lib/vagrant/lxd/action/bootstrap.rb diff --git a/example_box/metadata.json b/example_box/metadata.json deleted file mode 100644 index 10ccc02..0000000 --- a/example_box/metadata.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "provider": "lxd" -} diff --git a/gentoo.json b/gentoo.json index c6db7e8..f443ae4 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": "4f3d7bfe034fe9fb82179992fdc6803b6f96abfb" + "checksum": "9cf9ffd2c840680672a329a87abcd056b021d130" } ] } diff --git a/gentoo_001_lxd.box b/gentoo_001_lxd.box index f84fafcaabe882f916b3aa3e2aa3237d8fa94e1f..a9d38b91dbdc670cf4a0f050a5b131123af2738f 100644 GIT binary patch literal 742 zcmVYx@F0I67RSlcVEE3RjC!ihG}4${D11(9suPq3KGXe zQTZ5@y{%b3D2j_9z%h=DFs~M~@{%3I*f&<_v69!&7QuXcbw38+svM&{0+bJ(qK&CT zZwmoM6$1)kNIAZPt0wgr_T}K^waUEpbs+Rb>ht~ycvu1UPk*Grz`uTy_TgDfu{xiK z4aY}OvHZoj-@2=}#l__>_fdEK?Ok#?G@xr(D1PPNL@N|oXm|O;O_+bFyW@Cc1~269 zPQ8)|?Vw0W3@b8x3?sK^BQx~Yf$GAQL>oH7tm=yG zpw{=MXV8`Asyr3?nba`PC$bbY+l}tEIlQ1{!Q67gx{95x*7J{2*6t5FbT(4c8G<1f zW-Z=z5<#}p>#pNTW1bmzluAdPkJ!F;sY&`m=qyu5Klg=IORWutQs`sx0J%Xo=0nuu!5!dg%7zHcj5=@LneKl6jZ!^d641bbYsUdxvwDw(bGl5T1YQxc`WL?LTm+ zg*l8^f;pb$S_rd*kQRgPvdwQU=h7`0rWJ%)p8sL)qx=8VYnx#@d689-Pm9i1rjuO# zPWxN?U+)DzNAIC>gThVAUVho%>5Y%_PdNOZe-3km@3U|D=Mjede}?lp{8F4x_0W+Zegth3ITWLsK!C3ff%WumsLr+6-&f??3CmC~P0jEd;g&y!PT#okdt zu0u=KisezpnaOJ4#md&wSCZK%fakVRpp zCF#Z)HVc0u9Fo&fC~3I%L{GHC`O_2gGkQ#vo~d^!D09nT5_wEaYxsZx0000000000 Z0000000000008i}z5_2w#{d8*0076o4PO8N diff --git a/example_box/README.md b/gentoo_box/README.md similarity index 100% rename from example_box/README.md rename to gentoo_box/README.md diff --git a/gentoo_box/metadata.json b/gentoo_box/metadata.json new file mode 100644 index 0000000..59dd199 --- /dev/null +++ b/gentoo_box/metadata.json @@ -0,0 +1,23 @@ +{ + "provider": "lxd", + "bootstrap": { + "hostname": { + "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/hosts"] + ] + }, + "packages": { + "exec": [ + ["emerge", "--update", "sudo"] + ] + }, + "sudo": { + "exec": [ + ["echo", "vagrant ALL=(ALL) NOPASSWD: ALL", ">>/etc/sudoers"] + ] + } + } +} diff --git a/example_box/vagrant.pub b/gentoo_box/vagrant.pub similarity index 100% rename from example_box/vagrant.pub rename to gentoo_box/vagrant.pub diff --git a/lib/vagrant/lxd/action.rb b/lib/vagrant/lxd/action.rb index f015759..f1fba9e 100644 --- a/lib/vagrant/lxd/action.rb +++ b/lib/vagrant/lxd/action.rb @@ -7,6 +7,7 @@ module Vagrant module Lxd module Action action_root = Pathname.new(File.expand_path("../action", __FILE__)) + autoload :Bootstrap, action_root.join("bootstrap") autoload :Create, action_root.join("create") autoload :EnsureImage, action_root.join("ensure_image") autoload :EnsureSsh, action_root.join("ensure_ssh") @@ -31,11 +32,13 @@ module Vagrant 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 new file mode 100644 index 0000000..51caa33 --- /dev/null +++ b/lib/vagrant/lxd/action/bootstrap.rb @@ -0,0 +1,40 @@ +require 'erb' + +module Vagrant + module Lxd + module Action + class Bootstrap + def initialize(app, env) + @app = app + @logger = Log4r::Logger.new("vagrant::lxd::action::bootstrap") + end + + def call(env) + driver = env[:machine].provider.driver + bs_data = env[:machine].box.metadata["bootstrap"] + + bs_data.each do |name, actions| + env[:ui].info "--- Bootstrap #{name} ---", :prefix => false + actions.each do |action, data| + # right now I do not handle differnet actions just return if + # action is not "exec". + next if action != "exec" + + container = driver.name + 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 + end + end + end + + @app.call(env) + end + end + end + end +end + +# vim: set et ts=2 sw=2: diff --git a/lib/vagrant/lxd/driver.rb b/lib/vagrant/lxd/driver.rb index 5a0dabf..9e72c43 100644 --- a/lib/vagrant/lxd/driver.rb +++ b/lib/vagrant/lxd/driver.rb @@ -155,7 +155,7 @@ module Vagrant begin @bridge = YAML.load(execute("network", "show", "vagrantbr0")) rescue - execute("network", "create", "vagrantbr0") + execute("network", "create", "vagrantbr0", "dns.mode=dynamic") end end @bridge @@ -191,6 +191,10 @@ module Vagrant end end + def exec(*command) + execute("exec", @name, "--", *command) + end + # Taken from Virtualbox provider and modified in some parts. # Execute the given subcommand for Lxc and return the output. def execute(*command, &block)