Skip to content

Commit

Permalink
Merge pull request #262 from sghill/configurable-install-cmd
Browse files Browse the repository at this point in the history
Extract LegacyInstallCmd, Add installCmd to Daemon Definition
  • Loading branch information
sghill authored Oct 10, 2017
2 parents 7fc180d + ac77d28 commit b4ed064
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ class DaemonDefinition {
Boolean autoStart // default true
Integer startSequence // default 85
Integer stopSequence // default 15
String installCmd
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.netflix.gradle.plugins.daemon

class LegacyInstallCmd {
static String create(Map<String, Object> ctx) {
if (ctx.isRedHat) {
"/sbin/chkconfig ${ctx.daemonName} on"
} else {
def startRunLevels = ctx.runLevels.join(' ')
def stopRunLevels = ([0, 1, 2, 3, 4, 5, 6] - ctx.runLevels).join(' ')
"/usr/sbin/update-rc.d ${ctx.daemonName} start ${ctx.startSequence} ${startRunLevels} . stop ${ctx.stopSequence} ${stopRunLevels} ."
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,13 @@ class OspackageDaemonPlugin implements Plugin<Project> {
}

task.doFirst {
def startSequence = templateTask.getContext().startSequence
def stopSequence = templateTask.getContext().stopSequence
def runLevels = templateTask.getContext().runLevels

task.postInstall("[ -x /bin/touch ] && touch=/bin/touch || touch=/usr/bin/touch")
task.postInstall("\$touch /service/${daemonName}/down")
def ctx = templateTask.getContext()

def installCmd = isRedhat?
"/sbin/chkconfig ${daemonName} on":
"/usr/sbin/update-rc.d ${daemonName} start ${startSequence} ${runLevels.join(' ')} . stop ${stopSequence} ${([0,1,2,3,4,5,6]-runLevels).join(' ')} ."
def installCmd = definition.installCmd ?: LegacyInstallCmd.create(ctx)

if (templateTask.getContext().autoStart) {
if (ctx.autoStart) {
task.postInstall(installCmd)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class DaemonExtensionSpec extends Specification {
autoStart = false
startSequence = 85
stopSequence = 15
installCmd = 'exit 2'
}

then:
Expand All @@ -51,5 +52,6 @@ class DaemonExtensionSpec extends Specification {
!definition.autoStart
definition.startSequence == 85
definition.stopSequence == 15
definition.installCmd == 'exit 2'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.netflix.gradle.plugins.daemon

import spock.lang.Specification

class LegacyInstallCmdSpec extends Specification {
def 'should call chkconfig if redhat'() {
when:
String actual = LegacyInstallCmd.create(isRedHat: true, daemonName: 'ABC')

then:
actual == "/sbin/chkconfig ABC on"
}

def 'should call update-rc.d if not redhat'() {
when:
String actual = LegacyInstallCmd.create(isRedHat: false, daemonName: 'ABC', runLevels: [3, 4, 5], startSequence: 20, stopSequence: 21)

then:
actual == "/usr/sbin/update-rc.d ABC start 20 3 4 5 . stop 21 0 1 2 6 ."
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,29 @@ class OspackageDaemonPluginLauncherSpec extends IntegrationSpec {
files.any { it == './etc/rc.d/init.d/foobaz' }
}

def 'override install cmd'() {
File srcDir = new File(projectDir, 'src')
srcDir.mkdirs()
buildFile << """
${applyPlugin(OspackageDaemonPlugin)}
${applyPlugin(SystemPackagingPlugin)}
daemon {
daemonName = "foobaz" // default = packageName
command = "sleep infinity" // required
installCmd = "sleep 30"
}""".stripIndent()

when:
runTasksSuccessfully('buildDeb')

then:
def archivePath = file("build/distributions/${moduleName}_unspecified_all.deb")
def scan = new com.netflix.gradle.plugins.deb.Scanner(archivePath)

scan.controlContents.containsKey('./postinst')
def postInst = scan.controlContents['./postinst']
postInst.contains("sleep 30")
!postInst.contains("/usr/sbin/update-rc.d")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,16 @@ class OspackageDaemonPluginSpec extends PluginProjectSpec {
project.daemon {
daemonName = 'foobar'
command = 'exit 0'
installCmd = 'abc'
}

then: 'daemon was added to list'
!extension.daemons.isEmpty()

then: 'daemon configurate'
extension.daemons.iterator().next().daemonName == 'foobar'
def daemon = extension.daemons.iterator().next()
daemon.daemonName == 'foobar'
daemon.installCmd == 'abc'
}

def 'can call daemons extensions in project'() {
Expand Down

0 comments on commit b4ed064

Please sign in to comment.