tmatilai / vagrant-proxyconf

Vagrant plugin that configures the virtual machine to use proxies
MIT License
531 stars 74 forks source link

auto enable disable poxy in VM #47

Closed olekhy closed 10 years ago

olekhy commented 10 years ago

I've working with Vagrant in two differ environment one is behind proxy and other without.

My problem is that the proxy values aren't removed from VM when my environment has not proxy.

What steps will reproduce the problem?

  1. at office network that is behind the proxy after calling of vagrant up f.e. proxy values was written in VM. thats is ok. need to known that the proxy variables has been set by me previously.
  2. at home my notebook were direct connected to the internet. also no proxy vars are present, after calling of vagrant up or vagrant provision proxy in VM was not removed.

how could be proxyconf used with the vagrant [up|provision] to achive that the proxy values will be removed from VM at my home?

snowch commented 10 years ago

I think this issue is the same as this one: https://github.com/tmatilai/vagrant-proxyconf/issues/38.

johnbellone commented 10 years ago

@olekhy Just to make sure we're clear. For your second point are you looking for the proxy to be removed from your laptop from an already running virtual machine/provisioning?

olekhy commented 10 years ago

So on HOST in termnal I'l enter:

$ env | grep -i proxy

and I'l get empty output that is because proxy is't set.

next I'l export proxy at enter

export HTTP_PROXY="http://myproxy:8080/"

and test this one by env | grep -i proxy all ok proxy value is present

my next step is starting of vagrant so I'l enter vagrant up anyway I've seen that the proxy value is provided to GUEST.

I'l check this one and login over vagrant ssh in to the GUEST, after entering of env | grep -i proxy I see proxy value identicaly with HOST are present.

Back to the HOST and unset proxy from environment

PROBLEM: after vagrant reload or (vagrant halt, vagrant up) and login into the GUEST I see that the proxy still there...

olekhy commented 10 years ago

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

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

    if ENV['VAGRANT_BOX_TYPE']

        if ENV['VAGRANT_BOX_TYPE'].downcase == 'ci'
            config.vm.box     = 'ci'
            config.vm.box_url = 'https://s3-eu-west-1.amazonaws.com/...aws.box'
            #config.vm.box_url = 'http://localhost/packer_amazon-ebs_aws.box'
            config.ssh.pty = "true"
        end

    else

        config.vm.box = "dev-..."
        config.vm.box_url = "https://s3-...centos6_x86_64.virtualbox.box"

    end

    # auto detect proxy values from environment
    # =========================================

    # normally proxy address is http://...:8080/ for all protocols
    # no_proxy localhost,127.0.0.1,www.....local,....local,www.....local-trunk,....local-trunk

    if ENV['HTTP_PROXY']
        config.proxy.http     = ENV['HTTP_PROXY']
    elsif ENV['http_proxy']
        config.proxy.http     = ENV['http_proxy']
    end

    if ENV['HTTPS_PROXY']
        config.proxy.https     = ENV['HTTPS_PROXY']
    elsif ENV['http_proxy']
        config.proxy.https     = ENV['https_proxy']
    end

    if ENV['FTP_PROXY']
        config.proxy.ftp     = ENV['FTP_PROXY']
    elsif ENV['ftp_proxy']
        config.proxy.ftp     = ENV['ftp_proxy']
    end

    if ENV['NO_PROXY']
        config.proxy.no_proxy     = ENV['NO_PROXY']
    elsif ENV['ftp_proxy']
        config.proxy.no_proxy     = ENV['no_proxy']
    end

    config.vm.provider :aws do |aws, override|
        aws.access_key_id = "..."
        aws.secret_access_key = "..."
        aws.keypair_name = "..."
        aws.ami = "..."
        aws.region = "eu-west-1"
        override.ssh.username = "ec2-user"
        override.ssh.private_key_path = "/....pem"
        override.ssh.pty = "true"
    end

    # customize virtual machine
    # =========================
    config.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--memory", 2048]
    end

    # setup networking on guest
    # =========================
    config.vm.network :forwarded_port, guest: 80, host: 8080
    config.vm.network "private_network", ip: "192.168.50.4"
    config.vm.host_name="www.....local"

    config.hostsupdater.aliases = [
        "....local-trunk",
        "www.....local-trunk",
        "....local",
        "www.....local"
    ]

    # shared directories between guest and host
    # ========================================
    config.vm.synced_folder "vagrant-files", "/vagrant_data"
    config.vm.synced_folder "src/", "/var/opt/...-develop",
        owner: "vagrant",
        group: "apache",
        mount_options: ["dmode=775,fmode=775"]

  # wake puppeteer
  # ==============
    config.vm.provision :shell, :path => "vagrant-files/bootstrap.sh"
    config.vm.provision :shell, :inline => "service iptables stop"

    config.vm.provision :puppet do |config_puppet|
        config_puppet.working_directory = "/tmp/vagrant-puppet-1"
        config_puppet.manifests_path = "vagrant-files/puppet/manifests"
        config_puppet.module_path = "vagrant-files/puppet/modules"
        config_puppet.manifest_file = "site.pp"
        config_puppet.options = "--verbose --debug --hiera_config /vagrant_data/hiera.yaml"

        if ENV['VAGRANT_BOX_ENVIRONMENT']
            if ENV['VAGRANT_BOX_ENVIRONMENT'].downcase == 'external'
                config_puppet.facter = {"local_env" => "dev-local"}
            elsif ENV['VAGRANT_BOX_ENVIRONMENT'].downcase == 'internal'
                config_puppet.facter = {"local_env" => "dev-local-staff"}
            else
                raise(Exception, "undefined vagrant box environment must be internal or external: #{ENV['VAGRANT_BOX_ENVIRONMENT']}")
            end
        else
            raise(Exception, "undefined vagrant box environment must be internal or external: #{ENV['VAGRANT_BOX_ENVIRONMENT']}")
        end
    end

    # run shell commands on guest
    # ===========================
    #config.vm.provision :shell, :inline => "sed -i.bak 's/bind-address/bind-address = 0.0.0.0 #/g' /etc/my.cnf"
    config.vm.provision :shell, :inline => "service mysqld restart"
    #config.vm.provision :shell, :inline => "mysql -uroot -e \"GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';\""
    #config.vm.provision :shell, :inline => "gpasswd -a apache vagrant"
    config.vm.provision :shell, :inline => "service httpd restart"

end
johnbellone commented 10 years ago

I would suggest the following pattern so that your Vagrantfile is easy to read. This also happens to be the most flexible in corporate environments.

snowch commented 10 years ago

Any idea how to do pick up the proxy settings on a windows host without installing extra apps such as these: http://msdn.microsoft.com/en-us/library/windows/desktop/aa384069(v=vs.85).aspx

olekhy commented 10 years ago

thx @johnbellone and made this addition:

vagrant.proxy.enabled = ENV.fetch('HTTP_PROXY', false)

that job for removing proxy value from VM fe. when I do vagrant up in env wo proxy?

@snowch hm I use unix also centos and ubuntu do not known windows, must I read article from MSDN anyway?

snowch commented 10 years ago

I set the http_proxy to DIRECT rather than disabling. Also, make sure you do an operation that triggers the setting change such as vagrant provision. You don't need to read the MDN article. On 24 Jan 2014 17:29, "Oleksandr" notifications@github.com wrote:

thx @johnbellone https://github.com/johnbellone and made this addition:

vagrant.proxy.enabled = ENV.fetch('HTTP_PROXY', false)

that job for removing proxy value from VM fe. when I do vagrant up in env wo proxy?

@snowch https://github.com/snowch hm I use unix also centos and ubuntu do not known windows, must I read article from MSDN anyway?

— Reply to this email directly or view it on GitHubhttps://github.com/tmatilai/vagrant-proxyconf/issues/47#issuecomment-33243193 .

tmatilai commented 10 years ago

@olekhy vagrant.proxy.enabled is currently only in git, and not released yet. Until the release you can use something like:

config.proxy.http = ENV['HTTP_PROXY'] || ENV['http_proxy'] || false

@snowch note that 'DIRECT' only works for apt proxy configuration. For removing proxy settings in general, you should use fase (or empty string '').

So in general you can configure vagrant-proxyconf based on other environment variables, or use what ever ruby code to detect the environment. Verifying the configured proxy is coming on some point and is covered by #38. So I'm closing this now but feel free to keep commenting.

olekhy commented 10 years ago

thx @tmatilai