#
# CORE
# Copyright (c)2010-2012 the Boeing Company.
# See the LICENSE file included in this distribution.
#
''' Docker service.
'''

import os

from core.service import CoreService, addservice
from core.misc.ipaddr import IPv4Prefix, IPv6Prefix

class Docker(CoreService):
    ''' This is a service which will allow running docker containers in a Core node. 
    '''
    # a unique name is required, without spaces
    _name = "Docker"
    # you can create your own group here
    _group = "Utility"
    # list of other services this service depends on
    _depends = ()
    # per-node directories
    _dirs = ('/var/lib/docker/containers/', '/run/shm', '/run/resolvconf',)
    # generated files (without a full path this file goes in the node's dir,
    #  e.g. /tmp/pycore.12345/n1.conf/)
    _configs = ('docker.sh', )
    # this controls the starting order vs other enabled services
    _startindex = 50
    # list of startup commands, also may be generated during startup
    _startup = ('sh docker.sh',)
    # list of shutdown commands
    _shutdown = ('service docker.io stop')

    @classmethod
    def generateconfig(cls, node, filename, services):
        ''' Return a string that will be written to filename, or sent to the
            GUI for user customization.
        '''
        cfg = "#!/bin/sh\n"
        cfg += "# auto-generated by Docker (docker.py)\n"
        # Docker likes to think it has DNS set up or it complains. 
        # Unless your network was attached to the internet it is a bit pointless but hides error messages.
        cfg += 'echo "nameserver 8.8.8.8" >/etc/resolv.conf\n'
        # Starts the docker service. In Ubuntu this is docker.io in others distros may just be docker
        cfg += 'service docker.io start\n'
        cfg += "# you could add a command to start a container here eg:\n"
        cfg += "# docker run -d -p 5000:5000 training/webapp python app.py\n"
        return cfg

# this line is required to add the above class to the list of available services
addservice(Docker)