Skip to content

Commit

Permalink
Merge pull request #84 from muuki88/master
Browse files Browse the repository at this point in the history
Implemented chown file permissions and user/group creation
  • Loading branch information
jsuereth committed Nov 25, 2013
2 parents 8843f58 + 023223c commit 6092cad
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
chown ${{user}}:${{group}} ${{path}}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions src/main/resources/com/typesafe/sbt/packager/debian/postrm-purge
Original file line number Diff line number Diff line change
@@ -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
37 changes: 35 additions & 2 deletions src/main/scala/com/typesafe/sbt/packager/debian/DebianPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}

0 comments on commit 6092cad

Please sign in to comment.