Top Level Namespace

Defined Under Namespace

Modules: VagrantPlugins

Instance Method Summary collapse

Instance Method Details

#build_hosts_list(env_vms) ⇒ Object

Builds a file with the list of pairs IP address: hostname which will be read by Ansible to setup the hosts file in each VM Params: env_vms: object with the definition of the virtual machines



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vagrant-qienv/config/config_network.rb', line 9

def build_hosts_list(env_vms)

  int_id = 10

  first = true
  env_vms.each do |vm, vmconfig|
    vmconfig["networks"].each do |name, netcfg|
      if netcfg["type"] == "private" then
        if netcfg['ip'].nil? then
          netcfg['ip'] = "192.168.50." + int_id.to_s
          #add the default IP to the environment definnition
          env_vms[vm]["networks"][name]["ip"] = "192.168.50." + int_id.to_s
          int_id += 1
        end
        if first then
          $base_vars = "vms_hosts={"
          $base_vars << "\"#{netcfg['ip']}\":\"#{vm}\""
          first = false
        elsif
          $base_vars << ",\"#{netcfg['ip']}\":\"#{vm}\""
        end
      end
    end if vmconfig["networks"]
  end
  $base_vars << "}" if $base_vars
end

#config_folders(instance, vm_id, apps, vagrant_ci_path) ⇒ Object

-*- mode: ruby -*- vi: set ft=ruby :



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/vagrant-qienv/config/config_folders.rb', line 4

def config_folders(instance, vm_id, apps, vagrant_ci_path)

  instance.vm.synced_folder ".", "/vagrant" # Always needed by the provisioning
  instance.vm.synced_folder vagrant_ci_path + "/provisioning", "/provisioning" # Always needed by the provisioning

  # Share folder if the "src" variable is set
  apps.each do |config|
    if config["run_in"] == vm_id and config["folder"] and config["folder"]["src"] then 
      instance.vm.synced_folder config["folder"]["src"], config["folder"]["dest"]
    end
  end if apps

end

#config_network(instance, vm_config) ⇒ Object

Configure the network section of a virtual machine. You can set the type of the network, the ip address if needed and a list of forwarded ports. Params: instance: The instance of the VM to configure vm_config: the definition of the VM. There are to type of networks: private

  • where the IP setting is available, and public - where virtualbox will set

the interface as an external NIC.



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
# File 'lib/vagrant-qienv/config/config_network.rb', line 44

def config_network(instance, vm_config)

  vm_config["networks"].each do |network, config|
    if config["type"] == "private" then
      if config["ip"] then
        instance.vm.network :private_network, ip: config["ip"]
      end
    elsif config["type"] == "public" then
      instance.vm.network :public_network
    end
  end if vm_config["networks"]

  vm_config["ports"].each do |port, config|

    raise "At least the guest port is needed in 'guest_port' variable" \
      if config["guest_port"].nil?

    instance.vm.network "forwarded_port",
      guest: config["guest_port"],
      host: config["host_port"] || config["guest_port"],
      protocol: config["protocol"] || "tcp",
      auto_correct: config["auto_correct"] || true
  end if vm_config["ports"]

end

#config_provider(instance, vm_config, global_config) ⇒ Object

-*- mode: ruby -*- vi: set ft=ruby :



4
5
6
7
8
9
10
11
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
# File 'lib/vagrant-qienv/config/config_provider.rb', line 4

def config_provider(instance, vm_config, global_config)

  if global_config["provider"] == "virtualbox" then 

    instance.vm.provider :virtualbox do |vm|

      vm.linked_clone = vm_config["clone"] || global_config["clone"] || false
      
      vm.customize ["modifyvm", :id, "--memory", vm_config["memory"] || global_config["memory"] ]
      vm.customize ["modifyvm", :id, "--cpus", vm_config["cpu"] || global_config["cpu"] ]

      if vm_config["gui"] == true then
        vm.customize ["modifyvm", :id, "--vram", "128"]
        vm.customize ["modifyvm", :id, "--accelerate3d", "on"]
      end
      
      if vm_config["sound"] == true then
        vm.customize ["modifyvm", :id, "--audio", "null", "--audiocontroller", "ac97"]
      end

      vm.customize ["modifyvm", :id, "--ioapic", "on"]
      vm.customize ["setextradata", "global", "GUI/SuppressMessages", "all"]

      vm.gui = vm_config["gui"] || global_config["gui"] || false
    end

    instance.vm.box = vm_config["box"]

  elsif global_config["provider"] == "libvirt" then
      raise "Not supported yet"
  else
      raise "Provider not defined in global configuration" unless global_config.include?('provider')
      raise "Provider #{global_config['provider']} not supported"
  end

end

#config_provision(instance, vm_config, vm_id, apps, vagrant_ci_path) ⇒ Object



6
7
8
9
10
11
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
45
46
47
48
49
50
# File 'lib/vagrant-qienv/config/config_provision.rb', line 6

def config_provision(instance, vm_config, vm_id, apps, vagrant_ci_path)


  apps.each do |config|
    if config["run_in"] == vm_id then
      if ARGV[0] == "up" then
        app = config["app_name"] #app_name
        stack = config["software_stack"]

        #check if every file is in place
        raise "File vagrant/provisioning/#{stack}-requirements.yml doesn't exist"\
          unless File.file?(vagrant_ci_path + "/provisioning/#{stack}-requirements.yml")
        raise "File vagrant/provisioning/#{stack}-vars.yml doesn't exist"\
          unless File.file?(vagrant_ci_path + "/provisioning/#{stack}-vagrant-vars.yml")
        raise "File vagrant/provisioning/#{stack}-playbook.yml doesn't exist"\
          unless File.file?(vagrant_ci_path + "/provisioning/#{stack}-playbook.yml")
        raise "File vagrant/provisioning/base-playbook.yml doesn't exist"\
          unless File.file?(vagrant_ci_path + "/provisioning/base-playbook.yml")

        qi_vars = JSON.dump(YAML::load(config.to_yaml))

        basecmd = "sudo PYTHONUNBUFFERED=1 VM_HOSTNAME=#{vm_id} "\
                  "ansible-playbook --extra-vars='#$base_vars' /provisioning/base-playbook.yml"
        instance.vm.provision "shell", inline: basecmd

        cmds = \
        "sudo ansible-galaxy install -fr /provisioning/#{stack}-requirements.yml \n"\
        "sudo VARS_FILE=#{stack}-vagrant-vars.yml PYTHONUNBUFFERED=1 \\\n"
        if config["deploy"] then
          cmds << "ansible-playbook --tags='install,configure,deploy' --extra-vars='#{qi_vars}' /provisioning/#{stack}-playbook.yml"
        else
          cmds << "ansible-playbook --tags='install,configure' --extra-vars='#{qi_vars}' /provisioning/#{stack}-playbook.yml"
        end
        instance.vm.provision "shell", inline: cmds

      elsif ARGV[0] == "provision" and config["test_cmds"] and config["folder"] and config["folder"]["dest"] then
        test_cmds = "cd #{config['folder']['dest']} \n"
        config["test_cmds"].each do |cmd|
          test_cmds << "DISPLAY=:0 " + cmd + "\n"
        end unless config["test_cmds"].nil?
        instance.vm.provision "shell", privileged: false, inline: test_cmds
      end
    end
  end if apps
end