Class: Vagrant::Node::ConfigManager

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-node/configmanager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spath_config_file) ⇒ ConfigManager

Returns a new instance of ConfigManager.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/vagrant-node/configmanager.rb', line 10

def initialize(spath_config_file)
  @config_file_path = spath_config_file
  config_loader = Config::Loader.new(Config::VERSIONS, Config::VERSIONS_ORDER)
  config_loader.set(:conffile, File.expand_path(@config_file_path))
  @config_global,@config_warnings, @config_errors = config_loader.load([:confile])

  config_content = File.read(@config_file_path)
  
  @config_sexp = RubyParser.new.parse(config_content)

  #If config file is in the default style convert it to a block one 
  convert_default_to_block
  
end

Instance Attribute Details

#config_errorsObject

Returns the value of attribute config_errors.



9
10
11
# File 'lib/vagrant-node/configmanager.rb', line 9

def config_errors
  @config_errors
end

#config_globalObject

Returns the value of attribute config_global.



9
10
11
# File 'lib/vagrant-node/configmanager.rb', line 9

def config_global
  @config_global
end

#config_sexpObject

Returns the value of attribute config_sexp.



9
10
11
# File 'lib/vagrant-node/configmanager.rb', line 9

def config_sexp
  @config_sexp
end

#config_warningsObject

Returns the value of attribute config_warnings.



9
10
11
# File 'lib/vagrant-node/configmanager.rb', line 9

def config_warnings
  @config_warnings
end

Instance Method Details

#config_contentObject

Ruby2Ruby modify the parameter, so a deep cloned copy is passed



298
299
300
301
302
303
304
# File 'lib/vagrant-node/configmanager.rb', line 298

def config_content        
  begin        
    Ruby2Ruby.new.process(@config_sexp.dclone)
  rescue => e
    raise RestException.new(406,"There was an error processing the config file, check if there is any error")   
  end
end

#delete_vm(vm_name) ⇒ Object

Raises:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/vagrant-node/configmanager.rb', line 75

def delete_vm (vm_name)        
  index=get_start_index
  
  

  

  if (@config_sexp[0]==:iter)
    nodo=@config_sexp
  else
    nodo=@config_sexp.find_nodes(:iter).first
  end
  

  if (!nodo[VM_INDEX_START].nil?)

    if (nodo[VM_INDEX_START].node_type == :iter)
      vm = nodo[VM_INDEX_START].find_nodes(:call)
      
      if (!vm.empty?)
        leaf= vm.first.find_nodes(:lit)
        
        if (!leaf.empty? && leaf.first.value === vm_name.to_sym)
          nodo.delete_at(VM_INDEX_START)
          
          save
          return true
        end
      end
    elsif (nodo[VM_INDEX_START].node_type == :block)
       nodo[VM_INDEX_START].each_sexp do |vm|            

        
          subnode=vm.find_nodes(:call)              
          if (!subnode.empty?)                  
            leaf=subnode.first.find_nodes(:lit)                  
            if (!leaf.empty? && leaf.first.value === vm_name.to_sym)
              
              nodo[VM_INDEX_START].delete(vm)
              save
              
              return true                    
            end
          end          
        
       end
    end
    
    
  end

  

  raise RestException.new(404,"Virtual Machine #{vm_name} not found")


end

#extract_vms_sexpObject

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vagrant-node/configmanager.rb', line 26

def extract_vms_sexp
  

  index=VM_INDEX_START

  if (@config_sexp[0]==:iter)          
    block=@config_sexp
  else
    
    gindex=0
    block=nil
    @config_sexp.each_sexp do |conf|           
       gindex=gindex+1
       if (conf.node_type == :iter)
         block = @config_sexp[gindex]
       end             
    end

  end
    
  


  raise RestException.new(406,
                         "Config File has no virtual machine configured") if (block.nil? ||
                                                                              block.empty? ||
                                                                              block[index].nil?)
       

       
   
  if block[index].node_type==:block
      
      if (block[index].find_nodes(:iter).empty?)
        #One machine with default style
        return default_to_block
        
      else
        #Several machines configured
        return block[index].find_nodes(:iter)
      end

  else   
      
      return [block[index]]
  end
        
end

#get_vm_namesObject

Raises:



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/vagrant-node/configmanager.rb', line 134

def get_vm_names
  
  index=get_start_index
  raise RestException.new(406,"No virtual machine configured") if @config_sexp[index].nil?

  names = []
  if @config_sexp[index].node_type==:block
    #Entorno con una maquina configurada fuera de bloque
    if (@config_sexp[index].find_nodes(:iter).empty?)
      names.push(:default)
    else
    #Entorno multi vm
      @config_sexp[index].each_sexp do |vm|
        vm.find_nodes(:call).first.find_nodes(:lit).each do |node|
          names.push(node.value)
        end
      end
    end

  else
  #Este caso se produce cuando hay unicamente
  #una maquina virtual configurada. En este caso
  #@config_sexp[VM_INDEX_START] contiene la definicion
  #de la maquina directamente
    node=@config_sexp[index].find_nodes(:call).first.find_node(:lit)
  names.push(node.value)

  end

  names
end

#insert_vms_sexp(vms_sexp) ⇒ Object

Raises:



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/vagrant-node/configmanager.rb', line 181

def insert_vms_sexp(vms_sexp)    
  
  raise RestException.new(406,"Invalid configuration file supplied") if vms_sexp.nil? || vms_sexp.empty?        
  index=VM_INDEX_START        
  


  if @config_sexp[0]==:iter          
    block=@config_sexp
  else          
    gindex=0
    block=nil
    @config_sexp.each_sexp do |conf|           
       gindex=gindex+1
       if (!conf.nil? && conf.node_type == :iter)
         block = @config_sexp[gindex]
       end             
       
    end
  end

  
  
  

  if !block[VM_INDEX_START].nil?          
    # If @config_sexp[VM_INDEX_START] isn't nil could mean three things:
    #  -- There is a machine configured with a default style
    #  -- There are some machines inserted inside a block 
    #  -- There is only one machine and it is stored at block[VM_INDEX_START]
    if block[VM_INDEX_START].node_type==:block
      # If node is a block it could be the first two options
      if (block[VM_INDEX_START].find_nodes(:iter).empty?)
        # This case match the first option, so the steps to perform are the following:
        # -- Create a block node 
          new_block = s(:block)
        # -- Convert the current machine to a block style and insert into the block               
          new_block.add(default_to_block)
        # -- Insert the new vms              
          new_block.add(vms_sexp)                
          
          
        @config_sexp.delete_at(index)              
        
        @config_sexp[index] = new_block
        
         
      else              
        # This case means that there are some machines inserted inside a block
        # we only have to add thems 
        #FIXME FALTA COMPREOBAR SI HACE FALTA RENOMBRAR              
        if @config_sexp[0]==:iter
          @config_sexp[VM_INDEX_START].add(vms_sexp) 
        elsif @config_sexp[0]==:block
           @config_sexp[gindex][VM_INDEX_START].add(vms_sexp)
        end
        
      end

    else           
      

      #There is only one machine stored, we can store it at @config_sexp[index]            
      new_block = s(:block)

      result = extract_vms_sexp
      

      if (@config_sexp[0]==:block)
        #SI el elemento 0 es un bloque

        new_block.add(result)              
        new_block.add(vms_sexp)  
        

        block.delete_at(VM_INDEX_START)
        @config_sexp[gindex][VM_INDEX_START]=new_block

      else
        new_block.add(extract_vms_sexp)
        new_block.add(vms_sexp)
        @config_sexp.delete_at(index)
        

        @config_sexp[index] = new_block
      end            
      
    end
  else
      #If @config_sexp[VM_INDEX_START] is nil means that there isn't any
      #machine configured
      #In order to proceed correctly we have to check if there are one or more
      #vms to be inserted:
      #  -- If there is only one machine to be inserted is must be stored in
      #  @config_sexp[VM_INDEX_START] directly
      #  -- If there are two ore more vms to be inserted they have to be inserted inside 
      #  a block in @config_sexp[VM_INDEX_START]
      
      
      if (vms_sexp.length>1)              
        new_block = s(:block)
        new_block.add(vms_sexp)            
        @config_sexp[index] = new_block
      else              
        @config_sexp[index] = vms_sexp.first                                          
      end
      
  end
  
  # Saving the result to disk
  save

  true

end

#rename_vm(old_name, new_name) ⇒ Object

FIXME REVISAR PORQUE CUANDO QUEDA UNA ÚNICA máquina en un entorno multi vm el fichero cambia



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/vagrant-node/configmanager.rb', line 169

def rename_vm(old_name,new_name)
  
  machines = extract_vms_sexp 
    
  machines.each do |machine|
    machine.find_nodes(:call).first.find_nodes(:lit).each do |node|
      node[1] = new_name.to_sym if (node.value == old_name)
    end
  end  
  
end

#saveObject



306
307
308
309
310
311
312
313
314
# File 'lib/vagrant-node/configmanager.rb', line 306

def save        
  #Processing the content first. If there is any error
  #the file wont'be modified
  content= config_content
  f = File.open(@config_file_path, "w")        
  f.write(content)
  f.write("\n")
  f.close
end