shell/lib/shell/docker/docker.rb in sct-0.1.22 vs shell/lib/shell/docker/docker.rb in sct-0.1.23
- old
+ new
@@ -2,20 +2,21 @@
require "shell/ClassLevelInheritableAttributes"
module Shell
class Docker
include ClassLevelInheritableAttributes
- inheritable_attributes :image, :is_private_registry, :entrypoint, :volumes, :ports, :extra_arguments
+ inheritable_attributes :image, :is_private_registry, :entrypoint, :volumes, :ports, :extra_arguments, :env_variables
@image = nil
@is_private_registry = false
@entrypoint = nil
@volumes = {}
@ports = {}
@user_group = []
@extra_arguments = {}
+ @env_variables = {}
###
# The Docker command configuration. This has to be able to execute the command properly
###
def self.config
@@ -25,60 +26,60 @@
###
# Execute the configured command
###
def self.exec(cli_arguments)
- self.addExtraArgument(cli_arguments)
+ self.add_extra_argument(cli_arguments)
- self.config()
+ self.config
# Check if the basic variables are set
if !@image || @image.empty?
raise LoadError, "The required image is not defined for this command"
end
- if !self.checkForImage() && @is_private_registry
- if !self.loginToPrivateRegistry()
+ if !self.check_for_image && @is_private_registry
+ unless self.login_to_private_registry
raise RuntimeError, "Could not authenticate to GCR (" + $?.exitstatus + ")"
end
end
- self.runCommand()
+ self.run_command
end
###
# The image for this Docker command
###
- def self.setImage(image, is_private_registry=false)
+ def self.set_image(image, is_private_registry=false)
@image = image
@is_private_registry = is_private_registry
end
###
# Set the current PWD as a volume on the specified path in the container
###
- def self.setPwdAsVolume(volume_path)
+ def self.set_pwd_as_volume(volume_path)
pwd = `pwd -P`
if SctCore::Helper.operatingSystem == SctCore::Helper::WINDOWS
pwd=SctCore::Helper.convertWSLToWindowsPath(pwd)
end
- addVolume(pwd, volume_path)
+ add_volume(pwd, volume_path)
end
###
# Add an extra volume in the container
###
- def self.addVolume(localPath, volume_path)
- @volumes[localPath] = volume_path
+ def self.add_volume(local_path, volume_path)
+ @volumes[local_path] = volume_path
end
###
# Add a port mapping for the container
###
- def self.mapPort(external_port, container_port = nil)
+ def self.map_port(external_port, container_port = nil)
external_port=String(external_port)
container_port=String(container_port)
if container_port.empty?
@@ -89,111 +90,129 @@
end
###
# Set the current user and group in the container
###
- def self.setCurrentUserAndGroup
+ def self.set_current_user_and_group
@user_group = []
@user_group << `id -u`
@user_group << `id -g`
end
###
# Set the entrypoint for the command in the container
###
- def self.setEntrypoint(entrypoint)
+ def self.set_entrypoint(entrypoint)
@entrypoint=entrypoint
end
###
# Add extra arguments to be configured for the container
###
- def self.addExtraArgument(argument, value=nil)
+ def self.add_extra_argument(argument, value=nil)
argument = String(argument)
if value
value = String(value)
end
self.extra_arguments[argument] = value
end
###
+ # Set environment variables for the container
+ ###
+ def self.set_environment_variables(env_variables)
+ @env_variables = env_variables
+ end
+
+ ###
# Check if the image is already present in the system
###
- def self.checkForImage
+ def self.check_for_image
image_list=`docker images -q #{@image} 2> /dev/null`
return image_list && !image_list.empty?
end
###
# Login to the private container registry with the cloud credentials
###
- def self.loginToPrivateRegistry
+ def self.login_to_private_registry
`docker login -u oauth2accesstoken -p "$(gcloud auth application-default print-access-token)" https://eu.gcr.io &> /dev/null`
return $?.success?
end
###
# Run the command
###
- def self.runCommand()
- command = self.compileCommand()
+ def self.run_command
+ command = self.compile_command
SctCore::CommandExecutor.execute(command: command, print_all: true, suppress_output: false)
end
###
# Compile the command with all options
###
- def self.compileCommand()
+ def self.compile_command
# Basic docker run command
- command = self.addToCommandString("" , "docker run --rm -it")
+ command = self.add_to_command_string("" , "docker run --rm -it")
+ # Attach environment variables
+ if @env_variables
+ @env_variables.each do |key, value|
+ if value && !value.empty?
+ command = self.add_to_command_string(command, "-e " + key+"="+value)
+ else
+ command = self.add_to_command_string("-e " + key, value)
+ end
+ end
+ end
+
# Attach configured volumes
if @volumes
@volumes.each do |local_path, container_path|
- command = self.addToCommandString(command, "--volume " + local_path +":"+ container_path)
+ command = self.add_to_command_string(command, "--volume " + local_path +":"+ container_path)
end
end
# Map configured ports
if @ports
@ports.each do |external_port, container_port|
- command = self.addToCommandString(command, "-p " + external_port +":"+ container_port)
+ command = self.add_to_command_string(command, "-p " + external_port +":"+ container_port)
end
end
if @user_group
- command = self.addToCommandString(command, "--user "+ String(@user_group.shift()) +":"+ String(@user_group.shift()))
+ command = self.add_to_command_string(command, "--user "+ String(@user_group.shift) +":"+ String(@user_group.shift))
end
# Add image to command
- command = self.addToCommandString(command, @image)
+ command = self.add_to_command_string(command, @image)
if @entrypoint
- command = self.addToCommandString(command, String(@entrypoint))
+ command = self.add_to_command_string(command, String(@entrypoint))
end
# Append arguments and options to command
if @extra_arguments
@extra_arguments.each do |argument, value|
if value && !value.empty?
- command = self.addToCommandString(command, argument+"="+value)
+ command = self.add_to_command_string(command, argument+"="+value)
else
- command = self.addToCommandString(command, argument)
+ command = self.add_to_command_string(command, argument)
end
end
end
return command
end
###
# Append a part to the command string and remove any unwanted characters
###
- def self.addToCommandString(command_string, append)
+ def self.add_to_command_string(command_string, append)
return command_string + append.gsub(/\r?\n/, "") + " "
end
end
end