Container restart policy specificiation.

Allow users to set container restart policy using environment variables.

RESIN_APP_RESTART_POLICY sets the name of the policy, and
if policy is "on-failure", optionally, RESIN_APP_RESTART_RETRIES
sets the maximum number of retries.

More information on docker docs:
https://docs.docker.com/engine/reference/run/#restart-policies-restart

One major change we introduce here is that the default policy is set
to always while we used to have the default "no".

We validate the arguments and pass retries parameter only for the case
of "on-failure" as specified in Docker API as of v1.19.
We could let docker handle the arguments directly, gaining
forwards-compatibility with any new features, but I opted
for an implementation that is as well-defined as possible.
This commit is contained in:
Alexios Theodoros Brezas 2016-03-28 12:53:51 +01:00 committed by Pablo Carranza Velez
parent a6466b93aa
commit e3f480c217

View File

@ -228,11 +228,13 @@ application.start = start = (app) ->
if portList?
portList.forEach (port) ->
ports[port + '/tcp'] = [ HostPort: port ]
restartPolicy = createRestartPolicy({ name: env['RESIN_APP_RESTART_POLICY'], maximumRetryCount: env['RESIN_APP_RESTART_RETRIES'] })
container.startAsync(
Privileged: true
NetworkMode: 'host'
PortBindings: ports
Binds: binds
RestartPolicy: restartPolicy
)
.catch (err) ->
statusCode = '' + err.statusCode
@ -261,6 +263,22 @@ application.start = start = (app) ->
.finally ->
device.updateState(status: 'Idle')
validRestartPolicies = [ 'no', 'always', 'on-failure', 'unless-stopped' ]
# Construct a restart policy based on its name and maximumRetryCount.
# Both arguments are optional, and the default policy is "always".
#
# Throws exception if an invalid policy name is given.
# Returns a RestartPolicy { Name, MaximumRetryCount } object
createRestartPolicy = ({ name, maximumRetryCount }) ->
if not name?
name = 'always'
if not (name in validRestartPolicies)
throw new Error("Invalid restart policy: #{name}")
policy = { Name: name }
if name is 'on-failure' and maximumRetryCount?
policy.MaximumRetryCount = maximumRetryCount
return policy
getEnvironment = do ->
envApiEndpoint = url.resolve(config.apiEndpoint, '/environment')