Sha256: bf68dd7385945bdce3cf5dccf7703a6d86b32a2abd2fc779fe4e8333a6a59e7e

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

require 'mkit/cmd_runner'

module MKIt
  module DockerHelper
    # from ERB template
    def create_instance(cmd)
      MKIt::CmdRunner.run(cmd)
    end

    def start_instance(instance_id)
      MKIt::CmdRunner.run("docker start #{instance_id}")
    end

    def stop_instance(instance_id)
      MKIt::CmdRunner.run("docker stop #{instance_id}")
    end

    def remove_instance(instance)
      MKIt::CmdRunner.run("docker rm -f #{instance}")
    end

    def inspect_instance(instance_id)
      # this one does not work on ubunto MKIt::CmdRunner.run("docker inspect #{instance_id}")
      x = `docker inspect #{instance_id}`
      JSON.parse(x).first
    end

    def execute_local(instance_id, cmd)
      MKIt::CmdRunner.run("docker exec -it #{instance_id} #{cmd}")
    end

    #
    # network
    #

    def create_network(network_name)
      MKIt::CmdRunner.run("docker network create #{network_name}")
    end

    def network_exists?(network_name)
      x = MKIt::CmdRunner.run('docker network ls')
      x.match(/\b#{network_name}\b/)
    end

    def dettach_network(network_id, instance_id)
      MKIt::CmdRunner.run("docker network disconnect #{network_id} #{instance_id}")
    end

    def attach_network(network_id, instance_id)
      MKIt::CmdRunner.run("docker network connect #{network_id} #{instance_id}")
    end

    def remove_network(network_id)
      MKIt::CmdRunner.run("docker network rm #{network_id}")
    end

    #
    # volumes
    #

    def create_volume(volume_name)
      MKIt::CmdRunner.run("docker volume create #{volume_name}")
    end

    def delete_volume(volume_name)
      MKIt::CmdRunner.run("docker volume rm #{volume_name}")
    end

    def inspect_volume(volume_name)
      x = MKIt::CmdRunner.run("docker volume inspect #{volume_name}")
      JSON.parse(x).first
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mkit-0.4.2 lib/mkit/app/helpers/docker_helper.rb