vagrantomatic 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ec2f618d02c4bedd8f9af8deab061dc8d781d034
4
- data.tar.gz: ba2d50da5efe8a1a60913cde81d9120f364397b1
3
+ metadata.gz: fb2da2108c6e8b03cbc54d79c0f6461c31293d83
4
+ data.tar.gz: 649cce1ad226a8f2a6b5e0f5337da8c999937aa8
5
5
  SHA512:
6
- metadata.gz: dbbd77d09576ef3c372b9a1705407fa5fd4e3fdd5dddcbf553f7bc2ae0ede0d62e7837edeef83b097ecff0f94ec376c4b4ff9a9fe3008e5af2a24ce036c66b99
7
- data.tar.gz: e4508fee7a02185af499a5ff8bf03d7655c09d192a44458e1f394032e02e1f4b32cfc0f33b197e765fc9cbdce7e8c5e16154f8803f538dc764929be7fdb2d611
6
+ metadata.gz: bfef357e63871207431c4ba50c35a6b74eb4cf7575271e7320eac0b1371b2affec54df63f002dcac29f8f99bb36e491e0ddfa94e68064e5ff04ac0fcb54ffcdc
7
+ data.tar.gz: 7c8fc1638323131160fc2cac173276ce652f4861bae7f94c41005b494effd46b39a25da42372efa81db0ef9e036fb3758d2d0124f7981af24498ee88920fa786
@@ -11,17 +11,71 @@ module Vagrantomatic
11
11
 
12
12
  attr_accessor :config
13
13
 
14
+ def validate_config(fatal = true)
15
+ valid = true
16
+ if ! @config.has_key?("box")
17
+ valid = false
18
+ if fatal
19
+ raise "Node #{@name} must specify box"
20
+ end
21
+ end
22
+
23
+ valid
24
+ end
25
+
26
+
27
+ def fix_folders()
28
+ # can't use dig() might not be ruby 2.3
29
+ if @config.has_key?("folders")
30
+ @config["folders"] = Array(@config["folders"])
31
+
32
+ # all paths must be fully qualified. If we were asked to do a relative path, change
33
+ # it to the current directory since that's probably what the user wanted. Not right?
34
+ # user supply correct path!
35
+ @config["folders"] = @config["folders"].map { |folder|
36
+ if ! folder.start_with? '/'
37
+ folder = "#{Dir.pwd}/#{folder}"
38
+ end
39
+ folder
40
+ }
41
+ else
42
+ @config["folders"] = []
43
+ end
44
+ end
45
+
14
46
  def initialize(vagrant_vm_dir, name, logger: nil, config:nil)
15
47
  @name = name
16
48
  @vagrant_vm_dir = vagrant_vm_dir
17
- @logger = ::Vagrantomatic::Logger.new(logger).logger
49
+ @logger = Logger.new(logger).logger
50
+
51
+ # if we encounter conditions such as missing or damaged files we may need
52
+ # to force a save that would normally not be detected - eg if we load bad
53
+ # json it gets fixed up to an empty hash which would them be compared to a
54
+ # fresh read of the file (also results in empty hash) - so we must supply
55
+ # another way to force the save here
56
+ @force_save = false
18
57
 
19
58
  # use supplied config if present, otherwise load from file
20
59
  if config
21
60
  @config = config
61
+
62
+ # validate a user-supplied config now, at the point of insertion
63
+ # by this point we have a config either from file or supplied by user it
64
+ # must be valid for us to proceed!
65
+ @logger.debug "validating config for #{name}"
66
+ validate_config
67
+
68
+ # user may have specified relative folders at time of creation - if so
69
+ # we must now expand all paths in them and write them forever to config
70
+ # file
71
+ fix_folders
22
72
  else
23
73
  @config = configfile_hash
74
+
75
+ # this passed-in config could still be bad - we will validate it before
76
+ # use on either save() or get_vm()
24
77
  end
78
+
25
79
  @logger.debug "initialized vagrantomatic instance for #{name}"
26
80
  end
27
81
 
@@ -45,22 +99,47 @@ module Vagrantomatic
45
99
  json = File.read(configfile)
46
100
  config = JSON.parse(json)
47
101
  rescue Errno::ENOENT
48
- @logger.debug("#{configfile} does not exist")
102
+ # depending on whether the instance has been saved or not, we may not
103
+ # yet have a configfile - allow to proceed
104
+ @logger.debug "#{configfile} does not exist"
105
+ @force_save = true
49
106
  rescue JSON::ParserError
50
- @logger.error("JSON parser error in #{configfile}")
107
+ # swallow parse errors so that we can destroy and recreate automatically
108
+ @logger.debug "JSON parse error in #{configfile}"
109
+ @force_save = true
51
110
  end
52
111
  config
53
112
  end
54
113
 
55
114
  def configured?
56
- configured = false
57
- if Dir.exists? (vm_instance_dir) and File.exists?(configfile) and File.exists?(vagrantfile)
58
- configured = configfile_hash.has_key?("box")
115
+ configured = true
116
+
117
+ if ! Dir.exists? (vm_instance_dir)
118
+ @logger.debug "Vagrant instance directory #{vm_instance_dir} does not exist"
119
+ configured = false
120
+ end
121
+
122
+ if ! File.exists?(vagrantfile)
123
+ @logger.debug "#{VAGRANTFILE} not found at #{vagrantfile}"
124
+ configured = false
125
+ end
126
+
127
+ if ! File.exists?(configfile)
128
+ @logger.debug "#{VAGRANTFILE_JSON} not found at #{configfile}"
129
+ configured = false
59
130
  end
131
+
132
+ # check config hash is valid without causing a fatal error if its damaged
133
+ if ! validate_config(false)
134
+ configured = false
135
+ end
136
+
60
137
  configured
61
138
  end
62
139
 
63
140
  def save
141
+ @logger.debug("validating settings for save...")
142
+ validate_config
64
143
  @logger.debug("saving vm settings...")
65
144
  FileUtils.mkdir_p(vm_instance_dir)
66
145
  ensure_config
@@ -88,11 +167,17 @@ module Vagrantomatic
88
167
 
89
168
  def get_vm
90
169
  # Create an instance (represents a Vagrant **installation**)
91
- instance = Derelict.instance(::Vagrantomatic::Vagrantomatic::DEFAULT_VAGRANT_DIR)
92
- result = instance.execute('--version') # Derelict::Executer object (vagrant --version)
170
+ if ! in_sync?
171
+ raise "get_vm called for instance but it is not in_sync! (call save() first?)"
172
+ end
173
+
174
+ validate_config
175
+
176
+ vagrant_installation = Derelict.instance(Vagrantomatic::DEFAULT_VAGRANT_DIR)
177
+ result = vagrant_installation.execute('--version') # Derelict::Executer object (vagrant --version)
93
178
  if result.success?
94
179
  # vagrant present and working, connect to our vm INSTANCE
95
- vm = instance.connect(vm_instance_dir)
180
+ vm = vagrant_installation.connect(vm_instance_dir)
96
181
  else
97
182
  raise "Error connecting to vagrant! (vagrant --version failed)"
98
183
  end
@@ -111,7 +196,7 @@ module Vagrantomatic
111
196
  configured = false
112
197
  have_config = configfile_hash
113
198
 
114
- if have_config == @config
199
+ if (! @force_save) and (have_config == @config )
115
200
  configured = true
116
201
  end
117
202
 
@@ -10,13 +10,13 @@ module Vagrantomatic
10
10
 
11
11
  def initialize(vagrant_vm_dir: nil, logger: nil)
12
12
  @vagrant_vm_dir = vagrant_vm_dir || DEFAULT_VAGRANT_VM_DIR
13
- @logger = ::Vagrantomatic::Logger.new(logger).logger
13
+ @logger = Logger.new(logger).logger
14
14
  end
15
15
 
16
16
  # Return a has representing the named instance. This is suitable for Puppet
17
17
  # type and provider, or you can use the returned info for whatever you like
18
18
  def instance_metadata(name)
19
- instance = ::Vagrantomatic::Instance.new(@vagrant_vm_dir, name)
19
+ instance = instance(name)
20
20
  config = {}
21
21
  # annotate the raw config hash with data for puppet (and humans...)
22
22
  if instance.configured?
@@ -31,7 +31,7 @@ module Vagrantomatic
31
31
  config
32
32
  end
33
33
 
34
- # Return a has of instances
34
+ # Return a hash of all instances
35
35
  def instances_metadata()
36
36
  instance_wildcard = File.join(@vagrant_vm_dir, "*", ::Vagrantomatic::Instance::VAGRANTFILE)
37
37
  instances = {}
@@ -48,7 +48,7 @@ module Vagrantomatic
48
48
  end
49
49
 
50
50
  def instance(name)
51
- ::Vagrantomatic::Instance.new(@vagrant_vm_dir, name, logger: @logger)
51
+ Instance.new(@vagrant_vm_dir, name, logger: @logger)
52
52
  end
53
53
 
54
54
  end
@@ -1,3 +1,3 @@
1
1
  module Vagrantomatic
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrantomatic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoff Williams