Class: Fastlane::Helper::DockerCommander

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/mango/helper/docker_commander.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(container_name) ⇒ DockerCommander

Returns a new instance of DockerCommander.



8
9
10
# File 'lib/fastlane/plugin/mango/helper/docker_commander.rb', line 8

def initialize(container_name)
  @container_name = container_name
end

Instance Attribute Details

#container_nameObject

Returns the value of attribute container_name.



6
7
8
# File 'lib/fastlane/plugin/mango/helper/docker_commander.rb', line 6

def container_name
  @container_name
end

Instance Method Details

#delete_containerObject



44
45
46
47
48
49
50
# File 'lib/fastlane/plugin/mango/helper/docker_commander.rb', line 44

def delete_container
  Actions.sh("docker rm -f #{container_name}") if container_name
rescue StandardError
  sleep 5
  UI.important("Was not able to delete the container after the first attempt, trying again")
  retry
end

#disconnect_network_bridgeObject



52
53
54
55
56
57
# File 'lib/fastlane/plugin/mango/helper/docker_commander.rb', line 52

def disconnect_network_bridge
  UI.important('Disconnecting from the network bridge')
  Actions.sh("docker network disconnect -f bridge #{container_name}") if container_name
rescue StandardError
  # Do nothing if the network bridge is already gone
end

#exec(command:, raise_when_fail: true) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fastlane/plugin/mango/helper/docker_commander.rb', line 59

def exec(command:, raise_when_fail: true)
  if container_name
    begin
      Actions.sh("docker exec #{container_name} bash -l -c \"#{command}\"")
    rescue StandardError => e
      raise(e) if raise_when_fail
    end
  else
    raise('Cannot execute docker command because the container name is unknown')
  end
end

#pruneObject



71
72
73
# File 'lib/fastlane/plugin/mango/helper/docker_commander.rb', line 71

def prune
  Action.sh('docker system prune -f')
end

#pull_image(docker_image_name:) ⇒ Object



12
13
14
15
16
17
# File 'lib/fastlane/plugin/mango/helper/docker_commander.rb', line 12

def pull_image(docker_image_name:)
  Actions.sh("docker pull #{docker_image_name}")
rescue StandardError => e
  prune if e.message =~ /Create more free space in thin pool/
  Actions.sh("docker pull #{docker_image_name}")
end

#start_container(emulator_args:, docker_image:, core_amount:) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fastlane/plugin/mango/helper/docker_commander.rb', line 19

def start_container(emulator_args:, docker_image:, core_amount:)
  retries ||= 0
  docker_name = if container_name
                  "--name #{container_name}"
                else
                  ''
                end
  # if core_amount value is defined then limit the container while starting
  core_amount = if core_amount && core_amount > 0
                  "--cpus=#{core_amount}"
                else
                  ''
                end

  # Action.sh returns all output that the command produced but we are only
  # interested in the last line, since it contains the id of the created container.
  UI.important("Attaching #{ENV['PWD']} to the docker container")
  Actions.sh("docker run -v $PWD:/root/tests --privileged -t -d #{core_amount} #{emulator_args} #{docker_name} #{docker_image}").chomp
rescue StandardError => e
  if e.message =~ /Create more free space in thin pool/ && (retries += 1) < 2
    prune
    retry
  end
end