From ecd8be87baca766172c433042df40432385338f7 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Tue, 9 May 2017 08:33:44 +0200 Subject: [PATCH] add basic stop and destroy action and provision in up --- lib/vagrant/lxd/action.rb | 15 +++++++++++++++ lib/vagrant/lxd/action/destroy.rb | 27 +++++++++++++++++++++++++++ lib/vagrant/lxd/action/stop.rb | 26 ++++++++++++++++++++++++++ lib/vagrant/lxd/driver.rb | 8 ++++++++ 4 files changed, 76 insertions(+) create mode 100644 lib/vagrant/lxd/action/destroy.rb create mode 100644 lib/vagrant/lxd/action/stop.rb diff --git a/lib/vagrant/lxd/action.rb b/lib/vagrant/lxd/action.rb index 07fe64c..4cc731e 100644 --- a/lib/vagrant/lxd/action.rb +++ b/lib/vagrant/lxd/action.rb @@ -13,6 +13,8 @@ module Vagrant autoload :EnsureSsh, action_root.join("ensure_ssh") autoload :EnsureStarted, action_root.join("ensure_started") autoload :Network, action_root.join("network") + autoload :Stop, action_root.join("stop") + autoload :Destroy, action_root.join("destroy") include Vagrant::Action::Builtin @@ -35,6 +37,7 @@ module Vagrant b2.use action_start end end + b2.use action_provision end end @@ -50,6 +53,18 @@ module Vagrant end end + def self.action_halt + Vagrant::Action::Builder.new.tap do |b| + b.use Stop + end + end + + def self.action_destroy + Vagrant::Action::Builder.new.tap do |b| + b.use Destroy + end + end + def self.action_provision Vagrant::Action::Builder.new.tap do |b| #b.use CheckAccessible diff --git a/lib/vagrant/lxd/action/destroy.rb b/lib/vagrant/lxd/action/destroy.rb new file mode 100644 index 0000000..938d5d5 --- /dev/null +++ b/lib/vagrant/lxd/action/destroy.rb @@ -0,0 +1,27 @@ +module Vagrant + module Lxd + module Action + class Destroy + def initialize(app, env) + @app = app + @logger = Log4r::Logger.new("vagrant::lxd::action::destroy") + end + + def call(env) + driver = env[:machine].provider.driver + + if driver.container? + if driver.state == :running + driver.stop + end + driver.destroy + end + + @app.call(env) + end + end + end + end +end + +# vim: set et ts=2 sw=2: diff --git a/lib/vagrant/lxd/action/stop.rb b/lib/vagrant/lxd/action/stop.rb new file mode 100644 index 0000000..f44d87a --- /dev/null +++ b/lib/vagrant/lxd/action/stop.rb @@ -0,0 +1,26 @@ +module Vagrant + module Lxd + module Action + class Stop + def initialize(app, env) + @app = app + @logger = Log4r::Logger.new("vagrant::lxd::action::stop") + end + + def call(env) + driver = env[:machine].provider.driver + + if driver.container? + if driver.state == :running + driver.stop + 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 0dcdd7e..7aefba6 100644 --- a/lib/vagrant/lxd/driver.rb +++ b/lib/vagrant/lxd/driver.rb @@ -128,6 +128,14 @@ module Vagrant container_data["state"]["status"].downcase.to_sym end + def stop + execute "stop", @name + end + + def destroy + execute "delete", @name + end + def get_image(remote) return if image? # image already exists