vagrant-g5k 0.0.18 → 0.9.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +2 -0
- data/Vagrantfile +25 -30
- data/lib/vagrant-g5k/action.rb +0 -3
- data/lib/vagrant-g5k/action/connect_g5k.rb +33 -2
- data/lib/vagrant-g5k/action/read_ssh_info.rb +13 -32
- data/lib/vagrant-g5k/action/read_state.rb +4 -8
- data/lib/vagrant-g5k/command.rb +2 -2
- data/lib/vagrant-g5k/config.rb +4 -3
- data/lib/vagrant-g5k/disk/local.rb +68 -0
- data/lib/vagrant-g5k/disk/rbd.rb +92 -0
- data/lib/vagrant-g5k/g5k_connection.rb +144 -0
- data/lib/vagrant-g5k/network/bridge.rb +115 -0
- data/lib/vagrant-g5k/network/nat.rb +65 -0
- data/lib/vagrant-g5k/oar.rb +90 -0
- data/lib/vagrant-g5k/util/{launch_vm_fwd.sh → launch_vm.sh} +35 -5
- data/lib/vagrant-g5k/version.rb +1 -1
- data/spec/vagrant-g5k/oar_spec.rb +57 -0
- data/vagrant-g5k.gemspec +3 -3
- metadata +52 -6
- data/lib/vagrant-g5k/action/close_g5k.rb +0 -24
- data/lib/vagrant-g5k/util/g5k_utils.rb +0 -435
- data/lib/vagrant-g5k/util/launch_vm_bridge.sh +0 -58
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 9e0e51fdb1bc354bce80d807d3b6c7500aa32e61
         | 
| 4 | 
            +
              data.tar.gz: f326ea37cee6bc501848b8deafda61af85a320a7
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 0e632af665f7c61884dd62b0a105114fbe3a2896f848d66174cf09a79b394c6d365a04be4431e1c9425391c34a8d94d9e85ead25b516f691ef8950fa5175a2f0
         | 
| 7 | 
            +
              data.tar.gz: 8a67e80906ff550ed7befb257224cafdc01a613f7bc989889e552b46d4b046758dbb155fade5aca2eca7d3a618f174988255042a8a5f75da5368262b9cb1dd17
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    | @@ -1,3 +1,10 @@ | |
| 1 | 
            +
            # 0.9.0
         | 
| 2 | 
            +
             | 
| 3 | 
            +
              * Code refactoring : 
         | 
| 4 | 
            +
                * Introduce oar_driver to handle operation involving oar
         | 
| 5 | 
            +
                * Introduce net_driver to configure the network used by the VMs
         | 
| 6 | 
            +
                * Introduce disk_driver to configure the disk used by the VMs
         | 
| 7 | 
            +
             | 
| 1 8 | 
             
            # 0.0.18
         | 
| 2 9 |  | 
| 3 10 | 
             
              * Fix update_subnet_use when using nat network
         | 
    
        data/README.md
    CHANGED
    
    
    
        data/Vagrantfile
    CHANGED
    
    | @@ -17,49 +17,44 @@ Vagrant.configure(2) do |config| | |
| 17 17 | 
             
                  g5k.walltime = "01:00:00"
         | 
| 18 18 |  | 
| 19 19 | 
             
                  # Image backed by the ceph cluster
         | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 20 | 
            +
                      g5k.image = {
         | 
| 21 | 
            +
                         :pool      => "msimonin_rbds",
         | 
| 22 | 
            +
                         :rbd       => "bases/ubuntu1404-9p",
         | 
| 23 | 
            +
                         :snapshot  => "parent",
         | 
| 24 | 
            +
                         :id        => "$USER",
         | 
| 25 | 
            +
                         :conf      => "$HOME/.ceph/config",
         | 
| 26 | 
            +
                         :backing   => "copy"
         | 
| 27 | 
            +
                      }
         | 
| 28 28 | 
             
            #
         | 
| 29 29 | 
             
                  # Image backed on the frontend filesystem           
         | 
| 30 | 
            -
                  g5k.image = {
         | 
| 31 | 
            -
                     | 
| 32 | 
            -
                     | 
| 33 | 
            -
                  }
         | 
| 30 | 
            +
            #      g5k.image = {
         | 
| 31 | 
            +
            #        :path    => "/grid5000/virt-images/alpine_docker.qcow2",
         | 
| 32 | 
            +
            #        :backing => "copy"
         | 
| 33 | 
            +
            #      }
         | 
| 34 34 |  | 
| 35 | 
            -
            # | 
| 36 | 
            -
            #         | 
| 35 | 
            +
            #       g5k.net = {
         | 
| 36 | 
            +
            #        :type => "bridge"
         | 
| 37 37 | 
             
            #      }
         | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 41 | 
            -
             | 
| 38 | 
            +
                  g5k.net = {
         | 
| 39 | 
            +
                    :type => "nat",
         | 
| 40 | 
            +
                    :ports => ["2222-:22"]
         | 
| 41 | 
            +
                  }
         | 
| 42 42 |  | 
| 43 43 |  | 
| 44 44 | 
             
                  # oar selection of resource
         | 
| 45 45 | 
             
                  g5k.oar = "virtual != 'none'"
         | 
| 46 46 | 
             
                end #g5k
         | 
| 47 47 |  | 
| 48 | 
            -
                config.vm.define "vm1" do |my|
         | 
| 49 | 
            -
                  my.vm.box = "dummy"
         | 
| 50 | 
            -
             | 
| 51 | 
            -
                  my.ssh.username = "root"
         | 
| 52 | 
            -
                  my.ssh.password = ""
         | 
| 53 48 |  | 
| 54 | 
            -
                 | 
| 55 | 
            -
             | 
| 56 | 
            -
             | 
| 57 | 
            -
            #      my.vm.box = "dummy"
         | 
| 49 | 
            +
                (1..1).each do |i|
         | 
| 50 | 
            +
                  config.vm.define "vm#{i}" do |my|
         | 
| 51 | 
            +
                    my.vm.box = "dummy"
         | 
| 58 52 |  | 
| 59 | 
            -
            # | 
| 60 | 
            -
            # | 
| 53 | 
            +
            #        my.ssh.username = "root"
         | 
| 54 | 
            +
            #        my.ssh.password = ""
         | 
| 61 55 |  | 
| 62 | 
            -
             | 
| 56 | 
            +
                  end #vm
         | 
| 57 | 
            +
                end
         | 
| 63 58 | 
             
                # Repeat block to define another vm 
         | 
| 64 59 | 
             
                # config.vm.define "vm2" do |my|
         | 
| 65 60 | 
             
                #   
         | 
    
        data/lib/vagrant-g5k/action.rb
    CHANGED
    
    | @@ -26,8 +26,6 @@ module VagrantPlugins | |
| 26 26 | 
             
                    Vagrant::Action::Builder.new.tap do |b|
         | 
| 27 27 | 
             
                      b.use ConfigValidate
         | 
| 28 28 | 
             
                      b.use ConnectG5K
         | 
| 29 | 
            -
                      # read the state to find the enclosing ressource
         | 
| 30 | 
            -
                      b.use ReadState
         | 
| 31 29 | 
             
                      b.use ReadSSHInfo
         | 
| 32 30 | 
             
                    end
         | 
| 33 31 | 
             
                  end
         | 
| @@ -144,7 +142,6 @@ module VagrantPlugins | |
| 144 142 |  | 
| 145 143 | 
             
                  action_root = Pathname.new(File.expand_path("../action", __FILE__))
         | 
| 146 144 | 
             
                  autoload :ConnectG5K, action_root.join("connect_g5k")
         | 
| 147 | 
            -
                  autoload :CloseG5K, action_root.join("close_g5k")
         | 
| 148 145 | 
             
                  autoload :CreateLocalWorkingDir, action_root.join("create_local_working_dir")
         | 
| 149 146 | 
             
                  autoload :DeleteJob, action_root.join("delete_job")
         | 
| 150 147 | 
             
                  autoload :DeleteDisk, action_root.join("delete_disk")
         | 
| @@ -1,6 +1,11 @@ | |
| 1 1 | 
             
            require "log4r"
         | 
| 2 | 
            -
            require "vagrant-g5k/ | 
| 2 | 
            +
            require "vagrant-g5k/g5k_connection"
         | 
| 3 3 | 
             
            require "vagrant-g5k/driver"
         | 
| 4 | 
            +
            require "vagrant-g5k/oar"
         | 
| 5 | 
            +
            require "vagrant-g5k/network/nat"
         | 
| 6 | 
            +
            require "vagrant-g5k/network/bridge"
         | 
| 7 | 
            +
            require "vagrant-g5k/disk/local"
         | 
| 8 | 
            +
            require "vagrant-g5k/disk/rbd"
         | 
| 4 9 | 
             
            require 'thread'
         | 
| 5 10 |  | 
| 6 11 |  | 
| @@ -22,8 +27,12 @@ module VagrantPlugins | |
| 22 27 | 
             
                    end
         | 
| 23 28 |  | 
| 24 29 | 
             
                    def call(env)
         | 
| 30 | 
            +
                      cwd = File.join('.vagrant', env[:machine].provider_config.project_id)
         | 
| 25 31 | 
             
                      driver = _get_driver(env)
         | 
| 26 | 
            -
                       | 
| 32 | 
            +
                      oar_driver = VagrantPlugins::G5K::Oar.new(env[:ui], driver)
         | 
| 33 | 
            +
                      net_driver = _get_net_driver(env, driver, oar_driver)
         | 
| 34 | 
            +
                      disk_driver = _get_disk_driver(env, cwd, driver)
         | 
| 35 | 
            +
                      env[:g5k_connection] = Connection.new(env, cwd, driver, oar_driver, net_driver, disk_driver)
         | 
| 27 36 | 
             
                      @app.call(env)
         | 
| 28 37 | 
             
                    end
         | 
| 29 38 |  | 
| @@ -59,6 +68,28 @@ module VagrantPlugins | |
| 59 68 | 
             
                      end
         | 
| 60 69 | 
             
                      return VagrantPlugins::G5K.pool[key]
         | 
| 61 70 | 
             
                    end
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                    def _get_net_driver(env, driver, oar_driver)
         | 
| 73 | 
            +
                      net_type = env[:machine].provider_config.net[:type]
         | 
| 74 | 
            +
                      if net_type == "bridge"
         | 
| 75 | 
            +
                        klass = VagrantPlugins::G5K::Network::Bridge
         | 
| 76 | 
            +
                      else
         | 
| 77 | 
            +
                        klass = VagrantPlugins::G5K::Network::Nat
         | 
| 78 | 
            +
                      end
         | 
| 79 | 
            +
                      klass.new(env, driver, oar_driver)
         | 
| 80 | 
            +
                    end
         | 
| 81 | 
            +
             | 
| 82 | 
            +
                    def _get_disk_driver(env, cwd, driver)
         | 
| 83 | 
            +
                      image = env[:machine].provider_config.image
         | 
| 84 | 
            +
                      if image[:pool].nil?
         | 
| 85 | 
            +
                        klass = VagrantPlugins::G5K::Disk::Local
         | 
| 86 | 
            +
                      else
         | 
| 87 | 
            +
                        klass = VagrantPlugins::G5K::Disk::RBD
         | 
| 88 | 
            +
                      end
         | 
| 89 | 
            +
                      klass.new(env, cwd, driver)
         | 
| 90 | 
            +
                    end
         | 
| 91 | 
            +
             | 
| 92 | 
            +
             | 
| 62 93 | 
             
                  end
         | 
| 63 94 | 
             
                end
         | 
| 64 95 | 
             
              end
         | 
| @@ -12,46 +12,27 @@ module VagrantPlugins | |
| 12 12 | 
             
                    end
         | 
| 13 13 |  | 
| 14 14 | 
             
                    def call(env)
         | 
| 15 | 
            -
                       | 
| 16 | 
            -
                       | 
| 17 | 
            -
                       | 
| 18 | 
            -
             | 
| 19 | 
            -
                       | 
| 20 | 
            -
                         | 
| 21 | 
            -
                        ssh_fwd = ports.select{ |x| x.split(':')[1] == '22'}.first
         | 
| 22 | 
            -
                        if ssh_fwd.nil?
         | 
| 23 | 
            -
                          env[:ui].error "SSH port 22 must be forwarded"
         | 
| 24 | 
            -
                          raise Error "SSh port 22 isn't forwarded"
         | 
| 25 | 
            -
                        end
         | 
| 26 | 
            -
                        ssh_fwd = ssh_fwd.split('-:')[0]
         | 
| 27 | 
            -
                        env[:machine_ssh_info] = read_ssh_info(env[:g5k_connection], env[:machine], ssh_fwd)
         | 
| 15 | 
            +
                      conn = env[:g5k_connection]
         | 
| 16 | 
            +
                      ssh_info = conn.vm_ssh_info(env[:machine].id)
         | 
| 17 | 
            +
                      username = env[:machine].provider_config.username
         | 
| 18 | 
            +
                      gateway = env[:machine].provider_config.gateway
         | 
| 19 | 
            +
                      if !env[:machine].provider_config.gateway.nil?
         | 
| 20 | 
            +
                        ssh_info[:proxy_command] = "ssh #{username}@#{gateway} #{ssh_key(env)} nc %h %p"
         | 
| 28 21 | 
             
                      end
         | 
| 22 | 
            +
                      env[:machine_ssh_info] = ssh_info
         | 
| 23 | 
            +
             | 
| 29 24 | 
             
                      @app.call(env)
         | 
| 30 25 | 
             
                    end
         | 
| 31 26 |  | 
| 32 | 
            -
                    def ssh_key( | 
| 33 | 
            -
                       | 
| 27 | 
            +
                    def ssh_key(env)
         | 
| 28 | 
            +
                      private_key = env[:machine].provider_config.private_key
         | 
| 29 | 
            +
                      if private_key.nil?
         | 
| 34 30 | 
             
                        ""
         | 
| 35 31 | 
             
                      else
         | 
| 36 | 
            -
                         "-i #{ | 
| 37 | 
            -
                      end
         | 
| 38 | 
            -
                    end
         | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 41 | 
            -
                    def read_ssh_info(conn, machine, ssh_fwd = nil)
         | 
| 42 | 
            -
                      return nil if machine.id.nil?
         | 
| 43 | 
            -
             | 
| 44 | 
            -
                      ssh_info = {
         | 
| 45 | 
            -
                               :host          => conn.node,
         | 
| 46 | 
            -
                      }
         | 
| 47 | 
            -
             | 
| 48 | 
            -
                      ssh_info[:port] = ssh_fwd unless ssh_fwd.nil?
         | 
| 49 | 
            -
             | 
| 50 | 
            -
                      if !conn.gateway.nil?
         | 
| 51 | 
            -
                        ssh_info[:proxy_command] = "ssh #{conn.username}@#{conn.gateway} #{ssh_key(conn)} nc %h %p"
         | 
| 32 | 
            +
                         "-i #{private_key}"
         | 
| 52 33 | 
             
                      end
         | 
| 53 | 
            -
                      return ssh_info
         | 
| 54 34 | 
             
                    end
         | 
| 35 | 
            +
                  
         | 
| 55 36 | 
             
                  end
         | 
| 56 37 | 
             
                end
         | 
| 57 38 | 
             
              end
         | 
| @@ -20,7 +20,7 @@ module VagrantPlugins | |
| 20 20 | 
             
                      machine = env[:machine]
         | 
| 21 21 | 
             
                      conn = env[:g5k_connection]
         | 
| 22 22 | 
             
                      id = machine.id
         | 
| 23 | 
            -
                      local_storage = conn. | 
| 23 | 
            +
                      local_storage = conn.check_storage(env)
         | 
| 24 24 | 
             
                      if id.nil? and local_storage.nil?
         | 
| 25 25 | 
             
                        return :not_created
         | 
| 26 26 | 
             
                      end
         | 
| @@ -35,13 +35,9 @@ module VagrantPlugins | |
| 35 35 | 
             
                        if job.nil?
         | 
| 36 36 | 
             
                          return :not_created
         | 
| 37 37 | 
             
                        end
         | 
| 38 | 
            -
                         | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 41 | 
            -
                          if subnet_id.nil?
         | 
| 42 | 
            -
                            return :subnet_missing
         | 
| 43 | 
            -
                          end
         | 
| 44 | 
            -
                        end
         | 
| 38 | 
            +
                        
         | 
| 39 | 
            +
                        net_state = conn.check_net(id)
         | 
| 40 | 
            +
                        return net_state.to_sym unless net_state.nil?
         | 
| 45 41 |  | 
| 46 42 | 
             
                        return job["state"].to_sym
         | 
| 47 43 | 
             
                      end
         | 
    
        data/lib/vagrant-g5k/command.rb
    CHANGED
    
    | @@ -1,5 +1,5 @@ | |
| 1 1 | 
             
            require 'net/ssh/multi'
         | 
| 2 | 
            -
            require 'vagrant-g5k/util/ | 
| 2 | 
            +
            require 'vagrant-g5k/util/g5k_connection'
         | 
| 3 3 | 
             
            include Process
         | 
| 4 4 |  | 
| 5 5 | 
             
            module VagrantPlugins
         | 
| @@ -15,7 +15,7 @@ module VagrantPlugins | |
| 15 15 | 
             
                    # TODO
         | 
| 16 16 | 
             
                    options = {}
         | 
| 17 17 | 
             
                    opts = OptionParser.new do |o|
         | 
| 18 | 
            -
                      o.banner = 'Usage: vagrant  | 
| 18 | 
            +
                      o.banner = 'Usage: vagrant g5k [vm-name]'
         | 
| 19 19 | 
             
                      o.separator ''
         | 
| 20 20 | 
             
                      o.version = VagrantPlugins::G5K::VERSION
         | 
| 21 21 | 
             
                      o.program_name = 'vagrant g5k'
         | 
    
        data/lib/vagrant-g5k/config.rb
    CHANGED
    
    | @@ -60,8 +60,8 @@ module VagrantPlugins | |
| 60 60 | 
             
                    @walltime         = "01:00:00"
         | 
| 61 61 | 
             
                    @oar              = ""
         | 
| 62 62 | 
             
                    @net              = {
         | 
| 63 | 
            -
                       | 
| 64 | 
            -
                       | 
| 63 | 
            +
                      :type => 'nat',
         | 
| 64 | 
            +
                      :ports => ['2222-:22']
         | 
| 65 65 | 
             
                    }
         | 
| 66 66 | 
             
                  end
         | 
| 67 67 |  | 
| @@ -77,7 +77,8 @@ module VagrantPlugins | |
| 77 77 | 
             
                    errors << "g5k site is required" if @site.nil?
         | 
| 78 78 | 
             
                    errors << "g5k image is required" if @image.nil?
         | 
| 79 79 | 
             
                    errors << "g5k image must be a Hash" if !@image.is_a?(Hash)
         | 
| 80 | 
            -
                    
         | 
| 80 | 
            +
                    errors << "g5k net must be a Hash" if !@net.is_a?(Hash)
         | 
| 81 | 
            +
             | 
| 81 82 | 
             
                    { "G5K Provider" => errors }
         | 
| 82 83 | 
             
                  end
         | 
| 83 84 |  | 
| @@ -0,0 +1,68 @@ | |
| 1 | 
            +
            require 'log4r'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module VagrantPlugins
         | 
| 4 | 
            +
              module G5K
         | 
| 5 | 
            +
                module Disk
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                  class Local
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                    def initialize(env, cwd,  driver)
         | 
| 10 | 
            +
                      @logger = Log4r::Logger.new("vagrant::network::nat")
         | 
| 11 | 
            +
                      # command driver is unused
         | 
| 12 | 
            +
                      @cwd = cwd
         | 
| 13 | 
            +
                      @driver = driver
         | 
| 14 | 
            +
                      @env = env
         | 
| 15 | 
            +
                      @image = env[:machine].provider_config.image
         | 
| 16 | 
            +
                      @project_id = env[:machine].provider_config.project_id
         | 
| 17 | 
            +
                      @ui = env[:ui]
         | 
| 18 | 
            +
                    end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                    def generate_drive()
         | 
| 21 | 
            +
                      strategy = @image[:backing]
         | 
| 22 | 
            +
                      if [STRATEGY_SNAPSHOT, STRATEGY_DIRECT].include?(strategy)
         | 
| 23 | 
            +
                        file = @image[:path]
         | 
| 24 | 
            +
                      elsif strategy == STRATEGY_COW
         | 
| 25 | 
            +
                        file = _clone_or_copy_image(clone = true)
         | 
| 26 | 
            +
                      elsif strategy == STRATEGY_COPY
         | 
| 27 | 
            +
                        file = _clone_or_copy_image(clone = false)
         | 
| 28 | 
            +
                      end
         | 
| 29 | 
            +
                      return file
         | 
| 30 | 
            +
                    end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                    def check_storage()
         | 
| 33 | 
            +
                      strategy = @image[:backing]
         | 
| 34 | 
            +
                      file_to_check = ""
         | 
| 35 | 
            +
                      if [STRATEGY_SNAPSHOT, STRATEGY_DIRECT].include?(strategy)
         | 
| 36 | 
            +
                        file_to_check = @image[:path]
         | 
| 37 | 
            +
                      else
         | 
| 38 | 
            +
                        file_to_check = File.join(@cwd, @env[:machine].name.to_s)
         | 
| 39 | 
            +
                      end
         | 
| 40 | 
            +
                        @driver.exec("[ -f \"#{file_to_check}\" ] && echo #{file_to_check} || echo \"\"")
         | 
| 41 | 
            +
                    end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                    def delete_disk()
         | 
| 44 | 
            +
                      disk = File.join(@cwd, @env[:machine].name.to_s)
         | 
| 45 | 
            +
                      @driver.exec("rm -f #{disk}")
         | 
| 46 | 
            +
                    end
         | 
| 47 | 
            +
                    
         | 
| 48 | 
            +
                    def _clone_or_copy_image( clone = true)
         | 
| 49 | 
            +
                      @ui.info("Clone the file image")
         | 
| 50 | 
            +
                      file = File.join(@cwd, @env[:machine].name.to_s)
         | 
| 51 | 
            +
                      exists = check_storage()
         | 
| 52 | 
            +
                      if exists == ""
         | 
| 53 | 
            +
                        if clone 
         | 
| 54 | 
            +
                          @driver.exec("qemu-img create -f qcow2 -b #{@image[:path]} #{file}")
         | 
| 55 | 
            +
                        else
         | 
| 56 | 
            +
                          @driver.exec("cp #{@image[:path]} #{file}")
         | 
| 57 | 
            +
                        end
         | 
| 58 | 
            +
                      end
         | 
| 59 | 
            +
                      return file
         | 
| 60 | 
            +
                    end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
             
         | 
| 63 | 
            +
                  end
         | 
| 64 | 
            +
                end
         | 
| 65 | 
            +
              end
         | 
| 66 | 
            +
            end
         | 
| 67 | 
            +
             | 
| 68 | 
            +
              
         | 
| @@ -0,0 +1,92 @@ | |
| 1 | 
            +
            require 'log4r'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module VagrantPlugins
         | 
| 4 | 
            +
              module G5K
         | 
| 5 | 
            +
                module Disk
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                  class RBD
         | 
| 8 | 
            +
                  
         | 
| 9 | 
            +
                    include Vagrant::Util::Retryable
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                    def initialize(env, cwd,  driver)
         | 
| 12 | 
            +
                      @logger = Log4r::Logger.new("vagrant::network::nat")
         | 
| 13 | 
            +
                      # command driver is unused
         | 
| 14 | 
            +
                      @cwd = cwd
         | 
| 15 | 
            +
                      @driver = driver
         | 
| 16 | 
            +
                      @env = env
         | 
| 17 | 
            +
                      @image = env[:machine].provider_config.image
         | 
| 18 | 
            +
                      @project_id = env[:machine].provider_config.project_id
         | 
| 19 | 
            +
                      @ui = env[:ui]
         | 
| 20 | 
            +
                    end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                  def generate_drive()
         | 
| 23 | 
            +
                    strategy = @image[:backing]
         | 
| 24 | 
            +
                    @logger.debug(@image)
         | 
| 25 | 
            +
                    if [STRATEGY_SNAPSHOT, STRATEGY_DIRECT].include?(strategy)
         | 
| 26 | 
            +
                      @logger.debug(@image)
         | 
| 27 | 
            +
                      file = File.join(@image[:pool], @image[:rbd])
         | 
| 28 | 
            +
                      @logger.debug(file)
         | 
| 29 | 
            +
                    elsif strategy == STRATEGY_COW
         | 
| 30 | 
            +
                      file = _clone_or_copy_image(clone = true)
         | 
| 31 | 
            +
                    elsif strategy == STRATEGY_COPY
         | 
| 32 | 
            +
                      file = _clone_or_copy_image(clone = false)
         | 
| 33 | 
            +
                    end
         | 
| 34 | 
            +
                    # encapsulate the file to a qemu ready disk description
         | 
| 35 | 
            +
                    file = "rbd:#{file}:id=#{@image[:id]}:conf=#{@image[:conf]}:rbd_cache=true,cache=writeback"
         | 
| 36 | 
            +
                    @logger.debug("Generated drive string : #{file}")
         | 
| 37 | 
            +
                    return file
         | 
| 38 | 
            +
                  end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
                  def check_storage()
         | 
| 41 | 
            +
                    strategy = @image[:backing]
         | 
| 42 | 
            +
                    file_to_check = ""
         | 
| 43 | 
            +
                    if [STRATEGY_SNAPSHOT, STRATEGY_DIRECT].include?(strategy)
         | 
| 44 | 
            +
                      file_to_check = @image[:rbd]
         | 
| 45 | 
            +
                    else
         | 
| 46 | 
            +
                      file_to_check = File.join(@cwd, @env[:machine].name.to_s)
         | 
| 47 | 
            +
                    end
         | 
| 48 | 
            +
                    @driver.exec("(rbd --pool #{@image[:pool]} --id #{@image[:id]} --conf #{@image[:conf]} ls | grep \"^#{file_to_check}$\") || echo \"\"")
         | 
| 49 | 
            +
                  end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                  def delete_disk()   
         | 
| 52 | 
            +
                    disk = File.join(@image[:pool], @cwd, @env[:machine].name.to_s)
         | 
| 53 | 
            +
                    begin
         | 
| 54 | 
            +
                      retryable(:on => VagrantPlugins::G5K::Errors::CommandError, :tries => 10, :sleep => 5) do
         | 
| 55 | 
            +
                        @driver.exec("rbd rm  #{disk} --conf #{@image[:conf]} --id #{@image[:id]}" )
         | 
| 56 | 
            +
                        break
         | 
| 57 | 
            +
                      end
         | 
| 58 | 
            +
                    rescue VagrantPlugins::G5K::Errors::CommandError
         | 
| 59 | 
            +
                      @ui.error("Reach max attempt while trying to remove the rbd")
         | 
| 60 | 
            +
                      raise VagrantPlugins::G5K::Errors::CommandError
         | 
| 61 | 
            +
                    end
         | 
| 62 | 
            +
                  end
         | 
| 63 | 
            +
             | 
| 64 | 
            +
             | 
| 65 | 
            +
             | 
| 66 | 
            +
                  def _clone_or_copy_image(clone = true)
         | 
| 67 | 
            +
                    # destination in the same pool under the .vagrant ns
         | 
| 68 | 
            +
                    destination = File.join(@image[:pool], @cwd, @env[:machine].name.to_s)
         | 
| 69 | 
            +
                    # Even if nothing bad will happen when the destination already exist, we should test it before
         | 
| 70 | 
            +
                    exists = check_storage()
         | 
| 71 | 
            +
                    if exists == ""
         | 
| 72 | 
            +
                      # we create the destination
         | 
| 73 | 
            +
                      if clone
         | 
| 74 | 
            +
                        # parent = pool/rbd@snap
         | 
| 75 | 
            +
                        @ui.info("Cloning the rbd image")
         | 
| 76 | 
            +
                        parent = File.join(@image[:pool], "#{@image[:rbd]}@#{@image[:snapshot]}")
         | 
| 77 | 
            +
                        @driver.exec("rbd clone #{parent} #{destination} --conf #{@image[:conf]} --id #{@image[:id]}" )
         | 
| 78 | 
            +
                      else
         | 
| 79 | 
            +
                        @ui.info("Copying the rbd image (This may take some time)")
         | 
| 80 | 
            +
                        # parent = pool/rbd@snap
         | 
| 81 | 
            +
                        parent = File.join(@image[:pool], "#{@image[:rbd]}")
         | 
| 82 | 
            +
                        @driver.exec("rbd cp #{parent} #{destination} --conf #{@image[:conf]} --id #{@image[:id]}" )
         | 
| 83 | 
            +
                      end
         | 
| 84 | 
            +
                    end
         | 
| 85 | 
            +
                    return destination
         | 
| 86 | 
            +
                  end
         | 
| 87 | 
            +
             | 
| 88 | 
            +
             | 
| 89 | 
            +
                  end
         | 
| 90 | 
            +
                end
         | 
| 91 | 
            +
              end
         | 
| 92 | 
            +
            end
         |