vagrantomatic 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/vagrantomatic/instance.rb +95 -10
- data/lib/vagrantomatic/vagrantomatic.rb +4 -4
- data/lib/vagrantomatic/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb2da2108c6e8b03cbc54d79c0f6461c31293d83
|
4
|
+
data.tar.gz: 649cce1ad226a8f2a6b5e0f5337da8c999937aa8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 =
|
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
|
-
|
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
|
-
|
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 =
|
57
|
-
|
58
|
-
|
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
|
-
|
92
|
-
|
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 =
|
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 =
|
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 =
|
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
|
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
|
-
|
51
|
+
Instance.new(@vagrant_vm_dir, name, logger: @logger)
|
52
52
|
end
|
53
53
|
|
54
54
|
end
|