Skip to content

Commit 11c15ed

Browse files
committed
Treat symlinks as normal files in spec file
1 parent 8d4ad99 commit 11c15ed

File tree

7 files changed

+59
-129
lines changed

7 files changed

+59
-129
lines changed

src/main/scala/com/typesafe/sbt/packager/rpm/RpmHelper.scala

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.typesafe.sbt.packager.rpm
22

33
import sbt._
44
import com.typesafe.sbt.packager.Compat._
5+
import com.typesafe.sbt.packager.linux.LinuxSymlink
56

67
object RpmHelper {
78

@@ -65,6 +66,8 @@ object RpmHelper {
6566
if file.exists && !file.isDirectory()
6667
target = buildroot / dest
6768
} copyWithZip(file, target, mapping.zipped)
69+
70+
LinuxSymlink.makeSymLinks(spec.symlinks, buildroot, relativeLinks = false)
6871
}
6972

7073
private[this] def writeSpecFile(spec: RpmSpec, workArea: File, log: sbt.Logger): File = {

src/main/scala/com/typesafe/sbt/packager/rpm/RpmMetadata.scala

+20-72
Original file line numberDiff line numberDiff line change
@@ -60,35 +60,22 @@ case class RpmScripts(pretrans: Option[String] = None,
6060
preun: Option[String] = None,
6161
postun: Option[String] = None) {
6262

63-
def pretransContent(): String =
64-
pretrans.fold("")("\n%pretrans\n" + _ + "\n\n")
65-
66-
def preContent(): String =
67-
pre.fold("")("\n%pre\n" + _ + "\n\n")
68-
69-
def postContent(buildSymlinkScript: Option[String]): String = {
70-
val scripts = Seq(post, buildSymlinkScript).flatten
71-
if (scripts.isEmpty)
72-
""
73-
else
74-
"\n%post\n" + scripts.mkString("\n") + "\n\n"
75-
}
76-
77-
def posttransContent(): String =
78-
posttrans.fold("")("\n%posttrans\n" + _ + "\n\n")
79-
80-
def verifyscriptContent(): String =
81-
verifyscript.fold("")("\n%verifyscript\n" + _ + "\n\n")
82-
83-
def preunContent(): String =
84-
preun.fold("")("\n%preun\n" + _ + "\n\n")
85-
86-
def postunContent(tearDownSymlinkScript: Option[String]): String = {
87-
val scripts = Seq(postun, tearDownSymlinkScript).flatten
88-
if (scripts.isEmpty)
89-
""
90-
else
91-
"\n%postun\n" + scripts.mkString("\n") + "\n\n"
63+
def pretransContent(): String = buildScript("pretrans", pretrans)
64+
def preContent(): String = buildScript("pre", pre)
65+
def postContent(): String = buildScript("post", post)
66+
def posttransContent(): String = buildScript("posttrans", posttrans)
67+
def verifyscriptContent(): String = buildScript("verifyscript", verifyscript)
68+
def preunContent(): String = buildScript("preun", preun)
69+
def postunContent(): String = buildScript("postun", postun)
70+
71+
private def buildScript(name: String, script: Option[String]): String = {
72+
script.map { code =>
73+
s"""
74+
|%$name
75+
|$code
76+
|
77+
|""".stripMargin
78+
} getOrElse ""
9279
}
9380

9481
@deprecated(
@@ -207,6 +194,8 @@ case class RpmSpec(meta: RpmMetadata,
207194
mapping <- mappings
208195
(file, dest) <- mapping.mappings
209196
} sb append makeFilesLine(dest, mapping.fileData, file.isDirectory)
197+
198+
symlinks foreach (l => sb append s"${l.link}\n")
210199
sb.toString
211200
}
212201

@@ -276,11 +265,11 @@ case class RpmSpec(meta: RpmMetadata,
276265
// write scriptlets
277266
sb append scriptlets.pretransContent()
278267
sb append scriptlets.preContent()
279-
sb append scriptlets.postContent(buildSymlinkScript(meta.name, installDir, symlinks))
268+
sb append scriptlets.postContent()
280269
sb append scriptlets.verifyscriptContent()
281270
sb append scriptlets.posttransContent()
282271
sb append scriptlets.preunContent()
283-
sb append scriptlets.postunContent(teardownSymlinkScript(meta.name, installDir, symlinks))
272+
sb append scriptlets.postunContent()
284273

285274
// Write file mappings
286275
sb append fileSection
@@ -296,45 +285,4 @@ case class RpmSpec(meta: RpmMetadata,
296285
}
297286
sb.toString
298287
}
299-
300-
private def buildSymlinkScript(appName: String, installDir: String, symlinks: Seq[LinuxSymlink]): Option[String] =
301-
if (symlinks.isEmpty)
302-
None
303-
else {
304-
val relocateLinks = symlinks
305-
.map { symlink =>
306-
s"""rm -rf $$(relocateLink ${symlink.link} $installDir $appName $$RPM_INSTALL_PREFIX) && ln -s $$(relocateLink ${symlink.destination} $installDir $appName $$RPM_INSTALL_PREFIX) $$(relocateLink ${symlink.link} $installDir $appName $$RPM_INSTALL_PREFIX)"""
307-
}
308-
.mkString("\n")
309-
310-
Some(relocateLinkFunction + "\n" + relocateLinks)
311-
}
312-
313-
private def teardownSymlinkScript(appName: String, installDir: String, symlinks: Seq[LinuxSymlink]): Option[String] =
314-
if (symlinks.isEmpty)
315-
None
316-
else {
317-
val checkUninstall = "if [ $1 -eq 0 ] ;\nthen"
318-
val sourceAppConfig =
319-
s""" [ -e /etc/sysconfig/$appName ] && . /etc/sysconfig/$appName"""
320-
val cleanupLinks = symlinks
321-
.map { symlink =>
322-
s""" rm -rf $$(relocateLink ${symlink.link} $installDir $appName $$PACKAGE_PREFIX)"""
323-
}
324-
.mkString("\n")
325-
326-
Some(relocateLinkFunction + "\n" + checkUninstall + "\n" + sourceAppConfig + "\n" + cleanupLinks + "\nfi")
327-
}
328-
329-
private def relocateLinkFunction: String =
330-
"""
331-
|relocateLink() {
332-
| if [ -n "$4" ] ;
333-
| then
334-
| RELOCATED_INSTALL_DIR="$4/$3"
335-
| echo "${1/$2/$RELOCATED_INSTALL_DIR}"
336-
| else
337-
| echo "$1"
338-
| fi
339-
|}""".stripMargin
340288
}

src/sbt-test/rpm/scriptlets-rpm/build.sbt

-43
Original file line numberDiff line numberDiff line change
@@ -36,53 +36,10 @@ TaskKey[Unit]("checkSpecFile") := {
3636
val spec = IO.read(target.value / "rpm" / "SPECS" / "rpm-test.spec")
3737
assert(spec contains "%pre\necho \"pre-install\"", "Spec doesn't contain %pre scriptlet")
3838
assert(spec contains "%post\necho \"post-install\"", "Spec doesn't contain %post scriptlet")
39-
assert(
40-
spec contains
41-
"""
42-
|%post
43-
|echo "post-install"
44-
|
45-
|relocateLink() {
46-
| if [ -n "$4" ] ;
47-
| then
48-
| RELOCATED_INSTALL_DIR="$4/$3"
49-
| echo "${1/$2/$RELOCATED_INSTALL_DIR}"
50-
| else
51-
| echo "$1"
52-
| fi
53-
|}
54-
|rm -rf $(relocateLink /etc/rpm-test /usr/share/rpm-test rpm-test $RPM_INSTALL_PREFIX) && ln -s $(relocateLink /usr/share/rpm-test/conf /usr/share/rpm-test rpm-test $RPM_INSTALL_PREFIX) $(relocateLink /etc/rpm-test /usr/share/rpm-test rpm-test $RPM_INSTALL_PREFIX)
55-
|""".stripMargin,
56-
"%post scriptlet does not contain relocateLink"
57-
)
58-
5939
assert(spec contains "%pretrans\necho \"pretrans\"", "Spec doesn't contain %pretrans scriptlet")
6040
assert(spec contains "%posttrans\necho \"posttrans\"", "Spec doesn't contain %posttrans scriptlet")
6141
assert(spec contains "%preun\necho \"pre-uninstall\"", "Spec doesn't contain %preun scriptlet")
6242
assert(spec contains "%postun\necho \"post-uninstall\"", "Spec doesn't contain %postun scriptlet")
63-
assert(
64-
spec contains
65-
"""
66-
|%postun
67-
|echo "post-uninstall"
68-
|
69-
|relocateLink() {
70-
| if [ -n "$4" ] ;
71-
| then
72-
| RELOCATED_INSTALL_DIR="$4/$3"
73-
| echo "${1/$2/$RELOCATED_INSTALL_DIR}"
74-
| else
75-
| echo "$1"
76-
| fi
77-
|}
78-
|if [ $1 -eq 0 ] ;
79-
|then
80-
| [ -e /etc/sysconfig/rpm-test ] && . /etc/sysconfig/rpm-test
81-
| rm -rf $(relocateLink /etc/rpm-test /usr/share/rpm-test rpm-test $PACKAGE_PREFIX)
82-
|fi
83-
|""".stripMargin,
84-
"%postun scriptlet does not contain relocate link"
85-
)
8643
streams.value.log.success("Successfully tested rpm test file")
8744
()
8845
}

src/sbt-test/rpm/simple-rpm/build.sbt

-14
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ linuxPackageMappings in Rpm := {
2929
Seq(LinuxPackageMapping(Seq(mapping1, mapping2)))
3030
}
3131

32-
linuxPackageSymlinks in Rpm := Seq(LinuxSymlink("/etc/link1", "destination1"), LinuxSymlink("link2", "destination2"))
33-
3432
defaultLinuxInstallLocation in Rpm := "/opt/foo"
3533

3634
TaskKey[Unit]("checkSpecFile") := {
@@ -56,18 +54,6 @@ TaskKey[Unit]("checkSpecFile") := {
5654
"Contains package mappings"
5755
)
5856

59-
assert(
60-
spec contains
61-
"ln -s $(relocateLink destination1 /opt/foo/rpm-test rpm-test $RPM_INSTALL_PREFIX) $(relocateLink /etc/link1 /opt/foo/rpm-test rpm-test $RPM_INSTALL_PREFIX)",
62-
"Contains package symlink link (1)"
63-
)
64-
65-
assert(
66-
spec contains
67-
"ln -s $(relocateLink destination2 /opt/foo/rpm-test rpm-test $RPM_INSTALL_PREFIX) $(relocateLink link2 /opt/foo/rpm-test rpm-test $RPM_INSTALL_PREFIX)",
68-
"Contains package symlink link (2)"
69-
)
70-
7157
streams.value.log.success("Successfully tested rpm test file")
7258
()
7359
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import com.typesafe.sbt.packager.linux.{LinuxPackageMapping, LinuxSymlink}
2+
3+
enablePlugins(JavaAppPackaging)
4+
5+
name := "rpm-test"
6+
version := "0.1.0"
7+
maintainer := "David Pennell <[email protected]>"
8+
packageSummary := "Test rpm package"
9+
packageName in Linux := "rpm-package"
10+
packageDescription :=
11+
"""A fun package description of our software,
12+
with multiple lines."""
13+
14+
rpmRelease := "1"
15+
rpmVendor := "typesafe"
16+
rpmUrl := Some("http://github.com/sbt/sbt-native-packager")
17+
rpmLicense := Some("BSD")
18+
19+
linuxPackageSymlinks := {
20+
val helloSymlink = LinuxSymlink(((file(defaultLinuxInstallLocation.value) / (packageName in Linux).value / "lib") / "hello.link").toString, "/fake/hello.tx")
21+
Seq(helloSymlink)
22+
}
23+
24+
TaskKey[Unit]("check-spec-file") := {
25+
val spec = IO.read(target.value / "rpm" / "SPECS" / "rpm-package.spec")
26+
streams.value.log.success(spec)
27+
assert(spec contains "%attr(0644,root,root) /usr/share/rpm-package/lib/rpm-test.rpm-test-0.1.0.jar", "Wrong installation path")
28+
assert(spec contains "/usr/share/rpm-package/lib/hello.link", "Missing or incorrect symbolic link")
29+
streams.value.log.success("Successfully tested rpm-package file")
30+
()
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version"))

src/sbt-test/rpm/symlink-rpm/test

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
> rpm:packageBin
2+
$ exists target/rpm/RPMS/noarch/rpm-package-0.1.0-1.noarch.rpm
3+
$ exists target/rpm/SPECS/rpm-package.spec
4+
> checkSpecFile

0 commit comments

Comments
 (0)