Skip to content

Commit 6092cad

Browse files
committed
Merge pull request #84 from muuki88/master
Implemented chown file permissions and user/group creation
2 parents 8843f58 + 023223c commit 6092cad

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
chown ${{user}}:${{group}} ${{path}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Adding ${{group}} as system group
2+
if ! getent group ${{group}} > /dev/null 2>&1
3+
then
4+
echo "Creating system group: ${{group}}"
5+
addgroup --system ${{group}}
6+
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Adding ${{user}}
2+
if ! id -u $1 >/dev/null 2>&1; then
3+
echo "Creating user ${{user}} in group ${{group}}"
4+
useradd --system --no-create-home --gid ${{group}} --shell /bin/false ${{user}}
5+
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Deleting user: ${{user}} and group: ${{group}}
2+
case "$1" in
3+
remove|failed-upgrade|abort-upgrade|abort-install|disappear)
4+
;;
5+
purge)
6+
deluser --quiet --system ${{user}} > /dev/null || true
7+
delgroup --quiet --system ${{group}} > /dev/null || true
8+
;;
9+
upgrade)
10+
;;
11+
*)
12+
echo "postinst called with unknown argument \`\$1'" >&2
13+
;;
14+
esac

src/main/scala/com/typesafe/sbt/packager/debian/DebianPlugin.scala

+35-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ trait DebianPlugin extends Plugin with linux.LinuxPlugin {
111111
chmod(cfile, "0644")
112112
cfile
113113
},
114-
debianExplodedPackage <<= (linuxPackageMappings, debianControlFile, debianMaintainerScripts, debianConffilesFile, debianControlScriptsReplacements, linuxPackageSymlinks, target)
115-
map { (mappings, _, maintScripts, _, replacements, symlinks, t) =>
114+
debianExplodedPackage <<= (linuxPackageMappings, debianControlFile, debianMaintainerScripts, debianConffilesFile, debianControlScriptsReplacements, linuxPackageSymlinks, target, streams)
115+
map { (mappings, _, maintScripts, _, replacements, symlinks, t, streams) =>
116116

117117
// Create files and directories
118118
mappings foreach {
@@ -142,6 +142,32 @@ trait DebianPlugin extends Plugin with linux.LinuxPlugin {
142142
filterAndFixPerms(targetFile, replacements, LinuxFileMetaData())
143143
}
144144

145+
// Check for non root user/group and append to postinst / postrm
146+
// filter all root mappings, map to (user,group) key, group by, append everything
147+
mappings filter {
148+
case LinuxPackageMapping(_, LinuxFileMetaData("root", "root", _, _, _), _) => false
149+
case _ => true
150+
} map {
151+
case LinuxPackageMapping(paths, LinuxFileMetaData(user, group, _, _, _), _) => (user, group) -> paths
152+
} groupBy (_._1) foreach {
153+
case ((user, group), pathList) =>
154+
streams.log info ("Altering postrm/postinst files to add user " + user + " and group " + group)
155+
val postinst = t / "DEBIAN" / "postinst"
156+
val postrm = t / "DEBIAN" / "postrm"
157+
158+
val replacements = Seq("group" -> group, "user" -> user)
159+
IO.append(postinst, TemplateWriter.generateScript(DebianPlugin.postinstGroupaddTemplateSource, replacements))
160+
IO.append(postinst, TemplateWriter.generateScript(DebianPlugin.postinstUseraddTemplateSource, replacements))
161+
162+
// remove key, flatten it and then go through each file
163+
pathList.map(_._2).flatten foreach {
164+
case (_, target) =>
165+
val pathReplacements = replacements :+ ("path" -> target.toString)
166+
IO.append(postinst, TemplateWriter.generateScript(DebianPlugin.postinstChownTemplateSource, pathReplacements))
167+
}
168+
169+
IO.append(postrm, TemplateWriter.generateScript(DebianPlugin.postrmPurgeTemplateSource, replacements))
170+
}
145171
t
146172
},
147173
debianMD5sumsFile <<= (debianExplodedPackage, target) map {
@@ -180,3 +206,10 @@ trait DebianPlugin extends Plugin with linux.LinuxPlugin {
180206
}))
181207

182208
}
209+
210+
object DebianPlugin {
211+
private def postinstGroupaddTemplateSource: java.net.URL = getClass.getResource("postinst-groupadd")
212+
private def postinstUseraddTemplateSource: java.net.URL = getClass.getResource("postinst-useradd")
213+
private def postinstChownTemplateSource: java.net.URL = getClass.getResource("postinst-chown")
214+
private def postrmPurgeTemplateSource: java.net.URL = getClass.getResource("postrm-purge")
215+
}

0 commit comments

Comments
 (0)