Class: Docka::Machine

Inherits:
Object
  • Object
show all
Defined in:
lib/docka/machine.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Machine

Returns a new instance of Machine.



3
4
5
# File 'lib/docka/machine.rb', line 3

def initialize(app)
  @app = app
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/docka/machine.rb', line 72

def method_missing(name, *args, &block)
  if %i[host_ip no_share_option].include?(name)
    extend_driver
    return __send__ name, *args, &block
  end
  super
end

Instance Method Details

#createObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/docka/machine.rb', line 23

def create
  @app.logger.info "creating machine..."

  if exists?
    @app.logger.info "machine already exists."
  else
    @app.shell(%( docker-machine create --driver #{driver} #{no_share_option} #{name} ), raise: true, debug: true)
    @app.logger.info "machine created."
  end

  setup_sync
end

#create_sync_dirObject



42
43
44
45
46
# File 'lib/docka/machine.rb', line 42

def create_sync_dir
  unless @app.shell!(%( docker-machine ssh #{name} 'if [ -d /docka/sync ]; then echo 1; fi' ), raise: true).first.strip == '1'
    @app.shell!(%( docker-machine ssh #{name} sudo mkdir -p /docka/sync ), raise: true)
  end
end

#driverObject



11
12
13
# File 'lib/docka/machine.rb', line 11

def driver
  @driver ||= @app.config.get(:machine, :driver) || 'virtualbox'
end

#exists?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/docka/machine.rb', line 19

def exists?
  @app.shell!(%( docker-machine ls -q ), raise: true).first.split(/\n/).include?(name)
end

#extend_driverObject



80
81
82
# File 'lib/docka/machine.rb', line 80

def extend_driver
  extend "Docka::MachineDrivers::#{driver.camelize}".constantize
end

#ipObject



15
16
17
# File 'lib/docka/machine.rb', line 15

def ip
  @ip ||= @app.shell!(%( docker-machine ip #{name} ), raise: true).first.strip
end

#mount_syncObject



59
60
61
62
# File 'lib/docka/machine.rb', line 59

def mount_sync
  @app.shell! %( docker-machine ssh #{name} 'sudo /usr/local/etc/init.d/nfs-client start' ), raise: true, debug: true
  @app.shell! %( docker-machine ssh #{name} 'sudo mount -t nfs -o noacl,async "#{sync_mount_resource}" /docka/sync' ), raise: true, debug: true
end

#nameObject



7
8
9
# File 'lib/docka/machine.rb', line 7

def name
  @name ||= @app.config.get(:machine, :name, optional: false)
end

#remount_syncObject



48
49
50
51
52
53
# File 'lib/docka/machine.rb', line 48

def remount_sync
  @app.logger.info "(re)mounting synced directory..."
  unmount_sync if sync_mounted?
  mount_sync
  @app.logger.info "synced directory (re)mounted."
end

#setup_syncObject



36
37
38
39
40
# File 'lib/docka/machine.rb', line 36

def setup_sync
  @app.setup_sync
  create_sync_dir
  remount_sync
end

#sync_mount_resourceObject



68
69
70
# File 'lib/docka/machine.rb', line 68

def sync_mount_resource
  @sync_mount_resource ||= "#{host_ip}:#{@app.sync_dir}"
end

#sync_mounted?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/docka/machine.rb', line 64

def sync_mounted?
  @app.shell!(%( docker-machine ssh #{name} 'cat /proc/mounts | cut -d " " -f2' ), raise: true).first.split("\n").include?('/docka/sync')
end

#unmount_syncObject



55
56
57
# File 'lib/docka/machine.rb', line 55

def unmount_sync
  @app.shell! %( docker-machine ssh #{name} 'sudo umount /docka/sync' ), raise: true, debug: true
end