diff --git a/src/main/resources/com/typesafe/sbt/packager/debian/postinst-chown b/src/main/resources/com/typesafe/sbt/packager/debian/postinst-chown new file mode 100644 index 000000000..8076ed5e8 --- /dev/null +++ b/src/main/resources/com/typesafe/sbt/packager/debian/postinst-chown @@ -0,0 +1 @@ +chown ${{user}}:${{group}} ${{path}} diff --git a/src/main/resources/com/typesafe/sbt/packager/debian/postinst-groupadd b/src/main/resources/com/typesafe/sbt/packager/debian/postinst-groupadd new file mode 100644 index 000000000..20f624a69 --- /dev/null +++ b/src/main/resources/com/typesafe/sbt/packager/debian/postinst-groupadd @@ -0,0 +1,6 @@ +# Adding ${{group}} as system group +if ! getent group ${{group}} > /dev/null 2>&1 +then + echo "Creating system group: ${{group}}" + addgroup --system ${{group}} +fi diff --git a/src/main/resources/com/typesafe/sbt/packager/debian/postinst-useradd b/src/main/resources/com/typesafe/sbt/packager/debian/postinst-useradd new file mode 100644 index 000000000..fc640de99 --- /dev/null +++ b/src/main/resources/com/typesafe/sbt/packager/debian/postinst-useradd @@ -0,0 +1,5 @@ +# Adding ${{user}} +if ! id -u $1 >/dev/null 2>&1; then + echo "Creating user ${{user}} in group ${{group}}" + useradd --system --no-create-home --gid ${{group}} --shell /bin/false ${{user}} +fi diff --git a/src/main/resources/com/typesafe/sbt/packager/debian/postrm-purge b/src/main/resources/com/typesafe/sbt/packager/debian/postrm-purge new file mode 100644 index 000000000..adc349efe --- /dev/null +++ b/src/main/resources/com/typesafe/sbt/packager/debian/postrm-purge @@ -0,0 +1,14 @@ +# Deleting user: ${{user}} and group: ${{group}} +case "$1" in + remove|failed-upgrade|abort-upgrade|abort-install|disappear) + ;; + purge) + deluser --quiet --system ${{user}} > /dev/null || true + delgroup --quiet --system ${{group}} > /dev/null || true + ;; + upgrade) + ;; + *) + echo "postinst called with unknown argument \`\$1'" >&2 + ;; +esac diff --git a/src/main/scala/com/typesafe/sbt/packager/debian/DebianPlugin.scala b/src/main/scala/com/typesafe/sbt/packager/debian/DebianPlugin.scala index a3485ad8b..76a0d367d 100644 --- a/src/main/scala/com/typesafe/sbt/packager/debian/DebianPlugin.scala +++ b/src/main/scala/com/typesafe/sbt/packager/debian/DebianPlugin.scala @@ -111,8 +111,8 @@ trait DebianPlugin extends Plugin with linux.LinuxPlugin { chmod(cfile, "0644") cfile }, - debianExplodedPackage <<= (linuxPackageMappings, debianControlFile, debianMaintainerScripts, debianConffilesFile, debianControlScriptsReplacements, linuxPackageSymlinks, target) - map { (mappings, _, maintScripts, _, replacements, symlinks, t) => + debianExplodedPackage <<= (linuxPackageMappings, debianControlFile, debianMaintainerScripts, debianConffilesFile, debianControlScriptsReplacements, linuxPackageSymlinks, target, streams) + map { (mappings, _, maintScripts, _, replacements, symlinks, t, streams) => // Create files and directories mappings foreach { @@ -142,6 +142,32 @@ trait DebianPlugin extends Plugin with linux.LinuxPlugin { filterAndFixPerms(targetFile, replacements, LinuxFileMetaData()) } + // Check for non root user/group and append to postinst / postrm + // filter all root mappings, map to (user,group) key, group by, append everything + mappings filter { + case LinuxPackageMapping(_, LinuxFileMetaData("root", "root", _, _, _), _) => false + case _ => true + } map { + case LinuxPackageMapping(paths, LinuxFileMetaData(user, group, _, _, _), _) => (user, group) -> paths + } groupBy (_._1) foreach { + case ((user, group), pathList) => + streams.log info ("Altering postrm/postinst files to add user " + user + " and group " + group) + val postinst = t / "DEBIAN" / "postinst" + val postrm = t / "DEBIAN" / "postrm" + + val replacements = Seq("group" -> group, "user" -> user) + IO.append(postinst, TemplateWriter.generateScript(DebianPlugin.postinstGroupaddTemplateSource, replacements)) + IO.append(postinst, TemplateWriter.generateScript(DebianPlugin.postinstUseraddTemplateSource, replacements)) + + // remove key, flatten it and then go through each file + pathList.map(_._2).flatten foreach { + case (_, target) => + val pathReplacements = replacements :+ ("path" -> target.toString) + IO.append(postinst, TemplateWriter.generateScript(DebianPlugin.postinstChownTemplateSource, pathReplacements)) + } + + IO.append(postrm, TemplateWriter.generateScript(DebianPlugin.postrmPurgeTemplateSource, replacements)) + } t }, debianMD5sumsFile <<= (debianExplodedPackage, target) map { @@ -180,3 +206,10 @@ trait DebianPlugin extends Plugin with linux.LinuxPlugin { })) } + +object DebianPlugin { + private def postinstGroupaddTemplateSource: java.net.URL = getClass.getResource("postinst-groupadd") + private def postinstUseraddTemplateSource: java.net.URL = getClass.getResource("postinst-useradd") + private def postinstChownTemplateSource: java.net.URL = getClass.getResource("postinst-chown") + private def postrmPurgeTemplateSource: java.net.URL = getClass.getResource("postrm-purge") +}