Class: KuberKit::Shell::Commands::DockerCommands

Inherits:
Object
  • Object
show all
Defined in:
lib/kuber_kit/shell/commands/docker_commands.rb

Instance Method Summary collapse

Instance Method Details

#build(shell, build_dir, args = []) ⇒ Object



2
3
4
5
6
7
# File 'lib/kuber_kit/shell/commands/docker_commands.rb', line 2

def build(shell, build_dir, args = [])
  default_args = ["--rm=true"]
  args_list = (default_args + args).join(" ")

  shell.exec!(%Q{docker image build #{build_dir} #{args_list}}, merge_stderr: true)
end

#container_exists?(shell, container_name, status: nil) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/kuber_kit/shell/commands/docker_commands.rb', line 32

def container_exists?(shell, container_name, status: nil)
  result = get_containers(shell, container_name, status: status)
  result && result != ""
end

#create_network(shell, name) ⇒ Object



56
57
58
59
60
# File 'lib/kuber_kit/shell/commands/docker_commands.rb', line 56

def create_network(shell, name)
  unless network_exists?(shell, name)
    shell.exec!("docker network create #{name}")
  end
end

#create_volume(shell, name) ⇒ Object



76
77
78
79
80
# File 'lib/kuber_kit/shell/commands/docker_commands.rb', line 76

def create_volume(shell, name)
  unless volume_exists?(shell, name)
    shell.exec!("docker volume create #{name}")
  end
end

#delete_container(shell, container_name) ⇒ Object



52
53
54
# File 'lib/kuber_kit/shell/commands/docker_commands.rb', line 52

def delete_container(shell, container_name)
  shell.exec!("docker rm -f #{container_name}")
end

#get_containers(shell, container_name, only_healthy: false, status: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kuber_kit/shell/commands/docker_commands.rb', line 37

def get_containers(shell, container_name, only_healthy: false, status: nil)
  command_parts = []
  command_parts << "docker ps -a -q"

  if only_healthy
    command_parts << "--filter=\"health=healthy\""
  end
  if status
    command_parts << "--filter=\"status=#{status}\""
  end
  command_parts << "--filter=\"name=^/#{container_name}$\""

  shell.exec!(command_parts.join(" "))
end

#get_networks(shell, network_name) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/kuber_kit/shell/commands/docker_commands.rb', line 67

def get_networks(shell, network_name)
  command_parts = []
  command_parts << "docker network ls"
  command_parts << "--filter=\"name=#{network_name}\""
  command_parts << "--format \"{{.Name}}\""

  shell.exec!(command_parts.join(" "))
end

#get_volumes(shell, volume_name) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/kuber_kit/shell/commands/docker_commands.rb', line 87

def get_volumes(shell, volume_name)
  command_parts = []
  command_parts << "docker volume ls"
  command_parts << "--filter=\"name=#{volume_name}\""
  command_parts << "--format \"{{.Name}}\""

  shell.exec!(command_parts.join(" "))
end

#network_exists?(shell, network_name) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/kuber_kit/shell/commands/docker_commands.rb', line 62

def network_exists?(shell, network_name)
  result = get_networks(shell, network_name)
  result && result != ""
end

#push(shell, tag_name) ⇒ Object



13
14
15
# File 'lib/kuber_kit/shell/commands/docker_commands.rb', line 13

def push(shell, tag_name)
  shell.exec!(%Q{docker push #{tag_name}}, merge_stderr: true)
end

#run(shell, image_name, args: nil, command: nil, detached: false, interactive: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kuber_kit/shell/commands/docker_commands.rb', line 17

def run(shell, image_name, args: nil, command: nil, detached: false, interactive: false)
  command_parts = []
  command_parts << "docker run"
  command_parts << "-d" if detached
  command_parts << Array(args).join(" ") if args
  command_parts << image_name
  command_parts << command if command

  if interactive
    shell.interactive!(command_parts.join(" "))
  else
    shell.exec!(command_parts.join(" "), merge_stderr: true)
  end
end

#tag(shell, image_name, tag_name) ⇒ Object



9
10
11
# File 'lib/kuber_kit/shell/commands/docker_commands.rb', line 9

def tag(shell, image_name, tag_name)
  shell.exec!(%Q{docker tag #{image_name} #{tag_name}}, merge_stderr: true)
end

#volume_exists?(shell, volume_name) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
# File 'lib/kuber_kit/shell/commands/docker_commands.rb', line 82

def volume_exists?(shell, volume_name)
  result = get_volumes(shell, volume_name)
  result && result != ""
end