diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..aeaa50e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true +charset = utf-8 +indent_size = 2 +indent_style = space +trim_trailing_whitespace = true +continuation_indent_size = 4 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..cb5ab64 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +* text=auto +*.bat text eol=crlf +gradlew text eol=lf diff --git a/.github/actions/docker-install-macos/action.yml b/.github/actions/docker-install-macos/action.yml new file mode 100644 index 0000000..7068fbb --- /dev/null +++ b/.github/actions/docker-install-macos/action.yml @@ -0,0 +1,17 @@ +--- +name: "Install Docker on macOS" +description: "Performs an unattended install of Docker Desktop for MacOS" +runs: + using: "composite" + steps: + # From https://github.com/docker/for-mac/issues/2359#issuecomment-943131345 + - run: | + brew install --cask docker + sudo /Applications/Docker.app/Contents/MacOS/Docker --unattended --install-privileged-components + open -a /Applications/Docker.app --args --unattended --accept-license + while ! /Applications/Docker.app/Contents/Resources/bin/docker info &>/dev/null; do sleep 1; done + shell: bash +branding: + icon: "tag" + color: "blue" +... diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..6337115 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,11 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "daily" + - package-ecosystem: "gradle" + directory: "/" + schedule: + interval: "daily" + open-pull-requests-limit: 20 diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..b024026 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,33 @@ +--- +name: Publish +on: + push: + branches: + - main +jobs: + publish: + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 1 + - name: Set up JDK + uses: actions/setup-java@v3 + with: + distribution: 'zulu' + java-version: 8 + cache: 'gradle' + - name: build publish + run: ./gradlew clean build publish --info --stacktrace + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + ORG_GRADLE_PROJECT_signingKey: ${{ secrets.GPG_SIGNING_KEY }} + ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.GPG_SIGNING_PASSWORD }} + - name: Publish Test Report + if: ${{ always() }} + uses: scacap/action-surefire-report@v1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + report_paths: '**/build/test-results/test/TEST-*.xml' +... diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..5f5e70f --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,48 @@ +--- +name: CI +on: + workflow_dispatch: + push: + branches-ignore: + - main +jobs: + ci-build: + strategy: + matrix: + os: [ ubuntu-latest, windows-latest, macos-latest ] + java: [ 8, 11, 17 ] + runs-on: ${{ matrix.os }} + timeout-minutes: 20 + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 1 + - name: Set up JDK + uses: actions/setup-java@v3 + with: + distribution: 'zulu' + java-version: ${{ matrix.java }} + cache: 'gradle' + - name: Install Docker on macOS + if: matrix.os == 'macos-latest' + uses: ./.github/actions/docker-install-macos + - name: Login to Docker Hub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: docker version + run: docker version + - name: docker info + run: docker info + - name: java version + run: java -version + - name: clean build + run: ./gradlew clean build --info --stacktrace + - name: Publish Test Report + if: ${{ always() }} + uses: scacap/action-surefire-report@v1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + report_paths: '**/build/test-results/test/TEST-*.xml' +... diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..c318f23 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,41 @@ +--- +name: Release +on: + release: + types: + - released +# - published + +jobs: + release: + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 1 + - name: Set up JDK + uses: actions/setup-java@v3 + with: + distribution: 'zulu' + java-version: 8 + cache: 'gradle' + - name: Set artifact version + run: | + echo "RELEASE_VERSION=$(echo '${{ github.event.release.tag_name }}' | sed -e s/^v//)" >> $GITHUB_ENV + - name: build publish + run: ./gradlew clean build publish closeAndReleaseStagingRepository --info --stacktrace -Pversion="${{ env.RELEASE_VERSION }}" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + ORG_GRADLE_PROJECT_signingKey: ${{ secrets.GPG_SIGNING_KEY }} + ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.GPG_SIGNING_PASSWORD }} + SONATYPE_STAGING_PROFILE_ID: ${{ secrets.SONATYPE_STAGING_PROFILE_ID }} + SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }} + SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} + - name: Publish Test Report + if: ${{ always() }} + uses: scacap/action-surefire-report@v1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + report_paths: '**/build/test-results/test/TEST-*.xml' +... diff --git a/.github/workflows/update-gradle-wrapper.yml b/.github/workflows/update-gradle-wrapper.yml new file mode 100644 index 0000000..bcaeb4c --- /dev/null +++ b/.github/workflows/update-gradle-wrapper.yml @@ -0,0 +1,18 @@ +name: Update Gradle Wrapper + +on: + workflow_dispatch: + schedule: + # "weekly" https://crontab.guru/every-week + - cron: "0 0 * * 0" + +jobs: + update-gradle-wrapper: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Update Gradle Wrapper + uses: gradle-update/update-gradle-wrapper-action@v1 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + - uses: gradle/wrapper-validation-action@v1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fffb395 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +.gradle +.idea +*.iml +build/ +classes/ +.DS_Store +out/ +*.settings +*.project +*bin/ +*.classpath diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000..68f98a1 --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,15 @@ +## Publishing + +Packages are automatically published from the `main` branch to the GitHub Package Registry. +Manually cut releases will be published to both GitHub Package Registry and Maven Central. + +## Release Workflow + +There are multiple GitHub Action Workflows for the different steps in the package's lifecycle: + +- CI: Builds and checks incoming changes on a pull request + - triggered on every push to a non-default branch +- CD: Publishes the Gradle artifacts to GitHub Package Registry + - triggered only on pushes to the default branch +- Release: Publishes Gradle artifacts to Sonatype and releases them to Maven Central + - triggered on a published GitHub release using the underlying tag as artifact version, e.g. via `git tag -m "$MESSAGE" v$(date +"%Y-%m-%dT%H-%M-%S")` diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..f86229f --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,66 @@ +import java.text.SimpleDateFormat +import java.util.* + +rootProject.extra.set("artifactVersion", SimpleDateFormat("yyyy-MM-dd\'T\'HH-mm-ss").format(Date())) + +plugins { + id("maven-publish") + id("com.github.ben-manes.versions") version "0.42.0" + id("net.ossindex.audit") version "0.4.11" + id("io.freefair.maven-central.validate-poms") version "6.4.1" + id("io.github.gradle-nexus.publish-plugin") version "1.1.0" +} + +val dependencyVersions = listOf( + "org.jetbrains:annotations:23.0.0" +) + +val dependencyVersionsByGroup = mapOf( +) + +subprojects { + repositories { +// mavenLocal() +// fun findProperty(s: String) = project.findProperty(s) as String? +// maven { +// name = "github" +// setUrl("https://maven.pkg.github.com/docker-client/*") +// credentials { +// username = System.getenv("PACKAGE_REGISTRY_USER") ?: findProperty("github.package-registry.username") +// password = System.getenv("PACKAGE_REGISTRY_TOKEN") ?: findProperty("github.package-registry.password") +// } +// } + mavenCentral() + } +} + +allprojects { + configurations.all { + resolutionStrategy { + failOnVersionConflict() + force(dependencyVersions) + eachDependency { + val forcedVersion = dependencyVersionsByGroup[requested.group] + if (forcedVersion != null) { + useVersion(forcedVersion) + } + } + } + } +} + +fun findProperty(s: String) = project.findProperty(s) as String? + +val isSnapshot = project.version == "unspecified" +nexusPublishing { + repositories { + if (!isSnapshot) { + sonatype { + // 'sonatype' is pre-configured for Sonatype Nexus (OSSRH) which is used for The Central Repository + stagingProfileId.set(System.getenv("SONATYPE_STAGING_PROFILE_ID") ?: findProperty("sonatype.staging.profile.id")) //can reduce execution time by even 10 seconds + username.set(System.getenv("SONATYPE_USERNAME") ?: findProperty("sonatype.username")) + password.set(System.getenv("SONATYPE_PASSWORD") ?: findProperty("sonatype.password")) + } + } + } +} diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..04ebd19 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,14 @@ +org.gradle.daemon=true + +group=de.gesellix + +github.package-registry.owner=docker-client +github.package-registry.repository=docker-registry +github.package-registry.username= +github.package-registry.password= + +sonatype.snapshot.url=https://oss.sonatype.org/content/repositories/snapshots/ +sonatype.staging.url=https://oss.sonatype.org/service/local/staging/deploy/maven2/ +sonatype.staging.profile.id= +sonatype.username= +sonatype.password= diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..41d9927 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..41dfb87 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..1b6c787 --- /dev/null +++ b/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts new file mode 100644 index 0000000..4a651c8 --- /dev/null +++ b/lib/build.gradle.kts @@ -0,0 +1,129 @@ +import java.text.SimpleDateFormat +import java.util.* + +plugins { + id("groovy") + id("java-library") + id("maven-publish") + id("signing") + id("com.github.ben-manes.versions") + id("net.ossindex.audit") + id("io.freefair.maven-central.validate-poms") +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} + +dependencies { + constraints { + listOf( + "org.jetbrains.kotlin:kotlin-reflect", + "org.jetbrains.kotlin:kotlin-stdlib", + "org.jetbrains.kotlin:kotlin-stdlib-jdk8", + "org.jetbrains.kotlin:kotlin-stdlib-common", + "org.jetbrains.kotlin:kotlin-test" + ).onEach { + implementation(it) { + version { + strictly("[1.5,1.7)") + prefer("1.6.10") + } + } + } + implementation("com.squareup.okio:okio") { + version { + strictly("[2.5,4)") + prefer("3.0.0") + } + } + } + implementation("de.gesellix:docker-remote-api-client:2022-02-23T13-45-00") + implementation("de.gesellix:docker-remote-api-model-1-41:2022-02-23T11-47-00") + implementation("de.gesellix:docker-engine:2022-02-22T23-12-00") + + implementation("org.slf4j:slf4j-api:[1.7,)!!1.7.36") + testImplementation("ch.qos.logback:logback-classic:[1.2,2)!!1.2.10") + + testImplementation("org.codehaus.groovy:groovy:[3,4)!!3.0.9") + testImplementation("org.spockframework:spock-core:2.1-groovy-3.0") +} + +tasks.withType { + useJUnitPlatform() +} + +val javadocJar by tasks.registering(Jar::class) { + dependsOn("classes") + archiveClassifier.set("javadoc") + from(tasks.javadoc) +} + +val sourcesJar by tasks.registering(Jar::class) { + dependsOn("classes") + archiveClassifier.set("sources") + from(sourceSets.main.get().allSource) +} + +artifacts { + add("archives", sourcesJar.get()) + add("archives", javadocJar.get()) +} + +fun findProperty(s: String) = project.findProperty(s) as String? + +val isSnapshot = project.version == "unspecified" +val artifactVersion = if (!isSnapshot) project.version as String else SimpleDateFormat("yyyy-MM-dd\'T\'HH-mm-ss").format(Date())!! +val publicationName = "dockerRegistry" +publishing { + repositories { + maven { + name = "GitHubPackages" + url = uri("https://maven.pkg.github.com/${property("github.package-registry.owner")}/${property("github.package-registry.repository")}") + credentials { + username = System.getenv("GITHUB_ACTOR") ?: findProperty("github.package-registry.username") + password = System.getenv("GITHUB_TOKEN") ?: findProperty("github.package-registry.password") + } + } + } + publications { + register(publicationName, MavenPublication::class) { + pom { + name.set("docker-registry") + description.set("Docker Registry/Distribution abstraction") + url.set("https://github.com/docker-client/docker-registry") + licenses { + license { + name.set("MIT") + url.set("https://opensource.org/licenses/MIT") + } + } + developers { + developer { + id.set("gesellix") + name.set("Tobias Gesellchen") + email.set("tobias@gesellix.de") + } + } + scm { + connection.set("scm:git:github.com/docker-client/docker-registry.git") + developerConnection.set("scm:git:ssh://github.com/docker-client/docker-registry.git") + url.set("https://github.com/docker-client/docker-registry") + } + } + artifactId = "docker-docker-registry" + version = artifactVersion + from(components["java"]) + artifact(sourcesJar.get()) + artifact(javadocJar.get()) + } + } +} + +signing { + val signingKey: String? by project + val signingPassword: String? by project + useInMemoryPgpKeys(signingKey, signingPassword) + sign(publishing.publications[publicationName]) +} diff --git a/lib/src/main/java/de/gesellix/docker/registry/DockerRegistry.java b/lib/src/main/java/de/gesellix/docker/registry/DockerRegistry.java new file mode 100644 index 0000000..270d527 --- /dev/null +++ b/lib/src/main/java/de/gesellix/docker/registry/DockerRegistry.java @@ -0,0 +1,119 @@ +package de.gesellix.docker.registry; + +import de.gesellix.docker.engine.DockerClientConfig; +import de.gesellix.docker.remote.api.ContainerCreateRequest; +import de.gesellix.docker.remote.api.ContainerCreateResponse; +import de.gesellix.docker.remote.api.ContainerInspectResponse; +import de.gesellix.docker.remote.api.HostConfig; +import de.gesellix.docker.remote.api.PortBinding; +import de.gesellix.docker.remote.api.client.ContainerApi; +import de.gesellix.docker.remote.api.client.ImageApi; +import de.gesellix.docker.remote.api.core.ClientException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonList; + +public class DockerRegistry { + + private static final Logger log = LoggerFactory.getLogger(DockerRegistry.class); + + private final ContainerApi containerApi; + private final ImageApi imageApi; + + private String registryId; + + public DockerRegistry() { + DockerClientConfig dockerClientConfig = new DockerClientConfig(); + this.containerApi = new ContainerApi(dockerClientConfig); + this.imageApi = new ImageApi(dockerClientConfig); + } + + public String getImageName() { + return LocalDocker.isNativeWindows() ? "gesellix/registry" : "registry"; + } + + public String getImageTag() { + return LocalDocker.isNativeWindows() ? "2.7.1-windows" : "2.7.1"; + } + + public void run() { + ContainerCreateRequest containerConfig = new ContainerCreateRequest(); + containerConfig.setImage(getImageName() + ":" + getImageTag()); + containerConfig.setEnv(singletonList("REGISTRY_VALIDATION_DISABLED=true")); + Map exposedPorts = new HashMap<>(); + exposedPorts.put("5000/tcp", emptyMap()); + containerConfig.setExposedPorts(exposedPorts); + HostConfig hostConfig = new HostConfig(); + hostConfig.setPublishAllPorts(true); + containerConfig.setHostConfig(hostConfig); + + ContainerCreateResponse registryStatus = run(containerConfig); + registryId = registryStatus.getId(); + } + + ContainerCreateResponse run(ContainerCreateRequest containerCreateRequest) { + log.info("docker run {}", containerCreateRequest.getImage()); + ContainerCreateResponse createContainerResponse = createContainer(containerCreateRequest); + log.debug("create container result: {}", createContainerResponse); + String containerId = createContainerResponse.getId(); + containerApi.containerStart(containerId, null); + return createContainerResponse; + } + + ContainerCreateResponse createContainer(ContainerCreateRequest containerCreateRequest) { + log.debug("docker create"); + try { + return containerApi.containerCreate(containerCreateRequest, null); + } + catch (ClientException exception) { + if (exception.getStatusCode() == 404) { + log.debug("Image '{}' not found locally.", containerCreateRequest.getImage()); + imageApi.imageCreate(containerCreateRequest.getImage(), + null, null, null, null, + null, null, null, null); + return containerApi.containerCreate(containerCreateRequest, null); + } + throw exception; + } + } + + public String address() { +// String dockerHost = dockerClient.config.dockerHost +// return dockerHost.replaceAll("^(tcp|http|https)://", "").replaceAll(":\\d+\$", "") + +// def registryContainer = dockerClient.inspectContainer(registryId).content +// def portBinding = registryContainer.NetworkSettings.Ports["5000/tcp"] +// return portBinding[0].HostIp as String + + // 'localhost' allows to use the registry without TLS + return "localhost"; + } + + public int port() { + ContainerInspectResponse registryContainer = containerApi.containerInspect(registryId, null); + List portBinding = registryContainer.getNetworkSettings().getPorts().get("5000/tcp"); + PortBinding firstPortBinding = portBinding.stream() + .findFirst() + .orElseThrow(() -> new RuntimeException("No PortBinding for port 5000/tcp")); + if (firstPortBinding.getHostPort() == null) { + throw new RuntimeException("Null PortBinding for port 5000/tcp"); + } + return Integer.parseInt(firstPortBinding.getHostPort()); + } + + public String url() { + return address() + ":" + port(); + } + + public void rm() { + containerApi.containerStop(registryId, null); + containerApi.containerWait(registryId, null); + containerApi.containerDelete(registryId, null, null, null); + } +} diff --git a/lib/src/main/java/de/gesellix/docker/registry/LocalDocker.java b/lib/src/main/java/de/gesellix/docker/registry/LocalDocker.java new file mode 100644 index 0000000..3be212c --- /dev/null +++ b/lib/src/main/java/de/gesellix/docker/registry/LocalDocker.java @@ -0,0 +1,39 @@ +package de.gesellix.docker.registry; + +import de.gesellix.docker.engine.DockerClientConfig; +import de.gesellix.docker.remote.api.SystemVersion; +import de.gesellix.docker.remote.api.client.SystemApi; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LocalDocker { + + private static final Logger log = LoggerFactory.getLogger(LocalDocker.class); + + public static boolean available() { + try { + DockerClientConfig dockerClientConfig = new DockerClientConfig(); + SystemApi systemApi = new SystemApi(dockerClientConfig); + return "OK".equals(systemApi.systemPing()); + } + catch (Exception e) { + log.info("Docker not available", e); + return false; + } + } + + public static boolean isNativeWindows() { + DockerClientConfig dockerClientConfig = new DockerClientConfig(); + SystemApi systemApi = new SystemApi(dockerClientConfig); + try { + SystemVersion systemVersion = systemApi.systemVersion(); + String arch = systemVersion.getArch(); + String os = systemVersion.getOs(); + return "windows/amd64".equals(os + "/" + arch); + } + catch (Exception e) { + log.info("Docker not available", e); + throw new RuntimeException(e); + } + } +} diff --git a/lib/src/test/groovy/de/gesellix/docker/registry/DockerRegistrySpec.groovy b/lib/src/test/groovy/de/gesellix/docker/registry/DockerRegistrySpec.groovy new file mode 100644 index 0000000..7a8cbe6 --- /dev/null +++ b/lib/src/test/groovy/de/gesellix/docker/registry/DockerRegistrySpec.groovy @@ -0,0 +1,23 @@ +package de.gesellix.docker.registry + +import spock.lang.Requires +import spock.lang.Specification + +@Requires({ LocalDocker.available() }) +class DockerRegistrySpec extends Specification { + + def "can determine registry url"() { + given: + DockerRegistry registry = new DockerRegistry() + registry.run() + + when: + String registryUrl = registry.url() + + then: + registryUrl =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|localhost:\d{1,5}\u0024/ + + cleanup: + registry.rm() + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 0000000..f7e7b43 --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1,2 @@ +rootProject.name = "docker-registry" +include("lib")