github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/service/agent.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package service 5 6 import ( 7 "fmt" 8 9 "github.com/juju/errors" 10 "github.com/juju/utils/shell" 11 12 "github.com/juju/juju/juju/osenv" 13 "github.com/juju/juju/service/common" 14 ) 15 16 const ( 17 maxAgentFiles = 20000 18 19 agentServiceTimeout = 300 // 5 minutes 20 ) 21 22 // AgentConf returns the data that defines an init service config 23 // for the identified agent. 24 func AgentConf(info AgentInfo, renderer shell.Renderer) common.Conf { 25 conf := common.Conf{ 26 Desc: fmt.Sprintf("juju agent for %s", info.name), 27 ExecStart: info.cmd(renderer), 28 Logfile: info.logFile(renderer), 29 Env: osenv.FeatureFlags(), 30 Timeout: agentServiceTimeout, 31 ServiceBinary: info.jujud(renderer), 32 ServiceArgs: info.execArgs(renderer), 33 } 34 35 switch info.Kind { 36 case AgentKindMachine: 37 conf.Limit = map[string]int{ 38 "nofile": maxAgentFiles, 39 } 40 case AgentKindUnit: 41 conf.Desc = "juju unit agent for " + info.ID 42 } 43 44 return conf 45 } 46 47 // TODO(ericsnow) Eliminate ContainerAgentConf once it is no longer 48 // used in worker/deployer/simple.go. 49 50 // ContainerAgentConf returns the data that defines an init service config 51 // for the identified agent running in a container. 52 func ContainerAgentConf(info AgentInfo, renderer shell.Renderer, containerType string) common.Conf { 53 conf := AgentConf(info, renderer) 54 55 // TODO(thumper): 2013-09-02 bug 1219630 56 // As much as I'd like to remove JujuContainerType now, it is still 57 // needed as MAAS still needs it at this stage, and we can't fix 58 // everything at once. 59 envVars := map[string]string{ 60 osenv.JujuContainerTypeEnvKey: containerType, 61 } 62 osenv.MergeEnvironment(envVars, conf.Env) 63 conf.Env = envVars 64 65 return conf 66 } 67 68 // ShutdownAfterConf builds a service conf that will cause the host to 69 // shut down after the named service stops. 70 func ShutdownAfterConf(serviceName string) (common.Conf, error) { 71 if serviceName == "" { 72 return common.Conf{}, errors.New(`missing "after" service name`) 73 } 74 desc := "juju shutdown job" 75 return shutdownAfterConf(serviceName, desc), nil 76 } 77 78 func shutdownAfterConf(serviceName, desc string) common.Conf { 79 return common.Conf{ 80 Desc: desc, 81 Transient: true, 82 AfterStopped: serviceName, 83 ExecStart: "/sbin/shutdown -h now", 84 } 85 }