Class: VagrantPlugins::GPIICi::Action::BuildVagrantfile

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-gpii-ci/action/build_vagrantfile.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ BuildVagrantfile

Returns a new instance of BuildVagrantfile.



8
9
10
# File 'lib/vagrant-gpii-ci/action/build_vagrantfile.rb', line 8

def initialize(app, env)
  @app = app
end

Instance Method Details

#build_vms_config(vagrant_config, ci_environment_vms) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/vagrant-gpii-ci/action/build_vagrantfile.rb', line 156

def build_vms_config(vagrant_config, ci_environment_vms)
  ci_environment_vms.each do |ci_vm_id, ci_vm_definition|

    ci_autostart = true
    ci_autostart = ci_vm_definition["autostart"] if ci_vm_definition.key?("autostart")

    vagrant_config.vm.define ci_vm_id, autostart: ci_autostart do |vm_instance|
          vm_instance.vm.hostname = ci_vm_id
          set_provider_config(vm_instance, ci_vm_definition)
          set_network_config(vm_instance, ci_vm_definition)
    end
  end
end

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vagrant-gpii-ci/action/build_vagrantfile.rb', line 12

def call(env)
  @env = env

  # The file .gpii-ci.yml can be override using this environment variable
  ci_file ||= ENV["VAGRANT_CI_FILE"] if ENV.key?("VAGRANT_CI_FILE")
  ci_file ||= ".vagrant.yml"

  # Only if the ci_file is found the plugin will run
  if File.exist?(project_home(@env).join(ci_file)) 
  
    environment = @env[:env]
    ci_definition = get_ci_definition (ci_file)
    environment.instance_variable_set(:@ci_tests, get_ci_tests(ci_definition))
    vagrantfile_proc = Proc.new do
      Vagrant.configure(2) do |config|
        build_vms_config(config, get_ci_environment(ci_definition))
      end 
    end

    # The Environment instance has been instantiated without a Vagrantfile
    # that means that we need to store some internal variables and 
    # instantiate again the Vagrantfile instance with our previous code.
    environment.instance_variable_set(:@root_path, project_home(@env))
    environment.instance_variable_set(:@local_data_path, vagrant_home(@env))
    # the cienv code will be the first item to check in the list of
    # Vagrantfile sources
    config_loader = environment.config_loader
    config_loader.set(:cienv, vagrantfile_proc.call)
    environment.instance_variable_set(:@vagrantfile, Vagrant::Vagrantfile.new(config_loader, [:cienv, :home, :root]))

  end
  @app.call(env)
end

#get_ci_definition(definition_file) ⇒ Object



89
90
91
92
93
# File 'lib/vagrant-gpii-ci/action/build_vagrantfile.rb', line 89

def get_ci_definition(definition_file)
  # load the definition file
  ci_file = File.expand_path(project_home(@env).join(definition_file))
  @ci_definition = YAML.load(File.read(ci_file))
end

#get_ci_environment(definition) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/vagrant-gpii-ci/action/build_vagrantfile.rb', line 106

def get_ci_environment(definition)
  if not definition.include?("env")
    return {}
  end
  ci_environment_vms = inject_private_network_config(definition["env"]["vms"])
  #TODO: load additional yaml files to extend the definition of the vms
end

#get_ci_tests(definition) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vagrant-gpii-ci/action/build_vagrantfile.rb', line 55

def get_ci_tests(definition)
  ci_tests = {}
  if not definition.include?("env")
    puts "WARNING: env not declared in the definition file"
    return ci_tests
  elsif not definition.include?("stages")
    puts "WARNING: stages not declared in the definition file"
    return ci_tests
  end
  definition["env"]["vms"].each do | vmname, vmdetails |

    ci_tests["#{vmname}"] = {}
    ci_tests["#{vmname}"]["shell"] = {}
    definition["stages"].each do |stage|
      definition.each do |stagename, stagecontent|
        # Ignore the statements that start with a dot
        next if stagename.start_with?(".") or stagename.eql?("stages") or not stagecontent["stage"].eql?(stage)
        # Build the hash of tests for each VM
        if stagecontent.include?("stage")
          if vmdetails.include?("tags") and stagecontent.include?("tags")
            ci_tests["#{vmname}"]["shell"][stagecontent["stage"]] = stagecontent["script"] \
              if not (stagecontent["tags"] & vmdetails["tags"]).empty?
          elsif not stagecontent.include?("tags")
            ci_tests["#{vmname}"]["shell"][stagecontent["stage"]] = stagecontent["script"]
          else
            next
          end  
        end
      end
    end
  end
  ci_tests
end

#inject_private_network_config(ci_environment_vms) ⇒ Object

In the case of a multiVM environment, we need to connect the VMs using a private network



97
98
99
100
101
102
103
104
105
# File 'lib/vagrant-gpii-ci/action/build_vagrantfile.rb', line 97

def inject_private_network_config(ci_environment_vms)
  return ci_environment_vms if ci_environment_vms.count() == 1
  int_id = 10
  ci_environment_vms.each do |vm,config|
    ci_environment_vms[vm]["private_ip"] = "192.168.50." + int_id.to_s
    int_id += 1
  end
  ci_environment_vms
end

#project_home(env) ⇒ Object



50
51
52
53
# File 'lib/vagrant-gpii-ci/action/build_vagrantfile.rb', line 50

def project_home(env)
  environment = env[:env]
  environment.instance_variable_get(:@cwd)  
end

#set_network_config(vm_instance, ci_vm_definition) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/vagrant-gpii-ci/action/build_vagrantfile.rb', line 145

def set_network_config(vm_instance, ci_vm_definition)
  vm_instance.vm.network :private_network, ip: ci_vm_definition["private_ip"] if ci_vm_definition["private_ip"]
  ci_vm_definition["mapped_ports"].each do |port|
    vm_instance.vm.network "forwarded_port",
    guest: port,
    host: port,
    protocol: "tcp",
    auto_correct: true
  end if ci_vm_definition["mapped_ports"]
end

#set_provider_config(vm_instance, ci_vm_definition) ⇒ Object

Setup the provider using the definition of each VM.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/vagrant-gpii-ci/action/build_vagrantfile.rb', line 115

def set_provider_config(vm_instance, ci_vm_definition)
      
  vm_instance.vm.provider :virtualbox do |vm|
      
    vm.linked_clone = ci_vm_definition["clone"] || false
    
    vm.customize ["modifyvm", :id, "--memory", ci_vm_definition["memory"] ]
    vm.customize ["modifyvm", :id, "--cpus", ci_vm_definition["cpu"] ]

    vm.customize ["modifyvm", :id, "--vram", "256"]
    if ci_vm_definition["3d"] == true then
      vm.customize ["modifyvm", :id, "--accelerate3d", "on"]
    else
      vm.customize ["modifyvm", :id, "--accelerate3d", "off"]
    end
    
    if ci_vm_definition["sound"] == true then
      vm.customize ["modifyvm", :id, "--audio", "null", "--audiocontroller", "hda"]
    end
      
    vm.customize ["modifyvm", :id, "--ioapic", "on"]
    vm.customize ["setextradata", "global", "GUI/SuppressMessages", "all"]
      
    vm.gui = ci_vm_definition["gui"] || true
  end
      
  vm_instance.vm.box = ci_vm_definition["box"]

end

#vagrant_home(env) ⇒ Object



46
47
48
# File 'lib/vagrant-gpii-ci/action/build_vagrantfile.rb', line 46

def vagrant_home(env)
  project_home(env).join(Vagrant::Environment::DEFAULT_LOCAL_DATA)
end