class Lucian::Engine

Core module for Lucian framework is HERE !

Attributes

compose_data[R]
compose_directory[R]
compose_file[R]
docker_compose[R]
examples[R]
network_name[R]
running_services[R]

Public Class Methods

new(compose_file = nil, examples = []) click to toggle source

Initialize and fetch for compose file. if unable to find a docker-compose file then givving an error

# File lib/lucian/engine.rb, line 16
def initialize(compose_file = nil, examples = [])
  @compose_file = compose_file || fetch_docker_compose_file
  raise Error.new('Unable to find docker-compose.yml or docker-compose.yaml.') if (!@compose_file || !File.file?(@compose_file)) && ENV["LUCIAN_DOCKER"] == nil
  @compose_directory = File.expand_path(@compose_file+'/..')
  @compose_data = YAML.load_file(@compose_file) if ENV["LUCIAN_DOCKER"] == nil
  @lucian_directory = @compose_directory+'/'+DIRECTORY
  @lucian_helper = @lucian_directory+'/'+HELPER
  #@lucian_files = fetch_lucian_files(@lucian_directory)
  @network_name = File.basename(@compose_directory).gsub!(/[^0-9A-Za-z]?/, '')+"_default" if ENV["LUCIAN_DOCKER"] == nil
  $LOAD_PATH.unshift(@lucian_directory) unless $LOAD_PATH.include?(@lucian_directory)
  @docker_compose = Docker::Compose.new
  @examples = examples
  config_compose
  require 'lucian_helper' if File.exist?(@lucian_helper)
  @running_services ||= []
  Lucian.engine = self
end

Public Instance Methods

run() click to toggle source

Run

# File lib/lucian/engine.rb, line 36
def run
  BoardCaster.print("Start running Lucian ..", "yellow")
  RSpec.lucian_engine = self
  Lucian::Runner.invoke(self)
end
run_docker_service(services_names=[]) click to toggle source

Run and validate services status

# File lib/lucian/engine.rb, line 56
def run_docker_service(services_names=[])
  services_names.collect!(&:to_s)
  services_names = services_names - @running_services
  if services_names.count > 0
    @docker_compose.up(*services_names, {:detached => true})
    @running_services += services_names
    @running_services.uniq!
    exited = @docker_compose.ps.where { |c| !c.nil? && !c.up? && services_names.include?(c.image) }
    raise "We have some exited containers: " + exited.join(', ') if exited.count > 0
  end
end
run_lucian_test(example) click to toggle source

Run lucian test

# File lib/lucian/engine.rb, line 99
def run_lucian_test(example)
  # BoardCaster.print("Running lucian test ..", "yellow")
  Lucian.container.exec(['lucian', '--example', example])
end
shutdown() click to toggle source

Shutdown

# File lib/lucian/engine.rb, line 44
def shutdown
  # NOTE Check if running in docker or not
  if ENV["LUCIAN_DOCKER"] == nil
    stop_lucian_container
    remove_lucian_container
    @docker_compose.down
  end
  # remove_lucian_image # NOTE Not sure we need to remove this or not
end
start_lucian_docker() click to toggle source

Start lucian docker connect to compose

# File lib/lucian/engine.rb, line 91
def start_lucian_docker
  image = build_lucian_image
  container = run_lucian_image(image)
  connect_container_to_network(container)
end
stop_docker_service(services_names) click to toggle source

Stop docker service

# File lib/lucian/engine.rb, line 70
def stop_docker_service(services_names)
  if services_names.count > 0
    puts "\n"
    @docker_compose.stop(*services_names)
    @running_services -= services_names
    @running_services.uniq!
  end
end
stop_docker_service_if_not_in(services_names=[]) click to toggle source

Stop if not in

# File lib/lucian/engine.rb, line 81
def stop_docker_service_if_not_in(services_names=[])
  @running_services ||= []
  to_stop_service = @running_services - services_names
  if to_stop_service.count > 0
    stop_docker_service(to_stop_service)
  end
end

Private Instance Methods

build_lucian_image() click to toggle source

Build lucian docker image

# File lib/lucian/engine.rb, line 135
def build_lucian_image
  BoardCaster.print("Building lucian image ..", "yellow")
  FileUtils.cp(File.expand_path(File.expand_path(@compose_directory)+'/Gemfile'), @lucian_directory)
  image = Docker::Image.build_from_dir(@lucian_directory)
  FileUtils.rm_rf(File.expand_path(File.expand_path(@lucian_directory)+'/Gemfile'))
  Lucian.image = image
  return image
end
config_compose() click to toggle source

Config compose

# File lib/lucian/engine.rb, line 108
def config_compose
  @docker_compose.file = @compose_file
end
connect_container_to_network(container=Lucian.container) click to toggle source

Connect running container to compose network

# File lib/lucian/engine.rb, line 156
def connect_container_to_network(container=Lucian.container)
  raise "Container can not be nil" if container.nil?
  raise "Couldn't fetch docker-compose network's name" if @network_name.nil?
  network = Docker::Network.all.find do |nw|
    nw.info["Name"] == @network_name
  end
  raise "Couldn't find compose's network" if network.nil?
  begin
    network.connect(container.id)
    BoardCaster.print("Join lucian container to #{@network_name} network ..", "yellow")
  rescue Docker::Error::ServerError
  end
end
fetch_docker_compose_file(path = File.expand_path('.')) click to toggle source

Fetching compose file from curent directory and parents

# File lib/lucian/engine.rb, line 114
def fetch_docker_compose_file(path = File.expand_path('.'))
  files = Dir.glob(path+'/docker-compose.y*ml')
  files = fetch_docker_compose_file(File.expand_path(path+'/..')) if files.size == 0 && path != '/'
  if files.instance_of?(Array)
    compose_path = files[0] 
  else
    compose_path = files
  end
  return compose_path
end
remove_lucian_container(container=Lucian.container) click to toggle source

Remove docker container

# File lib/lucian/engine.rb, line 182
def remove_lucian_container(container=Lucian.container)
  unless container.nil?
    BoardCaster.print("Removing lucian contanier ..", "yellow")
    container.remove
    return true
  end
end
remove_lucian_image(image=Lucian.image) click to toggle source

Remove docker image

# File lib/lucian/engine.rb, line 172
def remove_lucian_image(image=Lucian.image)
  unless image.nil?
    BoardCaster.print("Removing lucian image ..", "yellow")
    image.remove
    return true
  end
end
run_lucian_image(image=Lucian.image) click to toggle source

Run lucian docker image

# File lib/lucian/engine.rb, line 146
def run_lucian_image(image=Lucian.image)
  raise "Image can not be nil" if image.nil?
  BoardCaster.print("Starting lucian container ..", "yellow")
  container = image.run
  Lucian.container = container
  return container
end
stop_lucian_container(container=Lucian.container) click to toggle source

Stop docker container

# File lib/lucian/engine.rb, line 192
def stop_lucian_container(container=Lucian.container)
  unless container.nil?
    BoardCaster.print("Stopping lucian contanier ..", "yellow")
    container.kill!
    return true
  end
end