Compare commits

...

62 Commits

Author SHA1 Message Date
Josh Stark
f331c22cd8 Updated project dependencies, specifically Flyway 2022-10-30 08:18:24 +00:00
Josh Stark
71335636dc
Merge pull request #15 from linuxserver/patch/CVE-2021-44228
Updated log4j version to mitigate CVE
2021-12-14 09:37:09 +00:00
Josh Stark
a055df7445 Updated log4j to 2.16.0 2021-12-14 09:33:40 +00:00
Josh Stark
730164547e Upped version 2021-12-14 08:40:50 +00:00
Josh Stark
d6f403cd31 Updated log4j version to mitigate CVE 2021-12-14 08:38:41 +00:00
Josh Stark
f6d3b07468 Incluide isDeprecated in API image response 2021-07-24 11:00:14 +01:00
Josh Stark
9b6d4ccc3a Added verbose output for public API to include docker template specification 2021-04-17 10:13:59 +01:00
Josh Stark
808fa21924 Merge remote-tracking branch 'origin/master' 2021-02-13 16:50:12 +00:00
Josh Stark
cc32989d81 Ensure TagDigest is comparable so Sets can adequately dedupe where necessary. 2021-02-13 16:49:05 +00:00
Josh Stark
964d2ecc3b
Update example.fleet.properties 2021-02-13 15:20:05 +00:00
Josh Stark
4aca22cf89 Re-implemented DockerHub auth to bypass unauthenticated rate limits 2021-02-13 15:18:21 +00:00
Josh Stark
7150b2c3a9 Quick refactor to introduce abstract base class for docker registry clients 2020-11-01 11:29:03 +00:00
Josh Stark
f407b15c92 Suffix DockerHub API URLs with forward slash to avoid potential 301 response codes 2020-08-21 17:46:57 +01:00
Josh Stark
8b1124353f Added example value support for DockerEnv templates 2020-06-27 11:54:44 +01:00
Josh Stark
994bcf8046 New favicon and tag finger enhancement
- Updated the favicon to new LSIO logo
- Updated TagFinder so it checks against full digest parity rather than image size.
2020-06-27 11:24:49 +01:00
Josh Stark
ac468df4a5 Upped jQuery 2020-05-09 13:48:45 +01:00
Josh Stark
34da9a4cd9 Added DockerHub URL 2020-04-28 20:35:51 +01:00
Josh Stark
d47b1d3236 Fix user creation bug
The username was being used for both user and password.
2020-04-28 15:54:51 +01:00
Josh Stark
14e99e609b Added extra check for error on task response. 2020-04-26 21:39:53 +01:00
Josh Stark
54881fb2b0 Updated version to 2.0.1 2020-04-26 20:25:11 +01:00
Josh Stark
7820617cd1 Added legacy API controller for external API calls. 2020-04-26 20:24:18 +01:00
Josh Stark
488fb67449 Updated version properties 2020-04-26 14:56:15 +01:00
Josh Stark
c6b3b36a7c Updated example properties 2020-04-26 14:49:35 +01:00
Josh Stark
4a62dbd997 Updated example properties 2020-04-26 14:49:00 +01:00
Josh Stark
1d48e254d2 Added user management 2020-04-26 14:30:56 +01:00
Josh Stark
9c9e2b8825
Update README.md 2020-04-26 12:00:28 +01:00
Josh Stark
a7ab0fead8
Merge pull request #8 from linuxserver/v2
V2
2020-04-26 11:58:35 +01:00
Josh Stark
3a00252a17 Added user management 2020-04-26 11:51:30 +01:00
Josh Stark
caf138415e Allow for editing of external urls 2020-03-07 17:03:39 +00:00
Josh Stark
34ad56cec6 Initial structure for multiple URLs 2020-02-22 13:57:19 +00:00
Josh Stark
248be84b5d Log In
- Added log in functionality
 - Updated gradle wrapper to latest
2020-01-19 14:16:54 +00:00
Josh Stark
bd1946f84e Core Meta
- Added logo uploads.
2020-01-19 11:19:16 +00:00
Josh Stark
b8f64bd4f2 Core Meta
- Store core meta as part of standard image save
  - Display core meta on main image display
  - TODO: upload image and save.
2020-01-18 17:31:21 +00:00
Josh Stark
cd0f025d55 Templating
- Generate snippets for running containers from images
2020-01-18 13:24:33 +00:00
Josh Stark
af7849cbb5 Templating
- Templates now fully persistable
2020-01-15 21:29:40 +00:00
Josh Stark
0ab771dec3 Templating
- Template merger for updating of port/volume definitions
2020-01-15 14:39:25 +00:00
Josh Stark
2a2631a738 Templating
- View page for templates
  - Start of codebase for metadata template
  - New styling for build display page.
  - Updated bulma and fontawesome versions.
2020-01-12 14:46:32 +00:00
Josh Stark
4ccca5e55a Graphing/Edit Image
- Updated graph on view image page
  - Initial form format for edit image page.
2020-01-04 18:29:03 +00:00
Josh Stark
cf5b277ff2 Image metadata
- Initial addition of metadata for images.
2020-01-03 17:26:25 +00:00
Josh Stark
754180a142 Added spec updates for images
- Refactoring of auth delegate
  - Can update image specs now
  - Various other bits and pieces.
2019-12-28 15:56:56 +00:00
Josh Stark
6ab2230115 JS
- Added ajax calls for repository admin page
2019-12-24 23:20:25 +00:00
Josh Stark
cd40995ba8 Schedules
- Added runNow API endpoint
  - Ensure no duplication allowed in async queue.
2019-12-22 17:37:56 +00:00
Josh Stark
999a20a0dc Refactoring
- Rejigged sync logic into separate service
 - Added validation for addition of new repository.
2019-12-21 18:48:41 +00:00
Josh Stark
daadc15d72 Scheduling
- Added extra schedules
2019-12-21 13:58:43 +00:00
Josh Stark
9c5a61030b Scheduling
- Added schedule service and basic task for sync
 - Refactored database provider.
2019-12-21 12:43:08 +00:00
Josh Stark
b399959e65 UI updates for admins. 2019-12-20 16:49:46 +00:00
Josh Stark
9dc0e98649 Made the sync process task-based.
Still work to do on error handling and what to do for missing images.
2019-12-17 19:25:41 +00:00
Josh Stark
11798d6464 Initial switchable view for image list. 2019-12-14 18:05:10 +00:00
Josh Stark
e0c9317225 Started implementing design for docker api requests.
Display page for image details.
2019-12-14 15:27:25 +00:00
Josh Stark
0fb61d05ef Improved main listing page design and responsiveness. 2019-12-14 12:39:02 +00:00
Josh Stark
6a29c5817c Deleted a load of old files which will eventually be replaced.
Added data migration for V2 tables.
2019-12-11 20:07:25 +00:00
Josh Stark
70a91c0d92 Further sproc updates. 2019-12-10 22:37:49 +00:00
Josh Stark
a48d47938e Started work on V2 sprocs. 2019-12-10 11:05:00 +00:00
Josh Stark
11b684fbe0 I thought I'd committed these! 2019-12-08 17:54:17 +00:00
Josh Stark
e27d7d31b8 Further work on DAO object creation.
Slight refactor on how branches/tags should get updated (incl digests).
2019-12-08 17:39:05 +00:00
Josh Stark
13bf8c2a2d DAO object creation. 2019-12-08 05:01:57 +00:00
Josh Stark
47c4faf8a6 Initial commit of some new v2 stuff. 2019-12-07 13:44:07 +00:00
Josh Stark
3b31e958bd
Merge pull request #7 from linuxserver/empty_api_bugfix
Fix issue with empty DockerHub Response
2019-11-20 18:57:23 +00:00
Josh Stark
d134756c78 Ensure an empty response from DockerHub does not trigger an enmasse deletion of stored images/repositories. 2019-11-20 18:36:14 +00:00
Josh Stark
b1a5a91032
Merge pull request #6 from linuxserver/develop
Bugfix for parsing DockerHub dates
2019-09-07 17:40:53 +01:00
Josh Stark
b38a5dde38 Upped version to 1.4.3 2019-09-07 17:37:56 +01:00
Josh Stark
a7f750369a Removed explicit format for dates when parsing incoming messages from DockerHub. 2019-09-07 17:37:21 +01:00
343 changed files with 25015 additions and 8804 deletions

6
.gitignore vendored
View File

@ -28,6 +28,7 @@ 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
@ -35,4 +36,7 @@ src/main/resources/log4j2.xml
.classpath
.project
.settings/
.settings/
.vscode/
**/.DS_Store

View File

@ -27,3 +27,6 @@ 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,63 +1,44 @@
plugins {
id 'java'
id 'com.eriwen.gradle.js' version '2.14.1'
id 'com.eriwen.gradle.css' version '2.14.0'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
version = '1.4.2'
sourceSets {
main {
java {
srcDir 'src/main/java'
}
}
test {
java {
srcDir 'src/test/java'
}
}
}
version = '2.3.3'
dependencies {
// Logging
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'
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'
// HTTP Framework
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'
implementation 'io.javalin:javalin:3.6.0'
implementation 'org.freemarker:freemarker:2.3.31'
implementation 'org.apache.httpcomponents:httpclient:4.5.13'
// JSON Mapping/Marshalling
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'
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'
// Database
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'
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'
// MISC
compile 'org.apache.commons:commons-lang3:3.7'
implementation 'org.apache.commons:commons-lang3:3.12.0'
// Unit Testing
testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-core:1.10.19'
testImplementation 'junit:junit:4.11'
testImplementation 'org.mockito:mockito-core:4.8.0'
}
jar {
@ -67,51 +48,12 @@ jar {
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
duplicatesStrategy = DuplicatesStrategy.INCLUDE
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')
exclude 'META-INF/INDEX.LIST', 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
task configureLogConfiguration(type: Copy) {
@ -123,4 +65,21 @@ task configureLogConfiguration(type: Copy) {
rename "log4j2.${logFile}.xml", 'log4j2.xml'
}
build.dependsOn minifyCss, minifyJs, configureLogConfiguration
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

View File

@ -3,15 +3,6 @@
# 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
@ -19,6 +10,7 @@ fleet.database.url=jdbc:mariadb://<IP_OR_URL>:3306/fleet
fleet.database.username=<fleet_sql_user>
fleet.database.password=<fleet_sql_password>
# Docker Hub
fleet.dockerhub.username=<username_for_your_dockerhub_account>
fleet.dockerhub.password=<password_for_your_dockerhub_account>
# DockerHub auth
fleet.dockerhub.auth.enabled=true
fleet.dockerhub.username=YOUR_USERNAME
fleet.dockerhub.password=YOUR_PASSWORD_OR_AUTH_TOKEN

View File

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

286
gradlew vendored
View File

@ -1,78 +1,129 @@
#!/usr/bin/env sh
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
##
## Gradle start up script for UN*X
##
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
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
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
APP_BASE_NAME=${0##*/}
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# 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
;;
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
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
@ -81,7 +132,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
@ -89,84 +140,95 @@ location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
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
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
# 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
# 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"`
# 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
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" ;;
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
# 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, 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.
# 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"
# 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" )
# 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")"
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@"
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

43
gradlew.bat vendored
View File

@ -1,3 +1,19 @@
@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
@ -13,15 +29,18 @@ 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=
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
if "%ERRORLEVEL%" == "0" goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
@ -35,7 +54,7 @@ goto fail
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
@ -45,28 +64,14 @@ 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 %CMD_LINE_ARGS%
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell

View File

@ -17,15 +17,25 @@
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 String name;
private final User user;
public AuthenticatedUser(String name) {
this.name = name;
public AuthenticatedUser(final User user) {
this.user = user;
}
public String getName() {
return name;
public final String getName() {
return user.getUsername();
}
public final Set<AppRole> getRoles() {
return Collections.singleton(user.getRole());
}
}

View File

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

View File

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

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.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

@ -1,60 +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.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,24 +20,36 @@ 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 PropertyLoadedUserAuthenticator implements UserAuthenticator {
public class DefaultUserAuthenticator implements UserAuthenticator {
private final String adminUsername;
private final String adminPassword;
private final UserService userService;
private final PasswordEncoder passwordEncoder;
public PropertyLoadedUserAuthenticator(String adminUsername, String adminPassword) {
public DefaultUserAuthenticator(final UserService userService,
final PasswordEncoder passwordEncoder) {
this.adminUsername = adminUsername;
this.adminPassword = adminPassword;
this.userService = userService;
this.passwordEncoder = passwordEncoder;
}
@Override
public AuthenticationResult authenticate(UserCredentials userCredentials) {
public AuthenticationResult authenticate(final UserCredentials userCredentials) {
if (adminUsername.equals(userCredentials.getUsername()) && adminPassword.equals(userCredentials.getPassword()))
return new AuthenticationResult(true, new AuthenticatedUser(userCredentials.getUsername()));
final User user = userService.lookUpUser(userCredentials.getUsername());
if (null != user && getPasswordEncoder().matches(userCredentials.getPassword(), user.getPassword())) {
return new AuthenticationResult(true, new AuthenticatedUser(user));
}
return AuthenticationResult.notAuthenticated();
}
@Override
public PasswordEncoder getPasswordEncoder() {
return passwordEncoder;
}
}

View File

@ -19,6 +19,7 @@ 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>
@ -35,4 +36,6 @@ public interface UserAuthenticator {
* </p>
*/
AuthenticationResult authenticate(UserCredentials userCredentials);
PasswordEncoder getPasswordEncoder();
}

View File

@ -1,97 +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.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

@ -0,0 +1,72 @@
/*
* 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,5 +36,6 @@ 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

@ -1,221 +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.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

@ -0,0 +1,180 @@
/*
* 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

@ -1,131 +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.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

@ -19,6 +19,11 @@ 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.
*/
@ -33,10 +38,4 @@ 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) {
FleetApp.instance().run();
FleetAppController.instance().run();
}
}

View File

@ -17,12 +17,14 @@
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;
/**
@ -38,7 +40,7 @@ class PropertiesLoader extends BaseRuntimeLoader {
private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesLoader.class);
private final FleetProperties properties;
private final AppProperties properties;
PropertiesLoader() {
@ -50,8 +52,10 @@ 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 FleetProperties(properties);
this.properties = new AppProperties(properties);
printProperties();
@ -85,7 +89,7 @@ class PropertiesLoader extends BaseRuntimeLoader {
private boolean createStaticFileDirectory() {
File staticFilesDir = new File(FleetRuntime.CONFIG_BASE + "/fleet_static");
File staticFilesDir = new File(properties.getStaticFilesPath().toString());
if (staticFilesDir.exists()) {
return true;
@ -113,7 +117,7 @@ class PropertiesLoader extends BaseRuntimeLoader {
* @return
* All application properties.
*/
FleetProperties getProperties() {
AppProperties getProperties() {
return properties;
}
@ -125,12 +129,10 @@ class PropertiesLoader extends BaseRuntimeLoader {
private void printProperties() {
LOGGER.info("fleet.app.port : " + properties.getAppPort());
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() : "***"));
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());
}
/**

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,25 +15,23 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.sync;
package io.linuxserver.fleet.core;
import io.linuxserver.fleet.delegate.DockerHubDelegate;
import io.linuxserver.fleet.delegate.ImageDelegate;
import io.linuxserver.fleet.delegate.RepositoryDelegate;
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;
import java.util.List;
public interface ServiceProvider {
public interface SynchronisationContext {
SynchronisationService getSynchronisationService();
void synchronise();
ImageService getImageService();
void registerListener(SynchronisationListener listener);
ScheduleService getScheduleService();
List<SynchronisationListener> getListeners();
UserService getUserService();
ImageDelegate getImageDelegate();
RepositoryDelegate getRepositoryDelegate();
DockerHubDelegate getDockerHubDelegate();
FileManager getFileManager();
}

View File

@ -15,38 +15,57 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.core;
package io.linuxserver.fleet.core.config;
import io.linuxserver.fleet.dockerhub.DockerHubCredentials;
import io.linuxserver.fleet.core.FleetRuntime;
import io.linuxserver.fleet.v2.client.docker.dockerhub.DockerHubCredentials;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
public class FleetProperties {
public class AppProperties {
private Properties properties;
FleetProperties(Properties properties) {
public AppProperties(final Properties properties) {
this.properties = properties;
}
public String getDatabaseDriverClassName() {
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() {
return getStringProperty("fleet.database.driver");
}
public String getDatabaseUrl() {
private String getDatabaseUrl() {
return getStringProperty("fleet.database.url");
}
public String getDatabaseUsername() {
private String getDatabaseUsername() {
return getStringProperty("fleet.database.username");
}
public String getDatabasePassword() {
private String getDatabasePassword() {
return getStringProperty("fleet.database.password");
}
public String getAuthenticationType() {
return getStringProperty("fleet.admin.authentication.type");
public final Path getStaticFilesPath() {
return Paths.get(FleetRuntime.CONFIG_BASE, getStringProperty("fleet.static.dirname")).toAbsolutePath();
}
public String getAppSecret() {
@ -55,41 +74,22 @@ public class FleetProperties {
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 int getRefreshIntervalInMinutes() {
return Integer.parseInt(getStringProperty("fleet.refresh.interval"));
public boolean isDockerHubAuthEnabled() {
return "true".equalsIgnoreCase(getStringProperty("fleet.dockerhub.auth.enabled"));
}
public DockerHubCredentials getDockerHubCredentials() {
String username = getStringProperty("fleet.dockerhub.username");
String password = getStringProperty("fleet.dockerhub.password");
final String username = getStringProperty("fleet.dockerhub.username");
final 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

@ -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.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

@ -0,0 +1,71 @@
/*
* 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

@ -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.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

@ -0,0 +1,31 @@
/*
* 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

@ -0,0 +1,29 @@
/*
* 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

@ -0,0 +1,27 @@
/*
* 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

@ -0,0 +1,41 @@
/*
* 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,15 +17,11 @@
package io.linuxserver.fleet.db;
import io.linuxserver.fleet.core.FleetProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.linuxserver.fleet.core.config.DatabaseConnectionProperties;
public class DefaultDatabaseConnection extends PoolingDatabaseConnection {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDatabaseConnection.class);
public DefaultDatabaseConnection(FleetProperties properties) {
public DefaultDatabaseConnection(final DatabaseConnectionProperties properties) {
super(properties);
}
}

View File

@ -11,14 +11,15 @@
* 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
* You should have received a copy of the GNU General Public LicensedatabaseConnection.getDataSource(
* 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.FleetProperties;
import io.linuxserver.fleet.core.config.DatabaseConnectionProperties;
import io.linuxserver.fleet.core.db.DatabaseConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -26,16 +27,16 @@ import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public abstract class PoolingDatabaseConnection {
public abstract class PoolingDatabaseConnection implements DatabaseConnection {
private static final Logger LOGGER = LoggerFactory.getLogger(PoolingDatabaseConnection.class);
private final HikariDataSource dataSource;
PoolingDatabaseConnection(FleetProperties properties) {
PoolingDatabaseConnection(final DatabaseConnectionProperties properties) {
dataSource = new HikariDataSource();
dataSource.setDriverClassName(properties.getDatabaseDriverClassName());
dataSource.setDriverClassName(properties.getDatabaseDriverClass());
dataSource.setJdbcUrl(properties.getDatabaseUrl());
dataSource.setUsername(properties.getDatabaseUsername());
dataSource.setPassword(properties.getDatabasePassword());
@ -43,11 +44,13 @@ public abstract class PoolingDatabaseConnection {
LOGGER.info("DataSource established: " + dataSource.getJdbcUrl());
}
@Override
public DataSource getDataSource() {
return dataSource;
}
public Connection getConnection() throws SQLException {
@Override
public final Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}

View File

@ -1,267 +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.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

@ -1,181 +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.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

@ -1,157 +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.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

@ -1,25 +0,0 @@
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

@ -1,20 +0,0 @@
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

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

View File

@ -23,6 +23,10 @@ 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;
@ -34,15 +38,19 @@ public class InsertUpdateResult<T> {
this(null, status, statusMessage);
}
public T getResult() {
public final T getResult() {
return result;
}
public int getStatus() {
public final int getStatus() {
return status;
}
public String getStatusMessage() {
public final 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

@ -1,129 +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.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

@ -1,128 +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.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

@ -1,62 +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.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

@ -1,89 +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.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

@ -1,66 +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.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

@ -1,63 +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.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

@ -1,88 +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.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

@ -1,35 +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.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

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

View File

@ -15,29 +15,37 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.web.websocket;
package io.linuxserver.fleet.dockerhub.model;
public class WebSocketMessage {
import com.fasterxml.jackson.annotation.JsonProperty;
public enum MessageType {
FORCE_SYNC, CONNECTED, DISCONNECTED, SYNC_START, SYNC_END, SYNC_SKIP, REPOSITORIES_SCANNED, IMAGE_UPDATED
public class DockerHubV2TagDigest {
@JsonProperty("size")
private long size;
@JsonProperty("digest")
private String digest;
@JsonProperty("architecture")
private String architecture;
@JsonProperty("variant")
private String variant;
public long getSize() {
return size;
}
private final MessageType messageType;
private final Object data;
public WebSocketMessage(MessageType messageType, Object data) {
this.messageType = messageType;
this.data = data;
public String getDigest() {
return digest;
}
public MessageType getMessageType() {
return messageType;
public String getArchitecture() {
return architecture;
}
public Object getData() {
return data;
public String getVariant() {
return variant;
}
}

View File

@ -1,47 +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.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

@ -1,55 +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.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

@ -1,100 +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.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,26 +17,64 @@
package io.linuxserver.fleet.dockerhub.util;
import io.linuxserver.fleet.model.docker.DockerTag;
import io.linuxserver.fleet.v2.types.docker.DockerTag;
import io.linuxserver.fleet.v2.types.docker.DockerTagManifestDigest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class DockerTagFinder {
public DockerTag findVersionedTagMatchingBranch(List<DockerTag> tags, String namedBranch) {
public static DockerTag findVersionedTagMatchingBranch(List<DockerTag> tags, String namedBranch) {
Optional<DockerTag> trueLatest = tags.stream().filter(tag -> namedBranch.equals(tag.getName())).findFirst();
Optional<DockerTag> tagBranchName = tags.stream().filter(tag -> namedBranch.equals(tag.getName())).findFirst();
if (trueLatest.isPresent()) {
if (tagBranchName.isPresent()) {
DockerTag trueLatestTag = trueLatest.get();
DockerTag namedTagForBranch = tagBranchName.get();
Optional<DockerTag> versionedLatestTag = tags.stream()
.filter(tag -> !tag.equals(trueLatestTag) && tag.getSize() == trueLatestTag.getSize()).findFirst();
.filter(tag -> !tag.equals(namedTagForBranch) && allManifestsMatch(namedTagForBranch, tag)).findFirst();
return versionedLatestTag.orElse(trueLatestTag);
return versionedLatestTag.orElse(namedTagForBranch);
}
return tags.get(0);
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;
}
}

View File

@ -1,74 +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.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

@ -1,81 +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.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

@ -1,187 +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.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

@ -1,76 +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.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

@ -1,85 +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.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

@ -1,76 +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.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

@ -1,87 +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.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

@ -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.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

@ -1,53 +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.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

@ -1,318 +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.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

@ -1,75 +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.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,46 +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.sync.event;
import io.linuxserver.fleet.model.internal.Image;
public class ImageUpdateEvent {
private final Image image;
private final int currentPosition;
private final int totalImages;
public ImageUpdateEvent(Image image, int currentPosition, int totalImages) {
this.image = image;
this.currentPosition = currentPosition;
this.totalImages = totalImages;
}
public Image getImage() {
return image;
}
public int getCurrentPosition() {
return currentPosition;
}
public int getTotalImages() {
return totalImages;
}
}

View File

@ -1,10 +0,0 @@
package io.linuxserver.fleet.thread;
public interface TaskListener {
void onTaskStart(String message);
void onTaskOutput(String output);
void onTaskEnd(String message);
}

View File

@ -1,48 +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.thread;
import io.linuxserver.fleet.core.FleetRuntime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TaskManager {
private static final Logger LOGGER = LoggerFactory.getLogger(TaskManager.class);
private static final ScheduledExecutorService EXECUTOR_SERVICE = Executors.newSingleThreadScheduledExecutor();
public void scheduleRecurringTask(FleetTask task, int interval, TimeUnit timeUnit) {
int initalDelay = 0;
if (FleetRuntime.SKIP_SYNC_ON_STARTUP) {
initalDelay = interval;
}
LOGGER.info("Scheduling task " + task);
EXECUTOR_SERVICE.scheduleAtFixedRate(task, initalDelay, interval, timeUnit);
}
public void runTaskOnce(FleetTask task) {
new Thread(task).start();
}
}

View File

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

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2020 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.v2;
public final class Utils {
public static <T> T ensureNotNull(final T obj) {
if (null == obj) {
throw new IllegalArgumentException("Parameter null");
}
return obj;
}
}

View File

@ -0,0 +1,104 @@
/*
* 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.v2.cache;
import io.linuxserver.fleet.v2.key.HasKey;
import io.linuxserver.fleet.v2.key.Key;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
public abstract class AbstractItemCache<KEY extends Key, ITEM extends HasKey<KEY>> implements ItemCache<KEY, ITEM> {
private final Logger LOGGER = LoggerFactory.getLogger(getClass());
private final List<ItemCacheListener<ITEM>> listeners;
private final Map<KEY, ITEM> items;
public AbstractItemCache() {
listeners = new ArrayList<>();
items = new HashMap<>();
}
public final void clear() {
LOGGER.info("Emptying cache");
items.clear();
}
public final void registerCacheListener(final ItemCacheListener<ITEM> listener) {
LOGGER.info("Registering new cache listener {}", listener);
listeners.add(listener);
}
@Override
public final void addItem(final ITEM item) {
final ITEM original = items.get(item.getKey());
final ITEM cached = items.put(item.getKey(), item);
LOGGER.info("Item {} cached", item);
if (null == original) {
listeners.forEach(l -> l.onItemAdded(cached));
} else {
listeners.forEach(l -> l.onItemUpdated(original, cached));
}
}
@Override
public boolean isEmpty() {
return items.isEmpty();
}
@Override
public final ITEM findItem(final KEY key) {
return items.get(key);
}
@Override
public final void removeItem(final KEY key) {
final ITEM removed = items.remove(key);
LOGGER.info("Item {} removed from cache", removed);
listeners.forEach(l -> l.onItemRemoved(removed));
}
@Override
public final boolean isItemCached(final KEY key) {
return items.containsKey(key);
}
@Override
public Collection<ITEM> getAllItems() {
return items.values();
}
@Override
public final void addAllItems(Collection<ITEM> allItems) {
allItems.forEach(this::addItem);
}
@Override
public int size() {
return items.size();
}
}

View File

@ -0,0 +1,25 @@
/*
* 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.v2.cache;
import io.linuxserver.fleet.v2.key.HasKey;
import io.linuxserver.fleet.v2.key.Key;
public final class BasicItemCache<KEY extends Key, ITEM extends HasKey<KEY>> extends AbstractItemCache<KEY, ITEM> {
// Default implementation
}

View File

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

View File

@ -15,31 +15,35 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.model.internal;
package io.linuxserver.fleet.v2.cache;
import java.util.Collections;
import java.util.List;
import io.linuxserver.fleet.v2.key.HasKey;
import io.linuxserver.fleet.v2.key.Key;
public class RepositoryWithImages {
import java.util.Collection;
private final Repository repository;
private final List<Image> images;
public interface ItemCache<KEY extends Key, ITEM extends HasKey<KEY>> {
public RepositoryWithImages(Repository repository, List<Image> images) {
boolean isEmpty();
this.repository = repository;
this.images = Collections.unmodifiableList(images);
}
void addItem(ITEM item);
public Repository getRepository() {
return repository;
}
ITEM findItem(KEY key);
public List<Image> getImages() {
return images;
}
void removeItem(KEY key);
public boolean isEveryImageStable() {
return images.stream().noneMatch(Image::isUnstable);
boolean isItemCached(KEY key);
Collection<ITEM> getAllItems();
void addAllItems(Collection<ITEM> items);
int size();
interface ItemCacheListener<ITEM> {
void onItemAdded(final ITEM item);
void onItemUpdated(final ITEM oldItem, final ITEM newItem);
void onItemRemoved(final ITEM item);
}
}

View File

@ -0,0 +1,56 @@
/*
* 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.v2.cache;
import io.linuxserver.fleet.v2.key.ImageKey;
import io.linuxserver.fleet.v2.key.ImageLookupKey;
import io.linuxserver.fleet.v2.key.RepositoryKey;
import io.linuxserver.fleet.v2.types.Image;
import io.linuxserver.fleet.v2.types.Repository;
public class RepositoryCache extends AbstractItemCache<RepositoryKey, Repository> {
public final Image lookupImage(final ImageLookupKey lookupKey) {
for (Repository repository : getAllItems()) {
for (Image image : repository.getImages()) {
if (lookupKey.isLookupKeyFor(image)) {
return image;
}
}
}
return null;
}
public final Image findImage(final ImageKey imageKey) {
if (isItemCached(imageKey.getRepositoryKey())) {
final Repository repository = findItem(imageKey.getRepositoryKey());
for (Image image : repository.getImages()) {
if (imageKey.equals(image.getKey())) {
return image;
}
}
}
return null;
}
}

View File

@ -0,0 +1,28 @@
/*
* 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.v2.cache;
import io.linuxserver.fleet.v2.key.ScheduleKey;
import io.linuxserver.fleet.v2.thread.schedule.AppSchedule;
public class ScheduleCache extends AbstractItemCache<ScheduleKey, AppSchedule> {
public final boolean isScheduleRunning(final ScheduleKey scheduleKey) {
return isItemCached(scheduleKey);
}
}

View File

@ -0,0 +1,60 @@
/*
* Copyright (c) 2020 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.v2.client.docker;
import io.linuxserver.fleet.v2.client.docker.converter.DockerResponseConverter;
import io.linuxserver.fleet.v2.types.docker.DockerImage;
import io.linuxserver.fleet.v2.types.docker.DockerTag;
import java.util.List;
import java.util.stream.Collectors;
public abstract class AbstractDockerApiClient<D, T, IC extends DockerResponseConverter<D, DockerImage>, TC extends DockerResponseConverter<T, DockerTag>> implements DockerApiClient {
private final IC imageConverter;
private final TC tagConverter;
public AbstractDockerApiClient(final IC imageConverter, final TC tagConverter) {
this.imageConverter = imageConverter;
this.tagConverter = tagConverter;
}
@Override
public final DockerImage fetchImage(String imageName) {
final D dockerModel = fetchImageFromApi(imageName);
if (null == dockerModel) {
return null;
}
return imageConverter.convert(dockerModel);
}
@Override
public final List<DockerImage> fetchAllImages(String repositoryName) {
return fetchAllImagesFromApi(repositoryName).stream().map(imageConverter::convert).collect(Collectors.toList());
}
@Override
public final List<DockerTag> fetchImageTags(String imageName) {
return fetchTagsFromApi(imageName).stream().map(tagConverter::convert).collect(Collectors.toList());
}
protected abstract D fetchImageFromApi(final String imageName);
protected abstract List<D> fetchAllImagesFromApi(final String repositoryName);
protected abstract List<T> fetchTagsFromApi(final String imageName);
}

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,20 +15,20 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.sync.event;
package io.linuxserver.fleet.v2.client.docker;
import io.linuxserver.fleet.v2.types.docker.DockerImage;
import io.linuxserver.fleet.v2.types.docker.DockerTag;
import java.util.Collections;
import java.util.List;
public class RepositoriesScannedEvent {
public interface DockerApiClient {
private final List<String> repositories;
boolean isRepositoryValid(final String repositoryName);
public RepositoriesScannedEvent(List<String> repositories) {
this.repositories = Collections.unmodifiableList(repositories);
}
DockerImage fetchImage(final String imageName);
public List<String> getRepositories() {
return repositories;
}
List<DockerImage> fetchAllImages(final String repositoryName);
List<DockerTag> fetchImageTags(final String imageName);
}

View File

@ -0,0 +1,25 @@
/*
* 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.v2.client.docker;
public class DockerImageNotFoundException extends RuntimeException {
public DockerImageNotFoundException(final String reason) {
super(reason);
}
}

View File

@ -0,0 +1,67 @@
/*
* 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.v2.client.docker.converter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
public abstract class AbstractDockerResponseConverter<D, I> implements DockerResponseConverter<D, I> {
private final Logger LOGGER = LoggerFactory.getLogger(getClass());
@Override
public final I convert(final D dockerModel) {
try {
if (null == dockerModel) {
LOGGER.warn("Attempted to convert null image");
} else {
return doPlainConvert(dockerModel);
}
} catch (Exception e) {
LOGGER.error("Unable to convert docker model to internal.", e);
}
return null;
}
protected abstract I doPlainConvert(final D dockerApiImage);
protected final LocalDateTime parseDockerHubDate(String date) {
if (null == date) {
return null;
}
try {
final String dateToParse = (date.endsWith("Z") ? date.substring(0, date.length() - 1) : date);
return LocalDateTime.parse(dateToParse);
} catch (DateTimeParseException e) {
LOGGER.error("parseDockerHubDate(" + date + ") unable to leniently parse date.", e);
return null;
}
}
}

View File

@ -0,0 +1,25 @@
/*
* 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.v2.client.docker.converter;
public interface DockerResponseConverter<DOCKER_MODEL, INTERNAL_MODEL> {
INTERNAL_MODEL convert(final DOCKER_MODEL dockerModel);
Class<DOCKER_MODEL> getConverterClass();
}

View File

@ -0,0 +1,159 @@
/*
* 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.v2.client.docker.dockerhub;
import io.linuxserver.fleet.dockerhub.DockerHubException;
import io.linuxserver.fleet.dockerhub.model.DockerHubV2Image;
import io.linuxserver.fleet.dockerhub.model.DockerHubV2ImageListResult;
import io.linuxserver.fleet.dockerhub.model.DockerHubV2Tag;
import io.linuxserver.fleet.dockerhub.model.DockerHubV2TagListResult;
import io.linuxserver.fleet.v2.Utils;
import io.linuxserver.fleet.v2.client.docker.AbstractDockerApiClient;
import io.linuxserver.fleet.v2.client.rest.HttpException;
import io.linuxserver.fleet.v2.client.rest.RestClient;
import io.linuxserver.fleet.v2.client.rest.RestResponse;
import java.util.ArrayList;
import java.util.List;
public class DockerHubApiClient extends AbstractDockerApiClient<DockerHubV2Image, DockerHubV2Tag, DockerHubImageConverter, DockerHubTagConverter> {
public static final String DockerHubApiUrl = "https://hub.docker.com/v2";
private static final int DefaultPageSize = 1000;
private final RestClient restClient;
private final IDockerHubAuthenticator authenticator;
public DockerHubApiClient(final RestClient restClient,
final IDockerHubAuthenticator authenticator) {
super(new DockerHubImageConverter(), new DockerHubTagConverter());
this.restClient = Utils.ensureNotNull(restClient);
this.authenticator = Utils.ensureNotNull(authenticator);
}
@Override
public final boolean isRepositoryValid(String repositoryName) {
try {
return !fetchAllImages(repositoryName).isEmpty();
} catch (HttpException e) {
throw new DockerHubException("Unable to verify repository " + repositoryName, e);
}
}
@Override
protected final DockerHubV2Image fetchImageFromApi(String imageName) {
try {
final String absoluteUrl = DockerHubApiUrl + "/repositories/" + imageName + "/";
final RestResponse<DockerHubV2Image> restResponse = doCall(absoluteUrl, DockerHubV2Image.class);
if (isResponseOK(restResponse)) {
return restResponse.getPayload();
}
return null;
} catch (HttpException e) {
throw new DockerHubException("Unable to get images for " + imageName, e);
}
}
@Override
protected final List<DockerHubV2Image> fetchAllImagesFromApi(String repositoryName) {
final List<DockerHubV2Image> images = new ArrayList<>();
try {
String url = DockerHubApiUrl + "/repositories/" + repositoryName + "/?page_size=" + DefaultPageSize;
while (url != null) {
final 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
protected final List<DockerHubV2Tag> fetchTagsFromApi(String imageName) {
try {
List<DockerHubV2Tag> tags = new ArrayList<>();
String absoluteUrl = DockerHubApiUrl + "/repositories/" + imageName + "/tags/?page_size=" + DefaultPageSize;
while (absoluteUrl != null) {
final RestResponse<DockerHubV2TagListResult> response = doCall(absoluteUrl, DockerHubV2TagListResult.class);
if (isResponseOK(response)) {
final DockerHubV2TagListResult payload = response.getPayload();
tags.addAll(payload.getResults());
absoluteUrl = payload.getNext();
}
}
return tags;
} catch (HttpException e) {
throw new DockerHubException("Unable to get tags for " + 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, authenticator.buildAuthHeaders(), responseType);
if (isResponseUnauthorised(restResponse)) {
authenticator.refreshToken();
restResponse = restClient.executeGet(url, null, authenticator.buildAuthHeaders(), responseType);
}
return restResponse;
}
private boolean isResponseOK(final RestResponse<?> restResponse) {
return restResponse.getStatusCode() == 200;
}
private boolean isResponseUnauthorised(RestResponse restResponse) {
return restResponse.getStatusCode() == 401;
}
}

View File

@ -0,0 +1,83 @@
package io.linuxserver.fleet.v2.client.docker.dockerhub;
import io.linuxserver.fleet.dockerhub.DockerHubException;
import io.linuxserver.fleet.v2.client.rest.RestClient;
import io.linuxserver.fleet.v2.client.rest.RestResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
public class DockerHubAuthenticator implements IDockerHubAuthenticator {
private static final Logger LOGGER = LoggerFactory.getLogger(DockerHubAuthenticator.class);
private final RestClient client;
private final DockerHubCredentials credentials;
private String token;
public DockerHubAuthenticator(final DockerHubCredentials credentials, final 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.
*/
public synchronized String refreshToken() {
LOGGER.info("Refreshing token for Docker Hub authentication");
final RestResponse<DockerHubTokenResponse> authenticationResponse = client.executePost(
DockerHubApiClient.DockerHubApiUrl + "/users/login", null, null, credentials, DockerHubTokenResponse.class);
if (authenticationResponse.getStatusCode() == 200) {
LOGGER.info("Refresh successful");
final 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;
}
@Override
public Map<String, String> buildAuthHeaders() {
final Map<String, String> authHeaders = new HashMap<>();
authHeaders.put("Authorization", "JWT " + getCurrentToken());
return authHeaders;
}
static class DockerHubTokenResponse {
private String token;
public String getToken() {
return token;
}
}
}

View File

@ -0,0 +1,23 @@
package io.linuxserver.fleet.v2.client.docker.dockerhub;
import io.linuxserver.fleet.v2.Utils;
public class DockerHubCredentials {
private final String username;
private final String password;
public DockerHubCredentials(final String username,
final String password) {
this.username = Utils.ensureNotNull(username);
this.password = Utils.ensureNotNull(password);
}
public final String getUsername() {
return username;
}
public final String getPassword() {
return password;
}
}

View File

@ -0,0 +1,41 @@
/*
* 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.v2.client.docker.dockerhub;
import io.linuxserver.fleet.dockerhub.model.DockerHubV2Image;
import io.linuxserver.fleet.v2.client.docker.converter.AbstractDockerResponseConverter;
import io.linuxserver.fleet.v2.types.docker.DockerImage;
public class DockerHubImageConverter extends AbstractDockerResponseConverter<DockerHubV2Image, DockerImage> {
@Override
protected final DockerImage doPlainConvert(final DockerHubV2Image dockerApiImage) {
return new DockerImage(dockerApiImage.getName(),
dockerApiImage.getNamespace(),
dockerApiImage.getDescription(),
dockerApiImage.getStarCount(),
dockerApiImage.getPullCount(),
parseDockerHubDate(dockerApiImage.getLastUpdated()));
}
@Override
public Class<DockerHubV2Image> getConverterClass() {
return DockerHubV2Image.class;
}
}

View File

@ -0,0 +1,50 @@
/*
* 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.v2.client.docker.dockerhub;
import io.linuxserver.fleet.dockerhub.model.DockerHubV2Tag;
import io.linuxserver.fleet.dockerhub.model.DockerHubV2TagDigest;
import io.linuxserver.fleet.v2.client.docker.converter.AbstractDockerResponseConverter;
import io.linuxserver.fleet.v2.types.docker.DockerTag;
import io.linuxserver.fleet.v2.types.docker.DockerTagManifestDigest;
public class DockerHubTagConverter extends AbstractDockerResponseConverter<DockerHubV2Tag, DockerTag> {
@Override
protected final DockerTag doPlainConvert(final DockerHubV2Tag dockerApiImage) {
final DockerTag dockerTag = new DockerTag(dockerApiImage.getName(),
dockerApiImage.getFullSize(),
parseDockerHubDate(dockerApiImage.getLastUpdated()));
for (DockerHubV2TagDigest tagImageDigest : dockerApiImage.getImages()) {
dockerTag.addDigest(new DockerTagManifestDigest(tagImageDigest.getSize(),
tagImageDigest.getDigest(),
tagImageDigest.getArchitecture(),
tagImageDigest.getVariant()));
}
return dockerTag;
}
@Override
public Class<DockerHubV2Tag> getConverterClass() {
return DockerHubV2Tag.class;
}
}

View File

@ -0,0 +1,10 @@
package io.linuxserver.fleet.v2.client.docker.dockerhub;
import java.util.Map;
public interface IDockerHubAuthenticator {
Map<String, String> buildAuthHeaders();
String refreshToken();
}

View File

@ -0,0 +1,17 @@
package io.linuxserver.fleet.v2.client.docker.dockerhub;
import java.util.HashMap;
import java.util.Map;
public class NoOpDockerHubAuthenticator implements IDockerHubAuthenticator {
@Override
public Map<String, String> buildAuthHeaders() {
return new HashMap<>();
}
@Override
public String refreshToken() {
return null;
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2020 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.v2.client.docker.github;
import io.linuxserver.fleet.v2.client.docker.AbstractDockerApiClient;
import io.linuxserver.fleet.v2.client.docker.github.model.GitHubImage;
import io.linuxserver.fleet.v2.client.docker.github.model.GitHubTag;
import java.util.List;
public class GitHubContainerRegistryClient extends AbstractDockerApiClient<GitHubImage, GitHubTag, GitHubImageConverter, GitHubTagConverter> {
public GitHubContainerRegistryClient() {
super(new GitHubImageConverter(), new GitHubTagConverter());
}
@Override
protected final GitHubImage fetchImageFromApi(String imageName) {
return null;
}
@Override
protected final List<GitHubImage> fetchAllImagesFromApi(String repositoryName) {
return null;
}
@Override
protected final List<GitHubTag> fetchTagsFromApi(String imageName) {
return null;
}
@Override
public final boolean isRepositoryValid(String repositoryName) {
return false;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 LinuxServer.io
* Copyright (c) 2020 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,25 +15,21 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.thread;
package io.linuxserver.fleet.v2.client.docker.github;
import io.linuxserver.fleet.delegate.SynchronisationDelegate;
import io.linuxserver.fleet.v2.client.docker.converter.AbstractDockerResponseConverter;
import io.linuxserver.fleet.v2.client.docker.github.model.GitHubImage;
import io.linuxserver.fleet.v2.types.docker.DockerImage;
/**
* <p>
* Asynchronous way to trigger a new synchronisation via the {@link SynchronisationDelegate}
* </p>
*/
public class SynchroniseAllRepositoriesTask extends FleetTask {
public class GitHubImageConverter extends AbstractDockerResponseConverter<GitHubImage, DockerImage> {
private final SynchronisationDelegate synchronisationDelegate;
public SynchroniseAllRepositoriesTask(SynchronisationDelegate synchronisationDelegate) {
this.synchronisationDelegate = synchronisationDelegate;
@Override
protected final DockerImage doPlainConvert(final GitHubImage dockerApiImage) {
return null;
}
@Override
protected void executeTask() {
synchronisationDelegate.synchronise();
public final Class<GitHubImage> getConverterClass() {
return GitHubImage.class;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 LinuxServer.io
* Copyright (c) 2020 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,26 +15,21 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.web;
package io.linuxserver.fleet.v2.client.docker.github;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import spark.ResponseTransformer;
import io.linuxserver.fleet.v2.client.docker.converter.AbstractDockerResponseConverter;
import io.linuxserver.fleet.v2.client.docker.github.model.GitHubTag;
import io.linuxserver.fleet.v2.types.docker.DockerTag;
public class JsonTransformer implements ResponseTransformer {
public class GitHubTagConverter extends AbstractDockerResponseConverter<GitHubTag, DockerTag> {
private static final ObjectMapper OBJECT_MAPPER;
static {
OBJECT_MAPPER = new ObjectMapper();
@Override
protected final DockerTag doPlainConvert(final GitHubTag dockerApiImage) {
return null;
}
@Override
public String render(Object model) {
try {
return OBJECT_MAPPER.writeValueAsString(model);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
public final Class<GitHubTag> getConverterClass() {
return GitHubTag.class;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 LinuxServer.io
* Copyright (c) 2020 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,9 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.queue;
package io.linuxserver.fleet.v2.client.docker.github.model;
public interface FleetResponse {
void handle();
public class GitHubImage {
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 LinuxServer.io
* Copyright (c) 2020 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,9 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.queue;
package io.linuxserver.fleet.v2.client.docker.github.model;
public interface QueueConsumer<T extends FleetRequest<?>> {
void consume();
public class GitHubTag {
}

View File

@ -0,0 +1,25 @@
/*
* 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.v2.client.docker.queue;
import io.linuxserver.fleet.v2.key.ImageKey;
import io.linuxserver.fleet.v2.thread.AsyncTask;
public interface AsyncDockerApiRequest<R extends AsyncDockerApiResponse> extends AsyncTask<DockerApiDelegate, R> {
ImageKey getImageKey();
}

View File

@ -0,0 +1,24 @@
/*
* 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.v2.client.docker.queue;
import io.linuxserver.fleet.v2.thread.AsyncTaskResponse;
public interface AsyncDockerApiResponse extends AsyncTaskResponse {
void handleDockerApiResponse();
}

View File

@ -0,0 +1,68 @@
/*
* 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.v2.client.docker.queue;
import io.linuxserver.fleet.core.FleetAppController;
import io.linuxserver.fleet.v2.Utils;
import io.linuxserver.fleet.v2.client.docker.DockerApiClient;
import io.linuxserver.fleet.v2.client.docker.DockerImageNotFoundException;
import io.linuxserver.fleet.v2.key.ImageKey;
import io.linuxserver.fleet.v2.key.RepositoryKey;
import io.linuxserver.fleet.v2.thread.AsyncTaskDelegate;
import io.linuxserver.fleet.v2.types.docker.DockerImage;
import io.linuxserver.fleet.v2.types.docker.DockerTag;
import java.util.List;
public class DockerApiDelegate implements AsyncTaskDelegate {
private final FleetAppController controller;
private final DockerApiClient apiClient;
public DockerApiDelegate(final FleetAppController controller,
final DockerApiClient dockerApiClient) {
this.controller = controller;
this.apiClient = Utils.ensureNotNull(dockerApiClient);
}
public final boolean isRepositoryValid(final String repositoryName) {
return apiClient.isRepositoryValid(repositoryName);
}
public final List<DockerImage> getImagesForRepository(final RepositoryKey repositoryKey) {
return apiClient.fetchAllImages(repositoryKey.getName());
}
public final DockerImage getCurrentImageView(final ImageKey imageKey) {
final DockerImage dockerImage = apiClient.fetchImage(imageKey.getAsRepositoryAndImageName());
if (null == dockerImage) {
throw new DockerImageNotFoundException("Image " + imageKey.getAsRepositoryAndImageName() + " was not found upstream.");
}
final List<DockerTag> allImageTags = apiClient.fetchImageTags(imageKey.getAsRepositoryAndImageName());
allImageTags.forEach(dockerImage::addTag);
return dockerImage;
}
@Override
public FleetAppController getController() {
return controller;
}
}

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.v2.client.docker.queue;
import io.linuxserver.fleet.v2.service.SynchronisationService;
import io.linuxserver.fleet.v2.thread.AbstractTaskQueueConsumer;
import io.linuxserver.fleet.v2.thread.TaskExecutionException;
public final class DockerApiTaskConsumer extends AbstractTaskQueueConsumer<DockerApiDelegate, DockerImageUpdateResponse, DockerImageUpdateRequest> {
public DockerApiTaskConsumer(final SynchronisationService syncService) {
super(syncService.getController(),
syncService.getConfiguredDockerDelegate(),
syncService.getSyncQueue(),
"DockerSyncConsumer");
}
@Override
protected void handleTaskResponse(final DockerImageUpdateResponse response) {
try {
response.handleDockerApiResponse();
} catch (Exception e) {
getLogger().error("handleTaskResponse caught unhandled error, but not something worthy of stalling thread", e);
throw new TaskExecutionException(e);
}
}
}

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