Skip to content

Commit

Permalink
Various gradle and docker fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dhedey committed Apr 28, 2022
1 parent dbb42f2 commit 8e3a9df
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 33 deletions.
37 changes: 31 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,48 @@ plugins {
id "com.diffplug.spotless" version "6.0.0"
}

/*
* Creates a nice version name we can use for built assets; from the git version.
* This is set to gradle's version property below.
*/
def radixVersion() {
/*
KNOWN ISSUE:
1. If there are two (simple) tags on the same commit, it selects the oldest. This can be a problem if (eg) a
release and a release candidate tag share the same commit.
EXPLANATION:
versionDetails is from https://github.com/palantir/gradle-git-version
Behind the scenes, it parses "git describe --tags --always --first-parent"
This is used to extract the closest tag information. If there are multiple, git describe attempts to select
the most recent tag - but this only works if the tags are annotated - if they're simple tags, it just
selects the first.
Github release creates simple tags, not annotated tags (as of April 2022) - see discussion here
for suggested change: https://github.com/github/feedback/discussions/4924
WORKAROUND:
Manually create the annotated tag yourself for the release, eg:
git tag -a v1.2.1 -m "Version 1.2.1"
See also: https://git-scm.com/book/en/v2/Git-Basics-Tagging
*/
def details = versionDetails()

def version
if (details.isCleanTag) {
version = details.lastTag
} else {
version = details.branchName
if (version == null) {
if (details.branchName == null) {
version = "detached-head-${details.gitHash}"
} else {
// Prepend last tag so builddeb doesn't choke on non-numeric branch names
version = "${details.lastTag}-${version}"
version = version.replaceAll('/', '~')
version = "${details.branchName}-${details.gitHash}"
}
version = "${version}-SNAPSHOT"
// Prepend SNAPSHOT so the tag name is valid regardless of the version
version = "SNAPSHOT-${version}"
}

// Ensure version is safe for gradle's purposes
// (we do further sanitizing for debian's purposes in debSafeVersionName())
version = version.replaceAll('/', '~')
return version
}

Expand Down
73 changes: 60 additions & 13 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -224,23 +224,58 @@ tasks.withType(Tar) {
compression = Compression.GZIP
}

def debSafeVersionName() {
// project.version comes from radixVersion() in the root gradle property
def safeVersion = project.version
.replaceAll('-', '~') // Debian doesn't like - in the name
.replaceAll(/[^A-Za-z0-9.+~]/, '') // Replace versions that dev doesn't like

// Note - The deb version must start with a number
if (!safeVersion.matches(~/\d.+/)) {
safeVersion = "0~~$safeVersion"
}

return safeVersion
}

def debPackageName() {
return "radixdlt"
}

def debOutputFileName() {
return "${debPackageName()}_${debSafeVersionName()}_all.deb"
}

ospackage {
os = LINUX

def x = "y'234"

postInstall file('ospackage/postinst.sh')
preUninstall file('ospackage/prerm.sh')
postUninstall file('ospackage/postrm.sh')

// These should match the scripts in ospackage
def serviceName = debPackageName()
def optDirectory = debPackageName()

from("$buildDir/install/$name") {
into "/opt/$name"
into "/opt/$optDirectory"
}
from("ospackage/${name}.service") {
from("ospackage/${serviceName}.service") {
into "/etc/systemd/system"
}

// See https://github.com/nebula-plugins/gradle-ospackage-plugin/wiki/Deb-Plugin#basic-usage
buildDeb {
dependsOn += [installDist]
version = project.version.replaceAll('-', '~').replaceAll(/[^A-Za-z0-9.+:~-]/, '')

version = debSafeVersionName()
packageName = debPackageName()

doLast {
println "SUCCESS: Built deb images: ${debOutputFileName()}"
}
}
buildRpm {
dependsOn += [installDist]
Expand Down Expand Up @@ -268,24 +303,36 @@ task depsize {
}

/**
* Manages the *.deb file in the docker directory
* Copies the *.deb file in the docker directory
*/
task deb4docker(type: Copy, dependsOn: buildDeb) {
def ospackageVersion = project.version.replaceAll('-', '~').replaceAll(/[^A-Za-z0-9.+:~-]/, '')

from("$buildDir/distributions") {
include "radixdlt_${ospackageVersion}_all.deb"
}
into project.file('../docker')
task deb4docker(dependsOn: buildDeb) {
doFirst {
def createdDebFileName = debOutputFileName()
def previousDebFileToDeleteRegex = ~/.+_all\.deb/
def builtDebFileLocation = "$buildDir/distributions/$createdDebFileName"

def destinationDir = project.file('../docker')
def destinationLocation = "$destinationDir/$createdDebFileName"

if (!file(builtDebFileLocation).exists()) {
throw new GradleException("Expected to find built deb package at $builtDebFileLocation but it wasn't there - perhaps the replacement regex in deb4docker is broken / needs fixing")
}
println "Built deb image found at: $builtDebFileLocation"

def names = [] as Set
destinationDir.eachFileMatch(groovy.io.FileType.FILES, ~/radixdlt_.+_all\.deb/) {
destinationDir.eachFileMatch(groovy.io.FileType.FILES, previousDebFileToDeleteRegex) {
names << it.name
}
names.toSorted().each {
def rip = new File(destinationDir, it)
rip.delete()
println "Deleted conflicting deb package: ${rip.name} ..."
println "Deleted previous deb package: ${rip.path}"
}

copy {
from builtDebFileLocation
into destinationDir
}
println "SUCCESS: Deb image copied for the docker build to: $destinationLocation"
}
}
17 changes: 12 additions & 5 deletions core/ospackage/postinst.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@

set -ex

# NB - If updating this script, also update ./docker/scripts/config_radixdlt.sh

# vars
RADIXDLT_USER=radixdlt
RADIXDLT_HOME=/opt/$RADIXDLT_USER
RADIXDLT_SERVICE_FILE=radixdlt.service
RADIXDLT_DIRECTORY_NAME=radixdlt

# paths
RADIXDLT_HOME="/opt/$RADIXDLT_DIRECTORY_NAME"
RADIXDLT_LOG_DIR="/var/log/$RADIXDLT_DIRECTORY_NAME"

# create user and group idempotently
getent group $RADIXDLT_USER >/dev/null || groupadd -r $RADIXDLT_USER
getent passwd $RADIXDLT_USER >/dev/null || useradd -r -d "$RADIXDLT_HOME" -g $RADIXDLT_USER $RADIXDLT_USER

# create log dir
mkdir -p /var/log/$RADIXDLT_USER
mkdir -p "$RADIXDLT_LOG_DIR"

# make sure all files are owned by the radixdlt user/group
chown -Rf "$RADIXDLT_USER:$RADIXDLT_USER" "$RADIXDLT_HOME" /var/log/$RADIXDLT_USER
chown -Rf "$RADIXDLT_USER:$RADIXDLT_USER" "$RADIXDLT_HOME" "$RADIXDLT_LOG_DIR"

# Make sure that systemd files are owned by root
chown root:root /etc/systemd/system/$RADIXDLT_USER.service
chown root:root "/etc/systemd/system/$RADIXDLT_SERVICE_FILE"

#systemctl daemon-reload
#systemctl start $RADIXDLT_USER.service
#systemctl start $RADIXDLT_SERVICE_FILE
5 changes: 4 additions & 1 deletion core/ospackage/prerm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

set -x

# vars
RADIXDLT_SERVICE_FILE=radixdlt.service

# kill the process
systemctl stop radixdlt.service || :
systemctl stop $RADIXDLT_SERVICE_FILE || :
4 changes: 2 additions & 2 deletions core/ospackage/radixdlt.service
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[Unit]
Description=Radix DLT Client
Description=Radix DLT Babylon Java Node
After=local-fs.target
After=network-online.target
After=nss-lookup.target
Expand All @@ -10,7 +10,7 @@ Wants=network-online.target
[Service]
User=radixdlt
WorkingDirectory=/opt/radixdlt
ExecStart=/opt/radixdlt/bin/radixdlt
ExecStart=/opt/radixdlt/bin/core
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.core
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ RUN apt-get -y --no-install-recommends install openjdk-17-jdk

# set entrypoint and default command
ENTRYPOINT ["/opt/radixdlt/config_radixdlt.sh"]
CMD ["/opt/radixdlt/bin/radixdlt"]
CMD ["/opt/radixdlt/bin/core"]

# set default environment variables
ENV RADIXDLT_HOME=/home/radixdlt \
Expand Down
1 change: 1 addition & 0 deletions docker/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
RADIXDLT_TRANSACTIONS_API_ENABLE: "true"
RADIXDLT_GENESIS_TXN: ${RADIXDLT_GENESIS_TXN}
JAVA_OPTS: -server -Xmx512m -Xmx512m -XX:+HeapDumpOnOutOfMemoryError -XX:+AlwaysPreTouch -Dguice_bytecode_gen_option=DISABLED -Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts -Djavax.net.ssl.trustStoreType=jks -Djava.security.egd=file:/dev/urandom -Dcom.sun.management.jmxremote.port=9011 -Dcom.sun.management.jmxremote.rmi.port=9011 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=core -agentlib:jdwp=transport=dt_socket,address=*:50505,suspend=n,server=y --enable-preview
# May need updating for Babylon
image: radixdlt/radixdlt-core:main
labels:
com.radixdlt.roles: "core"
Expand Down
10 changes: 5 additions & 5 deletions docker/scripts/config_radixdlt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

set -ex

# need this to run on Alpine (grsec) kernels without crashing
# Need this to run on Alpine (grsec) kernels without crashing
find /usr -type f -name java -exec setfattr -n user.pax.flags -v em {} \;

#Add user using env variable if provided
# Add user using env variable if provided
USER_ID=${LOCAL_USER_ID:-999}
USER_NAME=radixdlt
# check and delete the user that is created in postinstal action of deb package
# Check and delete the user that is created in postinstall action of deb package
getent group $USER_NAME >/dev/null && groupmod -g $USER_ID radixdlt || groupadd -r $USER_NAME -g $USER_ID
getent passwd $USER_NAME >/dev/null && usermod -u $USER_ID radixdlt || useradd -r -d "$RADIXDLT_HOME" -g $USER_NAME $USER_NAME
chown -R radixdlt:radixdlt /home/radixdlt/
chmod u=xr /opt/radixdlt/bin/radixdlt
chmod u=xr /opt/radixdlt/bin/core

#check for test network configs
# Check for test network configs
TEST_CONFIGS="${RADIXDLT_HOME:?}"/test.config
if test -f "$TEST_CONFIGS"; then
echo >> default.config.envsubst
Expand Down
7 changes: 7 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAME
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED

# April 2022
# There are as-of-yet unfixed deprecation warnings arising from some of the plugins we use - when running in Gradle 7,
# for features that will be removed in Gradle 8. Temporarily ignore these annoying warnings till they fix them:
# - https://github.com/nebula-plugins/gradle-ospackage-plugin/issues/403
# - https://jira.sonarsource.com/browse/SONARGRADL-84
org.gradle.warning.mode=none

0 comments on commit 8e3a9df

Please sign in to comment.