Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RpmNoReplaceplugin and LinuxMappingDSL for "noreplace" configs #896

Merged
merged 5 commits into from
Nov 2, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ trait LinuxMappingDSL {
(src, dest) <- dirs
path <- (src ***).get
} yield path -> path.toString.replaceFirst(src.toString, dest)

/**
* This method sets the config attribute of all files that are marked as configuration files to "noreplace". This is
* relevant for RPM packages as it controls the behaviour of RPM updates.
*
* See: http://www-uxsup.csx.cam.ac.uk/~jw35/docs/rpm_config.html
*
* @param mappings list of mappings to update
* @return updated list of mappings
*/
def configWithNoReplace(mappings: Seq[LinuxPackageMapping]): Seq[LinuxPackageMapping] = {
mappings.map {
case mapping if mapping.fileData.config != "false" => mapping.withConfig("noreplace")
case mapping => mapping
}
}
}

object Mapper extends LinuxMappingDSL
48 changes: 48 additions & 0 deletions src/sbt-test/rpm/config-no-replace/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
enablePlugins(RpmPlugin)

name := "rpm-test"

version := "0.1.0"

maintainer := "Josh Suereth <[email protected]>"

packageSummary := "Test rpm package"

packageDescription := """A fun package description of our software,
with multiple lines."""

rpmRelease := "1"

rpmVendor := "typesafe"

rpmUrl := Some("http://github.com/sbt/sbt-native-packager")

rpmLicense := Some("BSD")

packageArchitecture in Rpm := "x86_64"

linuxPackageMappings := configWithNoReplace(linuxPackageMappings.value)

TaskKey[Unit]("unzip") <<= (packageBin in Rpm, streams) map { (rpmFile, streams) =>
val rpmPath = Seq(rpmFile.getAbsolutePath)
Process("rpm2cpio", rpmPath) #| Process("cpio -i --make-directories") ! streams.log
}

TaskKey[Unit]("checkSpecFile") <<= (target, streams) map { (target, out) =>
val spec = IO.read(target / "rpm" / "SPECS" / "rpm-test.spec")

assert(
spec contains
"%files\n%dir %attr(0755,root,root) /usr/share/rpm-test/conf",
"Contains configuration directory."
)

assert(
spec contains
"%config(noreplace) %attr(0644,root,root) /usr/share/rpm-test/conf/test",
"Sets custom config to 'noreplace'"
)

out.log.success("Successfully tested rpm test file")
()
}
1 change: 1 addition & 0 deletions src/sbt-test/rpm/config-no-replace/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version"))
1 change: 1 addition & 0 deletions src/sbt-test/rpm/config-no-replace/src/universal/conf/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Test configuration file!
7 changes: 7 additions & 0 deletions src/sbt-test/rpm/config-no-replace/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Run the debian packaging.
> rpm:package-bin
$ exists target/rpm/RPMS/x86_64/rpm-test-0.1.0-1.x86_64.rpm

> unzip

> checkSpecFile
2 changes: 1 addition & 1 deletion src/sbt-test/rpm/override-start-script-systemd/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ TaskKey[Unit]("checkStartupScript") <<= (target, streams) map { (target, out) =>
val script = IO.read(file("usr/lib/systemd/system/rpm-test.service"))
assert(script.startsWith("# right systemd template"), s"override script wasn't picked, script is\n$script")
()
}
}
2 changes: 1 addition & 1 deletion src/sbt-test/rpm/override-start-script-systemv/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ TaskKey[Unit]("checkStartupScript") <<= (target, streams) map { (target, out) =>
val script = IO.read(file("etc/init.d/rpm-test"))
assert(script.startsWith("# right systemv template"), s"override script wasn't picked, script is\n$script")
()
}
}
2 changes: 1 addition & 1 deletion src/sbt-test/rpm/override-start-script-upstart/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ TaskKey[Unit]("checkStartupScript") <<= (target, streams) map { (target, out) =>
val script = IO.read(file("etc/init/rpm-test.conf"))
assert(script.startsWith("# right upstart template"), s"override script wasn't picked, script is\n$script")
()
}
}
15 changes: 15 additions & 0 deletions src/sphinx/formats/rpm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,18 @@ For more information on this topic follow these links:
.. _OpenSuse issue: https://github.com/sbt/sbt-native-packager/issues/215
.. _RPM Scaladocs: http://www.scala-sbt.org/sbt-native-packager/latest/api/#com.typesafe.sbt.packager.rpm.RpmPlugin$$Names$
.. _MaintainerScriptHelper Scaladocs: http://www.scala-sbt.org/sbt-native-packager/latest/api/#com.typesafe.sbt.packager.MaintainerScriptHelper$


Marking config files as ``noreplace``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

By default, rpm replaces config files on disk when the content has changed between two version. Often, this is not desirable
as configurations are often customized and should not change during updates. rpm provides a means to turn of the default behaviour
by marking config files as ``noreplace`` in the spec file. In order to enable this for the build, we provide a helper method that
can be used to modify all config file mappings:

.. code-block:: scala

linuxPackageMappings := configWithNoReplace(linuxPackageMappings.value)

This will mark all config files as ``noreplace`` and prevent them from being changed during updates.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.typesafe.sbt.packager.linux

import java.io.File

import org.scalatest.{Matchers, WordSpec}

class LinuxMappingDSLSpec extends WordSpec with Matchers with LinuxMappingDSL {

"The LinuxMappingDSL" should {

"map config files to noreplace" in {
val f1 = LinuxPackageMapping(Map(new File("/tmp/1") -> "/tmp/1"))
val f2 = LinuxPackageMapping(Map(new File("/tmp/1") -> "/tmp/1")).withConfig()

val f1Mapped :: f2Mapped :: Nil = configWithNoReplace(Seq(f1, f2))

f1Mapped.fileData.config should be("false")
f2Mapped.fileData.config should be("noreplace")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import com.typesafe.sbt.packager._
import com.typesafe.sbt.packager.permissions
import org.scalatest._
import java.io.File
import java.nio.file.{Path, Paths, Files}
import java.nio.file.{Files, Path, Paths}
import java.nio.file.attribute.PosixFilePermission._
import scala.collection.JavaConversions._

class ZipHelperSpec
extends WordSpec
with Matchers
with BeforeAndAfterEach
with BeforeAndAfterAll {
class ZipHelperSpec extends WordSpec with Matchers with BeforeAndAfterEach with BeforeAndAfterAll {

var tmp: Path = _
val toDelete = scala.collection.mutable.ListBuffer[Path]()
Expand Down
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version in ThisBuild := "1.2.0-SNAPSHOT"
version in ThisBuild := "1.2.0-SNAPSHOT"