cli/lib/rbld_engine.rb in rbld-1.0.2 vs cli/lib/rbld_engine.rb in rbld-1.1.0
- old
+ new
@@ -5,11 +5,13 @@
require 'os'
require_relative 'rbld_log'
require_relative 'rbld_config'
require_relative 'rbld_utils'
require_relative 'rbld_print'
-require_relative 'rbld_registry'
+require_relative 'rbld_reg_docker'
+require_relative 'rbld_reg_fs'
+require_relative 'rbld_fileops'
module Rebuild::Engine
extend Rebuild::Utils::Errors
class NamedDockerImage
@@ -269,75 +271,18 @@
rebuild_errors \
UnsupportedDockerService: 'Unsupported docker service: %s',
EnvironmentIsModified: 'Environment is modified, commit or checkout first',
EnvironmentNotKnown: 'Unknown environment %s',
NoChangesToCommit: 'No changes to commit for %s',
- EnvironmentLoadFailure: 'Failed to load environment from %s',
- EnvironmentSaveFailure: 'Failed to save environment %s to %s',
EnvironmentDeploymentFailure: 'Failed to deploy from %s',
EnvironmentAlreadyExists: 'Environment %s already exists',
EnvironmentNotFoundInTheRegistry: 'Environment %s does not exist in the registry',
RegistrySearchFailed: 'Failed to search in %s',
EnvironmentPublishCollision: 'Environment %s already published',
EnvironmentPublishFailure: 'Failed to publish on %s',
EnvironmentCreateFailure: 'Failed to create %s'
- class EnvironmentFile
- def initialize(filename, docker_api = Docker)
- @filename, @docker_api = filename, docker_api
- end
-
- def load!
- begin
- with_gzip_reader { |gz| Docker::Image.load(gz) }
- rescue => msg
- rbld_print.trace( msg )
- raise EnvironmentLoadFailure, @filename
- end
- end
-
- def save!(name, identity)
- begin
- with_gzip_writer do |gz|
- Docker::Image.save_stream( identity ) { |chunk| gz.write chunk }
- end
- rescue => msg
- rbld_print.trace( msg )
- raise EnvironmentSaveFailure, [name, @filename]
- end
- end
-
- private
-
- def with_gzip_writer
- begin
- File.open(@filename, 'w') do |f|
- f.binmode
- gz = Zlib::GzipWriter.new(f)
- begin
- yield gz
- ensure
- gz.close
- end
- end
- rescue
- FileUtils::safe_unlink( @filename )
- raise
- end
- end
-
- def with_gzip_reader
- Zlib::GzipReader.open( @filename ) do |gz|
- begin
- yield gz
- ensure
- gz.close
- end
- end
- end
- end
-
class DockerContext
def self.from_file(file)
base = %Q{
FROM scratch
ADD #{file} /
@@ -499,36 +444,36 @@
env = existing_env( env_name )
EnvironmentFile.new( filename ).save!(env_name, env.img.identity)
end
def search(env_name)
- rbld_print.progress "Searching in #{@cfg.remote!}..."
+ rbld_print.progress "Searching in #{@cfg.remote!.path}..."
begin
registry.search( env_name.name, env_name.tag )
rescue => msg
rbld_print.trace( msg )
- raise RegistrySearchFailed, @cfg.remote!
+ raise RegistrySearchFailed, @cfg.remote!.path
end
end
def deploy!(env_name)
nonexisting_env(env_name)
raise EnvironmentNotFoundInTheRegistry, env_name.full \
if registry.search( env_name.name, env_name.tag ).empty?
- rbld_print.progress "Deploying from #{@cfg.remote!}..."
+ rbld_print.progress "Deploying from #{@cfg.remote!.path}..."
begin
registry.deploy( env_name.name, env_name.tag ) do |img|
new_name = NameFactory.new(env_name).identity
img.tag( repo: new_name.name, tag: new_name.tag )
end
rescue => msg
rbld_print.trace( msg )
- raise EnvironmentDeploymentFailure, @cfg.remote!
+ raise EnvironmentDeploymentFailure, @cfg.remote!.path
end
@cache.refresh!
end
@@ -539,37 +484,37 @@
raise EnvironmentPublishCollision, env_name \
unless registry.search( env_name.name, env_name.tag ).empty?
begin
- rbld_print.progress "Publishing on #{@cfg.remote!}..."
- registry.publish( env.name, env.tag, env.img.api_obj )
+ rbld_print.progress "Publishing on #{@cfg.remote!.path}..."
+ registry.publish( env.name, env.tag, env.img )
rescue => msg
rbld_print.trace( msg )
- raise EnvironmentPublishFailure, @cfg.remote!
+ raise EnvironmentPublishFailure, @cfg.remote!.path
end
end
- def run(env_name, cmd)
+ def run(env_name, cmd, runopts = {})
env = existing_env( env_name )
- run_env_disposable( env, cmd )
+ run_env_disposable( env, cmd, runopts )
@cache.refresh!
@errno
end
- def modify!(env_name, cmd)
+ def modify!(env_name, cmd, runopts = {})
env = existing_env( env_name )
rbld_print.progress_start 'Initializing environment'
if env.modified?
rbld_log.info("Running container #{env.modification_container}")
- rerun_modification_cont(env, cmd)
+ rerun_modification_cont(env, cmd, runopts)
else
rbld_log.info("Running environment #{env.img}")
rbld_print.progress_end
- run_env(env, cmd)
+ run_env(env, cmd, runopts)
end
@cache.refresh!
@errno
end
@@ -665,12 +610,22 @@
raise UnsupportedDockerService, msg
end
end
def registry
- @registry ||= Rebuild::Registry::API.new( @cfg.remote! )
- @registry
+ return @registry if @registry
+
+ case @cfg.remote!.type
+ when 'docker'
+ reg_module = Rebuild::Registry::Docker
+ when 'rebuild'
+ reg_module = Rebuild::Registry::FS
+ else
+ raise "Remote type #{@cfg.remote!.type} is unknown"
+ end
+
+ @registry = reg_module::API.new( @cfg.remote!.path )
end
def run_external(cmdline)
rbld_log.info("Executing external command #{cmdline}")
system( cmdline )
@@ -704,27 +659,27 @@
-e REBUILD_GROUP_NAME=#{rs.group_name} \
-e REBUILD_USER_HOME=#{rs.home} \
-e REBUILD_PWD=#{rs.pwd} \
--security-opt label:disable \
#{trace_run_settings} \
+ #{opts[:privileged] ? "--privileged" : ""} \
#{opts[:rerun] ? env.rerun_img.id : env.img.id} \
"#{cmd.join(' ')}" \
}
end
- def run_env_disposable(env, cmd)
+ def run_env_disposable(env, cmd, runopts)
env.execution_container.remove! if env.execution_container
names = NameFactory.new(env)
cmdline = %Q{
- docker run \
- --rm \
- --name #{names.running} \
- --hostname #{names.hostname} \
- #{run_settings( env, cmd )} \
+ docker run \
+ --rm \
+ --name #{names.running} \
+ --hostname #{names.hostname} \
+ #{run_settings( env, cmd, runopts )} \
}
-
run_external( cmdline )
end
def run_env(env, cmd, opts = {})
names = NameFactory.new(env)
@@ -737,11 +692,11 @@
}
run_external( cmdline )
end
- def rerun_modification_cont(env, cmd)
+ def rerun_modification_cont(env, cmd, opts = {})
rbld_print.progress_tick
names = NameFactory.new( env )
new_img = env.modification_container.commit( names.rerun )
@@ -752,10 +707,10 @@
rbld_print.progress_end
@cache.refresh!
- run_env( @cache.get(env), cmd, rerun: true )
+ run_env( @cache.get(env), cmd, opts.merge(rerun: true) )
end
def existing_env(name)
env = @cache.get(name)
raise EnvironmentNotKnown, name.full unless env