vagrant-qienv 0.0.1

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.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +4 -0
  5. data/CODE_OF_CONDUCT.md +49 -0
  6. data/Gemfile +10 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +94 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/lib/envs/default.json +20 -0
  13. data/lib/envs/linux-desktop.json +22 -0
  14. data/lib/envs/production.yml +33 -0
  15. data/lib/provisioning/base-playbook.yml +16 -0
  16. data/lib/provisioning/couchdb-playbook.yml +11 -0
  17. data/lib/provisioning/couchdb-requirements.yml +5 -0
  18. data/lib/provisioning/couchdb-vagrant-vars.yml +1 -0
  19. data/lib/provisioning/gpii-linux-playbook.yml +11 -0
  20. data/lib/provisioning/gpii-linux-requirements.yml +6 -0
  21. data/lib/provisioning/gpii-linux-vagrant-vars.yml +23 -0
  22. data/lib/provisioning/nodejs-playbook.yml +12 -0
  23. data/lib/provisioning/nodejs-requirements.yml +7 -0
  24. data/lib/provisioning/nodejs-vagrant-vars.yml +11 -0
  25. data/lib/provisioning/preferences-playbook.yml +13 -0
  26. data/lib/provisioning/preferences-requirements.yml +9 -0
  27. data/lib/provisioning/preferences-vagrant-vars.yml +5 -0
  28. data/lib/vagrant-qienv.rb +18 -0
  29. data/lib/vagrant-qienv/action.rb +122 -0
  30. data/lib/vagrant-qienv/action/init_environment.rb +24 -0
  31. data/lib/vagrant-qienv/command/base.rb +19 -0
  32. data/lib/vagrant-qienv/command/init.rb +28 -0
  33. data/lib/vagrant-qienv/command/test.rb +65 -0
  34. data/lib/vagrant-qienv/config/config_folders.rb +17 -0
  35. data/lib/vagrant-qienv/config/config_network.rb +69 -0
  36. data/lib/vagrant-qienv/config/config_provider.rb +40 -0
  37. data/lib/vagrant-qienv/config/config_provision.rb +50 -0
  38. data/lib/vagrant-qienv/plugin.rb +59 -0
  39. data/lib/vagrant-qienv/version.rb +5 -0
  40. data/qi.yml.template +56 -0
  41. data/samples/README.md +12 -0
  42. data/samples/gpii-linux-production/.qi.yml +41 -0
  43. data/samples/gpii-linux/.qi.yml +22 -0
  44. data/samples/gpii-nexus/.qi.yml +24 -0
  45. data/samples/gpii-universal/.qi.yml +27 -0
  46. data/spec/spec_helper.rb +2 -0
  47. data/spec/vagrant/cienv_spec.rb +11 -0
  48. data/vagrant-cienv.gemspec +33 -0
  49. metadata +137 -0
@@ -0,0 +1,13 @@
1
+ ---
2
+
3
+ - hosts: localhost
4
+ become: true
5
+
6
+ vars_files:
7
+ - "{{ lookup('env', 'VARS_FILE') }}"
8
+
9
+ roles:
10
+ - facts
11
+ - nodejs
12
+ - preferences
13
+
@@ -0,0 +1,9 @@
1
+ - src: http://github.com/idi-ops/ansible-facts.git
2
+ name: facts
3
+
4
+ - src: https://github.com/idi-ops/ansible-nodejs.git
5
+ name: nodejs
6
+
7
+ - src: https://github.com/gpii-ops/ansible-preferences-server.git
8
+ name: preferences
9
+
@@ -0,0 +1,5 @@
1
+ ---
2
+
3
+ nodejs_app_install_dir: "{{ folder['dest'] }}"
4
+ nodejs_app_git_repo: https://github.com/GPII/universal.git
5
+ nodejs_app_git_branch: "{{ git_branch | default('master') }}"
@@ -0,0 +1,18 @@
1
+ require "pathname"
2
+
3
+ require "vagrant-qienv/plugin"
4
+
5
+ module VagrantPlugins
6
+ module Cienv
7
+ lib_path = Pathname.new(File.expand_path("../vagrant-qienv", __FILE__))
8
+ autoload :Action, lib_path.join("action")
9
+ autoload :Errors, lib_path.join("errors")
10
+
11
+ # This returns the path to the source of this plugin.
12
+ #
13
+ # @return [Pathname]
14
+ def self.source_root
15
+ @source_root ||= Pathname.new(File.expand_path("../../", __FILE__))
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,122 @@
1
+ require "pathname"
2
+ require 'json'
3
+ require 'yaml'
4
+
5
+ require "vagrant/action/builder"
6
+
7
+ module VagrantPlugins
8
+ module Cienv
9
+ module Action
10
+ # Include the built-in modules so we can use them as top-level things.
11
+ include Vagrant::Action::Builtin
12
+
13
+ def self.action_init
14
+ Vagrant::Action::Builder.new.tap do |b|
15
+ b.use InitEnvironment
16
+ end
17
+ end
18
+
19
+ # The autoload farm
20
+ action_root = Pathname.new(File.expand_path("../action", __FILE__))
21
+ autoload :InitEnvironment, action_root.join("init_environment")
22
+
23
+ class BuildVagrantfile
24
+
25
+ def gem_path
26
+ Pathname.new(File.dirname __dir__)
27
+ end
28
+
29
+ def project_home
30
+ environment = @env[:env]
31
+ environment.instance_variable_get(:@cwd)
32
+ end
33
+
34
+ def vagrant_home
35
+ project_home.join(Vagrant::Environment::DEFAULT_LOCAL_DATA)
36
+ end
37
+
38
+ def initialize(app, env)
39
+ @app = app
40
+ @env = env
41
+ environment = @env[:env]
42
+
43
+ # Only make all the magic if the .qi.yml definition file is found
44
+ return if !File.exist?(project_home.join(".qi.yml"))
45
+
46
+ require_relative "config/config_provider.rb"
47
+ require_relative "config/config_provision.rb"
48
+ require_relative "config/config_network.rb"
49
+ require_relative "config/config_folders.rb"
50
+
51
+ # $vagrant_vmenv_path = vagrant_home.to_s + "/provision-ci/"
52
+
53
+ # load the .qi.yml file
54
+ qi_file = File.expand_path (project_home.join(".qi.yml"))
55
+ qi_definition = YAML.load(File.read(qi_file))
56
+
57
+ # Copy enviroments and playbooks to home dir if needed
58
+ FileUtils.mkdir(vagrant_home) if !File.exist?(vagrant_home)
59
+ FileUtils.mkdir(vagrant_home.join('provision-ci')) if !File.exist?(vagrant_home.join('provision-ci'))
60
+ FileUtils.cp_r(gem_path.join('envs'), vagrant_home.join('provision-ci/envs')) if !File.exist?(vagrant_home.join('provision-ci/envs'))
61
+ FileUtils.cp_r(gem_path.join('provisioning'), vagrant_home.join('provision-ci/provisioning')) if !File.exist?(vagrant_home.join('provision-ci/provisioning'))
62
+
63
+ # load the environment based on "env_runtime" variable of .qi.yml
64
+ vagrant_env = qi_definition["env_runtime"] || "default"
65
+ environment_file = File.expand_path(vagrant_home.to_s + "/provision-ci/envs", File.dirname(__FILE__)) +
66
+ File::SEPARATOR + vagrant_env
67
+ if File.exists?(environment_file + ".json")
68
+ environment_ci = JSON.parse(File.read(environment_file + ".json"))
69
+ elsif File.exists?(environment_file + ".yml")
70
+ environment_ci = YAML.load(File.read(environment_file + ".yml"))
71
+ else
72
+ raise "Environment_ci config file not found, see envs directory\n #{environment_file}"
73
+ end
74
+
75
+ # build the host list of the VMs used, very useful to allow the communication
76
+ # between them based on the hostname and IP stored in the hosts file
77
+ build_hosts_list(environment_ci["vms"])
78
+
79
+ vagrantfile_proc = Proc.new do
80
+ Vagrant.configure(2) do |config|
81
+
82
+ environment_ci["vms"].each do |vm_id, vm_config|
83
+
84
+ config.vm.define vm_id, autostart: vm_config["autostart"] do |instance|
85
+
86
+ # Ansible handles this task better than Vagrant
87
+ #instance.vm.hostname = vm_id
88
+
89
+ config_provider(instance, vm_config, environment_ci["global"])
90
+
91
+ config_provision(instance, vm_config, vm_id, qi_definition["apps"], vagrant_home.join('provision-ci').to_s)
92
+
93
+ config_network(instance, vm_config)
94
+
95
+ config_folders(instance, vm_id, qi_definition["apps"], vagrant_home.join('provision-ci').to_s)
96
+
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ # The Environment instance has been instantiated without a Vagrantfile
103
+ # that means that we need to store some internal variables and
104
+ # instantiate again the Vagrantfile instance with our previous code.
105
+
106
+ environment.instance_variable_set(:@root_path, project_home)
107
+ environment.instance_variable_set(:@local_data_path, vagrant_home)
108
+
109
+ # the cienv code will be the first item to check in the list of
110
+ # Vagrantfile sources
111
+ config_loader = environment.config_loader
112
+ config_loader.set(:cienv, vagrantfile_proc.call)
113
+ environment.instance_variable_set(:@vagrantfile, Vagrant::Vagrantfile.new(config_loader, [:cienv, :home, :root]))
114
+
115
+ end
116
+ def call(env)
117
+ @app.call(env)
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,24 @@
1
+ require "log4r"
2
+
3
+ module VagrantPlugins
4
+ module Cienv
5
+ module Action
6
+
7
+ # This action sets up the QI QI environment
8
+ class InitEnvironment
9
+
10
+ def initialize(app, env)
11
+ @app = app
12
+ @logger = Log4r::Logger.new("vagrant_qi::action::init_environment")
13
+ end
14
+
15
+ def call(env)
16
+ puts "i am inside class InitEnvironment"
17
+ @logger.info('i am inside class InitEnvironment - call()')
18
+ @app.call(env)
19
+ end
20
+
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ require "vagrant/plugin/state_file"
2
+
3
+ module VagrantPlugins
4
+ module Cienv
5
+ module Command
6
+ class Base < Vagrant.plugin("2", :command)
7
+ # This is a helper for executing an action sequence with the proper
8
+ # environment hash setup so that the plugin specific helpers are
9
+ # in.
10
+ #
11
+ # @param [Object] callable the Middleware callable
12
+ # @param [Hash] env Extra environment hash that is merged in.
13
+ def action(callable, env=nil)
14
+ @env.action_runner.run(callable, env)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ require 'optparse'
2
+
3
+ require_relative "base"
4
+
5
+ module VagrantPlugins
6
+ module Cienv
7
+ module Command
8
+ class InitEnvironment < Base
9
+ def execute
10
+ opts = OptionParser.new do |o|
11
+ o.banner = "Usage: vagrant qi init [-h]"
12
+ end
13
+
14
+ # Parse the options
15
+ argv = parse_options(opts)
16
+ return if !argv
17
+ raise Vagrant::Errors::CLIInvalidUsage, help: opts.help.chomp if argv.length > 0
18
+
19
+ # List the installed plugins
20
+ action(Action.action_init)
21
+
22
+ # Success, exit status 0
23
+ 0
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,65 @@
1
+ require 'optparse'
2
+
3
+ module VagrantPlugins
4
+ module Cienv
5
+ module Command
6
+ class Root < Vagrant.plugin("2", :command)
7
+ def self.synopsis
8
+ "Manages QI environments"
9
+ end
10
+
11
+ def initialize(argv, env)
12
+ super
13
+
14
+ @main_args, @sub_command, @sub_args = split_main_and_subcommand(argv)
15
+
16
+ @subcommands = Vagrant::Registry.new
17
+ @subcommands.register(:init) do
18
+ require_relative "init"
19
+ InitEnvironment
20
+ end
21
+ end
22
+
23
+ def execute
24
+ if @main_args.include?("-h") || @main_args.include?("--help")
25
+ # Print the help for all the sub-commands.
26
+ puts "Help not completed yet"
27
+ return help
28
+ end
29
+
30
+ # If we reached this far then we must have a subcommand. If not,
31
+ # then we also just print the help and exit.
32
+ command_class = @subcommands.get(@sub_command.to_sym) if @sub_command
33
+ return help if !command_class || !@sub_command
34
+ @logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}")
35
+
36
+ # Initialize and execute the command class
37
+ command_class.new(@sub_args, @env).execute
38
+ end
39
+
40
+ # Prints the help out for this command
41
+ def help
42
+ opts = OptionParser.new do |o|
43
+ o.banner = "Usage: vagrant test [<args>]"
44
+ o.separator ""
45
+ o.separator "Available subcommands:"
46
+
47
+ # Add the available subcommands as separators in order to print them
48
+ # out as well.
49
+ keys = []
50
+ @subcommands.each { |key, value| keys << key.to_s }
51
+
52
+ keys.sort.each do |key|
53
+ o.separator " #{key}"
54
+ end
55
+
56
+ o.separator ""
57
+ o.separator "For help on any individual command run `vagrant test -h`"
58
+ end
59
+
60
+ @env.ui.info(opts.help, prefix: false)
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,17 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ def config_folders(instance, vm_id, apps, vagrant_ci_path)
5
+
6
+ instance.vm.synced_folder ".", "/vagrant" # Always needed by the provisioning
7
+ instance.vm.synced_folder vagrant_ci_path + "/provisioning", "/provisioning" # Always needed by the provisioning
8
+
9
+ # Share folder if the "src" variable is set
10
+ apps.each do |config|
11
+ if config["run_in"] == vm_id and config["folder"] and config["folder"]["src"] then
12
+ instance.vm.synced_folder config["folder"]["src"], config["folder"]["dest"]
13
+ end
14
+ end if apps
15
+
16
+ end
17
+
@@ -0,0 +1,69 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ # Builds a file with the list of pairs IP address: hostname which will be read
5
+ # by Ansible to setup the hosts file in each VM
6
+ # Params:
7
+ # +env_vms+: object with the definition of the virtual machines
8
+
9
+ def build_hosts_list(env_vms)
10
+
11
+ int_id = 10
12
+
13
+ first = true
14
+ env_vms.each do |vm, vmconfig|
15
+ vmconfig["networks"].each do |name, netcfg|
16
+ if netcfg["type"] == "private" then
17
+ if netcfg['ip'].nil? then
18
+ netcfg['ip'] = "192.168.50." + int_id.to_s
19
+ #add the default IP to the environment definnition
20
+ env_vms[vm]["networks"][name]["ip"] = "192.168.50." + int_id.to_s
21
+ int_id += 1
22
+ end
23
+ if first then
24
+ $base_vars = "vms_hosts={"
25
+ $base_vars << "\"#{netcfg['ip']}\":\"#{vm}\""
26
+ first = false
27
+ elsif
28
+ $base_vars << ",\"#{netcfg['ip']}\":\"#{vm}\""
29
+ end
30
+ end
31
+ end if vmconfig["networks"]
32
+ end
33
+ $base_vars << "}" if $base_vars
34
+ end
35
+
36
+ # Configure the network section of a virtual machine. You can set the type of
37
+ # the network, the ip address if needed and a list of forwarded ports.
38
+ # Params:
39
+ # +instance+: The instance of the VM to configure
40
+ # +vm_config+: the definition of the VM. There are to type of networks: private
41
+ # - where the IP setting is available, and public - where virtualbox will set
42
+ # the interface as an external NIC.
43
+
44
+ def config_network(instance, vm_config)
45
+
46
+ vm_config["networks"].each do |network, config|
47
+ if config["type"] == "private" then
48
+ if config["ip"] then
49
+ instance.vm.network :private_network, ip: config["ip"]
50
+ end
51
+ elsif config["type"] == "public" then
52
+ instance.vm.network :public_network
53
+ end
54
+ end if vm_config["networks"]
55
+
56
+ vm_config["ports"].each do |port, config|
57
+
58
+ raise "At least the guest port is needed in 'guest_port' variable" \
59
+ if config["guest_port"].nil?
60
+
61
+ instance.vm.network "forwarded_port",
62
+ guest: config["guest_port"],
63
+ host: config["host_port"] || config["guest_port"],
64
+ protocol: config["protocol"] || "tcp",
65
+ auto_correct: config["auto_correct"] || true
66
+ end if vm_config["ports"]
67
+
68
+ end
69
+
@@ -0,0 +1,40 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ def config_provider(instance, vm_config, global_config)
5
+
6
+ if global_config["provider"] == "virtualbox" then
7
+
8
+ instance.vm.provider :virtualbox do |vm|
9
+
10
+ vm.linked_clone = vm_config["clone"] || global_config["clone"] || false
11
+
12
+ vm.customize ["modifyvm", :id, "--memory", vm_config["memory"] || global_config["memory"] ]
13
+ vm.customize ["modifyvm", :id, "--cpus", vm_config["cpu"] || global_config["cpu"] ]
14
+
15
+ if vm_config["gui"] == true then
16
+ vm.customize ["modifyvm", :id, "--vram", "128"]
17
+ vm.customize ["modifyvm", :id, "--accelerate3d", "on"]
18
+ end
19
+
20
+ if vm_config["sound"] == true then
21
+ vm.customize ["modifyvm", :id, "--audio", "null", "--audiocontroller", "ac97"]
22
+ end
23
+
24
+ vm.customize ["modifyvm", :id, "--ioapic", "on"]
25
+ vm.customize ["setextradata", "global", "GUI/SuppressMessages", "all"]
26
+
27
+ vm.gui = vm_config["gui"] || global_config["gui"] || false
28
+ end
29
+
30
+ instance.vm.box = vm_config["box"]
31
+
32
+ elsif global_config["provider"] == "libvirt" then
33
+ raise "Not supported yet"
34
+ else
35
+ raise "Provider not defined in global configuration" unless global_config.include?('provider')
36
+ raise "Provider #{global_config['provider']} not supported"
37
+ end
38
+
39
+ end
40
+