Sha256: 439d443a660491486252640216f2f352dee7606703caed70f888ebbcc6c16936

Contents?: true

Size: 1.98 KB

Versions: 2

Compression:

Stored size: 1.98 KB

Contents

require 'digest/sha1'

module VagrantPlugins
  module Vocker
    class DockerClient
      def initialize(machine)
        @machine = machine
      end

      def pull_images(*images)
        @machine.communicate.tap do |comm|
          images.each do |image|
            comm.sudo("docker images | grep -q #{image} || docker pull #{image}")
          end
        end
      end

      def start_service
        if !daemon_running? && @machine.guest.capability?(:docker_start_service)
          @machine.guest.capability(:docker_start_service)
        end
      end

      def daemon_running?
        @machine.communicate.test('test -f /var/run/docker.pid')
      end

      def run(containers)
        containers.each do |name, config|
          cids_dir = "/var/lib/vocker/cids"
          config[:cidfile] ||= "#{cids_dir}/#{Digest::SHA1.hexdigest name}"

          @machine.communicate.sudo("mkdir -p #{cids_dir}")
          run_container(config)
        end
      end

      def run_container(config)
        raise "Container's cidfile was not provided!" unless config[:cidfile]

        id = "$(cat #{config[:cidfile]})"

        if container_exist?(id)
          start_container(id)
        else
          create_container(config)
        end
      end

      def container_exist?(id)
        @machine.communicate.test("sudo docker ps -a -q | grep -q #{id}")
      end

      def start_container(id)
        unless container_running?(id)
          @machine.communicate.sudo("docker start #{id}")
        end
      end

      def container_running?(id)
        @machine.communicate.test("sudo docker ps -q | grep #{id}")
      end

      def create_container(config)
        args = "-cidfile=#{config[:cidfile]} -d"
        args << " -dns=#{config[:dns]}" if config[:dns]
        args << " #{config[:additional_run_args]}" if config[:additional_run_args]
        @machine.communicate.sudo %[
          rm -f #{config[:cidfile]}
          docker run #{args} #{config[:image]} #{config[:cmd]}
        ]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vocker-0.3.3 lib/vocker/docker_client.rb
vocker-0.3.2 lib/vocker/docker_client.rb