Class: VagrantPlugins::Cienv::Action::BuildVagrantfile

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-qienv/action.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ BuildVagrantfile

Returns a new instance of BuildVagrantfile.



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
74
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
# File 'lib/vagrant-qienv/action.rb', line 38

def initialize(app, env)
  @app = app
  @env = env
  environment = @env[:env]

  # Only make all the magic if the .qi.yml definition file is found
  return if !File.exist?(project_home.join(".qi.yml"))

  require_relative "config/config_provider.rb"
  require_relative "config/config_provision.rb"
  require_relative "config/config_network.rb"
  require_relative "config/config_folders.rb"

  # $vagrant_vmenv_path = vagrant_home.to_s + "/provision-ci/"

  # load the .qi.yml file
  qi_file = File.expand_path (project_home.join(".qi.yml"))
  qi_definition = YAML.load(File.read(qi_file))

  # Copy enviroments and playbooks to home dir if needed
  FileUtils.mkdir(vagrant_home) if !File.exist?(vagrant_home)
  FileUtils.mkdir(vagrant_home.join('provision-ci')) if !File.exist?(vagrant_home.join('provision-ci'))
  FileUtils.cp_r(gem_path.join('envs'), vagrant_home.join('provision-ci/envs')) if !File.exist?(vagrant_home.join('provision-ci/envs'))
  FileUtils.cp_r(gem_path.join('provisioning'), vagrant_home.join('provision-ci/provisioning')) if !File.exist?(vagrant_home.join('provision-ci/provisioning'))

  # load the environment based on "env_runtime" variable of .qi.yml
  vagrant_env = qi_definition["env_runtime"] || "default"
  environment_file = File.expand_path(vagrant_home.to_s + "/provision-ci/envs", File.dirname(__FILE__)) +
                     File::SEPARATOR + vagrant_env
  if File.exists?(environment_file + ".json")
    environment_ci = JSON.parse(File.read(environment_file + ".json"))
  elsif File.exists?(environment_file + ".yml")
    environment_ci = YAML.load(File.read(environment_file + ".yml"))
  else
    raise "Environment_ci config file not found, see envs directory\n #{environment_file}"
  end

  # build the host list of the VMs used, very useful to allow the communication
  # between them based on the hostname and IP stored in the hosts file
  build_hosts_list(environment_ci["vms"])

  vagrantfile_proc = Proc.new do
    Vagrant.configure(2) do |config|

      environment_ci["vms"].each do |vm_id, vm_config|

        config.vm.define vm_id, autostart: vm_config["autostart"] do |instance|

          # Ansible handles this task better than Vagrant
          #instance.vm.hostname = vm_id

          config_provider(instance, vm_config, environment_ci["global"])

          config_provision(instance, vm_config, vm_id, qi_definition["apps"], vagrant_home.join('provision-ci').to_s)

          config_network(instance, vm_config)

          config_folders(instance, vm_id, qi_definition["apps"], vagrant_home.join('provision-ci').to_s)

        end
      end
    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)
  environment.instance_variable_set(:@local_data_path, vagrant_home) 

  # 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

Instance Method Details

#call(env) ⇒ Object



116
117
118
# File 'lib/vagrant-qienv/action.rb', line 116

def call(env)
  @app.call(env)
end

#gem_pathObject



25
26
27
# File 'lib/vagrant-qienv/action.rb', line 25

def gem_path
  Pathname.new(File.dirname __dir__)
end

#project_homeObject



29
30
31
32
# File 'lib/vagrant-qienv/action.rb', line 29

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

#vagrant_homeObject



34
35
36
# File 'lib/vagrant-qienv/action.rb', line 34

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