Skip to content

Instantly share code, notes, and snippets.

@cariandrum22
Forked from mattes/boot2docker-nfs.rb
Last active August 17, 2016 14:54

Revisions

  1. cariandrum22 revised this gist Aug 17, 2016. 1 changed file with 21 additions and 26 deletions.
    47 changes: 21 additions & 26 deletions boot2docker-nfs.rb
    Original file line number Diff line number Diff line change
    @@ -17,31 +17,31 @@
    sudo /usr/local/etc/init.d/nfs-client start
    sudo mount -t nfs -o noacl,async <%= ip %>:/Users /Users
    EOL
    BOOTLOCAL_SH_TEMPLATE.freeze
    .freeze

    class DockerMachineNfs

    def initialize(machine_name, driver)
    @bootlocal = BOOTLOCAL_SH_TEMPLATE
    case driver
    when 'virtualbox' then
    @uid = `id -u`.chomp
    @gid = `id -g`.chomp
    @dmachine_name = machine_name
    @dmachine_ip = `docker-machine ip #{machine_name}`.chomp
    @vboxnet_ip = VirtualBox::get_vboxnet_ip(VirtualBox::get_hostonly_adapter(machine_name))
    else
    raise 'This script dose not support selected driver: ' + driver
    when 'virtualbox' then
    @uid = `id -u`.chomp
    @gid = `id -g`.chomp
    @dmachine_name = machine_name
    @dmachine_ip = `docker-machine ip #{machine_name}`.chomp
    @vboxnet_ip = VirtualBox.get_vboxnet_ip(VirtualBox.get_hostonly_adapter(machine_name))
    else
    raise 'This script dose not support selected driver: ' + driver
    end
    yield self if block_given?
    end

    def setup_nfs_server
    # create record in local /etc/exports and restart nsfd
    puts "Update /etc/exports ..."
    puts 'Update /etc/exports ...'
    `echo '\n/Users #{@dmachine_ip} -alldirs -mapall=#{@uid}:#{@gid}\n' | sudo tee -a /etc/exports`
    `awk '!a[$0]++' /etc/exports | sudo tee /etc/exports` # removes duplicate lines
    `sudo nfsd restart`; sleep 2
    `sudo nfsd restart`
    sleep 2
    checkexports = `sudo nfsd checkexports 2>&1`
    if checkexports == ''
    puts ' > Done.'
    @@ -69,19 +69,17 @@ def setup_nfs_client
    end

    def check
    puts "Check if NFS is mounted on docker-machine \"#{@dmachine_name}\"."; sleep 2
    puts "Check if NFS is mounted on docker-machine \"#{@dmachine_name}\"."
    sleep 2
    result = <<-EOS
    > failed: Try `docker-machine ssh #{@dmachine_name} df`.
    Output should include something like this: '#{@vboxnet_ip}:/Users [...] /Users'
    EOS
    `docker-machine ssh #{@dmachine_name} df`.each_line do |l|
    if l.chomp.match(/^#{@vboxnet_ip}:\/Users.*\/Users$/)
    result = ' > Done.'
    end
    result = ' > Done.' if %r{^#{@vboxnet_ip}:/Users.*/Users$} =~ l.chomp
    end
    puts result
    end

    end

    module VirtualBox
    @@ -91,12 +89,9 @@ def get_hostonly_adapter(machine_name)
    puts 'Get vbox hostonly adapter name ...'
    vboxnet_name = `VBoxManage showvminfo #{machine_name} --machinereadable | grep hostonlyadapter`
    .scan(/"(.*)"/).flatten.first.chomp
    unless vboxnet_name == ''
    puts ' > ' + vboxnet_name
    vboxnet_name
    else
    raise 'error: unable to find name of vboxnet'
    end
    raise 'error: unable to find name of vboxnet' if vboxnet_name == ''
    puts ' > ' + vboxnet_name
    vboxnet_name
    end

    def get_vboxnet_ip(vboxnet_name)
    @@ -106,17 +101,18 @@ def get_vboxnet_ip(vboxnet_name)
    vboxip = vboxnet.scan(/IPAddress: *(.*)\n/).flatten.first.chomp
    puts ' > ' + vboxip
    return vboxip
    else
    next
    end
    end
    end

    end
    end

    def main
    options = { driver: 'virtualbox' }
    OptionParser.new do |op|
    op.on('-d', '--driver "virtualbox"', 'docker-machine driver (default:virtualbox)') {|v| options[:driver] = v }
    op.on('-d', '--driver "virtualbox"', 'docker-machine driver (default:virtualbox)') { |v| options[:driver] = v }
    op.parse!(ARGV)
    end

    @@ -133,7 +129,6 @@ def main
    d.setup_nfs_client
    d.check
    end

    end

    main
  2. cariandrum22 revised this gist Aug 17, 2016. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions boot2docker-nfs.rb
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,6 @@
    # fork from https://gist.github.com/mattes/4d7f435d759ca2581347
    # I owe you a lot, mattes.


    require 'erb'
    require 'optparse'

    @@ -22,7 +21,7 @@

    class DockerMachineNfs

    def initialize(machine_name, driver='')
    def initialize(machine_name, driver)
    @bootlocal = BOOTLOCAL_SH_TEMPLATE
    case driver
    when 'virtualbox' then
  3. cariandrum22 revised this gist Aug 17, 2016. 1 changed file with 120 additions and 55 deletions.
    175 changes: 120 additions & 55 deletions boot2docker-nfs.rb
    Original file line number Diff line number Diff line change
    @@ -2,74 +2,139 @@

    # Usage
    # $ docker-machine create my-machine123 -d virtualbox
    # $ ruby <(curl -L https://git.io/vvvco) my-machine123
    # https://gist.github.com/mattes/4d7f435d759ca2581347
    # $ ruby <(curl -L https://git.io/v6r1A) my-machine123
    # https://gist.github.com/cariandrum22/278973172442d6f54a0c3ab83a86469f

    # fork from https://gist.github.com/mattes/4d7f435d759ca2581347
    # I owe you a lot, mattes.


    require 'erb'
    require 'optparse'

    bootlocalsh = %Q(#/bin/bash
    BOOTLOCAL_SH_TEMPLATE = <<EOL
    #/bin/bash
    sudo umount /Users
    sudo /usr/local/etc/init.d/nfs-client start
    sudo mount -t nfs -o noacl,async <%= vboxnet_ip %>:/Users /Users
    )
    sudo mount -t nfs -o noacl,async <%= ip %>:/Users /Users
    EOL
    BOOTLOCAL_SH_TEMPLATE.freeze

    if ARGV.length != 1
    puts "usage: #{__FILE__} machine-name"
    exit 1
    end
    machine_name = ARGV[0]
    class DockerMachineNfs

    print "Get vboxnet ip addres ..."
    def initialize(machine_name, driver='')
    @bootlocal = BOOTLOCAL_SH_TEMPLATE
    case driver
    when 'virtualbox' then
    @uid = `id -u`.chomp
    @gid = `id -g`.chomp
    @dmachine_name = machine_name
    @dmachine_ip = `docker-machine ip #{machine_name}`.chomp
    @vboxnet_ip = VirtualBox::get_vboxnet_ip(VirtualBox::get_hostonly_adapter(machine_name))
    else
    raise 'This script dose not support selected driver: ' + driver
    end
    yield self if block_given?
    end

    # get host only adapter
    vboxnet_name = `VBoxManage showvminfo #{machine_name} --machinereadable | grep hostonlyadapter`
    vboxnet_name = vboxnet_name.scan(/"(.*)"/).flatten.first.chomp
    if vboxnet_name == ''
    puts "error: unable to find name of vboxnet"
    exit 1
    end
    def setup_nfs_server
    # create record in local /etc/exports and restart nsfd
    puts "Update /etc/exports ..."
    `echo '\n/Users #{@dmachine_ip} -alldirs -mapall=#{@uid}:#{@gid}\n' | sudo tee -a /etc/exports`
    `awk '!a[$0]++' /etc/exports | sudo tee /etc/exports` # removes duplicate lines
    `sudo nfsd restart`; sleep 2
    checkexports = `sudo nfsd checkexports 2>&1`
    if checkexports == ''
    puts ' > Done.'
    else
    raise 'exec `sudo nfsd checkexports`: ' + checkexports
    end
    end

    # get ip addr for vboxnet
    vboxnet_ip = ''
    vboxnets = `VBoxManage list hostonlyifs`.split("\n\n")
    vboxnets.each do |vboxnet|
    if vboxnet.scan(/Name: *(.+?)\n/).flatten.first.chomp == vboxnet_name
    vboxnet_ip = vboxnet.scan(/IPAddress: *(.*)\n/).flatten.first.chomp
    break
    def setup_nfs_client
    # render bootlocal.sh and copy bootlocal.sh over to boot2docker
    # (this will override an existing /var/lib/boot2docker/bootlocal.sh)
    puts 'Update boot2docker virtual machine ...'
    ip = @vboxnet_ip
    first = true
    ERB.new(@bootlocal).result(binding).split("\n").each do |l|
    `docker-machine ssh #{@dmachine_name} 'echo \"#{l}\" | sudo tee #{first ? '' : '-a'} /var/lib/boot2docker/bootlocal.sh'`
    first = false
    end
    `docker-machine ssh #{@dmachine_name} 'sudo chmod +x /var/lib/boot2docker/bootlocal.sh'`
    `docker-machine ssh #{@dmachine_name} 'sudo sync'`

    puts "Restart #{@dmachine_name} ..."
    `docker-machine restart #{@dmachine_name}`
    puts ' > Done.'
    end
    end
    if vboxnet_ip == ''
    puts "error: unable to find ip of vboxnet #{vboxnet_name}"
    exit 1

    def check
    puts "Check if NFS is mounted on docker-machine \"#{@dmachine_name}\"."; sleep 2
    result = <<-EOS
    > failed: Try `docker-machine ssh #{@dmachine_name} df`.
    Output should include something like this: '#{@vboxnet_ip}:/Users [...] /Users'
    EOS
    `docker-machine ssh #{@dmachine_name} df`.each_line do |l|
    if l.chomp.match(/^#{@vboxnet_ip}:\/Users.*\/Users$/)
    result = ' > Done.'
    end
    end
    puts result
    end

    end

    print " #{vboxnet_ip}\n"


    # create record in local /etc/exports and restart nsfd
    machine_ip = `docker-machine ip #{machine_name}`.chomp
    puts "Update /etc/exports ..."
    `echo '\n/Users #{machine_ip} -alldirs -maproot=root\n' | sudo tee -a /etc/exports`
    `awk '!a[$0]++' /etc/exports | sudo tee /etc/exports` # removes duplicate lines
    `sudo nfsd restart`; sleep 2
    puts `sudo nfsd checkexports`

    # render bootlocal.sh and copy bootlocal.sh over to boot2docker
    # (this will override an existing /var/lib/boot2docker/bootlocal.sh)
    puts "Update boot2docker virtual machine ..."
    bootlocalsh_rendered = ERB.new(bootlocalsh).result()
    first = true
    bootlocalsh_rendered.split("\n").each do |l|
    `docker-machine ssh #{machine_name} 'echo "#{l}" | sudo tee #{first ? '' : '-a'} /var/lib/boot2docker/bootlocal.sh'`
    first = false
    module VirtualBox
    class << self

    def get_hostonly_adapter(machine_name)
    puts 'Get vbox hostonly adapter name ...'
    vboxnet_name = `VBoxManage showvminfo #{machine_name} --machinereadable | grep hostonlyadapter`
    .scan(/"(.*)"/).flatten.first.chomp
    unless vboxnet_name == ''
    puts ' > ' + vboxnet_name
    vboxnet_name
    else
    raise 'error: unable to find name of vboxnet'
    end
    end

    def get_vboxnet_ip(vboxnet_name)
    puts 'Get vbox net ip address ...'
    `VBoxManage list hostonlyifs`.split("\n\n").each do |vboxnet|
    if vboxnet.scan(/Name: *(.+?)\n/).flatten.first.chomp == vboxnet_name
    vboxip = vboxnet.scan(/IPAddress: *(.*)\n/).flatten.first.chomp
    puts ' > ' + vboxip
    return vboxip
    end
    end
    end

    end
    end
    `docker-machine ssh #{machine_name} 'sudo chmod +x /var/lib/boot2docker/bootlocal.sh'`

    puts "Restart #{machine_name} ..."
    `docker-machine restart #{machine_name}`
    def main
    options = { driver: 'virtualbox' }
    OptionParser.new do |op|
    op.on('-d', '--driver "virtualbox"', 'docker-machine driver (default:virtualbox)') {|v| options[:driver] = v }
    op.parse!(ARGV)
    end

    puts "Done."
    if ARGV.length != 1
    puts "Usage: #{File.basename(__FILE__)} machine_name"
    exit 1
    end

    machine_name = ARGV[0]
    driver = options[:driver]

    DockerMachineNfs.new(machine_name, driver) do |d|
    d.setup_nfs_server
    d.setup_nfs_client
    d.check
    end

    end

    puts
    puts "Run `docker-machine ssh #{machine_name} df` to check if NFS is mounted."
    puts "Output should include something like this: '#{vboxnet_ip}:/Users [...] /Users'"
    main
  4. @mattes mattes revised this gist Apr 14, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion boot2docker-nfs.rb
    Original file line number Diff line number Diff line change
    @@ -52,7 +52,7 @@
    `echo '\n/Users #{machine_ip} -alldirs -maproot=root\n' | sudo tee -a /etc/exports`
    `awk '!a[$0]++' /etc/exports | sudo tee /etc/exports` # removes duplicate lines
    `sudo nfsd restart`; sleep 2
    `sudo nfsd checkexports`
    puts `sudo nfsd checkexports`

    # render bootlocal.sh and copy bootlocal.sh over to boot2docker
    # (this will override an existing /var/lib/boot2docker/bootlocal.sh)
  5. @mattes mattes revised this gist Apr 14, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion boot2docker-nfs.rb
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    bootlocalsh = %Q(#/bin/bash
    sudo umount /Users
    sudo /usr/local/etc/init.d/nfs-client start
    sudo mount -t nfs <%= vboxnet_ip %>:/Users /Users
    sudo mount -t nfs -o noacl,async <%= vboxnet_ip %>:/Users /Users
    )

    if ARGV.length != 1
  6. @mattes mattes revised this gist Apr 13, 2015. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions boot2docker-nfs.rb
    Original file line number Diff line number Diff line change
    @@ -49,8 +49,7 @@
    # create record in local /etc/exports and restart nsfd
    machine_ip = `docker-machine ip #{machine_name}`.chomp
    puts "Update /etc/exports ..."
    # TODO why 501:20 in -mapall ?!
    `echo '\n/Users #{machine_ip} -alldirs -mapall=501:20\n' | sudo tee -a /etc/exports`
    `echo '\n/Users #{machine_ip} -alldirs -maproot=root\n' | sudo tee -a /etc/exports`
    `awk '!a[$0]++' /etc/exports | sudo tee /etc/exports` # removes duplicate lines
    `sudo nfsd restart`; sleep 2
    `sudo nfsd checkexports`
  7. @mattes mattes revised this gist Apr 11, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion boot2docker-nfs.rb
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    # Usage
    # $ docker-machine create my-machine123 -d virtualbox
    # $ ruby <(curl -L https://git.io/vvvsH) my-machine123
    # $ ruby <(curl -L https://git.io/vvvco) my-machine123
    # https://gist.github.com/mattes/4d7f435d759ca2581347

    require 'erb'
  8. @mattes mattes revised this gist Apr 11, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions boot2docker-nfs.rb
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    # Usage
    # $ docker-machine create my-machine123 -d virtualbox
    # $ ruby <(curl -L https://git.io/vvvsH) my-machine123
    # https://gist.github.com/mattes/4d7f435d759ca2581347

    require 'erb'

  9. @mattes mattes revised this gist Apr 11, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions boot2docker-nfs.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,9 @@
    #!/usr/bin/env ruby

    # Usage
    # $ docker-machine create my-machine123 -d virtualbox
    # $ ruby <(curl -L https://git.io/vvvsH) my-machine123

    require 'erb'

    bootlocalsh = %Q(#/bin/bash
  10. @mattes mattes created this gist Apr 11, 2015.
    70 changes: 70 additions & 0 deletions boot2docker-nfs.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    #!/usr/bin/env ruby
    require 'erb'

    bootlocalsh = %Q(#/bin/bash
    sudo umount /Users
    sudo /usr/local/etc/init.d/nfs-client start
    sudo mount -t nfs <%= vboxnet_ip %>:/Users /Users
    )

    if ARGV.length != 1
    puts "usage: #{__FILE__} machine-name"
    exit 1
    end
    machine_name = ARGV[0]

    print "Get vboxnet ip addres ..."

    # get host only adapter
    vboxnet_name = `VBoxManage showvminfo #{machine_name} --machinereadable | grep hostonlyadapter`
    vboxnet_name = vboxnet_name.scan(/"(.*)"/).flatten.first.chomp
    if vboxnet_name == ''
    puts "error: unable to find name of vboxnet"
    exit 1
    end

    # get ip addr for vboxnet
    vboxnet_ip = ''
    vboxnets = `VBoxManage list hostonlyifs`.split("\n\n")
    vboxnets.each do |vboxnet|
    if vboxnet.scan(/Name: *(.+?)\n/).flatten.first.chomp == vboxnet_name
    vboxnet_ip = vboxnet.scan(/IPAddress: *(.*)\n/).flatten.first.chomp
    break
    end
    end
    if vboxnet_ip == ''
    puts "error: unable to find ip of vboxnet #{vboxnet_name}"
    exit 1
    end

    print " #{vboxnet_ip}\n"


    # create record in local /etc/exports and restart nsfd
    machine_ip = `docker-machine ip #{machine_name}`.chomp
    puts "Update /etc/exports ..."
    # TODO why 501:20 in -mapall ?!
    `echo '\n/Users #{machine_ip} -alldirs -mapall=501:20\n' | sudo tee -a /etc/exports`
    `awk '!a[$0]++' /etc/exports | sudo tee /etc/exports` # removes duplicate lines
    `sudo nfsd restart`; sleep 2
    `sudo nfsd checkexports`

    # render bootlocal.sh and copy bootlocal.sh over to boot2docker
    # (this will override an existing /var/lib/boot2docker/bootlocal.sh)
    puts "Update boot2docker virtual machine ..."
    bootlocalsh_rendered = ERB.new(bootlocalsh).result()
    first = true
    bootlocalsh_rendered.split("\n").each do |l|
    `docker-machine ssh #{machine_name} 'echo "#{l}" | sudo tee #{first ? '' : '-a'} /var/lib/boot2docker/bootlocal.sh'`
    first = false
    end
    `docker-machine ssh #{machine_name} 'sudo chmod +x /var/lib/boot2docker/bootlocal.sh'`

    puts "Restart #{machine_name} ..."
    `docker-machine restart #{machine_name}`

    puts "Done."

    puts
    puts "Run `docker-machine ssh #{machine_name} df` to check if NFS is mounted."
    puts "Output should include something like this: '#{vboxnet_ip}:/Users [...] /Users'"