Compare commits

..

No commits in common. "master" and "1.4.2" have entirely different histories.

348 changed files with 8938 additions and 25149 deletions

6
.gitignore vendored
View File

@ -28,7 +28,6 @@ build/
out/
*.iml
config/fleet.properties
/config/fleet_static/*
src/main/resources/assets/js/all*.js
src/main/resources/assets/css/all*.css
src/main/resources/log4j2.xml
@ -36,7 +35,4 @@ src/main/resources/log4j2.xml
.classpath
.project
.settings/
.vscode/
**/.DS_Store
.settings/

View File

@ -27,6 +27,3 @@ A specific version mask can be applied to an image, which will override the defa
## Documentation
Full documentation can be found here: https://docs.linuxserver.io/general/fleet
# Thanks
A huge thank you to JetBrains who kindly provided us with a license for IntelliJ Ultimate. It is without question the best IDE for Java application development.

View File

@ -1,44 +1,63 @@
plugins {
id 'java'
id 'com.eriwen.gradle.js' version '2.14.1'
id 'com.eriwen.gradle.css' version '2.14.0'
}
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
version = '2.3.3'
version = '1.4.2'
sourceSets {
main {
java {
srcDir 'src/main/java'
}
}
test {
java {
srcDir 'src/test/java'
}
}
}
dependencies {
// Logging
implementation 'org.apache.logging.log4j:log4j-api:2.19.0'
implementation 'org.apache.logging.log4j:log4j-core:2.19.0'
implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.19.0'
runtime 'org.apache.logging.log4j:log4j-api:2.11.1'
runtime 'org.apache.logging.log4j:log4j-core:2.11.1'
compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.11.1'
// HTTP Framework
implementation 'io.javalin:javalin:3.6.0'
implementation 'org.freemarker:freemarker:2.3.31'
implementation 'org.apache.httpcomponents:httpclient:4.5.13'
compile 'com.sparkjava:spark-core:2.7.2'
compile 'com.sparkjava:spark-template-freemarker:2.7.1'
compile 'org.apache.httpcomponents:httpclient:4.5.7'
// JSON Mapping/Marshalling
implementation 'com.fasterxml.jackson.core:jackson-databind:2.9.6'
implementation 'com.fasterxml.jackson.core:jackson-core:2.9.6'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.9.6'
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.6'
compile 'com.fasterxml.jackson.core:jackson-core:2.9.6'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.9.6'
// Database
runtimeOnly 'org.mariadb.jdbc:mariadb-java-client:3.0.6'
implementation 'org.flywaydb:flyway-core:8.2.0'
implementation 'com.zaxxer:HikariCP:5.0.1'
compile 'com.zaxxer:HikariCP:3.3.1'
compile 'org.mariadb.jdbc:mariadb-java-client:2.4.0'
compile 'org.flywaydb:flyway-core:6.0.0-beta'
// MISC
implementation 'org.apache.commons:commons-lang3:3.12.0'
compile 'org.apache.commons:commons-lang3:3.7'
// Unit Testing
testImplementation 'junit:junit:4.11'
testImplementation 'org.mockito:mockito-core:4.8.0'
testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-core:1.10.19'
}
jar {
@ -48,12 +67,51 @@ jar {
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
duplicatesStrategy = DuplicatesStrategy.INCLUDE
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
exclude 'META-INF/INDEX.LIST', 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
combineJs {
encoding = "UTF-8"
source = [
'src/main/resources/assets/js/jquery-3.3.1.min.js',
'src/main/resources/assets/js/bootstrap.bundle.min.js',
'src/main/resources/assets/js/fontawesome-all.min.js',
'src/main/resources/assets/js/jquery.tablesorter.js',
'src/main/resources/assets/js/fleet.js'
]
dest = file('src/main/resources/assets/js/all.js')
}
minifyJs {
source = combineJs
dest = file('src/main/resources/assets/js/all.min.js')
closure {
warningLevel = 'QUIET'
}
}
combineCss {
source = [
'src/main/resources/assets/css/bootstrap.css',
'src/main/resources/assets/css/fleet.css',
'src/main/resources/assets/css/fontawesome-all.css'
]
dest = file('src/main/resources/assets/css/all.css')
}
minifyCss {
source = combineCss
dest = file('src/main/resources/assets/css/all.min.css')
}
task configureLogConfiguration(type: Copy) {
@ -65,21 +123,4 @@ task configureLogConfiguration(type: Copy) {
rename "log4j2.${logFile}.xml", 'log4j2.xml'
}
task buildVersionProperties() {
doFirst {
def versionProperties = new Properties()
def propFile = new File("src/main/resources/version.properties")
versionProperties.load(propFile.newDataInputStream());
versionProperties.setProperty("app.version", project.version.toString())
versionProperties.setProperty("app.build.user", System.getProperty("user.name"))
versionProperties.setProperty("app.build.date", new Date().format("yyyy-MM-dd'T'HH:mm:ss"))
versionProperties.setProperty("app.build.os", System.getProperty("os.name"))
versionProperties.store(propFile.newWriter(), null)
}
}
processResources.dependsOn configureLogConfiguration, buildVersionProperties
build.dependsOn minifyCss, minifyJs, configureLogConfiguration

View File

@ -3,6 +3,15 @@
# Runtime
fleet.app.port=8080
fleet.refresh.interval=60
# If set to DATABASE, fleet.admin.username and fleet.admin.password are not used. They can be omitted.
fleet.admin.authentication.type=PROPERTIES|DATABASE
# User for management of images and repositories
# CHANGE THESE!!
fleet.admin.username=test
fleet.admin.password=test
# Database Connectivity
fleet.database.driver=org.mariadb.jdbc.Driver
@ -10,7 +19,6 @@ fleet.database.url=jdbc:mariadb://<IP_OR_URL>:3306/fleet
fleet.database.username=<fleet_sql_user>
fleet.database.password=<fleet_sql_password>
# DockerHub auth
fleet.dockerhub.auth.enabled=true
fleet.dockerhub.username=YOUR_USERNAME
fleet.dockerhub.password=YOUR_PASSWORD_OR_AUTH_TOKEN
# Docker Hub
fleet.dockerhub.username=<username_for_your_dockerhub_account>
fleet.dockerhub.password=<password_for_your_dockerhub_account>

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

286
gradlew vendored
View File

@ -1,129 +1,78 @@
#!/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.
#
#!/usr/bin/env sh
##############################################################################
#
# 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/.
#
##
## Gradle start up script for UN*X
##
##############################################################################
# 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
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
APP_NAME="Gradle"
APP_BASE_NAME=${0##*/}
APP_BASE_NAME=`basename "$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"'
DEFAULT_JVM_OPTS=""
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
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 ;;
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
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
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD=$JAVA_HOME/bin/java
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
@ -132,7 +81,7 @@ Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
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
@ -140,95 +89,84 @@ 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" )
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
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
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
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.
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@"
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# 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.
#
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Escape application args
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=$(save "$@")
# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi
exec "$JAVACMD" "$@"

43
gradlew.bat vendored
View File

@ -1,19 +1,3 @@
@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
@ -29,18 +13,15 @@ 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"
set DEFAULT_JVM_OPTS=
@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
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
@ -54,7 +35,7 @@ goto fail
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
@ -64,14 +45,28 @@ echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windows variants
if not "%OS%" == "Windows_NT" goto win9xME_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
: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 %*
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell

View File

@ -17,25 +17,15 @@
package io.linuxserver.fleet.auth;
import io.linuxserver.fleet.v2.types.User;
import io.linuxserver.fleet.v2.web.AppRole;
import java.util.Collections;
import java.util.Set;
public class AuthenticatedUser {
private final User user;
private final String name;
public AuthenticatedUser(final User user) {
this.user = user;
public AuthenticatedUser(String name) {
this.name = name;
}
public final String getName() {
return user.getUsername();
}
public final Set<AppRole> getRoles() {
return Collections.singleton(user.getRole());
public String getName() {
return name;
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.auth.authenticator;
import io.linuxserver.fleet.core.FleetBeans;
import io.linuxserver.fleet.core.FleetProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AuthenticatorFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticatorFactory.class);
public static UserAuthenticator getAuthenticator(FleetBeans beans) {
FleetProperties properties = beans.getProperties();
AuthenticationType authType = AuthenticationType.valueOf(properties.getAuthenticationType().toUpperCase());
switch (authType) {
case DATABASE:
LOGGER.info("Configuring new authenticator: DatabaseStoredUserAuthenticator");
return new DatabaseStoredUserAuthenticator(beans.getPasswordEncoder(), beans.getUserDelegate());
case PROPERTIES:
default:
LOGGER.info("Configuring new authenticator: PropertyLoadedUserAuthenticator");
return new PropertyLoadedUserAuthenticator(properties.getAppUsername(), properties.getAppPassword());
}
}
public enum AuthenticationType {
PROPERTIES, DATABASE
}
}

View File

@ -0,0 +1,60 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.auth.authenticator;
import io.linuxserver.fleet.auth.AuthenticatedUser;
import io.linuxserver.fleet.auth.AuthenticationResult;
import io.linuxserver.fleet.auth.UserCredentials;
import io.linuxserver.fleet.auth.security.PasswordEncoder;
import io.linuxserver.fleet.delegate.UserDelegate;
import io.linuxserver.fleet.model.internal.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DatabaseStoredUserAuthenticator implements UserAuthenticator {
private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseStoredUserAuthenticator.class);
private final PasswordEncoder passwordEncoder;
private final UserDelegate userDelegate;
public DatabaseStoredUserAuthenticator(PasswordEncoder passwordEncoder, UserDelegate userDelegate) {
this.passwordEncoder = passwordEncoder;
this.userDelegate = userDelegate;
}
@Override
public AuthenticationResult authenticate(UserCredentials userCredentials) {
User user = userDelegate.fetchUserByUsername(userCredentials.getUsername());
if (null == user) {
LOGGER.warn("Attempt to log in with user '{}' failed. Not found.", userCredentials.getUsername());
return AuthenticationResult.notAuthenticated();
}
boolean authenticated = passwordEncoder.matches(userCredentials.getPassword(), user.getPassword());
if (authenticated) {
return new AuthenticationResult(authenticated, new AuthenticatedUser(user.getUsername()));
}
LOGGER.warn("Unable to verify user credentials for user {}", userCredentials.getUsername());
return AuthenticationResult.notAuthenticated();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 LinuxServer.io
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -20,36 +20,24 @@ package io.linuxserver.fleet.auth.authenticator;
import io.linuxserver.fleet.auth.AuthenticatedUser;
import io.linuxserver.fleet.auth.AuthenticationResult;
import io.linuxserver.fleet.auth.UserCredentials;
import io.linuxserver.fleet.auth.security.PasswordEncoder;
import io.linuxserver.fleet.v2.service.UserService;
import io.linuxserver.fleet.v2.types.User;
public class DefaultUserAuthenticator implements UserAuthenticator {
public class PropertyLoadedUserAuthenticator implements UserAuthenticator {
private final UserService userService;
private final PasswordEncoder passwordEncoder;
private final String adminUsername;
private final String adminPassword;
public DefaultUserAuthenticator(final UserService userService,
final PasswordEncoder passwordEncoder) {
public PropertyLoadedUserAuthenticator(String adminUsername, String adminPassword) {
this.userService = userService;
this.passwordEncoder = passwordEncoder;
this.adminUsername = adminUsername;
this.adminPassword = adminPassword;
}
@Override
public AuthenticationResult authenticate(final UserCredentials userCredentials) {
public AuthenticationResult authenticate(UserCredentials userCredentials) {
final User user = userService.lookUpUser(userCredentials.getUsername());
if (null != user && getPasswordEncoder().matches(userCredentials.getPassword(), user.getPassword())) {
return new AuthenticationResult(true, new AuthenticatedUser(user));
}
if (adminUsername.equals(userCredentials.getUsername()) && adminPassword.equals(userCredentials.getPassword()))
return new AuthenticationResult(true, new AuthenticatedUser(userCredentials.getUsername()));
return AuthenticationResult.notAuthenticated();
}
@Override
public PasswordEncoder getPasswordEncoder() {
return passwordEncoder;
}
}

View File

@ -19,7 +19,6 @@ package io.linuxserver.fleet.auth.authenticator;
import io.linuxserver.fleet.auth.AuthenticationResult;
import io.linuxserver.fleet.auth.UserCredentials;
import io.linuxserver.fleet.auth.security.PasswordEncoder;
/**
* <p>
@ -36,6 +35,4 @@ public interface UserAuthenticator {
* </p>
*/
AuthenticationResult authenticate(UserCredentials userCredentials);
PasswordEncoder getPasswordEncoder();
}

View File

@ -0,0 +1,97 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.cache;
import io.linuxserver.fleet.model.internal.Image;
import io.linuxserver.fleet.model.key.ImageKey;
import io.linuxserver.fleet.model.key.RepositoryKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.stream.Collectors;
public class ImageCache {
private static final Logger LOGGER = LoggerFactory.getLogger(ImageCache.class);
private final Map<RepositoryKey, List<Image>> cachedImages;
public ImageCache() {
this.cachedImages = new HashMap<>();
}
public void updateCache(Image image) {
Image updatedImage = Image.copyOf(image);
if (cachedImages.containsKey(updatedImage.getKey().getRepositoryKey())) {
List<Image> images = cachedImages.get(updatedImage.getKey().getRepositoryKey());
int cachedImageLocation = images.indexOf(updatedImage);
if (cachedImageLocation > -1) {
LOGGER.info("updateCache({}) Updating existing cached image.", updatedImage);
images.set(cachedImageLocation, updatedImage);
} else {
LOGGER.info("updateCache({}) Adding image to cache", updatedImage);
images.add(updatedImage);
}
} else {
List<Image> images = new ArrayList<>();
images.add(updatedImage);
LOGGER.info("updateCache({}) Creating new cache for repository {}", updatedImage, updatedImage.getRepositoryId());
cachedImages.put(updatedImage.getKey().getRepositoryKey(), images);
}
}
public List<Image> getAll(RepositoryKey repositoryKey) {
if (cachedImages.containsKey(repositoryKey)) {
return cachedImages.get(repositoryKey).stream().map(Image::copyOf).collect(Collectors.toList());
}
return new ArrayList<>();
}
public Image get(ImageKey imageKey) {
final RepositoryKey repositoryKey = imageKey.getRepositoryKey();
if (cachedImages.containsKey(repositoryKey)) {
return cachedImages.get(repositoryKey).stream().filter(i -> i.getName().equals(imageKey.getName())).findFirst().orElse(null);
}
return null;
}
public void remove(ImageKey imageKey) {
final RepositoryKey repositoryKey = imageKey.getRepositoryKey();
if (cachedImages.containsKey(repositoryKey)) {
cachedImages.get(repositoryKey).removeIf(image -> image.getKey().equals(imageKey));
}
}
}

View File

@ -1,72 +0,0 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.core;
import io.linuxserver.fleet.core.config.AppProperties;
import io.linuxserver.fleet.core.db.DatabaseProvider;
import io.linuxserver.fleet.core.db.DefaultDatabaseProvider;
import io.linuxserver.fleet.db.DefaultDatabaseConnection;
import io.linuxserver.fleet.v2.cache.BasicItemCache;
import io.linuxserver.fleet.v2.key.AlertKey;
import io.linuxserver.fleet.v2.types.AppAlert;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public abstract class AbstractAppController {
private final AppProperties appProperties;
private final DatabaseProvider databaseProvider;
private final BasicItemCache<AlertKey, AppAlert> alertCache;
public AbstractAppController() {
this.appProperties = new PropertiesLoader().getProperties();
this.databaseProvider = new DefaultDatabaseProvider(new DefaultDatabaseConnection(appProperties.getDatabaseProperties()));
this.alertCache = new BasicItemCache<>();
}
public final DatabaseProvider getDatabaseProvider() {
return databaseProvider;
}
public final AppProperties getAppProperties() {
return appProperties;
}
public final List<AppAlert> getAlerts() {
return new ArrayList<>(alertCache.getAllItems());
}
public final List<AppAlert> getSystemAlerts() {
return getAlerts().stream().filter(AppAlert::isSystemAlert).collect(Collectors.toList());
}
public final void addAlert(final AppAlert appAlert) {
alertCache.addItem(appAlert);
}
public final void clearAlert(final AlertKey alertKey) {
alertCache.removeItem(alertKey);
}
protected void run() {
}
}

View File

@ -36,6 +36,5 @@ abstract class BaseRuntimeLoader {
LOGGER.info("Initalising...");
LOGGER.info("Config base : " + FleetRuntime.CONFIG_BASE);
LOGGER.info("Show Passwords : " + FleetRuntime.SHOW_PASSWORDS);
LOGGER.info("Nuke database : " + FleetRuntime.NUKE_DATABASE);
}
}

View File

@ -0,0 +1,221 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.core;
import io.linuxserver.fleet.auth.AuthenticatedUser;
import io.linuxserver.fleet.auth.authenticator.AuthenticatorFactory.AuthenticationType;
import io.linuxserver.fleet.model.api.ApiResponse;
import io.linuxserver.fleet.model.api.FleetApiException;
import io.linuxserver.fleet.web.JsonTransformer;
import io.linuxserver.fleet.web.SessionAttribute;
import io.linuxserver.fleet.web.pages.HomePage;
import io.linuxserver.fleet.web.pages.LoginPage;
import io.linuxserver.fleet.web.pages.ManageRepositoriesPage;
import io.linuxserver.fleet.web.pages.SetupPage;
import io.linuxserver.fleet.web.routes.*;
import io.linuxserver.fleet.web.websocket.SynchronisationWebSocket;
import spark.Session;
import java.util.concurrent.TimeUnit;
import static spark.Spark.*;
/**
* <p>
* Primary entry point for the application. All contexts and resources are loaded
* through this class.
* </p>
*/
public class FleetApp {
private static final String FLEET_USER_UNDEFINED = "fleet.user.undefined";
private static FleetApp instance;
public static FleetApp instance() {
if (null == instance) {
synchronized (FleetApp.class) {
if (null == instance) {
instance = new FleetApp();
}
}
}
return instance;
}
private final FleetBeans beans;
private FleetApp() {
beans = new FleetBeans();
}
void run() {
migrateDatabase();
configureWeb();
scheduleSync();
}
private void migrateDatabase() {
beans.getDatabaseVersion().migrate();
}
private void configureWeb() {
port(beans.getProperties().getAppPort());
staticFiles.location("/assets");
staticFiles.expireTime(600);
SynchronisationWebSocket synchronisationWebSocket = new SynchronisationWebSocket();
beans.getSynchronisationDelegate().registerListener(synchronisationWebSocket);
webSocket("/admin/ws/sync", synchronisationWebSocket);
init();
/* -----------------------
* Set Up
* -----------------------
*/
if (initialUserNeedsConfiguring()) {
path("/setup", () -> {
before("", (request, response) -> {
if (!initialUserNeedsConfiguring()) {
halt(401);
}
});
get("", new SetupPage());
post("", new RegisterInitialUserRoute(beans.getUserDelegate()));
});
}
/* -----------------------
* Image List and Log In
* -----------------------
*/
path("/", () -> {
get("", new HomePage(beans.getRepositoryDelegate(), beans.getImageDelegate()));
get("/login", new LoginPage());
post("/login", new LoginRoute(beans.getAuthenticationDelegate()));
post("/logout", new LogoutRoute());
});
/* -----------------------
* API
* -----------------------
*/
path("/api/v1", () -> {
get("/images", new AllImagesApi(beans.getRepositoryDelegate(), beans.getImageDelegate()), new JsonTransformer());
get("/image", new GetImageApi(beans.getImageDelegate()), new JsonTransformer());
get("/pullHistory", new GetImagePullHistoryApi(beans.getImageDelegate()), new JsonTransformer());
after("/*", (request, response) -> {
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Methods", "GET");
response.header("Content-Type","application/json");
});
});
/* -----------------------
* Admin
* -----------------------
*/
path("/admin", () -> {
before("", (request, response) -> {
Session session = request.session(false);
if (null == session)
response.redirect("/login");
else {
AuthenticatedUser user = session.attribute(SessionAttribute.USER);
if (null == user)
response.redirect("/login");
}
});
get("", new ManageRepositoriesPage(beans.getRepositoryDelegate()));
before("/api/*", (request, response) -> {
Session session = request.session(false);
if (null == session)
halt(403);
else {
AuthenticatedUser user = session.attribute(SessionAttribute.USER);
if (null == user)
halt(403);
}
});
get("/api/getImage", new GetImageApi(beans.getImageDelegate()), new JsonTransformer());
post("/api/manageImage", new ManageImageApi(beans.getImageDelegate()), new JsonTransformer());
post("/api/manageRepository", new ManageRepositoryApi(beans.getRepositoryDelegate()), new JsonTransformer());
post("/api/forceSync", new ForceSyncApi(beans.getTaskDelegate()), new JsonTransformer());
after("/api/*", (request, response) -> response.header("Content-Type", "application/json"));
});
/* -----------------------
* API Error Handling
* -----------------------
*/
exception(FleetApiException.class, (exception, request, response) -> {
response.body(new JsonTransformer().render(new ApiResponse<>("ERROR", exception.getMessage())));
response.header("Content-Type", "application/json");
response.status(exception.getStatusCode());
});
}
private void scheduleSync() {
beans.getTaskDelegate().scheduleSynchronisationTask(beans.getProperties().getRefreshIntervalInMinutes(), TimeUnit.MINUTES);
}
private boolean initialUserNeedsConfiguring() {
String configured = System.getProperty(FLEET_USER_UNDEFINED);
if (null == configured || "true".equalsIgnoreCase(configured)) {
System.setProperty(FLEET_USER_UNDEFINED, String.valueOf(beans.getUserDelegate().isUserRepositoryEmpty()));
}
return "true".equalsIgnoreCase(System.getProperty(FLEET_USER_UNDEFINED)) && databaseAuthenticationEnabled();
}
private boolean databaseAuthenticationEnabled() {
return AuthenticationType.DATABASE == AuthenticationType.valueOf(beans.getProperties().getAuthenticationType());
}
}

View File

@ -1,180 +0,0 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.core;
import io.linuxserver.fleet.auth.AuthenticationResult;
import io.linuxserver.fleet.core.config.WebConfiguration;
import io.linuxserver.fleet.v2.client.docker.DockerApiClient;
import io.linuxserver.fleet.v2.client.docker.dockerhub.DockerHubApiClient;
import io.linuxserver.fleet.v2.client.docker.dockerhub.DockerHubAuthenticator;
import io.linuxserver.fleet.v2.client.docker.dockerhub.IDockerHubAuthenticator;
import io.linuxserver.fleet.v2.client.docker.dockerhub.NoOpDockerHubAuthenticator;
import io.linuxserver.fleet.v2.client.docker.queue.DockerApiDelegate;
import io.linuxserver.fleet.v2.client.rest.RestClient;
import io.linuxserver.fleet.v2.db.DefaultImageDAO;
import io.linuxserver.fleet.v2.db.DefaultScheduleDAO;
import io.linuxserver.fleet.v2.db.DefaultUserDAO;
import io.linuxserver.fleet.v2.file.FileManager;
import io.linuxserver.fleet.v2.key.ImageKey;
import io.linuxserver.fleet.v2.service.ImageService;
import io.linuxserver.fleet.v2.service.ScheduleService;
import io.linuxserver.fleet.v2.service.SynchronisationService;
import io.linuxserver.fleet.v2.service.UserService;
import io.linuxserver.fleet.v2.types.Image;
import io.linuxserver.fleet.v2.types.Repository;
import io.linuxserver.fleet.v2.types.internal.RepositoryOutlineRequest;
import io.linuxserver.fleet.v2.web.WebRouteController;
/**
* <p>
* Primary entry point for the application. All contexts and resources are loaded
* through this class.
* </p>
*/
public class FleetAppController extends AbstractAppController implements ServiceProvider {
private final DockerApiDelegate dockerApiDelegate;
private final ImageService imageService;
private final ScheduleService scheduleService;
private final SynchronisationService syncService;
private final UserService userService;
private final FileManager fileManager;
public FleetAppController() {
fileManager = new FileManager(this);
imageService = new ImageService(this, new DefaultImageDAO(getDatabaseProvider()));
scheduleService = new ScheduleService(this, new DefaultScheduleDAO(getDatabaseProvider()));
dockerApiDelegate = new DockerApiDelegate(this, configureDockerApiClient());
syncService = new SynchronisationService(this);
userService = new UserService(this, new DefaultUserDAO(getDatabaseProvider()));
}
private static FleetAppController instance;
public static FleetAppController instance() {
if (null == instance) {
synchronized (FleetAppController.class) {
if (null == instance) {
instance = new FleetAppController();
}
}
}
return instance;
}
@Override
protected final void run() {
super.run();
configureWeb();
scheduleService.initialiseSchedules();
}
public final WebConfiguration getWebConfiguration() {
return new WebConfiguration(getAppProperties());
}
private void configureWeb() {
new WebRouteController(this);
}
public final void handleException(final Exception e) {
}
public final boolean synchroniseImage(final ImageKey imageKey) {
return syncService.synchroniseImage(imageKey);
}
public final void synchroniseRepository(final Repository repository) {
syncService.synchroniseCachedRepository(repository);
}
public final DockerApiDelegate getConfiguredDockerDelegate() {
return dockerApiDelegate;
}
public final ImageService getImageService() {
return imageService;
}
public final Image storeUpdatedImage(final Image updatedImage) {
return imageService.storeImage(updatedImage);
}
@Override
public ScheduleService getScheduleService() {
return scheduleService;
}
public final Repository verifyRepositoryAndCreateOutline(final RepositoryOutlineRequest request) {
if (getConfiguredDockerDelegate().isRepositoryValid(request.getRepositoryName())) {
final Repository repositoryOutline = getImageService()
.createRepositoryOutline(new RepositoryOutlineRequest(request.getRepositoryName()));
getSynchronisationService().synchroniseUpstreamRepository(repositoryOutline);
return repositoryOutline;
}
throw new IllegalArgumentException("Repository " + request.getRepositoryName() + " does not exist upstream");
}
@Override
public final SynchronisationService getSynchronisationService() {
return syncService;
}
@Override
public final UserService getUserService() {
return userService;
}
@Override
public FileManager getFileManager() {
return fileManager;
}
public final AuthenticationResult authenticateCredentials(final String username, final String password) {
return userService.authenticateCredentials(username, password);
}
public final void trackBranch(final ImageKey imageKey, final String branchName) {
getImageService().trackBranchOnImage(imageKey, branchName);
synchroniseImage(imageKey);
}
private DockerApiClient configureDockerApiClient() {
final RestClient dockerHubApiRestClient = new RestClient();
final IDockerHubAuthenticator dockerHubAuthenticator;
if (getAppProperties().isDockerHubAuthEnabled()) {
dockerHubAuthenticator = new DockerHubAuthenticator(getAppProperties().getDockerHubCredentials(), dockerHubApiRestClient);
} else {
dockerHubAuthenticator = new NoOpDockerHubAuthenticator();
}
return new DockerHubApiClient(dockerHubApiRestClient, dockerHubAuthenticator);
}
}

View File

@ -0,0 +1,131 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.core;
import io.linuxserver.fleet.auth.authenticator.AuthenticatorFactory;
import io.linuxserver.fleet.auth.security.PBKDF2PasswordEncoder;
import io.linuxserver.fleet.auth.security.PasswordEncoder;
import io.linuxserver.fleet.db.DefaultDatabaseConnection;
import io.linuxserver.fleet.db.dao.DefaultImageDAO;
import io.linuxserver.fleet.db.dao.DefaultRepositoryDAO;
import io.linuxserver.fleet.db.dao.DefaultUserDAO;
import io.linuxserver.fleet.db.migration.DatabaseVersion;
import io.linuxserver.fleet.delegate.*;
import io.linuxserver.fleet.dockerhub.DockerHubV2Client;
import io.linuxserver.fleet.dockerhub.queue.DockerHubSyncConsumer;
import io.linuxserver.fleet.dockerhub.queue.DockerHubSyncQueue;
import io.linuxserver.fleet.thread.TaskManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* <p>
* Initialises all relevant dependencies for the Fleet application
* </p>
*/
public class FleetBeans {
private static final Logger LOGGER = LoggerFactory.getLogger(FleetBeans.class);
private final FleetProperties properties;
private final ImageDelegate imageDelegate;
private final RepositoryDelegate repositoryDelegate;
private final AuthenticationDelegate authenticationDelegate;
private final DockerHubDelegate dockerHubDelegate;
private final SynchronisationDelegate synchronisationDelegate;
private final TaskManager taskManager;
private final TaskDelegate taskDelegate;
private final UserDelegate userDelegate;
private final PasswordEncoder passwordEncoder;
private final DockerHubSyncQueue dockerHubSyncQueue;
/**
* Ensures the database is kept up to date.
*/
private final DatabaseVersion databaseVersion;
FleetBeans() {
properties = new PropertiesLoader().getProperties();
final DefaultDatabaseConnection databaseConnection = new DefaultDatabaseConnection(properties);
passwordEncoder = new PBKDF2PasswordEncoder(properties.getAppSecret());
databaseVersion = new DatabaseVersion(databaseConnection);
imageDelegate = new ImageDelegate(new DefaultImageDAO(databaseConnection));
repositoryDelegate = new RepositoryDelegate(new DefaultRepositoryDAO(databaseConnection));
dockerHubDelegate = new DockerHubDelegate(new DockerHubV2Client(properties.getDockerHubCredentials()));
taskManager = new TaskManager();
synchronisationDelegate = new SynchronisationDelegate(imageDelegate, repositoryDelegate, dockerHubDelegate);
userDelegate = new UserDelegate(passwordEncoder, new DefaultUserDAO(databaseConnection));
taskDelegate = new TaskDelegate(this);
authenticationDelegate = new DefaultAuthenticationDelegate(AuthenticatorFactory.getAuthenticator(this));
dockerHubSyncQueue = new DockerHubSyncQueue();
final int consumerThreadCount = properties.getQueueThreadCount();
for (int i = 0; i < consumerThreadCount; i++) {
LOGGER.info("Starting consumer thread " + i + "...");
new DockerHubSyncConsumer(imageDelegate, dockerHubSyncQueue, "SyncThread-" + i).start();
}
}
public FleetProperties getProperties() {
return properties;
}
public ImageDelegate getImageDelegate() {
return imageDelegate;
}
public DockerHubDelegate getDockerHubDelegate() {
return dockerHubDelegate;
}
public RepositoryDelegate getRepositoryDelegate() {
return repositoryDelegate;
}
public SynchronisationDelegate getSynchronisationDelegate() {
return synchronisationDelegate;
}
public AuthenticationDelegate getAuthenticationDelegate() {
return authenticationDelegate;
}
public DatabaseVersion getDatabaseVersion() {
return databaseVersion;
}
public TaskManager getTaskManager() {
return taskManager;
}
public TaskDelegate getTaskDelegate() {
return taskDelegate;
}
public UserDelegate getUserDelegate() {
return userDelegate;
}
public PasswordEncoder getPasswordEncoder() {
return passwordEncoder;
}
}

View File

@ -15,57 +15,38 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.core.config;
package io.linuxserver.fleet.core;
import io.linuxserver.fleet.core.FleetRuntime;
import io.linuxserver.fleet.v2.client.docker.dockerhub.DockerHubCredentials;
import io.linuxserver.fleet.dockerhub.DockerHubCredentials;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
public class AppProperties {
public class FleetProperties {
private Properties properties;
public AppProperties(final Properties properties) {
FleetProperties(Properties properties) {
this.properties = properties;
}
public DatabaseConnectionProperties getDatabaseProperties() {
return new DatabaseConnectionProperties(getDatabaseDriverClassName(),
getDatabaseUrl(),
getDatabaseUsername(),
getDatabasePassword());
}
public final VersionProperties getVersionProperties() {
return new VersionProperties(getStringProperty("app.version"),
getStringProperty("app.build.user"),
getStringProperty("app.build.date"),
getStringProperty("app.build.os"));
}
private String getDatabaseDriverClassName() {
public String getDatabaseDriverClassName() {
return getStringProperty("fleet.database.driver");
}
private String getDatabaseUrl() {
public String getDatabaseUrl() {
return getStringProperty("fleet.database.url");
}
private String getDatabaseUsername() {
public String getDatabaseUsername() {
return getStringProperty("fleet.database.username");
}
private String getDatabasePassword() {
public String getDatabasePassword() {
return getStringProperty("fleet.database.password");
}
public final Path getStaticFilesPath() {
return Paths.get(FleetRuntime.CONFIG_BASE, getStringProperty("fleet.static.dirname")).toAbsolutePath();
public String getAuthenticationType() {
return getStringProperty("fleet.admin.authentication.type");
}
public String getAppSecret() {
@ -74,22 +55,41 @@ public class AppProperties {
return null == secret ? "" : secret;
}
public String getAppUsername() {
return getStringProperty("fleet.admin.username");
}
public String getAppPassword() {
return getStringProperty("fleet.admin.password");
}
public int getAppPort() {
return Integer.parseInt(getStringProperty("fleet.app.port"));
}
public boolean isDockerHubAuthEnabled() {
return "true".equalsIgnoreCase(getStringProperty("fleet.dockerhub.auth.enabled"));
public int getRefreshIntervalInMinutes() {
return Integer.parseInt(getStringProperty("fleet.refresh.interval"));
}
public DockerHubCredentials getDockerHubCredentials() {
final String username = getStringProperty("fleet.dockerhub.username");
final String password = getStringProperty("fleet.dockerhub.password");
String username = getStringProperty("fleet.dockerhub.username");
String password = getStringProperty("fleet.dockerhub.password");
return new DockerHubCredentials(username, password);
}
public int getQueueThreadCount() {
final String numThreads = getStringProperty("fleet.queue.threads");
if (null == numThreads) {
return 0;
}
return Integer.parseInt(numThreads);
}
/**
* <p>
* Obtains the property value from three separate sources: first from the config file. If not present, it will look

View File

@ -19,11 +19,6 @@ package io.linuxserver.fleet.core;
public interface FleetRuntime {
/**
* If set will switch specific properties to allow more streamlined development
*/
boolean DEV_MODE = System.getProperty("enable.dev") != null;
/**
* Base directory for the config file.
*/
@ -38,4 +33,10 @@ public interface FleetRuntime {
* Tells Fleet to completely wipe the database and recreate it.
*/
boolean NUKE_DATABASE = System.getProperty("fleet.nuke.database") != null;
/**
* Tells Fleet not to run a synchronisation when the app starts up. The first run
* will be at the next interval
*/
boolean SKIP_SYNC_ON_STARTUP = System.getProperty("fleet.skip.sync.on.startup") != null;
}

View File

@ -20,6 +20,6 @@ package io.linuxserver.fleet.core;
public class Main {
public static void main(String[] args) {
FleetAppController.instance().run();
FleetApp.instance().run();
}
}

View File

@ -17,14 +17,12 @@
package io.linuxserver.fleet.core;
import io.linuxserver.fleet.core.config.AppProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.Properties;
/**
@ -40,7 +38,7 @@ class PropertiesLoader extends BaseRuntimeLoader {
private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesLoader.class);
private final AppProperties properties;
private final FleetProperties properties;
PropertiesLoader() {
@ -52,10 +50,8 @@ class PropertiesLoader extends BaseRuntimeLoader {
Properties properties = new Properties();
properties.load(new FileInputStream(FleetRuntime.CONFIG_BASE + "/fleet.properties"));
properties.load(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("version.properties")));
properties.setProperty("fleet.static.dirname", "fleet_static");
this.properties = new AppProperties(properties);
this.properties = new FleetProperties(properties);
printProperties();
@ -89,7 +85,7 @@ class PropertiesLoader extends BaseRuntimeLoader {
private boolean createStaticFileDirectory() {
File staticFilesDir = new File(properties.getStaticFilesPath().toString());
File staticFilesDir = new File(FleetRuntime.CONFIG_BASE + "/fleet_static");
if (staticFilesDir.exists()) {
return true;
@ -117,7 +113,7 @@ class PropertiesLoader extends BaseRuntimeLoader {
* @return
* All application properties.
*/
AppProperties getProperties() {
FleetProperties getProperties() {
return properties;
}
@ -129,10 +125,12 @@ class PropertiesLoader extends BaseRuntimeLoader {
private void printProperties() {
LOGGER.info("fleet.app.port : " + properties.getAppPort());
LOGGER.info("fleet.database.url : " + properties.getDatabaseProperties().getDatabaseUrl());
LOGGER.info("fleet.database.username : " + properties.getDatabaseProperties().getDatabaseUsername());
LOGGER.info("fleet.database.password : " + (showPasswords() ? properties.getDatabaseProperties().getDatabasePassword() : "***"));
LOGGER.info("app.version : " + getProperties().getVersionProperties());
LOGGER.info("fleet.refresh.interval : " + properties.getRefreshIntervalInMinutes());
LOGGER.info("fleet.database.url : " + properties.getDatabaseUrl());
LOGGER.info("fleet.database.username : " + properties.getDatabaseUsername());
LOGGER.info("fleet.database.password : " + (showPasswords() ? properties.getDatabasePassword() : "***"));
LOGGER.info("fleet.dockerhub.username : " + properties.getDockerHubCredentials().getUsername());
LOGGER.info("fleet.dockerhub.password : " + (showPasswords() ? properties.getDockerHubCredentials().getPassword() : "***"));
}
/**

View File

@ -1,37 +0,0 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.core;
import io.linuxserver.fleet.v2.file.FileManager;
import io.linuxserver.fleet.v2.service.ImageService;
import io.linuxserver.fleet.v2.service.ScheduleService;
import io.linuxserver.fleet.v2.service.SynchronisationService;
import io.linuxserver.fleet.v2.service.UserService;
public interface ServiceProvider {
SynchronisationService getSynchronisationService();
ImageService getImageService();
ScheduleService getScheduleService();
UserService getUserService();
FileManager getFileManager();
}

View File

@ -1,52 +0,0 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.core.config;
public class DatabaseConnectionProperties {
private final String driverClass;
private final String url;
private final String username;
private final String password;
public DatabaseConnectionProperties(final String driverClass,
final String url,
final String username,
final String password) {
this.driverClass = driverClass;
this.url = url;
this.username = username;
this.password = password;
}
public final String getDatabaseDriverClass() {
return driverClass;
}
public final String getDatabaseUrl() {
return url;
}
public final String getDatabaseUsername() {
return username;
}
public final String getDatabasePassword() {
return password;
}
}

View File

@ -1,71 +0,0 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.core.config;
public class Version {
private int major;
private int minor;
private int patch;
public Version(int major, int minor, int patch) {
this.major = major;
this.minor = minor;
this.patch = patch;
}
public Version(String version) {
String[] bits = version.split("\\.");
this.major = Integer.parseInt(bits[0]);
this.minor = Integer.parseInt(bits[1]);
this.patch = Integer.parseInt(bits[2]);
}
public int getMajor() {
return major;
}
public int getMinor() {
return minor;
}
public int getPatch() {
return patch;
}
public boolean isNewerThan(Version version) {
if (this.major > version.major) {
return true;
} if (this.minor > version.minor) {
return true;
} else if (this.minor < version.minor) {
return false;
}
return this.patch > version.patch;
}
@Override
public String toString() {
return major + "." + minor + "." + patch;
}
}

View File

@ -1,64 +0,0 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.core.config;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class VersionProperties {
private final Version version;
private final String buildUser;
private final LocalDateTime buildDate;
private final String buildPlatform;
public VersionProperties(final String version,
final String buildUser,
final String buildDate,
final String buildPlatform) {
this.version = new Version(version);
this.buildUser = buildUser;
this.buildDate = LocalDateTime.parse(buildDate, DateTimeFormatter.ISO_DATE_TIME);
this.buildPlatform = buildPlatform;
}
public final Version getVersion() {
return version;
}
public final String getBuildUser() {
return buildUser;
}
public final LocalDateTime getBuildDate() {
return buildDate;
}
public final String getBuildPlatform() {
return buildPlatform;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}

View File

@ -1,31 +0,0 @@
/*
* Copyright (c) 2019 Wallett
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.core.config;
public class WebConfiguration {
private final AppProperties appProperties;
public WebConfiguration(final AppProperties properties) {
appProperties = properties;
}
public final int getPort() {
return appProperties.getAppPort();
}
}

View File

@ -1,29 +0,0 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.core.db;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public interface DatabaseConnection {
DataSource getDataSource();
Connection getConnection() throws SQLException;
}

View File

@ -1,27 +0,0 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.core.db;
import io.linuxserver.fleet.db.migration.DatabaseVersion;
public interface DatabaseProvider {
DatabaseConnection getDatabaseConnection();
DatabaseVersion getVersionHandler();
}

View File

@ -1,41 +0,0 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.core.db;
import io.linuxserver.fleet.db.migration.DatabaseVersion;
public class DefaultDatabaseProvider implements DatabaseProvider {
private final DatabaseConnection databaseConnection;
private final DatabaseVersion databaseVersion;
public DefaultDatabaseProvider(final DatabaseConnection databaseConnection) {
this.databaseConnection = databaseConnection;
this.databaseVersion = new DatabaseVersion(databaseConnection);
}
@Override
public DatabaseConnection getDatabaseConnection() {
return databaseConnection;
}
@Override
public DatabaseVersion getVersionHandler() {
return databaseVersion;
}
}

View File

@ -17,11 +17,15 @@
package io.linuxserver.fleet.db;
import io.linuxserver.fleet.core.config.DatabaseConnectionProperties;
import io.linuxserver.fleet.core.FleetProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DefaultDatabaseConnection extends PoolingDatabaseConnection {
public DefaultDatabaseConnection(final DatabaseConnectionProperties properties) {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDatabaseConnection.class);
public DefaultDatabaseConnection(FleetProperties properties) {
super(properties);
}
}

View File

@ -11,15 +11,14 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public LicensedatabaseConnection.getDataSource(
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.db;
import com.zaxxer.hikari.HikariDataSource;
import io.linuxserver.fleet.core.config.DatabaseConnectionProperties;
import io.linuxserver.fleet.core.db.DatabaseConnection;
import io.linuxserver.fleet.core.FleetProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -27,16 +26,16 @@ import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public abstract class PoolingDatabaseConnection implements DatabaseConnection {
public abstract class PoolingDatabaseConnection {
private static final Logger LOGGER = LoggerFactory.getLogger(PoolingDatabaseConnection.class);
private final HikariDataSource dataSource;
PoolingDatabaseConnection(final DatabaseConnectionProperties properties) {
PoolingDatabaseConnection(FleetProperties properties) {
dataSource = new HikariDataSource();
dataSource.setDriverClassName(properties.getDatabaseDriverClass());
dataSource.setDriverClassName(properties.getDatabaseDriverClassName());
dataSource.setJdbcUrl(properties.getDatabaseUrl());
dataSource.setUsername(properties.getDatabaseUsername());
dataSource.setPassword(properties.getDatabasePassword());
@ -44,13 +43,11 @@ public abstract class PoolingDatabaseConnection implements DatabaseConnection {
LOGGER.info("DataSource established: " + dataSource.getJdbcUrl());
}
@Override
public DataSource getDataSource() {
return dataSource;
}
@Override
public final Connection getConnection() throws SQLException {
public Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}

View File

@ -0,0 +1,267 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.db.dao;
import io.linuxserver.fleet.db.PoolingDatabaseConnection;
import io.linuxserver.fleet.db.query.InsertUpdateResult;
import io.linuxserver.fleet.db.query.InsertUpdateStatus;
import io.linuxserver.fleet.db.query.LimitedResult;
import io.linuxserver.fleet.model.internal.Image;
import io.linuxserver.fleet.model.internal.ImagePullStat;
import io.linuxserver.fleet.model.internal.Tag;
import io.linuxserver.fleet.model.key.ImageKey;
import io.linuxserver.fleet.model.key.RepositoryKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import static io.linuxserver.fleet.db.dao.Utils.*;
public class DefaultImageDAO implements ImageDAO {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultImageDAO.class);
private final PoolingDatabaseConnection databaseConnection;
public DefaultImageDAO(PoolingDatabaseConnection databaseConnection) {
this.databaseConnection = databaseConnection;
}
@Override
public Image findImageByRepositoryAndImageName(ImageKey imageKey) {
CallableStatement call = null;
try (Connection connection = databaseConnection.getConnection()) {
call = connection.prepareCall("{CALL Image_GetByName(?,?)}");
call.setInt(1, imageKey.getRepositoryKey().getId());
call.setString(2, imageKey.getName());
ResultSet results = call.executeQuery();
if (results.next())
return parseImageFromResultSet(results);
} catch (SQLException e) {
LOGGER.error("Unable to fetch image", e);
} finally {
safeClose(call);
}
return null;
}
@Override
public Image fetchImage(ImageKey imageKey) {
LOGGER.debug("Fetching image by ID: " + imageKey);
CallableStatement call = null;
try (Connection connection = databaseConnection.getConnection()) {
call = connection.prepareCall("{CALL Image_Get(?)}");
call.setInt(1, imageKey.getId());
ResultSet results = call.executeQuery();
if (results.next())
return parseImageFromResultSet(results);
} catch (SQLException e) {
LOGGER.error("Unable to fetch image.", e);
} finally {
safeClose(call);
}
return null;
}
@Override
public LimitedResult<Image> fetchImagesByRepository(final RepositoryKey repositoryKey) {
List<Image> images = new ArrayList<>();
CallableStatement call = null;
try (Connection connection = databaseConnection.getConnection()) {
call = connection.prepareCall("{CALL Image_GetAll(?,?)}");
call.setInt(1, repositoryKey.getId());
call.registerOutParameter(2, Types.INTEGER);
ResultSet results = call.executeQuery();
while (results.next())
images.add(parseImageFromResultSet(results));
int totalRecords = call.getInt(2);
return new LimitedResult<>(images, totalRecords);
} catch (SQLException e) {
LOGGER.error("Unable to get all images", e);
} finally {
safeClose(call);
}
return new LimitedResult<>(images, images.size());
}
@Override
public InsertUpdateResult<Image> saveImage(Image image) {
CallableStatement call = null;
try (Connection connection = databaseConnection.getConnection()) {
call = connection.prepareCall("{CALL Image_Save(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
setNullableInt(call, 1, image.getKey().getId());
call.setInt(2, image.getRepositoryId());
call.setString(3, image.getName());
setNullableLong(call, 4, image.getPullCount());
call.setString(5, image.getMaskedVersion());
setNullableString(call, 6, image.getVersionMask());
call.setBoolean(7, image.isHidden());
call.setBoolean(8, image.isUnstable());
call.setBoolean(9, image.isDeprecated());
setNullableString(call, 10, image.getDeprecationReason());
call.setString(11, image.getRawVersion());
setNullableTimestamp(call, 12, image.getBuildDate());
call.registerOutParameter(13, Types.INTEGER);
call.registerOutParameter(14, Types.INTEGER);
call.registerOutParameter(15, Types.VARCHAR);
call.executeUpdate();
int imageId = call.getInt(13);
int status = call.getInt(14);
String statusMessage = call.getString(15);
if (InsertUpdateStatus.OK == status)
return new InsertUpdateResult<>(fetchImage(image.getKey().cloneWithId(imageId)), status, statusMessage);
return new InsertUpdateResult<>(status, statusMessage);
} catch (SQLException e) {
LOGGER.error("Unable to save image", e);
return new InsertUpdateResult<>(null, InsertUpdateStatus.OK, "Unable to save image");
} finally {
safeClose(call);
}
}
@Override
public void removeImage(final ImageKey imageKey) {
CallableStatement call = null;
try (Connection connection = databaseConnection.getConnection()) {
call = connection.prepareCall("{CALL Image_Delete(?)}");
call.setInt(1, imageKey.getId());
call.executeUpdate();
call.close();
} catch (SQLException e) {
LOGGER.error("Error when removing image", e);
} finally {
safeClose(call);
}
}
@Override
public List<ImagePullStat> fetchImagePullHistory(final ImageKey imageKey, ImagePullStat.GroupMode groupMode) {
List<ImagePullStat> pullHistory = new ArrayList<>();
CallableStatement call = null;
try (Connection connection = databaseConnection.getConnection()) {
call = connection.prepareCall("CALL Image_GetPullHistory(?, ?)");
call.setInt(1, imageKey.getId());
call.setString(2, groupMode.toString());
ResultSet results = call.executeQuery();
while (results.next())
pullHistory.add(parseImagePullHistoryFromResultSet(results, groupMode));
call.close();
} catch (SQLException e) {
LOGGER.error("Error when fetching image pull history", e);
} finally {
safeClose(call);
}
return pullHistory;
}
private ImagePullStat parseImagePullHistoryFromResultSet(ResultSet results, ImagePullStat.GroupMode groupMode) throws SQLException {
return new ImagePullStat(
results.getInt("ImageId"),
results.getString("TimeGroup"),
results.getLong("ImagePulls"),
groupMode
);
}
private Image parseImageFromResultSet(ResultSet results) throws SQLException {
Image image = new Image(
new ImageKey(
results.getInt("ImageId"),
results.getString("ImageName"),
new RepositoryKey(
results.getInt("RepositoryId"),
results.getString("RepositoryName")
)
),
new Tag(
results.getString("LatestTagVersion"),
results.getString("LatestMaskedTagVersion"),
safeParseLocalDateTime(results.getTimestamp("LatestTagBuildDate"))
)
);
return image
.withPullCount(results.getLong("ImagePullCount"))
.withVersionMask(results.getString("ImageVersionMask"))
.withModifiedTime(results.getTimestamp("ModifiedTime").toLocalDateTime())
.withHidden(results.getBoolean("ImageHidden"))
.withUnstable(results.getBoolean("ImageUnstable"))
.withDeprecated(results.getBoolean("ImageDeprecated"))
.withDeprecationReason(results.getString("ImageDeprecationReason"));
}
private LocalDateTime safeParseLocalDateTime(Timestamp timestamp) {
if (timestamp == null) {
return null;
}
return timestamp.toLocalDateTime();
}
}

View File

@ -0,0 +1,181 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.db.dao;
import io.linuxserver.fleet.db.PoolingDatabaseConnection;
import io.linuxserver.fleet.db.query.InsertUpdateResult;
import io.linuxserver.fleet.db.query.InsertUpdateStatus;
import io.linuxserver.fleet.model.internal.Repository;
import io.linuxserver.fleet.model.key.RepositoryKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import static io.linuxserver.fleet.db.dao.Utils.*;
public class DefaultRepositoryDAO implements RepositoryDAO {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultImageDAO.class);
private final PoolingDatabaseConnection databaseConnection;
public DefaultRepositoryDAO(PoolingDatabaseConnection databaseConnection) {
this.databaseConnection = databaseConnection;
}
@Override
public Repository fetchRepository(RepositoryKey repositoryKey) {
CallableStatement call = null;
try (Connection connection = databaseConnection.getConnection()) {
call = connection.prepareCall("{CALL Repository_Get(?)}");
call.setInt(1, repositoryKey.getId());
ResultSet results = call.executeQuery();
if (results.next())
return parseRepositoryFromResultSet(results);
} catch (SQLException e) {
LOGGER.error("Unable to retrieve repository", e);
} finally {
safeClose(call);
}
return null;
}
@Override
public InsertUpdateResult<Repository> saveRepository(Repository repository) {
CallableStatement call = null;
try (Connection connection = databaseConnection.getConnection()) {
call = connection.prepareCall("{CALL Repository_Save(?,?,?,?,?,?,?)}");
setNullableInt(call, 1, repository.getKey().getId());
call.setString(2, repository.getName());
setNullableString(call, 3, repository.getVersionMask());
call.setBoolean(4, repository.isSyncEnabled());
call.registerOutParameter(5, Types.INTEGER);
call.registerOutParameter(6, Types.INTEGER);
call.registerOutParameter(7, Types.VARCHAR);
call.executeUpdate();
int repositoryId = call.getInt(5);
int status = call.getInt(6);
String statusMessage = call.getString(7);
if (InsertUpdateStatus.OK == status)
return new InsertUpdateResult<>(fetchRepository(repository.getKey().cloneWithId(repositoryId)), status, statusMessage);
return new InsertUpdateResult<>(status, statusMessage);
} catch (SQLException e) {
LOGGER.error("Unable to save repository", e);
return new InsertUpdateResult<>(null, InsertUpdateStatus.OK, "Unable to save repository");
} finally {
safeClose(call);
}
}
@Override
public List<Repository> fetchAllRepositories() {
List<Repository> repositories = new ArrayList<>();
CallableStatement call = null;
try (Connection connection = databaseConnection.getConnection()) {
call = connection.prepareCall("{CALL Repository_GetAll()}");
ResultSet results = call.executeQuery();
while (results.next())
repositories.add(parseRepositoryFromResultSet(results));
} catch (SQLException e) {
LOGGER.error("Unable to get all repositories", e);
} finally {
safeClose(call);
}
return repositories;
}
@Override
public Repository findRepositoryByName(String name) {
CallableStatement call = null;
try (Connection connection = databaseConnection.getConnection()) {
call = connection.prepareCall("{CALL Repository_GetByName(?)}");
call.setString(1, name);
ResultSet results = call.executeQuery();
if (results.next())
return parseRepositoryFromResultSet(results);
} catch (SQLException e) {
LOGGER.error("Unable to retrieve repository", e);
} finally {
safeClose(call);
}
return null;
}
@Override
public void removeRepository(int id) {
CallableStatement call = null;
try (Connection connection = databaseConnection.getConnection()) {
call = connection.prepareCall("{CALL Repository_Delete(?)}");
call.setInt(1, id);
call.executeUpdate();
} catch (SQLException e) {
LOGGER.error("Error when removing repository", e);
} finally {
safeClose(call);
}
}
private Repository parseRepositoryFromResultSet(ResultSet results) throws SQLException {
Repository repository = new Repository(new RepositoryKey(results.getInt("RepositoryId"), results.getString("RepositoryName")))
.withSyncEnabled(results.getBoolean("SyncEnabled"))
.withVersionMask(results.getString("RepositoryVersionMask"))
.withModifiedTime(results.getTimestamp("ModifiedTime").toLocalDateTime());
LOGGER.debug("Parsed repository: " + repository);
return repository;
}
}

View File

@ -0,0 +1,157 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.db.dao;
import io.linuxserver.fleet.db.PoolingDatabaseConnection;
import io.linuxserver.fleet.db.query.InsertUpdateResult;
import io.linuxserver.fleet.db.query.InsertUpdateStatus;
import io.linuxserver.fleet.model.internal.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import static io.linuxserver.fleet.db.dao.Utils.setNullableInt;
import static io.linuxserver.fleet.db.dao.Utils.setNullableString;
public class DefaultUserDAO implements UserDAO {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultUserDAO.class);
private final PoolingDatabaseConnection databaseConnection;
public DefaultUserDAO(PoolingDatabaseConnection databaseConnection) {
this.databaseConnection = databaseConnection;
}
@Override
public User fetchUser(int id) {
try (Connection connection = databaseConnection.getConnection()) {
CallableStatement call = connection.prepareCall("{CALL User_Get(?)}");
call.setInt(1, id);
ResultSet results = call.executeQuery();
if (results.next())
return parseUserFromResultSet(results);
} catch (SQLException e) {
LOGGER.error("Unable to retrieve user", e);
}
return null;
}
@Override
public User fetchUserByUsername(String username) {
try (Connection connection = databaseConnection.getConnection()) {
CallableStatement call = connection.prepareCall("{CALL User_GetByName(?)}");
call.setString(1, username);
ResultSet results = call.executeQuery();
if (results.next())
return parseUserFromResultSet(results);
} catch (SQLException e) {
LOGGER.error("Unable to retrieve user", e);
}
return null;
}
@Override
public InsertUpdateResult<User> saveUser(User user) {
try (Connection connection = databaseConnection.getConnection()) {
CallableStatement call = connection.prepareCall("{CALL User_Save(?,?,?,?,?,?)}");
setNullableInt(call, 1, user.getKey().getId());
call.setString(2, user.getUsername());
setNullableString(call, 3, user.getPassword());
call.registerOutParameter(4, Types.INTEGER);
call.registerOutParameter(5, Types.INTEGER);
call.registerOutParameter(6, Types.VARCHAR);
call.executeUpdate();
int userId = call.getInt(4);
int status = call.getInt(5);
String statusMessage = call.getString(6);
if (InsertUpdateStatus.OK == status)
return new InsertUpdateResult<>(fetchUser(userId), status, statusMessage);
return new InsertUpdateResult<>(status, statusMessage);
} catch (SQLException e) {
LOGGER.error("Unable to save user", e);
return new InsertUpdateResult<>(null, InsertUpdateStatus.OK, "Unable to save user");
}
}
@Override
public List<User> fetchAllUsers() {
List<User> repositories = new ArrayList<>();
try (Connection connection = databaseConnection.getConnection()) {
CallableStatement call = connection.prepareCall("{CALL User_GetAll()}");
ResultSet results = call.executeQuery();
while (results.next())
repositories.add(parseUserFromResultSet(results));
} catch (SQLException e) {
LOGGER.error("Unable to get all users", e);
}
return repositories;
}
@Override
public void removeUser(User user) {
try (Connection connection = databaseConnection.getConnection()) {
CallableStatement call = connection.prepareCall("{CALL User_Delete(?)}");
call.setInt(1, user.getKey().getId());
call.executeUpdate();
} catch (SQLException e) {
LOGGER.error("Error when removing user", e);
}
}
private User parseUserFromResultSet(ResultSet results) throws SQLException {
return new User(
results.getInt("UserId"),
results.getString("UserName"),
results.getString("UserPassword")
).withModifiedTime(results.getTimestamp("ModifiedTime").toLocalDateTime());
}
}

View File

@ -0,0 +1,25 @@
package io.linuxserver.fleet.db.dao;
import io.linuxserver.fleet.db.query.InsertUpdateResult;
import io.linuxserver.fleet.db.query.LimitedResult;
import io.linuxserver.fleet.model.internal.Image;
import io.linuxserver.fleet.model.internal.ImagePullStat;
import io.linuxserver.fleet.model.key.ImageKey;
import io.linuxserver.fleet.model.key.RepositoryKey;
import java.util.List;
public interface ImageDAO {
Image findImageByRepositoryAndImageName(ImageKey imageKey);
Image fetchImage(ImageKey imageKey);
LimitedResult<Image> fetchImagesByRepository(RepositoryKey repositoryKey);
InsertUpdateResult<Image> saveImage(Image image);
void removeImage(ImageKey imageKey);
List<ImagePullStat> fetchImagePullHistory(ImageKey imageKey, ImagePullStat.GroupMode groupMode);
}

View File

@ -0,0 +1,20 @@
package io.linuxserver.fleet.db.dao;
import io.linuxserver.fleet.db.query.InsertUpdateResult;
import io.linuxserver.fleet.model.internal.Repository;
import io.linuxserver.fleet.model.key.RepositoryKey;
import java.util.List;
public interface RepositoryDAO {
Repository fetchRepository(RepositoryKey repositoryKey);
InsertUpdateResult<Repository> saveRepository(Repository repository);
List<Repository> fetchAllRepositories();
Repository findRepositoryByName(String name);
void removeRepository(int id);
}

View File

@ -15,17 +15,22 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.types;
package io.linuxserver.fleet.db.dao;
public interface HasSyncSpec {
import io.linuxserver.fleet.db.query.InsertUpdateResult;
import io.linuxserver.fleet.model.internal.User;
boolean isSyncEnabled();
import java.util.List;
boolean isStable();
public interface UserDAO {
boolean isDeprecated();
User fetchUser(int id);
String getVersionMask();
User fetchUserByUsername(String username);
boolean isHidden();
InsertUpdateResult<User> saveUser(User user);
List<User> fetchAllUsers();
void removeUser(User user);
}

View File

@ -18,7 +18,7 @@
package io.linuxserver.fleet.db.migration;
import io.linuxserver.fleet.core.FleetRuntime;
import io.linuxserver.fleet.core.db.DatabaseConnection;
import io.linuxserver.fleet.db.PoolingDatabaseConnection;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.FlywayException;
import org.slf4j.Logger;
@ -35,9 +35,8 @@ public class DatabaseVersion {
private final Flyway flyway;
public DatabaseVersion(final DatabaseConnection databaseConnection) {
public DatabaseVersion(PoolingDatabaseConnection databaseConnection) {
flyway = Flyway.configure().dataSource(databaseConnection.getDataSource()).load();
migrate();
}
/**

View File

@ -23,10 +23,6 @@ public class InsertUpdateResult<T> {
private final int status;
private final String statusMessage;
public InsertUpdateResult(T result) {
this(result, InsertUpdateStatus.OK, "OK");
}
public InsertUpdateResult(T result, int status, String statusMessage) {
this.result = result;
@ -38,19 +34,15 @@ public class InsertUpdateResult<T> {
this(null, status, statusMessage);
}
public final T getResult() {
public T getResult() {
return result;
}
public final int getStatus() {
public int getStatus() {
return status;
}
public final String getStatusMessage() {
public String getStatusMessage() {
return statusMessage;
}
public final boolean isError() {
return status != InsertUpdateStatus.OK;
}
}

View File

@ -31,9 +31,9 @@ public class LimitedResult<T> {
public LimitedResult(List<T> results, int totalCount, LimitOffset next) {
this.results = results;
this.results = results;
this.totalCount = totalCount;
this.next = next;
this.next = next;
}
public List<T> getResults() {

View File

@ -15,11 +15,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.auth;
package io.linuxserver.fleet.delegate;
import io.linuxserver.fleet.auth.AuthenticationResult;
public interface AuthenticationDelegate {
AuthenticationResult authenticate(String username, String password);
String encodePassword(String rawPassword);
}

View File

@ -15,26 +15,22 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.auth;
package io.linuxserver.fleet.delegate;
import io.linuxserver.fleet.auth.AuthenticationResult;
import io.linuxserver.fleet.auth.UserCredentials;
import io.linuxserver.fleet.auth.authenticator.UserAuthenticator;
import io.linuxserver.fleet.auth.security.PasswordEncoder;
public class DefaultAuthenticationDelegate implements AuthenticationDelegate {
private final UserAuthenticator authenticator;
public DefaultAuthenticationDelegate(final UserAuthenticator authenticator) {
this.authenticator = authenticator;
public DefaultAuthenticationDelegate(UserAuthenticator authenticator) {
this.authenticator = authenticator;
}
@Override
public AuthenticationResult authenticate(final String username, final String password) {
public AuthenticationResult authenticate(String username, String password) {
return authenticator.authenticate(new UserCredentials(username, password));
}
@Override
public String encodePassword(final String rawPassword) {
return authenticator.getPasswordEncoder().encode(rawPassword);
}
}

View File

@ -0,0 +1,129 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.delegate;
import io.linuxserver.fleet.dockerhub.DockerHubClient;
import io.linuxserver.fleet.dockerhub.model.DockerHubV2Image;
import io.linuxserver.fleet.dockerhub.model.DockerHubV2Tag;
import io.linuxserver.fleet.dockerhub.util.DockerTagFinder;
import io.linuxserver.fleet.model.docker.DockerImage;
import io.linuxserver.fleet.model.docker.DockerTag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class DockerHubDelegate {
private static final Logger LOGGER = LoggerFactory.getLogger(DockerHubDelegate.class);
private final DockerHubClient dockerHubClient;
private final DockerTagFinder dockerTagFinder;
public DockerHubDelegate(DockerHubClient dockerHubClient) {
this.dockerHubClient = dockerHubClient;
this.dockerTagFinder = new DockerTagFinder();
}
public List<String> fetchAllRepositories() {
return dockerHubClient.fetchAllRepositories().getNamespaces();
}
public List<DockerImage> fetchAllImagesFromRepository(String repositoryName) {
List<DockerImage> images = new ArrayList<>();
for (DockerHubV2Image apiImage : dockerHubClient.fetchImagesFromRepository(repositoryName))
images.add(convertImage(apiImage));
images.sort(Comparator.comparing(DockerImage::getName));
return images;
}
public DockerImage fetchImageFromRepository(String repositoryName, String imageName) {
return convertImage(dockerHubClient.fetchImageFromRepository(repositoryName, imageName));
}
public List<DockerTag> fetchAllTagsForImage(String repositoryName, String imageName) {
return dockerHubClient.fetchAllTagsForImage(repositoryName, imageName).stream().map(this::convertTag).collect(Collectors.toList());
}
public DockerTag fetchLatestImageTag(String repositoryName, String imageName) {
List<DockerTag> tags = fetchAllTagsForImage(repositoryName, imageName);
if (tags.isEmpty()) {
return null;
}
return dockerTagFinder.findVersionedTagMatchingBranch(tags, "latest");
}
private DockerImage convertImage(DockerHubV2Image dockerHubV2Image) {
if (dockerHubV2Image == null) {
return null;
}
return new DockerImage(
dockerHubV2Image.getName(),
dockerHubV2Image.getNamespace(),
dockerHubV2Image.getDescription(),
dockerHubV2Image.getStarCount(),
dockerHubV2Image.getPullCount(),
parseDockerHubDate(dockerHubV2Image.getLastUpdated())
);
}
private DockerTag convertTag(DockerHubV2Tag dockerHubV2Tag) {
if (dockerHubV2Tag == null) {
return null;
}
return new DockerTag(
dockerHubV2Tag.getName(),
dockerHubV2Tag.getFullSize(),
parseDockerHubDate(dockerHubV2Tag.getLastUpdated())
);
}
private LocalDateTime parseDockerHubDate(String date) {
if (null == date)
return null;
try {
return LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'"));
} catch (DateTimeParseException e) {
return LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"));
} catch (Exception e) {
LOGGER.warn("parseDockerHubDate(" + date + ") unable to parse date.");
return null;
}
}
}

View File

@ -0,0 +1,128 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.delegate;
import io.linuxserver.fleet.cache.ImageCache;
import io.linuxserver.fleet.db.dao.ImageDAO;
import io.linuxserver.fleet.db.query.InsertUpdateResult;
import io.linuxserver.fleet.db.query.InsertUpdateStatus;
import io.linuxserver.fleet.exception.SaveException;
import io.linuxserver.fleet.model.internal.Image;
import io.linuxserver.fleet.model.internal.ImagePullStat;
import io.linuxserver.fleet.model.key.ImageKey;
import io.linuxserver.fleet.model.key.RepositoryKey;
import java.util.List;
public class ImageDelegate {
private final ImageCache imageCache;
private final ImageDAO imageDAO;
public ImageDelegate(ImageDAO imageDAO) {
this.imageDAO = imageDAO;
this.imageCache = new ImageCache();
}
/**
* <p>
* Performs a look-up to see if an image exists in the persisted database which matches
* the given repository and image name. This is used to determine if, when scanning, Fleet
* already has the image stored against the repository.
* </p>
*/
public Image findImageByRepositoryAndImageName(final ImageKey imageKey) {
Image cachedImage = imageCache.get(imageKey);
if (null == cachedImage) {
Image image = imageDAO.findImageByRepositoryAndImageName(imageKey);
if (null != image) {
imageCache.updateCache(image);
}
return image;
}
return cachedImage;
}
public Image fetchImage(ImageKey imageKey) {
Image cachedImage = imageCache.get(imageKey);
if (null == cachedImage) {
Image image = imageDAO.fetchImage(imageKey);
if (null != image) {
imageCache.updateCache(image);
}
return image;
}
return cachedImage;
}
public List<Image> fetchImagesByRepository(final RepositoryKey repositoryKey) {
List<Image> cachedImages = imageCache.getAll(repositoryKey);
if (cachedImages.isEmpty()) {
List<Image> images = imageDAO.fetchImagesByRepository(repositoryKey).getResults();
images.forEach(imageCache::updateCache);
return images;
}
return cachedImages;
}
public void removeImage(ImageKey imageKey) {
Image existingImage = imageDAO.fetchImage(imageKey);
if (null != existingImage) {
imageDAO.removeImage(imageKey);
imageCache.remove(imageKey);
}
}
public Image saveImage(Image image) throws SaveException {
InsertUpdateResult<Image> result = imageDAO.saveImage(image);
if (result.getStatus() == InsertUpdateStatus.OK) {
Image updatedImage = result.getResult();
imageCache.updateCache(updatedImage);
return result.getResult();
}
throw new SaveException(result.getStatusMessage());
}
public List<ImagePullStat> fetchImagePullHistory(ImageKey imageKey, ImagePullStat.GroupMode groupMode) {
return imageDAO.fetchImagePullHistory(imageKey, groupMode);
}
}

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.delegate;
import io.linuxserver.fleet.db.dao.RepositoryDAO;
import io.linuxserver.fleet.db.query.InsertUpdateResult;
import io.linuxserver.fleet.db.query.InsertUpdateStatus;
import io.linuxserver.fleet.exception.SaveException;
import io.linuxserver.fleet.model.internal.Repository;
import io.linuxserver.fleet.model.key.RepositoryKey;
import java.util.List;
public class RepositoryDelegate {
private final RepositoryDAO repositoryDAO;
public RepositoryDelegate(RepositoryDAO repositoryDAO) {
this.repositoryDAO = repositoryDAO;
}
public Repository fetchRepository(RepositoryKey repositoryKey) {
return repositoryDAO.fetchRepository(repositoryKey);
}
public Repository saveRepository(Repository repository) throws SaveException {
InsertUpdateResult<Repository> result = repositoryDAO.saveRepository(repository);
if (result.getStatus() == InsertUpdateStatus.OK)
return result.getResult();
throw new SaveException(result.getStatusMessage());
}
public List<Repository> fetchAllRepositories() {
return repositoryDAO.fetchAllRepositories();
}
public Repository findRepositoryByName(String name) {
return repositoryDAO.findRepositoryByName(name);
}
public void removeRepository(int id) {
repositoryDAO.removeRepository(id);
}
}

View File

@ -0,0 +1,89 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.delegate;
import io.linuxserver.fleet.sync.DefaultLoggingSyncListener;
import io.linuxserver.fleet.sync.DefaultSynchronisationState;
import io.linuxserver.fleet.sync.SynchronisationContext;
import io.linuxserver.fleet.sync.SynchronisationListener;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* Handles the one-way synchronisation of Docker Hub repositories and images over to the Fleet
* database. Any newly created image in Docker Hub will be automatically picked up and stored, but any
* new repositories will be marked as skipped until someone manually sets it to be synchronised.
* </p>
*/
public class SynchronisationDelegate implements SynchronisationContext {
private final ImageDelegate imageDelegate;
private final RepositoryDelegate repositoryDelegate;
private final DockerHubDelegate dockerHubDelegate;
private List<SynchronisationListener> listeners;
public SynchronisationDelegate(ImageDelegate imageDelegate, RepositoryDelegate repositoryDelegate, DockerHubDelegate dockerHubDelegate) {
this.imageDelegate = imageDelegate;
this.repositoryDelegate = repositoryDelegate;
this.dockerHubDelegate = dockerHubDelegate;
this.listeners = new ArrayList<>();
registerListener(new DefaultLoggingSyncListener());
}
/**
* <p>
* Starts a new synchronisation. This uses the statically assigned synchronisation state
* to determine whether or not a synchronisation will actually take place. Consider triggering
* this via {@link io.linuxserver.fleet.thread.SynchroniseAllRepositoriesTask}.
* </p>
*/
@Override
public void synchronise() {
DefaultSynchronisationState.instance().synchronise(this);
}
@Override
public void registerListener(SynchronisationListener listener) {
listeners.add(listener);
}
@Override
public List<SynchronisationListener> getListeners() {
return listeners;
}
@Override
public ImageDelegate getImageDelegate() {
return imageDelegate;
}
@Override
public RepositoryDelegate getRepositoryDelegate() {
return repositoryDelegate;
}
@Override
public DockerHubDelegate getDockerHubDelegate() {
return dockerHubDelegate;
}
}

View File

@ -0,0 +1,66 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.delegate;
import io.linuxserver.fleet.core.FleetBeans;
import io.linuxserver.fleet.thread.SynchroniseAllRepositoriesTask;
import io.linuxserver.fleet.thread.TaskManager;
import java.util.concurrent.TimeUnit;
/**
* <p>
* Manager of the {@link TaskManager} as a means to keep a reference to all dependencies
* that a task may need. Ensures that calling classes do not need to know the dependencies
* of each specific task.
* </p>
*/
public class TaskDelegate {
private final TaskManager taskManager;
private final SynchronisationDelegate synchronisationDelegate;
public TaskDelegate(FleetBeans beans) {
taskManager = beans.getTaskManager();
synchronisationDelegate = beans.getSynchronisationDelegate();
}
/**
* <p>
* Asynchronously triggers a new run of the synchronisation process.
* </p>
*/
public void runSynchronisationTask() {
taskManager.runTaskOnce(new SynchroniseAllRepositoriesTask(synchronisationDelegate));
}
/**
* <p>
* Creates a new schedule for the synchronisation process.
* </p>
*
* @param interval
* How often the process should run.
* @param timeUnit
* The relative unit of time of the interval.
*/
public void scheduleSynchronisationTask(int interval, TimeUnit timeUnit) {
taskManager.scheduleRecurringTask(new SynchroniseAllRepositoriesTask(synchronisationDelegate), interval, timeUnit);
}
}

View File

@ -0,0 +1,63 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.delegate;
import io.linuxserver.fleet.auth.security.PasswordEncoder;
import io.linuxserver.fleet.db.dao.UserDAO;
import io.linuxserver.fleet.db.query.InsertUpdateResult;
import io.linuxserver.fleet.db.query.InsertUpdateStatus;
import io.linuxserver.fleet.exception.SaveException;
import io.linuxserver.fleet.model.internal.User;
public class UserDelegate {
private final PasswordEncoder passwordEncoder;
private final UserDAO userDAO;
public UserDelegate(PasswordEncoder passwordEncoder, UserDAO userDAO) {
this.passwordEncoder = passwordEncoder;
this.userDAO = userDAO;
}
public User fetchUser(int id) {
return userDAO.fetchUser(id);
}
public User fetchUserByUsername(String username) {
return userDAO.fetchUserByUsername(username);
}
public boolean isUserRepositoryEmpty() {
return userDAO.fetchAllUsers().isEmpty();
}
public User createNewUser(String username, String password) throws SaveException {
InsertUpdateResult<User> result = userDAO.saveUser(new User(username, passwordEncoder.encode(password)));
if (result.getStatus() == InsertUpdateStatus.OK)
return result.getResult();
throw new SaveException(result.getStatusMessage());
}
public void removeUser(User user) {
userDAO.removeUser(user);
}
}

View File

@ -0,0 +1,88 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.dockerhub;
import io.linuxserver.fleet.rest.RestClient;
import io.linuxserver.fleet.rest.RestResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class DockerHubAuthenticator {
private static final Logger LOGGER = LoggerFactory.getLogger(DockerHubAuthenticator.class);
private final RestClient client;
private final DockerHubCredentials credentials;
private String token;
DockerHubAuthenticator(DockerHubCredentials credentials, RestClient client) {
this.credentials = credentials;
this.client = client;
refreshToken();
}
/**
* <p>
* Re-authenticates with Docker Hub to obtain a fresh JWT.
* </p>
*
* @return
* The new JWT to be used in authenticated requests.
*/
synchronized String refreshToken() {
LOGGER.info("Refreshing token for Docker Hub authentication");
RestResponse<DockerHubTokenResponse> authenticationResponse = client.executePost(
DockerHubV2Client.DOCKERHUB_BASE_URI + "/users/login", null, null, credentials, DockerHubTokenResponse.class);
if (authenticationResponse.getStatusCode() == 200) {
LOGGER.info("Refresh successful");
String token = authenticationResponse.getPayload().getToken();
this.token = token;
return token;
}
LOGGER.info("Unable to refresh token.");
throw new DockerHubException("Unable to authenticate with Docker Hub. Check credentials");
}
synchronized String getCurrentToken() {
if (null == token)
return refreshToken();
return token;
}
static class DockerHubTokenResponse {
private String token;
public String getToken() {
return token;
}
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.dockerhub;
import io.linuxserver.fleet.dockerhub.model.DockerHubV2Image;
import io.linuxserver.fleet.dockerhub.model.DockerHubV2NamespaceLookupResult;
import io.linuxserver.fleet.dockerhub.model.DockerHubV2Tag;
import java.util.List;
public interface DockerHubClient {
DockerHubV2NamespaceLookupResult fetchAllRepositories();
List<DockerHubV2Image> fetchImagesFromRepository(String repositoryName);
DockerHubV2Image fetchImageFromRepository(String repositoryName, String imageName);
List<DockerHubV2Tag> fetchAllTagsForImage(String repositoryName, String imageName);
}

View File

@ -15,24 +15,24 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.types;
package io.linuxserver.fleet.dockerhub;
public class ImageCountData {
public class DockerHubCredentials {
private final long pullCount;
private final int starCount;
private final String username;
private final String password;
public ImageCountData(final long pullCount, final int starCount) {
public DockerHubCredentials(String username, String password) {
this.pullCount = pullCount;
this.starCount = starCount;
this.username = username;
this.password = password;
}
public final long getPullCount() {
return pullCount;
public String getUsername() {
return username;
}
public final int getStarCount() {
return starCount;
public String getPassword() {
return password;
}
}

View File

@ -0,0 +1,169 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.dockerhub;
import io.linuxserver.fleet.dockerhub.model.*;
import io.linuxserver.fleet.rest.HttpException;
import io.linuxserver.fleet.rest.RestClient;
import io.linuxserver.fleet.rest.RestResponse;
import java.util.*;
/**
* Client class to interface with Docker Hub's V2 API.
*/
public class DockerHubV2Client implements DockerHubClient {
private static final int DEFAULT_PAGE_SIZE = 1000;
static final String DOCKERHUB_BASE_URI = "https://hub.docker.com/v2";
private final RestClient restClient;
private final DockerHubAuthenticator authenticator;
public DockerHubV2Client(DockerHubCredentials credentials) {
this.restClient = new RestClient();
this.authenticator = new DockerHubAuthenticator(credentials, restClient);
}
@Override
public DockerHubV2NamespaceLookupResult fetchAllRepositories() {
try {
String url = DOCKERHUB_BASE_URI + "/repositories/namespaces";
RestResponse<DockerHubV2NamespaceLookupResult> response = doCall(url, DockerHubV2NamespaceLookupResult.class);
if (isResponseOK(response)) {
return response.getPayload();
}
return new DockerHubV2NamespaceLookupResult();
} catch (HttpException e) {
throw new DockerHubException("Unable to get repositories", e);
}
}
@Override
public List<DockerHubV2Image> fetchImagesFromRepository(String repositoryName) {
List<DockerHubV2Image> images = new ArrayList<>();
try {
String url = DOCKERHUB_BASE_URI + "/repositories/" + repositoryName + "?page_size=" + DEFAULT_PAGE_SIZE;
while (url != null) {
RestResponse<DockerHubV2ImageListResult> response = doCall(url, DockerHubV2ImageListResult.class);
if (isResponseOK(response)) {
DockerHubV2ImageListResult payload = response.getPayload();
images.addAll(payload.getResults());
url = payload.getNext();
}
}
return images;
} catch (HttpException e) {
throw new DockerHubException("Unable to get images for " + repositoryName, e);
}
}
@Override
public DockerHubV2Image fetchImageFromRepository(String repositoryName, String imageName) {
try {
String absoluteUrl = DOCKERHUB_BASE_URI + "/repositories/" + repositoryName + "/" + imageName;
RestResponse<DockerHubV2Image> restResponse = doCall(absoluteUrl, DockerHubV2Image.class);
return restResponse.getPayload();
} catch (HttpException e) {
throw new DockerHubException("Unable to get images for " + repositoryName + "/" + imageName, e);
}
}
@Override
public List<DockerHubV2Tag> fetchAllTagsForImage(String repositoryName, String imageName) {
try {
List<DockerHubV2Tag> tags = new ArrayList<>();
String absoluteUrl = DOCKERHUB_BASE_URI + "/repositories/" + repositoryName + "/" + imageName + "/tags?page_size=" + DEFAULT_PAGE_SIZE;
while (absoluteUrl != null) {
RestResponse<DockerHubV2TagListResult> response = doCall(absoluteUrl, DockerHubV2TagListResult.class);
if (isResponseOK(response)) {
DockerHubV2TagListResult payload = response.getPayload();
tags.addAll(payload.getResults());
absoluteUrl = payload.getNext();
}
}
return tags;
} catch (HttpException e) {
throw new DockerHubException("Unable to get tags for " + repositoryName + "/" + imageName, e);
}
}
/**
* <p>
* Attempts to call DockerHub with the currently set credentials. If they have expired, it will refresh them and try again.
* This process will only try again once, so if the refresh resulted in another stale token, it will need to be handled.
* </p>
*/
private <T> RestResponse<T> doCall(String url, Class<T> responseType) {
RestResponse<T> restResponse = restClient.executeGet(url, null, buildHeaders(), responseType);
if (isResponseUnauthorised(restResponse)) {
authenticator.refreshToken();
restResponse = restClient.executeGet(url, null, buildHeaders(), responseType);
}
return restResponse;
}
private Map<String, String> buildHeaders() {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "JWT " + authenticator.getCurrentToken());
return headers;
}
private boolean isResponseOK(RestResponse restResponse) {
return restResponse.getStatusCode() == 200;
}
private boolean isResponseUnauthorised(RestResponse restResponse) {
return restResponse.getStatusCode() == 401;
}
}

View File

@ -19,7 +19,6 @@ package io.linuxserver.fleet.dockerhub.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import java.util.Objects;
public class DockerHubV2Tag {
@ -33,9 +32,6 @@ public class DockerHubV2Tag {
@JsonProperty("last_updated")
private String lastUpdated;
@JsonProperty("images")
private List<DockerHubV2TagDigest> images;
public String getName() {
return name;
}
@ -48,10 +44,6 @@ public class DockerHubV2Tag {
return lastUpdated;
}
public List<DockerHubV2TagDigest> getImages() {
return images;
}
@Override
public String toString() {
return name;

View File

@ -0,0 +1,47 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.dockerhub.queue;
import io.linuxserver.fleet.delegate.ImageDelegate;
import io.linuxserver.fleet.exception.SaveException;
import io.linuxserver.fleet.model.internal.Image;
import io.linuxserver.fleet.queue.AbstractQueueConsumer;
import io.linuxserver.fleet.queue.RequestQueue;
public class DockerHubSyncConsumer extends AbstractQueueConsumer<DockerHubSyncResponse, DockerHubSyncRequest> {
private final ImageDelegate imageDelegate;
public DockerHubSyncConsumer(ImageDelegate imageDelegate, RequestQueue<DockerHubSyncRequest> requestQueue, String name) {
super(requestQueue, name);
this.imageDelegate = imageDelegate;
}
@Override
protected void handleResponse(DockerHubSyncResponse response) {
try {
final Image image = response.getImage();
imageDelegate.saveImage(image);
} catch (SaveException e) {
getLogger().error("handleResponse unable to save image: {}", e.getMessage());
}
}
}

View File

@ -15,10 +15,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.cache;
package io.linuxserver.fleet.dockerhub.queue;
import io.linuxserver.fleet.v2.key.ImageKey;
import io.linuxserver.fleet.v2.types.Image;
import io.linuxserver.fleet.queue.AbstractRequestQueue;
public class ImageCache extends AbstractItemCache<ImageKey, Image> {
public class DockerHubSyncQueue extends AbstractRequestQueue<DockerHubSyncRequest> {
}

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.dockerhub.queue;
import io.linuxserver.fleet.delegate.DockerHubDelegate;
import io.linuxserver.fleet.model.internal.Image;
import io.linuxserver.fleet.queue.FleetRequest;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
public class DockerHubSyncRequest implements FleetRequest<DockerHubSyncResponse> {
private final DockerHubDelegate dockerHubDelegate;
private final Image image;
public DockerHubSyncRequest(DockerHubDelegate dockerHubDelegate, Image image) {
this.dockerHubDelegate = dockerHubDelegate;
this.image = image;
}
@Override
public DockerHubSyncResponse execute() {
return new DockerHubSyncResponse(dockerHubDelegate, image);
}
@Override
public boolean equals(Object o) {
if (!(o instanceof DockerHubSyncRequest))
return false;
return EqualsBuilder.reflectionEquals(this, "dockerHubDelegate");
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this, "dockerHubDelegate");
}
}

View File

@ -0,0 +1,100 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.dockerhub.queue;
import io.linuxserver.fleet.delegate.DockerHubDelegate;
import io.linuxserver.fleet.model.docker.DockerImage;
import io.linuxserver.fleet.model.docker.DockerTag;
import io.linuxserver.fleet.model.internal.Image;
import io.linuxserver.fleet.model.internal.Tag;
import io.linuxserver.fleet.queue.FleetResponse;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DockerHubSyncResponse implements FleetResponse {
private final DockerHubDelegate dockerHubDelegate;
private final Image image;
public DockerHubSyncResponse(DockerHubDelegate dockerHubDelegate, Image image) {
this.dockerHubDelegate = dockerHubDelegate;
this.image = Image.copyOf(image);
}
@Override
public void handle() {
DockerImage dockerImage = dockerHubDelegate.fetchImageFromRepository(image.getKey().getRepositoryKey().getName(), image.getName());
String versionMask = getVersionMask(null, image.getVersionMask());
Tag maskedVersion = getLatestTagAndCreateMaskedVersion(versionMask);
image.withPullCount(dockerImage.getPullCount());
image.updateTag(maskedVersion);
}
public Image getImage() {
return image;
}
private String getVersionMask(String repositoryMask, String imageMask) {
return imageMask == null ? repositoryMask : imageMask;
}
private Tag getLatestTagAndCreateMaskedVersion(String versionMask) {
DockerTag tag = dockerHubDelegate.fetchLatestImageTag(image.getKey().getRepositoryKey().getName(), image.getName());
if (null == tag)
return Tag.NONE;
if (isTagJustLatestAndNotAVersion(tag) || null == versionMask)
return new Tag(tag.getName(), tag.getName(), tag.getBuildDate());
return new Tag(tag.getName(), extractMaskedVersion(tag.getName(), versionMask), tag.getBuildDate());
}
private String extractMaskedVersion(String fullTag, String versionMask) {
Pattern pattern = Pattern.compile(versionMask);
Matcher matcher = pattern.matcher(fullTag);
if (matcher.matches()) {
StringBuilder tagBuilder = new StringBuilder();
for (int groupNum = 1; groupNum <= matcher.groupCount(); groupNum++)
tagBuilder.append(matcher.group(groupNum));
return tagBuilder.toString();
}
return fullTag;
}
/**
* <p>
* If the top-level tag is not versioned, a mask can't be applied.
* </p>
*/
private boolean isTagJustLatestAndNotAVersion(DockerTag tag) {
return "latest".equals(tag.getName());
}
}

View File

@ -17,64 +17,26 @@
package io.linuxserver.fleet.dockerhub.util;
import io.linuxserver.fleet.v2.types.docker.DockerTag;
import io.linuxserver.fleet.v2.types.docker.DockerTagManifestDigest;
import io.linuxserver.fleet.model.docker.DockerTag;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class DockerTagFinder {
public static DockerTag findVersionedTagMatchingBranch(List<DockerTag> tags, String namedBranch) {
public DockerTag findVersionedTagMatchingBranch(List<DockerTag> tags, String namedBranch) {
Optional<DockerTag> tagBranchName = tags.stream().filter(tag -> namedBranch.equals(tag.getName())).findFirst();
Optional<DockerTag> trueLatest = tags.stream().filter(tag -> namedBranch.equals(tag.getName())).findFirst();
if (tagBranchName.isPresent()) {
if (trueLatest.isPresent()) {
DockerTag namedTagForBranch = tagBranchName.get();
DockerTag trueLatestTag = trueLatest.get();
Optional<DockerTag> versionedLatestTag = tags.stream()
.filter(tag -> !tag.equals(namedTagForBranch) && allManifestsMatch(namedTagForBranch, tag)).findFirst();
.filter(tag -> !tag.equals(trueLatestTag) && tag.getSize() == trueLatestTag.getSize()).findFirst();
return versionedLatestTag.orElse(namedTagForBranch);
return versionedLatestTag.orElse(trueLatestTag);
}
return tags.isEmpty() ? null : tags.get(0);
}
private static boolean allManifestsMatch(final DockerTag namedTag, final DockerTag toCheck) {
final List<DockerTagManifestDigest> namedDigests = namedTag.getDigests();
final List<DockerTagManifestDigest> digestsToCheck = toCheck.getDigests();
boolean allMatch = true;
if (namedDigests.size() == digestsToCheck.size()) {
final Map<String, String> namedDigestsAsMap = toMapKeyedByArch(namedDigests);
for (DockerTagManifestDigest digestToCheck : digestsToCheck) {
final String archPlusVariant = digestToCheck.getArchitecture() + digestToCheck.getArchVariant();
final String foundDigest = namedDigestsAsMap.get(archPlusVariant);
allMatch = allMatch && (null != foundDigest) && foundDigest.equals(digestToCheck.getDigest());
}
} else {
allMatch = false;
}
return allMatch;
}
private static Map<String, String> toMapKeyedByArch(final List<DockerTagManifestDigest> initialList) {
final Map<String, String> map = new HashMap<>();
for (DockerTagManifestDigest digest : initialList) {
map.put(digest.getArchitecture() + digest.getArchVariant(), digest.getDigest());
}
return map;
return tags.get(0);
}
}

View File

@ -0,0 +1,74 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.model.api;
import io.linuxserver.fleet.model.internal.Image;
public class ApiImage {
private String imageKey;
private String name;
private long pullCount;
private String version;
private boolean stable;
private boolean deprecated;
private String buildDate;
public String getImageKey() {
return imageKey;
}
public String getName() {
return name;
}
public long getPullCount() {
return pullCount;
}
public String getVersion() {
return version;
}
public boolean isStable() {
return stable;
}
public boolean isDeprecated() {
return deprecated;
}
public String getBuildDate() {
return buildDate;
}
public static ApiImage fromImage(Image image) {
ApiImage apiImage = new ApiImage();
apiImage.imageKey = image.getKey().toString();
apiImage.name = image.getName();
apiImage.pullCount = image.getPullCount();
apiImage.version = image.getMaskedVersion();
apiImage.stable = !image.isUnstable();
apiImage.deprecated = image.isDeprecated();
apiImage.buildDate = image.getBuildDateAsString();
return apiImage;
}
}

View File

@ -0,0 +1,81 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.model.api;
import io.linuxserver.fleet.model.internal.Image;
import io.linuxserver.fleet.model.internal.ImagePullStat;
import java.util.ArrayList;
import java.util.List;
public class ApiImagePullHistory {
private int imageId;
private String imageName;
private String groupMode;
private List<ApiImagePullStat> pullHistory = new ArrayList<>();
public int getImageId() {
return imageId;
}
public String getImageName() {
return imageName;
}
public String getGroupMode() {
return groupMode;
}
public List<ApiImagePullStat> getPullHistory() {
return pullHistory;
}
public static ApiImagePullHistory fromPullStats(Image image, List<ImagePullStat> stats) {
ApiImagePullHistory history = new ApiImagePullHistory();
history.imageId = image.getKey().getId();
history.imageName = image.getName();
history.groupMode = stats.get(0).getGroupMode().toString();
for (ImagePullStat stat : stats)
history.pullHistory.add(new ApiImagePullStat(stat.getTimeGroup(), stat.getPullCount()));
return history;
}
public static class ApiImagePullStat {
private final String timeGroup;
private final long pullCount;
ApiImagePullStat(String timeGroup, long pullCount) {
this.timeGroup = timeGroup;
this.pullCount = pullCount;
}
public String getTimeGroup() {
return timeGroup;
}
public long getPullCount() {
return pullCount;
}
}
}

View File

@ -15,31 +15,27 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.web;
package io.linuxserver.fleet.model.api;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PageModelSpec {
public class ApiImagesWithTotalCount {
private final String viewName;
private final Map<String, Object> model;
private final long totalPullCount;
private final Map<String, List<ApiImage>> repositories;
public PageModelSpec(final String viewName) {
public ApiImagesWithTotalCount(long totalPullCount, Map<String, List<ApiImage>> repositories) {
this.viewName = viewName;
this.model = new HashMap<>();
this.totalPullCount = totalPullCount;
this.repositories = repositories;
}
public final String getViewName() {
return viewName;
public long getTotalPullCount() {
return totalPullCount;
}
public final Map<String, Object> getModel() {
return model;
}
public void addModelAttribute(final String key, final Object value) {
model.put(key, value);
public Map<String, List<ApiImage>> getRepositories() {
return repositories;
}
}

View File

@ -15,18 +15,24 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.key;
package io.linuxserver.fleet.model.api;
public class TagBranchKey extends AbstractDatabaseKey {
public class ApiResponse<T> {
private final ImageKey imageKey;
private final String status;
private final T data;
public TagBranchKey(final Integer id, final ImageKey imageKey) {
super(id);
this.imageKey = imageKey;
public ApiResponse(String status, T data) {
this.status = status;
this.data = data;
}
public final ImageKey getImageKey() {
return imageKey;
public String getStatus() {
return status;
}
public T getData() {
return data;
}
}

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.model.api;
public class FleetApiException extends RuntimeException {
private final int statusCode;
public FleetApiException(int statusCode, String message) {
super(message);
this.statusCode = statusCode;
}
public FleetApiException(int statusCode, String message, Throwable cause) {
super(message, cause);
this.statusCode = statusCode;
}
public int getStatusCode() {
return statusCode;
}
}

View File

@ -15,11 +15,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.types.docker;
package io.linuxserver.fleet.model.docker;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
@ -36,8 +34,6 @@ public class DockerImage {
private final long pullCount;
private final LocalDateTime buildDate;
private List<DockerTag> tags = new ArrayList<>();
public DockerImage(String name, String repository, String description, int starCount, long pullCount, LocalDateTime buildDate) {
this.name = name;
@ -48,40 +44,27 @@ public class DockerImage {
this.buildDate = buildDate;
}
public final void addTag(final DockerTag tag) {
tags.add(tag);
}
public final List<DockerTag> getTags() {
return tags;
}
public final String getName() {
public String getName() {
return name;
}
public final String getRepository() {
public String getRepository() {
return repository;
}
public final String getDescription() {
public String getDescription() {
return description;
}
public final int getStarCount() {
public int getStarCount() {
return starCount;
}
public final long getPullCount() {
public long getPullCount() {
return pullCount;
}
public final LocalDateTime getBuildDate() {
public LocalDateTime getBuildDate() {
return buildDate;
}
@Override
public final String toString() {
return getRepository() + "/" + getName();
}
}

View File

@ -15,14 +15,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.types.docker;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
package io.linuxserver.fleet.model.docker;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
public class DockerTag {
@ -30,8 +25,6 @@ public class DockerTag {
private final long size;
private final LocalDateTime buildDate;
private final List<DockerTagManifestDigest> digests = new ArrayList<>();
public DockerTag(String name, long size, LocalDateTime buildDate) {
this.name = name;
@ -39,14 +32,6 @@ public class DockerTag {
this.buildDate = buildDate;
}
public final void addDigest(final DockerTagManifestDigest digest) {
digests.add(digest);
}
public final List<DockerTagManifestDigest> getDigests() {
return digests;
}
public String getName() {
return name;
}
@ -58,9 +43,4 @@ public class DockerTag {
public LocalDateTime getBuildDate() {
return buildDate;
}
@Override
public final String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}

View File

@ -0,0 +1,187 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.model.internal;
import io.linuxserver.fleet.model.key.AbstractHasKey;
import io.linuxserver.fleet.model.key.ImageKey;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* <p>
* Representation of a stored image in the Fleet database. Each image contains
* specific information regarding its build status and pull count.
* </p>
*/
public class Image extends AbstractHasKey<ImageKey> {
private static final DateTimeFormatter DATE_PATTERN = DateTimeFormatter.ofPattern("dd MMM yyyy HH:mm:ss");
private Tag tag;
private long pullCount;
private String versionMask;
private boolean unstable;
private boolean hidden;
private LocalDateTime modifiedTime;
private boolean deprecated;
private String deprecationReason;
public Image(final ImageKey imageKey, final Tag latestVersion) {
super(imageKey);
this.tag = new Tag(latestVersion.getVersion(), latestVersion.getMaskedVersion(), latestVersion.getBuildDate());
}
public Image(final ImageKey imageKey) {
this(imageKey, Tag.NONE);
}
public static Image makeFromKey(ImageKey imageKey) {
return new Image(imageKey);
}
public static Image copyOf(Image image) {
Image cloned = new Image(image.getKey(), image.tag);
cloned.pullCount = image.pullCount;
cloned.versionMask = image.versionMask;
cloned.unstable = image.unstable;
cloned.hidden = image.hidden;
cloned.deprecated = image.deprecated;
cloned.deprecationReason = image.deprecationReason;
cloned.modifiedTime = image.modifiedTime;
return cloned;
}
public Image withPullCount(long pullCount) {
this.pullCount = pullCount;
return this;
}
public Image withVersionMask(String versionMask) {
this.versionMask = versionMask;
return this;
}
public Image withHidden(boolean hidden) {
this.hidden = hidden;
return this;
}
public Image withUnstable(boolean unstable) {
this.unstable = unstable;
return this;
}
public Image withDeprecated(boolean deprecated) {
this.deprecated = deprecated;
return this;
}
public Image withDeprecationReason(String deprecationReason) {
this.deprecationReason = deprecationReason;
return this;
}
public Image withModifiedTime(final LocalDateTime modifiedTime) {
this.modifiedTime = modifiedTime;
return this;
}
public void updateTag(Tag maskedVersion) {
this.tag = new Tag(maskedVersion.getVersion(), maskedVersion.getMaskedVersion(), maskedVersion.getBuildDate());
}
public int getRepositoryId() {
return getKey().getRepositoryKey().getId();
}
public String getName() {
return getKey().getName();
}
public long getPullCount() {
return pullCount;
}
public String getMaskedVersion() {
return tag.getMaskedVersion();
}
public String getRawVersion() {
return tag.getVersion();
}
public LocalDateTime getBuildDate() {
return tag.getBuildDate();
}
public String getBuildDateAsString() {
if (getBuildDate() != null) {
return getBuildDate().format(DATE_PATTERN);
}
return null;
}
public String getVersionMask() {
return versionMask;
}
public boolean isUnstable() {
return unstable;
}
public boolean isHidden() {
return hidden;
}
public boolean isDeprecated() {
return deprecated;
}
public String getDeprecationReason() {
return deprecationReason;
}
public LocalDateTime getModifiedTime() {
if (null != modifiedTime) {
return LocalDateTime.of(
modifiedTime.getYear(),
modifiedTime.getMonth(),
modifiedTime.getDayOfMonth(),
modifiedTime.getHour(),
modifiedTime.getMinute(),
modifiedTime.getSecond()
);
}
return null;
}
}

View File

@ -0,0 +1,76 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.model.internal;
public class ImagePullStat {
private final int imageId;
private final String timeGroup;
private final long pullCount;
private final GroupMode groupMode;
public ImagePullStat(int imageId, String timeGroup, long pullCount, GroupMode groupMode) {
this.imageId = imageId;
this.timeGroup = timeGroup;
this.pullCount = pullCount;
this.groupMode = groupMode;
}
public int getImageId() {
return imageId;
}
public String getTimeGroup() {
return timeGroup;
}
public long getPullCount() {
return pullCount;
}
public GroupMode getGroupMode() {
return groupMode;
}
public enum GroupMode {
HOUR, DAY, WEEK, MONTH, YEAR;
@Override
public String toString() {
return name().toLowerCase();
}
public static boolean isValid(String value) {
try {
if (null == value) {
return false;
}
valueOf(value);
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
}
}

View File

@ -0,0 +1,85 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.model.internal;
import io.linuxserver.fleet.model.key.AbstractHasKey;
import io.linuxserver.fleet.model.key.RepositoryKey;
import java.time.LocalDateTime;
public class Repository extends AbstractHasKey<RepositoryKey> {
private String versionMask;
private boolean syncEnabled;
private LocalDateTime modifiedTime;
public Repository(final RepositoryKey repositoryKey) {
super(repositoryKey);
}
public static Repository copyOf(Repository repository) {
return new Repository(repository.getKey()).withVersionMask(repository.versionMask).withSyncEnabled(repository.syncEnabled);
}
public Repository withVersionMask(String versionMask) {
this.versionMask = versionMask;
return this;
}
public Repository withSyncEnabled(boolean syncEnabled) {
this.syncEnabled = syncEnabled;
return this;
}
public Repository withModifiedTime(LocalDateTime modifiedTime) {
this.modifiedTime = modifiedTime;
return this;
}
public String getName() {
return getKey().getName();
}
public String getVersionMask() {
return versionMask;
}
public boolean isSyncEnabled() {
return syncEnabled;
}
public LocalDateTime getModifiedTime() {
if (null != modifiedTime) {
return LocalDateTime.of(
modifiedTime.getYear(),
modifiedTime.getMonth(),
modifiedTime.getDayOfMonth(),
modifiedTime.getHour(),
modifiedTime.getMinute(),
modifiedTime.getSecond()
);
}
return null;
}
}

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.model.internal;
import java.util.Collections;
import java.util.List;
public class RepositoryWithImages {
private final Repository repository;
private final List<Image> images;
public RepositoryWithImages(Repository repository, List<Image> images) {
this.repository = repository;
this.images = Collections.unmodifiableList(images);
}
public Repository getRepository() {
return repository;
}
public List<Image> getImages() {
return images;
}
public boolean isEveryImageStable() {
return images.stream().noneMatch(Image::isUnstable);
}
}

View File

@ -15,34 +15,39 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.types;
package io.linuxserver.fleet.model.internal;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import java.time.LocalDateTime;
import java.util.*;
public class Tag {
public static final Tag DefaultUnknown = new Tag("Unknown", null, Collections.emptySet());
public static final Tag NONE = new Tag("<Never Built>", "<Never Built>", null);
private final String version;
private final Set<TagDigest> digests;
private final LocalDateTime buildDate;
private String version;
private String maskedVersion;
private LocalDateTime buildDate;
public Tag(final String version, final LocalDateTime buildDate, final Set<TagDigest> digests) {
public Tag(String version, String maskedVersion, LocalDateTime buildDate) {
this.version = version;
this.digests = Collections.unmodifiableSet(digests);
this.buildDate = (null == buildDate ? null : LocalDateTime.of(buildDate.toLocalDate(), buildDate.toLocalTime()));
}
this.version = version;
this.maskedVersion = maskedVersion;
public final List<TagDigest> getDigests() {
return new ArrayList<>(digests);
if (null != buildDate) {
this.buildDate = LocalDateTime.of(buildDate.toLocalDate(), buildDate.toLocalTime());
}
}
public String getVersion() {
return version;
}
public String getMaskedVersion() {
return maskedVersion;
}
public LocalDateTime getBuildDate() {
if (null != buildDate) {
@ -54,6 +59,6 @@ public class Tag {
@Override
public String toString() {
return version;
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}

View File

@ -0,0 +1,76 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.model.internal;
import io.linuxserver.fleet.model.key.AbstractHasKey;
import io.linuxserver.fleet.model.key.UserKey;
import java.time.LocalDateTime;
public class User extends AbstractHasKey<UserKey> {
private final String username;
private final String password;
private LocalDateTime modifiedTime;
public User(String username, String password) {
super(new UserKey());
this.username = username;
this.password = password;
}
public User(Integer id, String username, String password) {
super(new UserKey(id));
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public User withModifiedTime(LocalDateTime modifiedTime) {
this.modifiedTime = modifiedTime;
return this;
}
public LocalDateTime getModifiedTime() {
if (null != modifiedTime) {
return LocalDateTime.of(
modifiedTime.getYear(),
modifiedTime.getMonth(),
modifiedTime.getDayOfMonth(),
modifiedTime.getHour(),
modifiedTime.getMinute(),
modifiedTime.getSecond()
);
}
return null;
}
}

View File

@ -15,16 +15,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.key;
import io.linuxserver.fleet.v2.Utils;
package io.linuxserver.fleet.model.key;
public abstract class AbstractHasKey<KEY extends Key> implements HasKey<KEY> {
private final KEY key;
public AbstractHasKey(final KEY key) {
this.key = Utils.ensureNotNull(key);
this.key = key;
}
@Override
@ -48,28 +46,7 @@ public abstract class AbstractHasKey<KEY extends Key> implements HasKey<KEY> {
}
@Override
public String toString() {
public final String toString() {
return key.toString();
}
@Override
public int compareTo(HasKey<KEY> o) {
if (null == o) {
return -1;
}
final Integer otherId = o.getKey().getId();
final Integer thisId = getKey().getId();
if (null == otherId && null == thisId) {
return 0;
}
if (null == otherId) {
return -1;
} else {
return o.getKey().getId().compareTo(getKey().getId());
}
}
}

View File

@ -15,13 +15,13 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.key;
package io.linuxserver.fleet.model.key;
public abstract class AbstractDatabaseKey implements Key {
public abstract class AbstractKey implements Key {
private final Integer id;
public AbstractDatabaseKey(final Integer id) {
public AbstractKey(final Integer id) {
this.id = id;
}

View File

@ -15,9 +15,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.key;
package io.linuxserver.fleet.model.key;
public interface HasKey<KEY extends Key> extends Comparable<HasKey<KEY>> {
public interface HasKey<KEY extends Key> {
KEY getKey();
}

View File

@ -15,16 +15,15 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.key;
package io.linuxserver.fleet.model.key;
public class ImageKey extends AbstractDatabaseKey {
public class ImageKey extends AbstractKey {
private static final String KeyPattern = "^\\d+:\\d+:[^/]+/[^/]+$";
private final RepositoryKey repositoryKey;
private final String name;
@Deprecated
public ImageKey(final String name, final RepositoryKey repositoryKey) {
this(null, name, repositoryKey);
}
@ -40,12 +39,12 @@ public class ImageKey extends AbstractDatabaseKey {
if (keyAsString.matches(KeyPattern)) {
final String[] keyParts = keyAsString.split(":");
final String[] names = keyParts[2].split("/");
final int repositoryId = Integer.parseInt(keyParts[0]);
final int imageId = Integer.parseInt(keyParts[1]);
final String[] keyParts = keyAsString.split(":");
final String[] names = keyParts[2].split("/");
final int repositoryId = Integer.parseInt(keyParts[0]);
final int imageId = Integer.parseInt(keyParts[1]);
final String repositoryName = names[0];
final String imageName = names[1];
final String imageName = names[1];
return new ImageKey(imageId, imageName, new RepositoryKey(repositoryId, repositoryName));
@ -54,12 +53,8 @@ public class ImageKey extends AbstractDatabaseKey {
}
}
public final ImageLookupKey getAsLookupKey() {
return new ImageLookupKey(getRepositoryKey().getName() + "/" + getName());
}
public final String getAsRepositoryAndImageName() {
return getAsLookupKey().toString();
public ImageKey cloneWithId(int id) {
return new ImageKey(id, name, repositoryKey);
}
public final RepositoryKey getRepositoryKey() {
@ -74,9 +69,4 @@ public class ImageKey extends AbstractDatabaseKey {
public String toString() {
return repositoryKey.getId() + ":" + super.toString() + ":" + repositoryKey.getName() + "/" + name;
}
@Override
public boolean equals(Object o) {
return super.equals(o) && ((ImageKey) o).name.equals(name);
}
}

View File

@ -15,9 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.key;
import javax.naming.OperationNotSupportedException;
package io.linuxserver.fleet.model.key;
public interface Key {

View File

@ -15,27 +15,18 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.key;
package io.linuxserver.fleet.model.key;
public class RepositoryKey extends AbstractDatabaseKey {
private static final String KeyPattern = "^\\d+:[^/]++$";
public class RepositoryKey extends AbstractKey {
private final String name;
public static RepositoryKey parse(final String keyAsString) {
public RepositoryKey(final int id) {
this(id, "<LookupKey>");
}
if (keyAsString.matches(KeyPattern)) {
final String[] keyParts = keyAsString.split(":");
final int repositoryId = Integer.parseInt(keyParts[0]);
final String repositoryName = keyParts[1];
return new RepositoryKey(repositoryId, repositoryName);
} else {
throw new IllegalArgumentException("Key pattern is malformed");
}
public RepositoryKey(final String name) {
this(null, name);
}
public RepositoryKey(final Integer id, final String name) {

View File

@ -15,9 +15,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.key;
package io.linuxserver.fleet.model.key;
public class UserKey extends AbstractDatabaseKey {
public class UserKey extends AbstractKey {
public UserKey() {
this(null);

View File

@ -0,0 +1,87 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.queue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.atomic.AtomicReference;
public abstract class AbstractQueueConsumer<R extends FleetResponse, T extends FleetRequest<R>> extends Thread implements QueueConsumer<T> {
private final Logger LOGGER = LoggerFactory.getLogger(getClass().getName());
private final RequestQueue<T> requestQueue;
private final AtomicReference<ConsumerState> state;
public AbstractQueueConsumer(final RequestQueue<T> requestQueue, final String name) {
super(name + "-Consumer");
this.requestQueue = requestQueue;
this.state = new AtomicReference<>(ConsumerState.Stopped);
}
@Override
public void run() {
while (isRunning()) {
consume();
}
}
@Override
public void start() {
getLogger().info("Starting...");
super.start();
state.set(ConsumerState.Running);
}
@Override
public void consume() {
try {
T request = requestQueue.takeOneRequest();
R response = request.execute();
handleResponse(response);
} catch (Exception e) {
getLogger().error("consume caught unhandled exception", e);
}
}
public boolean isRunning() {
return state.get().isRunning();
}
protected abstract void handleResponse(R response);
public Logger getLogger() {
return LOGGER;
}
private enum ConsumerState {
Running, Stopped;
final boolean isRunning() {
return this == Running;
}
}
}

View File

@ -0,0 +1,64 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.queue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
public class AbstractRequestQueue<T extends FleetRequest<? extends FleetResponse>> implements RequestQueue<T> {
private final Logger LOGGER = LoggerFactory.getLogger(getClass().getName());
private final BlockingQueue<T> requestQueue;
public AbstractRequestQueue() {
this.requestQueue = new LinkedBlockingDeque<>();
}
@Override
public synchronized void enqueueRequest(final T request) {
try {
if (!requestQueue.contains(request)) {
final boolean added = requestQueue.add(request);
if (!added) {
LOGGER.warn("enqueueRequest unable to add request to queue");
}
}
} catch (Exception e) {
LOGGER.error("enqueueRequest caught unhandled exception {}", e.getMessage());
}
}
@Override
public synchronized T takeOneRequest() {
try {
return requestQueue.take();
} catch (InterruptedException e) {
LOGGER.error("takeOneRequest was interrupted: {}", e.getMessage());
throw new RuntimeException(e);
}
}
}

View File

@ -15,11 +15,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2;
package io.linuxserver.fleet.queue;
import org.slf4j.Logger;
public interface FleetRequest<T extends FleetResponse> {
public interface LoggerOwner {
Logger getLogger();
T execute();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 LinuxServer.io
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -15,7 +15,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.client.docker.github.model;
package io.linuxserver.fleet.queue;
public class GitHubImage {
public interface FleetResponse {
void handle();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 LinuxServer.io
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -15,7 +15,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.client.docker.github.model;
package io.linuxserver.fleet.queue;
public class GitHubTag {
public interface QueueConsumer<T extends FleetRequest<?>> {
void consume();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 LinuxServer.io
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -15,11 +15,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.types.meta.template;
package io.linuxserver.fleet.queue;
public interface TemplateItem<T> {
public interface RequestQueue<T extends FleetRequest> {
T getName();
void enqueueRequest(T request);
String getDescription();
T takeOneRequest();
}

View File

@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.client.rest;
package io.linuxserver.fleet.rest;
public class HttpException extends RuntimeException {

View File

@ -15,11 +15,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.client.rest;
package io.linuxserver.fleet.rest;
import io.linuxserver.fleet.v2.client.rest.marshalling.JacksonMarshallingStrategy;
import io.linuxserver.fleet.v2.client.rest.marshalling.MarshallingStrategy;
import io.linuxserver.fleet.v2.client.rest.proxy.LazyLoadPayloadProxy;
import io.linuxserver.fleet.rest.marshalling.JacksonMarshallingStrategy;
import io.linuxserver.fleet.rest.marshalling.MarshallingStrategy;
import io.linuxserver.fleet.rest.proxy.LazyLoadPayloadProxy;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
@ -36,7 +36,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
import java.util.Map;
/**
@ -85,7 +85,7 @@ public class RestClient {
try {
HttpPost post = new HttpPost(url + parseQueryParameters(queryParameters));
post.setEntity(new StringEntity(marshallingStrategy.marshall(payload), StandardCharsets.UTF_8));
post.setEntity(new StringEntity(marshallingStrategy.marshall(payload), Charset.forName("UTF-8")));
post.setHeader("Content-Type", marshallingStrategy.getContentType());
return executeBaseRequest(responseType, headers, post);

View File

@ -15,9 +15,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.client.rest;
package io.linuxserver.fleet.rest;
import io.linuxserver.fleet.v2.client.rest.proxy.PayloadProxy;
import io.linuxserver.fleet.rest.proxy.PayloadProxy;
public class RestResponse<T> {

View File

@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.client.rest.marshalling;
package io.linuxserver.fleet.rest.marshalling;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;

View File

@ -1,4 +1,4 @@
package io.linuxserver.fleet.v2.client.rest.marshalling;
package io.linuxserver.fleet.rest.marshalling;
import java.io.IOException;

View File

@ -15,10 +15,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.client.rest.proxy;
package io.linuxserver.fleet.rest.proxy;
import io.linuxserver.fleet.v2.client.rest.HttpException;
import io.linuxserver.fleet.v2.client.rest.marshalling.MarshallingStrategy;
import io.linuxserver.fleet.rest.HttpException;
import io.linuxserver.fleet.rest.marshalling.MarshallingStrategy;
import java.io.IOException;

View File

@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.client.rest.proxy;
package io.linuxserver.fleet.rest.proxy;
public interface PayloadProxy<T> {

View File

@ -0,0 +1,53 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.sync;
import io.linuxserver.fleet.sync.event.ImageUpdateEvent;
import io.linuxserver.fleet.sync.event.RepositoriesScannedEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DefaultLoggingSyncListener implements SynchronisationListener {
private final Logger LOGGER = LoggerFactory.getLogger(DefaultLoggingSyncListener.class);
@Override
public void onSynchronisationStart() {
LOGGER.info("Sync started.");
}
@Override
public void onRepositoriesScanned(RepositoriesScannedEvent event) {
LOGGER.info("Found repositories: {}", event.getRepositories());
}
@Override
public void onImageUpdated(ImageUpdateEvent event) {
}
@Override
public void onSynchronisationFinish() {
LOGGER.info("Sync finished.");
}
@Override
public void onSynchronisationSkipped() {
LOGGER.info("Sync process already running, so will skip.");
}
}

View File

@ -0,0 +1,318 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.sync;
import io.linuxserver.fleet.dockerhub.DockerHubException;
import io.linuxserver.fleet.exception.SaveException;
import io.linuxserver.fleet.model.docker.DockerImage;
import io.linuxserver.fleet.model.docker.DockerTag;
import io.linuxserver.fleet.model.internal.Image;
import io.linuxserver.fleet.model.internal.Repository;
import io.linuxserver.fleet.model.internal.Tag;
import io.linuxserver.fleet.model.key.ImageKey;
import io.linuxserver.fleet.model.key.RepositoryKey;
import io.linuxserver.fleet.sync.event.ImageUpdateEvent;
import io.linuxserver.fleet.sync.event.RepositoriesScannedEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class DefaultSynchronisationState implements SynchronisationState {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultSynchronisationState.class);
private static DefaultSynchronisationState state;
private final AtomicBoolean running = new AtomicBoolean(false);
private DefaultSynchronisationState() {}
/**
* <p>
* Returns the current instance of the SyncState. Only one SyncState can be used
* at runtime.
* </p>
*/
public static DefaultSynchronisationState instance() {
if (null == state) {
synchronized (DefaultSynchronisationState.class) {
if (null == state) {
LOGGER.info("Creating single instance of sychronisation state");
state = new DefaultSynchronisationState();
}
}
}
return state;
}
/**
* <p>
* Performs a single synchronisation of Docker Hub repositories for the user. This state is aware
* of its current running state and will only run if currently idle. It uses the provided dependencies
* in the context.
* </p>
*
* @param context
* All necessary dependencies required to perform a synchronisation. This should also include any
* listeners for feedback on the process.
*/
@Override
public void synchronise(SynchronisationContext context) {
if (isProcessIdle()) {
try {
onStart(context);
List<String> repositories = context.getDockerHubDelegate().fetchAllRepositories();
onRepositoryScanned(context, repositories);
checkAndRemoveMissingRepositories(repositories, context);
for (String repositoryName : repositories)
synchroniseRepository(repositoryName, context);
} catch (DockerHubException e) {
LOGGER.error("Synchronisation process failed on the first step. Will skip for now.", e);
onSkip(context);
} finally {
onFinish(context);
}
} else {
onSkip(context);
}
}
/**
* <p>
* Checks the currently running status to ensure the sync process is not run more than once.
* </p>
*/
private boolean isProcessIdle() {
synchronized (running) {
return !running.get();
}
}
private void synchroniseRepository(String repositoryName, SynchronisationContext context) {
Repository repository = configureRepository(repositoryName, context);
if (repository.isSyncEnabled()) {
List<DockerImage> images = context.getDockerHubDelegate().fetchAllImagesFromRepository(repository.getName());
checkAndRemoveMissingImages(repository, images, context);
int totalSize = images.size();
LOGGER.info("Found {} images in Docker Hub", totalSize);
for (int i = 0; i < totalSize; i++) {
try {
DockerImage dockerImage = images.get(i);
Image image = configureImage(repository, dockerImage, context);
String versionMask = getVersionMask(repository.getVersionMask(), image.getVersionMask());
Tag maskedVersion = getLatestTagAndCreateMaskedVersion(repository.getName(), image.getName(), versionMask, context);
LOGGER.debug("Updated image version using mask. Mask=" + versionMask + ", MaskedVersion=" + maskedVersion);
image.withPullCount(dockerImage.getPullCount());
image.updateTag(maskedVersion);
context.getImageDelegate().saveImage(image);
onImageUpdated(context, new ImageUpdateEvent(image, i + 1, totalSize));
} catch (SaveException e) {
LOGGER.error("Unable to save updated image", e);
}
}
} else {
LOGGER.info("Skipping " + repositoryName);
}
}
private void checkAndRemoveMissingRepositories(List<String> repositories, SynchronisationContext context) {
LOGGER.info("Checking for any removed repositories.");
for (Repository storedRepository : context.getRepositoryDelegate().fetchAllRepositories()) {
if (!repositories.contains(storedRepository.getName())) {
LOGGER.info("Found repository which no longer exists in Docker Hub. Removing {}", storedRepository.getName());
context.getRepositoryDelegate().removeRepository(storedRepository.getKey().getId());
}
}
}
private void checkAndRemoveMissingImages(Repository repository, List<DockerImage> images, SynchronisationContext context) {
List<String> dockerHubImageNames = images.stream().map(DockerImage::getName).collect(Collectors.toList());
LOGGER.info("Checking for any removed images under {}", repository.getName());
for (Image storedImage : context.getImageDelegate().fetchImagesByRepository(repository.getKey())) {
if (!dockerHubImageNames.contains(storedImage.getName())) {
LOGGER.info("Found image which no longer exists in Docker Hub. Removing {}", storedImage.getName());
context.getImageDelegate().removeImage(storedImage.getKey());
}
}
}
private String getVersionMask(String repositoryMask, String imageMask) {
return imageMask == null ? repositoryMask : imageMask;
}
private Tag getLatestTagAndCreateMaskedVersion(String repositoryName, String imageName, String versionMask, SynchronisationContext context) {
DockerTag tag = context.getDockerHubDelegate().fetchLatestImageTag(repositoryName, imageName);
if (null == tag)
return Tag.NONE;
if (isTagJustLatestAndNotAVersion(tag) || null == versionMask)
return new Tag(tag.getName(), tag.getName(), tag.getBuildDate());
return new Tag(tag.getName(), extractMaskedVersion(tag.getName(), versionMask), tag.getBuildDate());
}
/**
* Given the repository name from Docker Hub, this will attempt to find it in the database. If it exists, it gets returned,
* otherwise a new record is created (with sync disabled) and that new record is returned instead.
*/
private Repository configureRepository(String repositoryName, SynchronisationContext context) {
Repository repository = context.getRepositoryDelegate().findRepositoryByName(repositoryName);
if (isRepositoryNew(repository)) {
try {
return context.getRepositoryDelegate().saveRepository(new Repository(new RepositoryKey(repositoryName)));
} catch (SaveException e) {
LOGGER.error("Tried to save new repository during sync but failed", e);
}
}
return repository;
}
/**
* Looks up the image in the database to see if it already exists. If it does, it gets returned, otherwise a base
* image is created with just the top-level information, as the rest will get updated later.
*/
private Image configureImage(Repository repository, DockerImage dockerHubImage, SynchronisationContext context) {
final ImageKey baseImageKey = new ImageKey(dockerHubImage.getName(), repository.getKey());
final Image image = context.getImageDelegate().findImageByRepositoryAndImageName(baseImageKey);
if (isImageNew(image)) {
try {
return context.getImageDelegate().saveImage(Image.makeFromKey(baseImageKey));
} catch (SaveException e) {
LOGGER.error("Tried to save new image during sync but failed", e);
}
}
return image;
}
private String extractMaskedVersion(String fullTag, String versionMask) {
Pattern pattern = Pattern.compile(versionMask);
Matcher matcher = pattern.matcher(fullTag);
if (matcher.matches()) {
StringBuilder tagBuilder = new StringBuilder();
for (int groupNum = 1; groupNum <= matcher.groupCount(); groupNum++)
tagBuilder.append(matcher.group(groupNum));
return tagBuilder.toString();
}
return fullTag;
}
/**
* <p>
* If the top-level tag is not versioned, a mask can't be applied.
* </p>
*/
private boolean isTagJustLatestAndNotAVersion(DockerTag tag) {
return "latest".equals(tag.getName());
}
private boolean isRepositoryNew(Repository repository) {
return null == repository;
}
private boolean isImageNew(Image image) {
return null == image;
}
private void onStart(SynchronisationContext context) {
synchronized (running) {
running.set(true);
}
context.getListeners().forEach(SynchronisationListener::onSynchronisationStart);
}
private void onFinish(SynchronisationContext context) {
synchronized (running) {
running.set(false);
}
context.getListeners().forEach(SynchronisationListener::onSynchronisationFinish);
}
private void onSkip(SynchronisationContext context) {
context.getListeners().forEach(SynchronisationListener::onSynchronisationSkipped);
}
private void onRepositoryScanned(SynchronisationContext context, List<String> repositories) {
context.getListeners().forEach(l -> l.onRepositoriesScanned(new RepositoriesScannedEvent(repositories)));
}
private void onImageUpdated(SynchronisationContext context, ImageUpdateEvent event) {
context.getListeners().forEach(l -> l.onImageUpdated(event));
}
}

View File

@ -15,26 +15,25 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.types.internal;
package io.linuxserver.fleet.sync;
import io.linuxserver.fleet.v2.key.ImageKey;
import io.linuxserver.fleet.delegate.DockerHubDelegate;
import io.linuxserver.fleet.delegate.ImageDelegate;
import io.linuxserver.fleet.delegate.RepositoryDelegate;
public class TagBranchOutlineRequest {
import java.util.List;
private final ImageKey imageKey;
private final String branchName;
public interface SynchronisationContext {
public TagBranchOutlineRequest(final ImageKey imageKey, final String branchName) {
void synchronise();
this.imageKey = imageKey;
this.branchName = branchName;
}
void registerListener(SynchronisationListener listener);
public final ImageKey getImageKey() {
return imageKey;
}
List<SynchronisationListener> getListeners();
public final String getBranchName() {
return branchName;
}
ImageDelegate getImageDelegate();
RepositoryDelegate getRepositoryDelegate();
DockerHubDelegate getDockerHubDelegate();
}

View File

@ -0,0 +1,75 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.sync;
import io.linuxserver.fleet.sync.event.ImageUpdateEvent;
import io.linuxserver.fleet.sync.event.RepositoriesScannedEvent;
/**
* <p>
* Allows for the receiving of messages from the synchronisation process at
* specific stages.
* </p>
*/
public interface SynchronisationListener {
/**
* <p>
* Triggered when the synchronisation process starts
* </p>
*/
void onSynchronisationStart();
/**
* <p>
* Triggered when the synchronisation process has successfully scanned for all known
* repositories against the Docker Hub user.
* </p>
*
* @param event
* The list of all found repositories
*/
void onRepositoriesScanned(RepositoriesScannedEvent event);
/**
* <p>
* Triggered when the synchronisation process as successfully scanned and updated a single
* image from Docker Hub, and is about to save it back to the internal database. This will
* contain the updated version of the image.
* </p>
*
* @param event
* The new view of the image. This also contains positional information pertaining to
* how far in the overall list the image is.
*/
void onImageUpdated(ImageUpdateEvent event);
/**
* <p>
* Triggered when the synchronisation process has finished.
* </p>
*/
void onSynchronisationFinish();
/**
* <p>
* Triggered when a calling task forces a synchronisation but a process is already running.
* </p>
*/
void onSynchronisationSkipped();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 LinuxServer.io
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -15,8 +15,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.thread;
package io.linuxserver.fleet.sync;
public interface AsyncTaskResponse {
void handleResponse();
public interface SynchronisationState {
void synchronise(SynchronisationContext context);
}

View File

@ -15,37 +15,32 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.dockerhub.model;
package io.linuxserver.fleet.sync.event;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.linuxserver.fleet.model.internal.Image;
public class DockerHubV2TagDigest {
public class ImageUpdateEvent {
@JsonProperty("size")
private long size;
private final Image image;
private final int currentPosition;
private final int totalImages;
@JsonProperty("digest")
private String digest;
public ImageUpdateEvent(Image image, int currentPosition, int totalImages) {
@JsonProperty("architecture")
private String architecture;
@JsonProperty("variant")
private String variant;
public long getSize() {
return size;
this.image = image;
this.currentPosition = currentPosition;
this.totalImages = totalImages;
}
public String getDigest() {
return digest;
public Image getImage() {
return image;
}
public String getArchitecture() {
return architecture;
public int getCurrentPosition() {
return currentPosition;
}
public String getVariant() {
return variant;
public int getTotalImages() {
return totalImages;
}
}

View File

@ -15,13 +15,20 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.v2.web;
package io.linuxserver.fleet.sync.event;
public interface PageModelAttributes {
import java.util.Collections;
import java.util.List;
String AuthenticatedUser = "__AuthenticatedUser";
public class RepositoriesScannedEvent {
String ContextAdapter = "__ContextAdapter";
private final List<String> repositories;
String SystemAlerts = "__SystemAlerts";
public RepositoriesScannedEvent(List<String> repositories) {
this.repositories = Collections.unmodifiableList(repositories);
}
public List<String> getRepositories() {
return repositories;
}
}

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.thread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class FleetTask implements Runnable {
private final Logger logger;
FleetTask() {
logger = LoggerFactory.getLogger(getClass().getSimpleName());
}
@Override
public void run() {
try {
executeTask();
} catch (Exception e) {
logger.error("run() Caught unhandled exception during task execution.", e);
}
}
protected abstract void executeTask();
@Override
public String toString() {
return getClass().getSimpleName();
}
}

Some files were not shown because too many files have changed in this diff Show More