Class: DevDock::DevContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/dev_dock/container.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DevContainer

Returns a new instance of DevContainer.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dev_dock/container.rb', line 13

def initialize(options)
  @options = options
  @image = DevDock::DevImage.new(options.image_name)
  @volumes = DevDock::DevVolumes.new(@image)
  @binds = DevDock::DevBinds.new([
    '/var/run/docker.sock:/var/run/docker.sock'
  ])
  @name = DevDock::Util::snake_case("dev_dock_#{options.image_name}")

  init_binds
end

Instance Attribute Details

#bindsObject (readonly)

Returns the value of attribute binds.



11
12
13
# File 'lib/dev_dock/container.rb', line 11

def binds
  @binds
end

#imageObject (readonly)

Returns the value of attribute image.



11
12
13
# File 'lib/dev_dock/container.rb', line 11

def image
  @image
end

#volumesObject (readonly)

Returns the value of attribute volumes.



11
12
13
# File 'lib/dev_dock/container.rb', line 11

def volumes
  @volumes
end

Instance Method Details

#docker_groupObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dev_dock/container.rb', line 42

def docker_group
  docker_line = File
    .read('/etc/group')
    .lines
    .find { |line| line.start_with?('docker') }
  group = docker_line and docker_line.split(':')[1]
  if docker_line.nil?
    group = docker_line
  else
    group = docker_line.split(':')[2]
  end
  Log::debug("Docker gid is #{group}")
  group
end

#enable_x11(arguments) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/dev_dock/container.rb', line 77

def enable_x11(arguments)
  if x11?
    Log::debug('X11 socket file found')
    arguments.push '-e'
    arguments.push 'DISPLAY'
  else
    Log::debug('Did not find X11 socket file')
  end
end

#exist?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
# File 'lib/dev_dock/container.rb', line 57

def exist?
  Docker::Container.get(@name)
  true
rescue Docker::Error::NotFoundError
  false
end

#init_bindsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dev_dock/container.rb', line 25

def init_binds

  ['workspaces', '.gitconfig', '.ssh'].each do |directory|
    source = File.join(ENV['HOME'], directory)
    target = File.join("/home", @image.user, directory)
    @binds.push("#{source}:#{target}")
  end

  if x11?
    @binds.push('/tmp/.X11-unix:/tmp/.X11-unix:ro')
  end

  if linux?
    @binds.push( '/etc/localtime:/etc/localtime:ro')
  end
end

#killObject

kill container



65
66
67
# File 'lib/dev_dock/container.rb', line 65

def kill
  Docker::Container.get(@name).kill   
end

#linux?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/dev_dock/container.rb', line 73

def linux?
  RUBY_PLATFORM.start_with?("x86_64-linux")
end

#runObject



131
132
133
# File 'lib/dev_dock/container.rb', line 131

def run
  exec *run_arguments
end

#run_argumentsObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/dev_dock/container.rb', line 87

def run_arguments
  arguments = [
    'docker',
    'run',
    '--privileged',
    '--name', @name,
    '--group-add', docker_group,
    '--net=host',
    '--rm',
    '-ti',
    '--detach-keys',
    'ctrl-q,ctrl-q',
    '-e', 'GH_USER',
    '-e', 'GH_PASS',
    '-e', "DEV_DOCK_HOST_HOME=#{@options.host_home}"
  ]

  if linux?
    enable_x11(arguments)
  end

  @volumes.list.each do |volume|
    arguments.push '--mount', "source=#{volume.name},target=#{volume.path}"
  end

  @options.volumes.each do |volume|
    arguments.push '-v', volume
  end

  @binds.list.each do |bind|
    arguments.push '-v', bind.to_argument
  end

  @options.environment.each do |environment|
    arguments.push '-e', environment
  end

  arguments.push @image.name

  arguments.push *@options.run_command

  arguments
end

#x11?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/dev_dock/container.rb', line 69

def x11?
  File.exists?('/tmp/.X11-unix')
end