Skip to content

Commit

Permalink
Add additional options for docToolchain container
Browse files Browse the repository at this point in the history
solves docToolchain#1338.

- add additional option to use a docker environment file
- add additional parameter to pass extra docker parameter.

Signed-off-by: rmi2hi <michael.rossner@de.bosch.com>
  • Loading branch information
MichaelRossner committed Jan 17, 2024
1 parent 35d0aec commit 75e0aa6
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ src/test/docs/structurizr/

resources/reveal.js
resources/asciidoctor-reveal.js

# VSC config file
.vscode/
1 change: 1 addition & 0 deletions changelog.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project tries to adhere to https://semver.org/spec/v2.0.0.html[Semantic
* https://github.com/docToolchain/docToolchain/issues/1335[#1335 publishToConfluence throws exception when uploading attachments in v3.2.0]

=== added
* https://github.com/docToolchain/docToolchain/issues/1338[#1338: extend the options for the docker environment by additional environment variables and additional parameter sets.]

=== changed

Expand Down
19 changes: 17 additions & 2 deletions dtcw
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ main() {
# No install command, so forward call to docToolchain but first we check if
# everything is there.
docker_image_name=""
docker_extra_arguments=""
if [[ ${environment} != docker ]]; then
assert_doctoolchain_installed "${environment}" "${DTC_VERSION}"
assert_java_version_supported
Expand All @@ -103,9 +104,15 @@ main() {
assert_argument_exists "$@"
fi
echo "Using docker image: ${docker_image_name}"
if [ "${1}" = "extra_arguments" ]; then
docker_extra_arguments="${2-}"
shift 2
assert_argument_exists "$@"
echo "Extra arguments passed to 'docker run' ${docker_extra_arguments}"
fi
fi

command=$(build_command "${environment}" "${DTC_VERSION}" "${docker_image_name}" "$@")
command=$(build_command "${environment}" "${DTC_VERSION}" "${docker_image_name}" "${docker_extra_arguments}" "$@")

[[ "${DTC_HEADLESS}" = true ]] && echo "Using headless mode since there is no (terminal) interaction possible"

Expand Down Expand Up @@ -595,7 +602,8 @@ build_command() {
local env=${1}
local version=${2}
local docker_image=${3}
shift 3
local docker_extra_arguments=${4}
shift 4
local cmd
if [ "${env}" = docker ]; then
# TODO: DTC_PROJECT_BRANCH is not passed into the docker environment
Expand All @@ -605,8 +613,15 @@ build_command() {
container_name+="-$(date '+%Y%m%d_%H%M%S')"
pwd=$(has cygpath && cygpath -w "${PWD}" || echo "${PWD}")

docker_env_file=dtcw_docker.env
env_file_option=""
if [ -f "$docker_env_file" ]; then
env_file_option="--env-file ${docker_env_file}"
fi

docker_args="run --rm -i --platform linux/amd64 -u $(id -u):$(id -g) --name ${container_name} \
-e DTC_HEADLESS=true -e DTC_SITETHEME -e DTC_PROJECT_BRANCH=${DTC_PROJECT_BRANCH} \
${docker_extra_arguments} ${env_file_option} \
--entrypoint /bin/bash -v '${pwd}:/project' ${docker_image}:v${version}"

cmd="docker ${docker_args} -c \"doctoolchain . ${*} ${DTC_OPTS} && exit\""
Expand Down
116 changes: 73 additions & 43 deletions dtcw.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ function main($_args) {

if (is_supported_environment $_args[0] -And assert_environment_available $_args[0] $DTC_VERSION) {
# User enforced environment
$environment= $_args[0]
$environment = $_args[0]
# shift 1
$null, $_args = $_args
assert_argument_exists $_args
} else {
}
else {
$environment = get_environment $_args
}
Write-Output "Using environment: $environment"
Expand All @@ -84,21 +85,23 @@ function main($_args) {
# shift 1
$null, $_args = $_args
install_component_and_exit $environment $_args
} elseif ( $_args[0] -eq "getJava" ) {
}
elseif ( $_args[0] -eq "getJava" ) {
# TODO: remove getJava in the next major release
Write-Warning "Warning: 'getJava' is deprecated and and will be removed. Use './dtcw install java' instead."
install_component_and_exit $environment "java"
}
# No install command, so forward call to docToolchain but first we check if
# everything is there.
$docker_image_name = ""
if ($environment -ne "docker")
{
$docker_extra_arguments = ""
if ($environment -ne "docker") {
assert_doctoolchain_installed "$environment" "$DTC_VERSION"
assert_java_version_supported
# TODO: what if 'doctoolchain' found by $PATH does not match the one from the local environment?
# The version provided by $DTC_VERSION could be a different one.
} else {
}
else {
$docker_image_name = "doctoolchain/doctoolchain"
if ( $_args[0] -eq "install" ) {
# shift 1
Expand All @@ -109,11 +112,20 @@ function main($_args) {
assert_argument_exists $_args
}
Write-Output "Using docker image: $docker_image_name"
if ($args[0] -eq "extra_arguments") {
# shift 1
$null, $_args = $_args
$docker_extra_arguments = $args[0]
# shift 1
$null, $_args = $_args
assert_argument_exists $args
Write-Host "Extra arguments passed to docker run $docker_extra_arguments"
}
}
# TODO: can generateDeck, bakePreview be used in combination with other commands?
# The code below assumes we have just one task.

$global:command = build_command "$environment" "$DTC_VERSION" "$docker_image_name" $_args
$global:command = build_command "$environment" "$DTC_VERSION" "$docker_image_name" "$docker_extra_arguments" $_args

#TODO: implement HEADLESS mode
# [[ "${DTC_HEADLESS}" = true ]] && echo "Using headless mode since there is no (terminal) interaction possible"
Expand Down Expand Up @@ -174,10 +186,11 @@ Use './dtcw.ps1 tasks' to see all tasks.
function print_version_info() {
Write-Host "dtcw ${DTCW_VERSION} - ${DTCW_GIT_HASH}"
if (is_doctoolchain_development_version ${DTC_VERSION}) {
$dtc_git_hash="unknown"
$dtc_git_hash=(git -C "$DTC_HOME" rev-parse --short=8 HEAD 2> $null)
$dtc_git_hash = "unknown"
$dtc_git_hash = (git -C "$DTC_HOME" rev-parse --short=8 HEAD 2> $null)
Write-Host "docToolchain ${DTC_VERSION} - ${dtc_git_hash}"
} else {
}
else {
Write-Host "docToolchain ${DTC_VERSION}"
}
Write-Host "OS/arch: pwsh $os $arch"
Expand All @@ -196,7 +209,8 @@ function get_available_environments() {
function has($global:command) {
if (Get-Command docker -ErrorAction SilentlyContinue) {
return $True
} else {
}
else {
return $False
}
}
Expand All @@ -210,7 +224,7 @@ function get_dtc_installations($envs, $version) {

if ($envs.Contains("docker")) {
# Having docker installed means docToolchain is available
$installations+=" docker"
$installations += " docker"
}

#TODO: was bedeutet das?
Expand All @@ -230,12 +244,12 @@ function is_supported_environment($environment) {

function assert_environment_available($environment, $version) {
# If environment not available, exit with error
if ((is_environment_available $environment) -eq $False)
{
if ((is_environment_available $environment) -eq $False) {
Write-Error "argument error - environment '$environment' not available"
if ($environment -eq "sdk") {
Write-Output "sdkman is not supported on windows"
} else {
}
else {
# docker
Write-Output "Install 'docker' on your host to execute docToolchain in a container."
}
Expand All @@ -257,7 +271,8 @@ function is_doctoolchain_development_version($version) {
# Alternatives: 'testing', 'dev' (used for development)
if ( ($version -eq "latest") -Or ($version -eq "latestdev") ) {
return $True
} else {
}
else {
return $False
}
}
Expand Down Expand Up @@ -345,16 +360,19 @@ function local_install_doctoolchain($version) {
if (Test-Path "$DTC_HOME/.git" ) {
Invoke-Expression "git -C ""$DTC_HOME"" pull"
Write-Output "Updated docToolchain in local environment to latest version"
} else {
}
else {
if ($version -eq "latest") {
Invoke-Expression "git clone ""${GITHUB_PROJECT_URL}.git"" ""$DTC_HOME"" "
} else {
}
else {
# TODO: derive the ssh URL from GITHUB_PROJECT_URL
Invoke-Expression "git clone git@github.com:docToolchain/docToolchain.git ""$DTC_HOME"" "
}
Write-Output "Cloned docToolchain in local environment to latest version"
}
} else {
}
else {
New-Item -Path ${DTC_ROOT} -ItemType "directory" -Force > $null
if ($proxy) {
# Pass Proxy-Settings to Gradle
Expand All @@ -370,26 +388,29 @@ function local_install_doctoolchain($version) {
}
# Use Proxy for downloading the distribution
Invoke-WebRequest $distribution_url -OutFile "${DTC_ROOT}\source.zip" -Proxy $proxy -ProxyUseDefaultCredentials
} else {
}
else {
Invoke-WebRequest $distribution_url -OutFile "${DTC_ROOT}\source.zip"
}
Expand-Archive -Force -LiteralPath "${DTC_ROOT}\source.zip" -DestinationPath "${DTC_ROOT}\"
Remove-Item "${DTC_ROOT}\source.zip" # << Remove .zip ?
if ($global:os -eq "windows") {
$global:command = "&'${DTC_ROOT}\docToolchain-$DTC_VERSION\bin\doctoolchain.bat' . $global:commandArgs $DTC_OPTS"
} else {
}
else {
$global:command = "&'${DTC_ROOT}\docToolchain-$DTC_VERSION\bin\doctoolchain' . $global:commandArgs $DTC_OPTS"
}
Write-Output "Installed docToolchain successfully in '${DTC_HOME}'."
}
# Add it to the existing installations so the output to guide the user can adjust accordingly.
$dtc_installations+=" local"
$dtc_installations += " local"
}

function assert_git_installed($message) {
if (has git) {
return
} else {
}
else {
Write-Warning "git - command not found"
Write-Warning $message
exit $ERR_DTCW
Expand Down Expand Up @@ -442,7 +463,8 @@ function assert_java_version_supported() {
unsupported Java version ${javaversion} [$JAVA_CMD]
"@
java_help_and_die
} else {
}
else {
if ([int]$javaversion -gt 17 ) {
Write-Warning @"
unsupported Java version ${javaversion} [$JAVA_CMD]
Expand All @@ -454,8 +476,7 @@ unsupported Java version ${javaversion} [$JAVA_CMD]
return
}

function java_help_and_die()
{
function java_help_and_die() {
Write-Host @"
docToolchain supports Java versions 11, 14 or 17 (preferred). In case one of those
Java versions is installed make sure 'java' is found with your PATH environment
Expand Down Expand Up @@ -490,30 +511,31 @@ function local_install_java() {

switch ($global:arch) {
'x86_64' { $global:arch = "x64" }
'arm64' { $global:arch = "aarch64"}
'arm64' { $global:arch = "aarch64" }
}
if ( ${os} -eq "MINGW64" ) {
Write-Error "MINGW64 is not supported, Please use powershell or WSL"
}
switch ($global:os) {
'Linux' { $global:os = 'linux' }
'Darwin' { $global:os = 'max' }
'Linux' { $global:os = 'linux' }
'Darwin' { $global:os = 'max' }
'Cygwin' { $global:os = 'linux' }
}
if ($global:os -eq 'windows') { $targetFile = 'jdk.zip'} else { $targetFile = 'jdk.tar.gz'}
if ($global:os -eq 'windows') { $targetFile = 'jdk.zip' } else { $targetFile = 'jdk.tar.gz' }

if(!(Test-Path -Path $DTC_JAVA_HOME )){
if (!(Test-Path -Path $DTC_JAVA_HOME )) {
New-Item -ItemType directory -Path $DTC_JAVA_HOME > $null
}
Write-Output "Downloading JDK Temurin $version [$global:os/$global:arch] from Adoptium to $DTC_JAVA_HOME/${targetFile}"
$adoptium_java_url="https://api.adoptium.net/v3/binary/latest/$version/$releasetype/$global:os/$global:arch/$imagetype/$implementation/$heapsize/eclipse?project=jdk"
$adoptium_java_url = "https://api.adoptium.net/v3/binary/latest/$version/$releasetype/$global:os/$global:arch/$imagetype/$implementation/$heapsize/eclipse?project=jdk"
download_file "$adoptium_java_url" "$DTC_JAVA_HOME/${targetFile}"
Write-Output "Extracting JDK from archive file."
# TODO: should we not delete a previsouly installed on?
# Otherwise we may get a mix of different Java versions.
if ($global:os -eq 'windows') {
Expand-Archive -Force -LiteralPath "$DTC_JAVA_HOME\${targetFile}" -DestinationPath "$DTC_JAVA_HOME"
} else {
}
else {
tar -zxf "${DTC_JAVA_HOME}/${targetFile}" --strip 1 -C "${DTC_JAVA_HOME}/."
}
Remove-Item "$DTC_JAVA_HOME\${targetFile}"
Expand Down Expand Up @@ -553,7 +575,7 @@ Java runtime (major version 11, 14 or 17) installed on your host.
"@
}

function build_command($environment, $version, $docker_image, $_args) {
function build_command($environment, $version, $docker_image, $docker_extra_arguments , $_args) {
$cmd = ""
if ( $environment -eq "docker") {
if (-not (Invoke-Expression "docker ps")) {
Expand All @@ -566,20 +588,28 @@ function build_command($environment, $version, $docker_image, $_args) {
Write-Output ""
exit 1
}
$container_name="doctoolchain-${version}-$(date -uFormat '+%Y%m%d_%H%M%S')"
$container_name = "doctoolchain-${version}-$(Get-date -uFormat '+%Y%m%d_%H%M%S')"
$docker_cmd = Get-Command docker

$docker_env_file = "dtcw_docker.env"
$env_file_option = ""
if (Test-Path $docker_env_file) {
$env_file_option = "--env-file ${docker_env_file}"
}

# TODO: DTC_PROJECT_BRANCH is not passed into the docker environment
# See https://github.com/docToolchain/docToolchain/issues/1087
$docker_args = "run --rm -i --name ${container_name} -e DTC_HEADLESS=1 -e DTC_SITETHEME -e DTC_PROJECT_BRANCH=${DTC_PROJECT_BRANCH} --entrypoint /bin/bash -v '${PWD}:/project' ${docker_image}:v${version}"
$docker_args = "run --rm -i --name ${container_name} -e DTC_HEADLESS=1 -e DTC_SITETHEME -e DTC_PROJECT_BRANCH=${DTC_PROJECT_BRANCH} $docker_extra_arguments $env_file_option --entrypoint /bin/bash -v '${PWD}:/project' ${docker_image}:v${version}"
$cmd = "$docker_cmd ${docker_args} -c ""doctoolchain . $_args ${DTC_OPTS} && exit "" "

} else {
}
else {
if ( $environment -eq "local" ) {
if ($global:os -eq "windows") {
$cmd="${DTC_HOME}/bin/doctoolchain.bat . $_args ${DTC_OPTS}"
} else {
$cmd="${DTC_HOME}/bin/doctoolchain . $_args ${DTC_OPTS}"
$cmd = "${DTC_HOME}/bin/doctoolchain.bat . $_args ${DTC_OPTS}"
}
else {
$cmd = "${DTC_HOME}/bin/doctoolchain . $_args ${DTC_OPTS}"
}
}
}
Expand All @@ -591,13 +621,13 @@ function show_os_related_info() {
}

# Location where local installations are placed.
$DTC_ROOT="$HOME/.doctoolchain"
$DTC_ROOT = "$HOME/.doctoolchain"

# More than one docToolchain version may be installed locally.
# This is the directory for the specific version.
$DTC_HOME="$DTC_ROOT/docToolchain-$DTC_VERSION"
$DTC_HOME = "$DTC_ROOT/docToolchain-$DTC_VERSION"

# Directory for local Java installation
$DTC_JAVA_HOME="$DTC_ROOT/jdk"
$DTC_JAVA_HOME = "$DTC_ROOT/jdk"

main $args
5 changes: 5 additions & 0 deletions dtcw_docker.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# set environment variables for docToolchain docker container
# see https://docs.docker.com/engine/reference/commandline/run/#env
#
# example:
# PROJECT=EXAMPLE_PROJECT

0 comments on commit 75e0aa6

Please sign in to comment.