Permalink
Browse files

Add command snapshot

  • Loading branch information...
Loïc PORTE
Loïc PORTE committed Jun 17, 2014
1 parent 8fac1c6 commit 6c5c70b734a113291a73b175697312ae60e12882
View
@@ -1,5 +1,6 @@
*.gem
*.rbc
.idea
.bundle
.config
.vagrant
View
@@ -171,6 +171,13 @@ the remote machine over SSH.
This is good enough for all built-in Vagrant provisioners (shell,
chef, and puppet) to work!
## Command
### Snapshot
`vagrant openstack snapshot <vmname> -n <snapshotname>`
Take snapshot of ***vmname*** with name ***snapshotname***
## Contributors
- [mitchellh](https://github.com/mitchellh)
@@ -196,6 +196,34 @@ def self.action_suspend
end
end
def self.action_take_snapshot
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use Call, IsCreated do |env, b1|
if env[:result]
b1.use ConnectOpenStack
b1.use Call, IsSnapshoting do |env,b2|
if env[:result]
b2.use MessageSnapshotInProgress
else
b2.use TakeSnapshot
end
b2.use Call, WaitForTask, [nil], 1200 do |env3, b3|
if env3[:result]
b3.use MessageSnapshotDone
end
end
end
else
b1.use MessageNotCreated
end
end
end
end
# The autoload farm
action_root = Pathname.new(File.expand_path("../action", __FILE__))
autoload :ConnectOpenStack, action_root.join("connect_openstack")
@@ -204,12 +232,15 @@ def self.action_suspend
autoload :HardRebootServer, action_root.join("hard_reboot_server")
autoload :IsCreated, action_root.join("is_created")
autoload :IsPaused, action_root.join("is_paused")
autoload :IsSnapshoting, action_root.join("is_snapshoting")
autoload :IsSuspended, action_root.join("is_suspended")
autoload :MessageAlreadyCreated, action_root.join("message_already_created")
autoload :MessageAlreadyPaused, action_root.join("message_already_paused")
autoload :MessageAlreadySuspended, action_root.join("message_already_suspended")
autoload :MessageNotCreated, action_root.join("message_not_created")
autoload :MessageNotSuspended, action_root.join("message_not_suspended")
autoload :MessageSnapshotDone, action_root.join("message_snapshot_done")
autoload :MessageSnapshotInProgress, action_root.join("message_snapshot_in_progress")
autoload :MessageWillNotDestroy, action_root.join("message_will_not_destroy")
autoload :MessageServerRunning, action_root.join("message_server_running")
autoload :PauseServer, action_root.join("pause_server")
@@ -219,7 +250,9 @@ def self.action_suspend
autoload :ResumeServer, action_root.join("resume_server")
autoload :SuspendServer, action_root.join("suspend_server")
autoload :SyncFolders, action_root.join("sync_folders")
autoload :TakeSnapshot, action_root.join("take_snapshot")
autoload :WaitForState, action_root.join("wait_for_state")
autoload :WaitForTask, action_root.join("wait_for_task")
autoload :WarnNetworks, action_root.join("warn_networks")
end
end
@@ -0,0 +1,24 @@
module VagrantPlugins
module OpenStack
module Action
class IsSnapshoting
def initialize(app, env)
@app = app
end
def call(env)
if env[:machine].id
infos = env[:openstack_compute].get_server_details(env[:machine].id)
task = infos.body['server']['OS-EXT-STS:task_state']
if task == 'image_snapshot' || task == 'image_pending_upload'
env[:result] = true
else
env[:result] = false
end
end
@app.call(env)
end
end
end
end
end
@@ -1,5 +1,5 @@
module VagrantPlugins
module OpenStack
module OpenStack
module Action
class MessageAlreadyCreated
def initialize(app, env)
@@ -0,0 +1,16 @@
module VagrantPlugins
module OpenStack
module Action
class MessageSnapshotDone
def initialize(app, env)
@app = app
end
def call(env)
env[:ui].info(I18n.t("vagrant_openstack.snapshot_done"))
@app.call(env)
end
end
end
end
end
@@ -0,0 +1,16 @@
module VagrantPlugins
module OpenStack
module Action
class MessageSnapshotInProgress
def initialize(app, env)
@app = app
end
def call(env)
env[:ui].info(I18n.t("vagrant_openstack.snapshot_in_progress"))
@app.call(env)
end
end
end
end
end
@@ -0,0 +1,26 @@
require "log4r"
module VagrantPlugins
module OpenStack
module Action
# This reboots a running server, if there is one.
class TakeSnapshot
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant_openstack::action::take_snapshot")
end
def call(env)
if env[:machine].id
env[:ui].info(I18n.t("vagrant_openstack.snapshoting_server"))
infos = env[:openstack_compute].get_server_details(env[:machine].id)
env[:openstack_compute].create_image(env[:machine].id,env[:openstack_snapshot_name] || 'snapshot')
end
@app.call(env)
end
end
end
end
end
@@ -0,0 +1,44 @@
# coding: utf-8
require 'log4r'
require 'timeout'
module VagrantPlugins
module OpenStack
module Action
# This action will wait for a machine to reach a specific state or quit by timeout.
class WaitForTask
def initialize(app, env, task, timeout)
@app = app
@logger = Log4r::Logger.new('vagrant_openstack::action::wait_for_task')
@task = Array.new(task).flatten
@timeout = timeout
end
def call(env)
env[:result] = true
task = get_task(env)
if @task.include?(task)
@logger.info("Machine already at task #{ task.to_s }")
else
@logger.info("Waiting for machine to reach task...")
begin
Timeout.timeout(@timeout) do
sleep 5 until @task.include?(get_task(env))
end
rescue Timeout::Error
env[:result] = false
end
@app.call(env)
end
end
def get_task(env)
infos = env[:openstack_compute].get_server_details(env[:machine].id)
infos.body['server']['OS-EXT-STS:task_state']
end
end
end
end
end
@@ -0,0 +1,70 @@
require "vagrant"
require "vagrant/action/builder"
require "pathname"
module VagrantPlugins
module OpenStack
module Action
class Command < Vagrant.plugin(2, :command)
include Vagrant::Action::Builtin
# include VagrantPlugins::OpenStack::Action
def initialize(argv, env)
super
@main_args, @sub_command, @sub_args = split_main_and_subcommand(argv)
@subcommands = Vagrant::Registry.new
@subcommands.register(:snapshot) do
require_relative 'command/command_snapshot'
CommandTakeSnapshot
end
end
def execute
if @main_args.include?("-h") || @main_args.include?("--help")
# Print the help for all the sub-commands.
return help
end
# If we reached this far then we must have a subcommand. If not,
# then we also just print the help and exit.
command_class = @subcommands.get(@sub_command.to_sym) if @sub_command
return help if !command_class || !@sub_command
@logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}")
# Initialize and execute the command class
command_class.new(@sub_args, @env).execute
end
# Prints the help out for this command
def help
opts = OptionParser.new do |o|
o.banner = "Usage: vagrant openstack <command> [<args>]"
o.separator ""
o.separator "Available subcommands:"
# Add the available subcommands as separators in order to print them
# out as well.
keys = []
@subcommands.each { |key, value| keys << key.to_s }
keys.sort.each do |key|
o.separator " #{key}"
end
o.separator ""
o.separator "For help on any individual command run `vagrant openstack <command> -h`"
end
@env.ui.info(opts.help, :prefix => false)
end
end
end
end
end
@@ -0,0 +1,43 @@
require "vagrant"
require "vagrant/action/builder"
require "pathname"
require "vagrant-openstack-plugin/action"
module VagrantPlugins
module OpenStack
module Action
class CommandTakeSnapshot < Vagrant.plugin("2", :command)
include Vagrant::Action::Builtin
def execute
options = {:openstack_snapshot_name => 'snapshot'}
opts = OptionParser.new do |opts|
opts.banner = "Enters openstack"
opts.separator ""
opts.separator "Usage: vagrant openstack snapshot <vmname> -n <snapshotname>"
opts.on( '-n', '--name NAME', 'snapshotname' ) do |name|
options[:openstack_snapshot_name] = name
end
end
# Parse the options
argv = parse_options(opts)
return if !argv
with_target_vms(argv, :reverse => true) do |vm|
if vm.provider.to_s == VagrantPlugins::OpenStack::Provider.new(nil).to_s
vm.action(:take_snapshot,options)
end
end
end
end
end
end
end
@@ -12,25 +12,33 @@
module VagrantPlugins
module OpenStack
class Plugin < Vagrant.plugin("2")
name "OpenStack Cloud"
description <<-DESC
module Action
class Plugin < Vagrant.plugin("2")
name "OpenStack Cloud"
description <<-DESC
This plugin enables Vagrant to manage machines in OpenStack Cloud.
DESC
DESC
config(:openstack, :provider) do
require_relative "config"
Config
end
config(:openstack, :provider) do
require_relative "config"
Config
end
provider(:openstack, parallel: true) do
# Setup some things
OpenStack.init_i18n
OpenStack.init_logging
# Load the actual provider
require_relative "provider"
Provider
end
provider(:openstack, parallel: true) do
# Setup some things
OpenStack.init_i18n
OpenStack.init_logging
command "openstack" do
require_relative "command"
Command
end
# Load the actual provider
require_relative "provider"
Provider
end
end
end
View
@@ -26,6 +26,12 @@ en:
The server is ready!
rebooting_server: |-
This server instance is rebooting!
snapshoting_server: |-
This server instance is snapshoting!
snapshot_done: |-
Snapshot is ok
snapshot_in_progress: |-
Snapshot is in progress
resuming_server: |-
The server instance has been resumed.
rsync_folder: |-

0 comments on commit 6c5c70b

Please sign in to comment.