Class: Sidedock::Machine

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

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Machine

Returns a new instance of Machine.



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

def initialize(name)
  @name = name
end

Instance Method Details

#cliObject



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

def cli
  @cli ||= DockerCLI.new options_to_let_docker_connect_to_it
end

#createObject



22
23
24
25
# File 'lib/sidedock/machine.rb', line 22

def create
  puts "Creating docker machine `#{@name}`. This may take a while."
  docker_machine "create -d virtualbox #{@name}"
end

#docker_machine(command) ⇒ Object



50
51
52
53
# File 'lib/sidedock/machine.rb', line 50

def docker_machine(command)
  @machine_cli ||= MachineCLI.new
  @machine_cli.execute command
end

#ensure_existsObject



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

def ensure_exists
  create unless exists?
  raise "Could not create docker machine #{@name}" unless exists?
end

#ensure_runningObject



12
13
14
15
16
# File 'lib/sidedock/machine.rb', line 12

def ensure_running
  ensure_exists
  start unless running?
  raise "Could not start docker machine #{@name}" unless running?
end

#execute(command) ⇒ Object



45
46
47
48
# File 'lib/sidedock/machine.rb', line 45

def execute(command)
  ensure_running
  cli.execute command
end

#exists?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/sidedock/machine.rb', line 34

def exists?
  return true if @exists
  if docker_machine("ls -q").include? @name
    @exists = true
  end
end

#ipObject



41
42
43
# File 'lib/sidedock/machine.rb', line 41

def ip
  docker_machine "ip #{@name}"
end

#options_to_let_docker_connect_to_itObject



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

def options_to_let_docker_connect_to_it
  docker_machine("config #{@name}").gsub("\n", ' ')
end

#running?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
# File 'lib/sidedock/machine.rb', line 27

def running?
  return true if @running
  if docker_machine("ls -q --filter state=Running").include? @name
    @running = true
  end
end

#startObject



18
19
20
# File 'lib/sidedock/machine.rb', line 18

def start
  docker_machine "start #{@name}"
end