vagrant-snap 0.04 → 0.06

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.04
1
+ 0.06
@@ -3,18 +3,24 @@ require "colored"
3
3
  module Snap
4
4
  module VBox
5
5
  class SnapShot #{{{
6
- @@snaps = []
7
6
  class << self
8
- def is_endnode?() @@current.uuid == @@snaps.last.uuid end
9
-
10
- def snaps() @@snaps end
7
+ def tree
8
+ @@tree
9
+ end
10
+
11
+ Snap = Struct.new(:name, :time_stamp, :description, :uuid, :current)
12
+
13
+ def init
14
+ @@current = nil
15
+ @@tree = nil
16
+ end
11
17
 
12
18
  def parse_tree(vmname)
19
+ init
13
20
  vm = VirtualBox::VM.find( vmname )
14
21
  @@current = vm.current_snapshot
15
- @@indent = ""
16
22
  return unless @@current
17
- _parse(vm.root_snapshot)
23
+ @@tree = _parse(vm.root_snapshot)
18
24
  end
19
25
 
20
26
  # [TODO] need refactoring
@@ -37,30 +43,53 @@ module Snap
37
43
  end
38
44
  end
39
45
 
40
- ## [TODO] darty hack, should be written more simply
41
- def _parse(snaps, guide = "")
42
- @@snaps << snaps.name
43
- time = time_elapse(Time.now - snaps.time_stamp)
44
- snapinfo = "#{snaps.name} [ #{time} ]"
45
- snapinfo = snapinfo.yellow if snaps.uuid == @@current.uuid
46
- result = "#{guide} #{snapinfo}"
47
- result << " #{snaps.description}" unless snaps.description.empty?
48
- result << "\n"
49
- snaps.children.each_with_index do |e, idx|
50
- tmp = guide.sub("`", " ").chop.chop + " "
51
- nextguide = if snaps.children.size == idx + 1
52
- "`"
53
- else
54
- "|"
55
- end
56
- tmp << nextguide << "--"
57
- result << _parse(e, "#{tmp}")
46
+ def _parse(s)
47
+ tree = [ Snap.new(s.name , s.time_stamp, s.description, s.uuid, s.uuid == @@current.uuid) ]
48
+ s.children.each do |c|
49
+ tree.concat [_parse(c)]
50
+ end
51
+ tree
52
+ end
53
+
54
+ def format(guide, s)
55
+ time = time_elapse(Time.now - s.time_stamp)
56
+ snapinfo = "#{s.name} [ #{time} ]"
57
+ snapinfo = snapinfo.yellow if s.current
58
+ result = "#{guide} #{snapinfo}"
59
+ result << " #{s.description}" unless s.description.empty?
60
+ result << "\n"
61
+ end
62
+
63
+ def lastname
64
+ if tree
65
+ tree.flatten.sort_by(&:time_stamp).last.name
66
+ else
67
+ nil
68
+ end
69
+ end
70
+
71
+ def include?(name)
72
+ tree.flatten.map(&:name).include? name
73
+ end
74
+
75
+ def show(t=tree, guide="")
76
+ result = ""
77
+ t.each_with_index do |v, idx|
78
+ case v
79
+ when Array
80
+ tmp = guide.dup.chop.chop.sub("`", " ") << " "
81
+ tmp << "#{t.size == idx + 1 ? '`' : '|'}" << "--"
82
+ result << show(v, tmp)
83
+ else
84
+ result << format(guide, v)
85
+ end
58
86
  end
59
87
  result
60
88
  end
61
89
  end
62
90
  end #}}}
63
91
  end
92
+
64
93
  class Command < Vagrant::Command::GroupBase
65
94
  register "snap","Manages a snap"
66
95
 
@@ -73,16 +102,14 @@ module Snap
73
102
  target_found = false
74
103
  env.vms.each do |name, vm|
75
104
  vagvmname = vm.name
76
- vmname = vm.vm.name
77
- if target
78
- blk.call(vmname, vagvmname) if target.to_sym == vagvmname
79
- target_found = true
80
- else
105
+ vmname = vm.vm.name
106
+
107
+ if target.nil? or target.to_sym == vagvmname
81
108
  blk.call(vmname, vagvmname)
82
109
  target_found = true
83
110
  end
84
111
  end
85
- warn "you need to select collect vmname" unless target_found
112
+ warn "A VM by the name of `#{target}' was not found".red unless target_found
86
113
  end
87
114
  }
88
115
 
@@ -90,8 +117,13 @@ module Snap
90
117
  def list(target=nil)
91
118
  with_target(target) do |vmname, vagvmname|
92
119
  puts "[#{vagvmname}]"
93
- result = VBox::SnapShot.parse_tree( vmname )
94
- puts result ? result : "no snapshot"
120
+ VBox::SnapShot.parse_tree( vmname )
121
+ if VBox::SnapShot.tree
122
+ result = VBox::SnapShot.show
123
+ else
124
+ result = "no snapshot"
125
+ end
126
+ puts result
95
127
  end
96
128
  end
97
129
 
@@ -99,9 +131,14 @@ module Snap
99
131
  def go(snapshot_name, target=nil)
100
132
  with_target(target) do |vmname, vagvmname|
101
133
  puts "[#{vagvmname}]"
102
- system "VBoxManage controlvm #{vmname} poweroff"
103
- system "VBoxManage snapshot #{vmname} restore #{snapshot_name}"
104
- system "VBoxManage startvm #{vmname} --type headless"
134
+ VBox::SnapShot.parse_tree( vmname )
135
+ if VBox::SnapShot.include?( snapshot_name )
136
+ system "VBoxManage controlvm #{vmname} poweroff"
137
+ system "VBoxManage snapshot #{vmname} restore #{snapshot_name}"
138
+ system "VBoxManage startvm #{vmname} --type headless"
139
+ else
140
+ warn "#{snapshot_name} is not exist".red
141
+ end
105
142
  end
106
143
  end
107
144
 
@@ -115,14 +152,31 @@ module Snap
115
152
  end
116
153
  end
117
154
 
118
- desc "take [NAME] [-d DESC]", "take snapshot"
155
+ desc "take [TARGET] [-n SNAP_NAME] [-d DESC]", "take snapshot"
119
156
  method_option :desc, :type => :string, :aliases => "-d"
157
+ method_option :name, :type => :string, :aliases => "-n"
120
158
  def take(target=nil)
121
159
  with_target(target) do |vmname, vagvmname|
122
160
  puts "[#{vagvmname}]"
123
161
  VBox::SnapShot.parse_tree( vmname )
124
- last_name = VBox::SnapShot.snaps.sort.reverse.first
125
- new_name = last_name.nil? ? "#{vagvmname}-01" : last_name.succ
162
+ if options.name
163
+ if VBox::SnapShot.include? options.name
164
+ warn "#{options.name} is already exist".red
165
+ next
166
+ else
167
+ new_name = options.name
168
+ end
169
+ end
170
+ unless new_name
171
+ lastname = VBox::SnapShot.lastname
172
+ new_name = if lastname.nil?
173
+ "001"
174
+ else
175
+ n = lastname.succ
176
+ n = n.succ while VBox::SnapShot.include? n
177
+ n
178
+ end
179
+ end
126
180
  desc = options.desc ? " --description '#{options.desc}'" : ""
127
181
  system "VBoxManage snapshot #{vmname} take #{new_name} #{desc} --pause"
128
182
  end
@@ -132,7 +186,12 @@ module Snap
132
186
  def delete(snapshot_name, target=nil)
133
187
  with_target(target) do |vmname, vagvmname|
134
188
  puts "[#{vagvmname}]"
135
- system "VBoxManage snapshot #{vmname} delete #{snapshot_name}"
189
+ VBox::SnapShot.parse_tree( vmname )
190
+ if VBox::SnapShot.include?( snapshot_name )
191
+ system "VBoxManage snapshot #{vmname} delete #{snapshot_name}"
192
+ else
193
+ warn "#{snapshot_name} is not exist".red
194
+ end
136
195
  end
137
196
  end
138
197
  end
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{vagrant-snap}
5
- s.version = "0.04"
5
+ s.version = "0.06"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["t9md"]
9
- s.date = %q{2011-06-06}
9
+ s.date = %q{2011-06-08}
10
10
  s.description = %q{vagrant snapshot managemen plugin}
11
11
  s.email = %q{taqumd@gmail.com}
12
12
  s.extra_rdoc_files = ["LICENSE.txt", "README.md", "lib/vagrant_init.rb", "lib/vagrant_snap.rb"]
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-snap
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 4
9
- version: "0.04"
8
+ - 6
9
+ version: "0.06"
10
10
  platform: ruby
11
11
  authors:
12
12
  - t9md
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-06-06 00:00:00 +09:00
17
+ date: 2011-06-08 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency