Exemplo n.º 1
0
    def __executeCommand__(command, cwd, pidfile):
        p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd)

        response = ''
        for line in p.stdout.readlines():
            response+=line+os.linesep

        createFile(pidfile, str(p.pid))

        return p.wait()
Exemplo n.º 2
0
    def buildThread():
        cwd =  os.path.join(settings.FS_BUILDS, token)
        command = 'r10k puppetfile install 1> '+ settings.FS_DEF_CONTEXT_LOG +' 2> '+ settings.FS_DEF_CONTEXT_ERR_LOG
        p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd)

        response = ''
        for line in p.stdout.readlines():
            response+=line+os.linesep

        createFile(os.path.join(cwd, settings.FS_DEF_CONTEXT_PID), str(p.pid))

        retval = p.wait()
Exemplo n.º 3
0
    def __executeCommand__(command, cwd, pidfile):
        p = subprocess.Popen(command,
                             shell=True,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT,
                             cwd=cwd)

        response = ''
        for line in p.stdout.readlines():
            response += line + os.linesep

        createFile(pidfile, str(p.pid))

        return p.wait()
Exemplo n.º 4
0
    def buildThread():
        cwd = os.path.join(settings.FS_BUILDS, token)
        command = 'r10k puppetfile install 1> ' + settings.FS_DEF_CONTEXT_LOG + ' 2> ' + settings.FS_DEF_CONTEXT_ERR_LOG
        p = subprocess.Popen(command,
                             shell=True,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT,
                             cwd=cwd)

        response = ''
        for line in p.stdout.readlines():
            response += line + os.linesep

        createFile(os.path.join(cwd, settings.FS_DEF_CONTEXT_PID), str(p.pid))

        retval = p.wait()
Exemplo n.º 5
0
    def __buildThread__():
        err = None
        cwd = os.path.join(settings.FS_BASES, name)

        dockerfilepath = cwd

        pidfile = os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_PID)

        try:
            daemon_opts = optionsFromClient(dk)
            tag = settings.DK_DEFAULT_BASE_PROVIDER + '/' + name

            # build
            if err is None:
                command = 'docker '+daemon_opts+' build -f '+ dockerfilepath+'/'+ settings.FS_DEF_DOCKERFILE +' -t '+ tag +' . ' + '1> '+ os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_LOG) +' 2> '+ os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_ERR_LOG)

                if __executeCommand__(command, cwd, pidfile)!=0:
                    err = "Error while building"
                else:
                    createFile(os.path.join(dockerfilepath, "build_command"), command)

            if settings.DK_RG_SWITCH:
                # tag
                if err is None:
                    command = 'docker '+daemon_opts+' tag ' + tag +' '+ settings.DK_RG_ENDPOINT +'/'+ tag
                    if __executeCommand__(command, cwd, pidfile)!=0:
                        err = "Error tagging image. Maybe an image with the same name for this group already exists."
                    else:
                        createFile(os.path.join(dockerfilepath, "tag_command"), command)

                # push
                if err is None:
                    command = 'docker '+daemon_opts+' push ' + settings.DK_RG_ENDPOINT +'/'+ tag
                    if __executeCommand__(command, cwd, pidfile)!=0:
                        err = "Error while pushing to registry"
                    else:
                        createFile(os.path.join(dockerfilepath, "push_command"), command)

            # Error case
            if err is not None:
                deleteImage(datastore, imageToken)
                createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_ERR_LOG), err)
                createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_FLAG), '1')
            else:
                createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_FLAG), '0')

        except Exception, e:
            createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_ERR_LOG), "Unexpected error in build thread."+str(e))
            createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_FLAG), '1')
Exemplo n.º 6
0
    def __buildThread__():
        err = None

        cwd =  os.path.join(settings.FS_BUILDS, contextToken)

        dockerfilepath = cwd
        dockerfilepath = os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_IMAGES_FOLDER)
        dockerfilepath = os.path.join(dockerfilepath, imageName)

        pidfile = os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_PID)
        try:
            daemon_opts = optionsFromClient(dk)

            # extract base name from dockerfile
            if err is None:
                if settings.DK_RG_SWITCH:
                    dkfRgEnd, dkfProv, dkfName = __extractBaseImageName__(dockerfilepath)
                    if not dkfName:
                        err = "Cannot extract image name from Dockerfile"
                    else:
                        pass

            # Pull image from registry
            if err is None:
                # if registry specified, pull from that. If not, try private reg.
                if dkfProv:
                    completeName = dkfProv+"/"+dkfName
                else:
                    completeName = dkfName
                if settings.DK_RG_SWITCH:
                    output = None
                    try:
                        if not dkfRgEnd and not settings.ENABLE_PULL_FROM_PUBLIC:
                            dkfRgEnd = settings.DK_RG_ENDPOINT
                        output = pullImage(completeName, registry=dkfRgEnd, dk=dk, imageFolder=dockerfilepath)
                        # re-tag pulled image
                        if output:
                            if not tagImage(dkfRgEnd+'/'+completeName, completeName, dk=dk, imageFolder=dockerfilepath):
                                err = "Cannot tag '"+completeName+"'."
                    except Exception, e:
                        pass

                    if dkfRgEnd and dkfRgEnd!=settings.DK_RG_ENDPOINT and not output and not err:
                        err = "Cannot pull '"+completeName+"' from registry '"+dkfRgEnd+"'"

                # if not, try with public registry
                # if err is None and not output:
                #     try:
                #         output = pullImage(completeName, registry=None, dk=dk, imageFolder=dockerfilepath)
                #     except Exception, e:
                #         pass
                #
                #     if not output:
                #         err = "Cannot pull '"+completeName+"' from public nor private registry."


            # build
            if err is None:

                command = 'docker '+daemon_opts+' build -f '+ dockerfilepath+ '/'+ settings.FS_DEF_DOCKERFILE +' -t '+ datastore.getImage(imageToken)['tag'] +' . ' + '1> '+ os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_LOG) +' 2> '+ os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_ERR_LOG)

                if __executeCommand__(command, cwd, pidfile)!=0:
                    err = "Error while building"
                else:
                    createFile(os.path.join(dockerfilepath, "build_command"), command)

            if settings.DK_RG_SWITCH:
                # tag
                if err is None:
                    command = 'docker '+daemon_opts+' tag ' + datastore.getImage(imageToken)['tag']+' '+settings.DK_RG_ENDPOINT+'/'+datastore.getImage(imageToken)['tag']
                    if __executeCommand__(command, cwd, pidfile)!=0:
                        err = "Error tagging image. Maybe an image with the same name for this group already exists."
                    else:
                        pass
                    createFile(os.path.join(dockerfilepath, "tag_command"), command)

                # push
                if err is None:
                    command = 'docker '+daemon_opts+' push ' + settings.DK_RG_ENDPOINT+'/'+datastore.getImage(imageToken)['tag']
                    if __executeCommand__(command, cwd, pidfile)!=0:
                        err = "Error while pushing to registry"
                    else:
                        createFile(os.path.join(dockerfilepath, "push_command"), command)

            # Error case
            if err is not None:
                deleteImage(datastore, imageToken)
                createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_ERR_LOG), err)
                createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_FLAG), '1')
            else:
                createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_FLAG), '0')
Exemplo n.º 7
0
def buildImage(datastore, contextToken, imageName, imageToken, dk=defaultDockerClient):
    # launch build
    # TODO: In the future, replace commandline docker API with docker-py client. The docker-py api for build do not support context path different from Dockerfile path.
    def __executeCommand__(command, cwd, pidfile):
        p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd)

        response = ''
        for line in p.stdout.readlines():
            response+=line+os.linesep

        createFile(pidfile, str(p.pid))

        return p.wait()

    def __extractBaseImageName__(dockerfilepath):
        """
        Returns the name after the 'FROM' directive in the dockerfile (registry endpoint + provider + image name). Raises exception if not possible to extract the name.
        """
        res = None
        regEndpoint, provider, name = None, None, None
        with open(os.path.join(dockerfilepath, settings.FS_DEF_DOCKERFILE), "r") as f:
            for line in f:
                try:
                    res = re.search(r'^FROM\s*(.*)$', line).group(1)
                except AttributeError:
                    continue
                break
            f.close()

        if res:
            splitted = res.split("/")
            i = 0
            if len(splitted) == 3:
                regEndpoint=''.join(e for e in splitted[i] if (e.isalnum() or e == ':' or e == '.'))
                i = i+1
            if len(splitted) >= 2:
                provider=''.join(e for e in splitted[i] if e.isalnum())
                i = i+1
            name=''.join(e for e in splitted[i] if (e.isalnum() or e == ':' or e == '.'))
        return regEndpoint, provider, name

    def __buildThread__():
        err = None

        cwd =  os.path.join(settings.FS_BUILDS, contextToken)

        dockerfilepath = cwd
        dockerfilepath = os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_IMAGES_FOLDER)
        dockerfilepath = os.path.join(dockerfilepath, imageName)

        pidfile = os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_PID)
        try:
            daemon_opts = optionsFromClient(dk)

            # extract base name from dockerfile
            if err is None:
                if settings.DK_RG_SWITCH:
                    dkfRgEnd, dkfProv, dkfName = __extractBaseImageName__(dockerfilepath)
                    if not dkfName:
                        err = "Cannot extract image name from Dockerfile"
                    else:
                        pass

            # Pull image from registry
            if err is None:
                # if registry specified, pull from that. If not, try private reg.
                if dkfProv:
                    completeName = dkfProv+"/"+dkfName
                else:
                    completeName = dkfName
                if settings.DK_RG_SWITCH:
                    output = None
                    try:
                        if not dkfRgEnd and not settings.ENABLE_PULL_FROM_PUBLIC:
                            dkfRgEnd = settings.DK_RG_ENDPOINT
                        output = pullImage(completeName, registry=dkfRgEnd, dk=dk, imageFolder=dockerfilepath)
                        # re-tag pulled image
                        if output:
                            if not tagImage(dkfRgEnd+'/'+completeName, completeName, dk=dk, imageFolder=dockerfilepath):
                                err = "Cannot tag '"+completeName+"'."
                    except Exception, e:
                        pass

                    if dkfRgEnd and dkfRgEnd!=settings.DK_RG_ENDPOINT and not output and not err:
                        err = "Cannot pull '"+completeName+"' from registry '"+dkfRgEnd+"'"

                # if not, try with public registry
                # if err is None and not output:
                #     try:
                #         output = pullImage(completeName, registry=None, dk=dk, imageFolder=dockerfilepath)
                #     except Exception, e:
                #         pass
                #
                #     if not output:
                #         err = "Cannot pull '"+completeName+"' from public nor private registry."


            # build
            if err is None:

                command = 'docker '+daemon_opts+' build -f '+ dockerfilepath+ '/'+ settings.FS_DEF_DOCKERFILE +' -t '+ datastore.getImage(imageToken)['tag'] +' . ' + '1> '+ os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_LOG) +' 2> '+ os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_ERR_LOG)

                if __executeCommand__(command, cwd, pidfile)!=0:
                    err = "Error while building"
                else:
                    createFile(os.path.join(dockerfilepath, "build_command"), command)

            if settings.DK_RG_SWITCH:
                # tag
                if err is None:
                    command = 'docker '+daemon_opts+' tag ' + datastore.getImage(imageToken)['tag']+' '+settings.DK_RG_ENDPOINT+'/'+datastore.getImage(imageToken)['tag']
                    if __executeCommand__(command, cwd, pidfile)!=0:
                        err = "Error tagging image. Maybe an image with the same name for this group already exists."
                    else:
                        pass
                    createFile(os.path.join(dockerfilepath, "tag_command"), command)

                # push
                if err is None:
                    command = 'docker '+daemon_opts+' push ' + settings.DK_RG_ENDPOINT+'/'+datastore.getImage(imageToken)['tag']
                    if __executeCommand__(command, cwd, pidfile)!=0:
                        err = "Error while pushing to registry"
                    else:
                        createFile(os.path.join(dockerfilepath, "push_command"), command)

            # Error case
            if err is not None:
                deleteImage(datastore, imageToken)
                createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_ERR_LOG), err)
                createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_FLAG), '1')
            else:
                createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_FLAG), '0')


        except Exception, e:
            createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_ERR_LOG), "Unexpected error in build thread.\n"+str(e))
            createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_FLAG), '1')
Exemplo n.º 8
0
                    if __executeCommand__(command, cwd, pidfile) != 0:
                        err = "Error while pushing to registry"
                    else:
                        createFile(os.path.join(dockerfilepath, "push_command"), command)

            # Error case
            if err is not None:
                deleteImage(datastore, imageToken)
                createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_ERR_LOG), err)
                createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_FLAG), "1")
            else:
                createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_FLAG), "0")

        except Exception, e:
            createFile(
                os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_ERR_LOG),
                "Unexpected error in build thread.\n" + str(e),
            )
            createFile(os.path.join(dockerfilepath, settings.FS_DEF_DOCKER_BUILD_FLAG), "1")

    thread = Thread(target=__buildThread__)
    thread.start()


def deleteImage(datastore, imageToken, dk=defaultDockerClient):
    try:
        daemon_opts = optionsFromClient(dk)
        # get tag of the docker image
        image = datastore.getImage(imageToken)
        context = datastore.getContext(image["context"])
        dockerImageTag = context["group"] + "/" + image["imageName"].lower()
Exemplo n.º 9
0
    def __buildThread__():
        err = None
        cwd = os.path.join(settings.FS_BASES, name)

        dockerfilepath = cwd

        pidfile = os.path.join(dockerfilepath,
                               settings.FS_DEF_DOCKER_BUILD_PID)

        try:
            daemon_opts = optionsFromClient(dk)
            tag = settings.DK_DEFAULT_BASE_PROVIDER + '/' + name

            # build
            if err is None:
                command = 'docker ' + daemon_opts + ' build -f ' + dockerfilepath + '/' + settings.FS_DEF_DOCKERFILE + ' -t ' + tag + ' . ' + '1> ' + os.path.join(
                    dockerfilepath,
                    settings.FS_DEF_DOCKER_BUILD_LOG) + ' 2> ' + os.path.join(
                        dockerfilepath, settings.FS_DEF_DOCKER_BUILD_ERR_LOG)

                if __executeCommand__(command, cwd, pidfile) != 0:
                    err = "Error while building"
                else:
                    createFile(os.path.join(dockerfilepath, "build_command"),
                               command)

            if settings.DK_RG_SWITCH:
                # tag
                if err is None:
                    command = 'docker ' + daemon_opts + ' tag ' + tag + ' ' + settings.DK_RG_ENDPOINT + '/' + tag
                    if __executeCommand__(command, cwd, pidfile) != 0:
                        err = "Error tagging image. Maybe an image with the same name for this group already exists."
                    else:
                        createFile(os.path.join(dockerfilepath, "tag_command"),
                                   command)

                # push
                if err is None:
                    command = 'docker ' + daemon_opts + ' push ' + settings.DK_RG_ENDPOINT + '/' + tag
                    if __executeCommand__(command, cwd, pidfile) != 0:
                        err = "Error while pushing to registry"
                    else:
                        createFile(
                            os.path.join(dockerfilepath, "push_command"),
                            command)

            # Error case
            if err is not None:
                deleteImage(datastore, imageToken)
                appendInFile(
                    os.path.join(dockerfilepath,
                                 settings.FS_DEF_DOCKER_BUILD_ERR_LOG), err)
                createFile(
                    os.path.join(dockerfilepath,
                                 settings.FS_DEF_DOCKER_BUILD_FLAG), '1')
            else:
                createFile(
                    os.path.join(dockerfilepath,
                                 settings.FS_DEF_DOCKER_BUILD_FLAG), '0')

        except Exception, e:
            appendInFile(
                os.path.join(dockerfilepath,
                             settings.FS_DEF_DOCKER_BUILD_ERR_LOG),
                "Unexpected error in build thread." + str(e))
            createFile(
                os.path.join(dockerfilepath,
                             settings.FS_DEF_DOCKER_BUILD_FLAG), '1')
Exemplo n.º 10
0
    def __buildThread__():
        err = None

        cwd = os.path.join(settings.FS_BUILDS, contextToken)

        dockerfilepath = cwd
        dockerfilepath = os.path.join(dockerfilepath,
                                      settings.FS_DEF_DOCKER_IMAGES_FOLDER)
        dockerfilepath = os.path.join(dockerfilepath, imageName)

        pidfile = os.path.join(dockerfilepath,
                               settings.FS_DEF_DOCKER_BUILD_PID)
        try:
            daemon_opts = optionsFromClient(dk)

            # extract base name from dockerfile
            if err is None:
                if settings.DK_RG_SWITCH:
                    dkfRgEnd, dkfProv, dkfName = __extractBaseImageName__(
                        dockerfilepath)
                    if not dkfName:
                        err = "Cannot extract image name from Dockerfile"
                    else:
                        pass

            # Pull image from registry
            if err is None:
                # if registry specified, pull from that. If not, try private reg.
                if dkfProv:
                    completeName = dkfProv + "/" + dkfName
                else:
                    completeName = dkfName
                if settings.DK_RG_SWITCH:
                    output = None
                    try:
                        if not dkfRgEnd and not settings.ENABLE_PULL_FROM_PUBLIC:
                            dkfRgEnd = settings.DK_RG_ENDPOINT
                        output = pullImage(completeName,
                                           registry=dkfRgEnd,
                                           dk=dk,
                                           imageFolder=dockerfilepath)
                        # re-tag pulled image
                        if output:
                            if not tagImage(dkfRgEnd + '/' + completeName,
                                            completeName,
                                            dk=dk,
                                            imageFolder=dockerfilepath):
                                err = "Cannot tag '" + completeName + "'."
                    except Exception, e:
                        pass

                    if dkfRgEnd and dkfRgEnd != settings.DK_RG_ENDPOINT and not output and not err:
                        err = "Cannot pull '" + completeName + "' from registry '" + dkfRgEnd + "'"

                # if not, try with public registry
                # if err is None and not output:
                #     try:
                #         output = pullImage(completeName, registry=None, dk=dk, imageFolder=dockerfilepath)
                #     except Exception, e:
                #         pass
                #
                #     if not output:
                #         err = "Cannot pull '"+completeName+"' from public nor private registry."

            # build
            if err is None:

                command = 'docker ' + daemon_opts + ' build -f ' + dockerfilepath + '/' + settings.FS_DEF_DOCKERFILE + ' -t ' + datastore.getImage(
                    imageToken)['tag'] + ' . ' + '1> ' + os.path.join(
                        dockerfilepath, settings.FS_DEF_DOCKER_BUILD_LOG
                    ) + ' 2> ' + os.path.join(
                        dockerfilepath, settings.FS_DEF_DOCKER_BUILD_ERR_LOG)

                if __executeCommand__(command, cwd, pidfile) != 0:
                    err = "Error while building"
                else:
                    createFile(os.path.join(dockerfilepath, "build_command"),
                               command)

            if settings.DK_RG_SWITCH:
                # tag
                if err is None:
                    command = 'docker ' + daemon_opts + ' tag ' + datastore.getImage(
                        imageToken
                    )['tag'] + ' ' + settings.DK_RG_ENDPOINT + '/' + datastore.getImage(
                        imageToken)['tag']
                    if __executeCommand__(command, cwd, pidfile) != 0:
                        err = "Error tagging image. Maybe an image with the same name for this group already exists."
                    else:
                        pass
                    createFile(os.path.join(dockerfilepath, "tag_command"),
                               command)

                # push
                if err is None:
                    command = 'docker ' + daemon_opts + ' push ' + settings.DK_RG_ENDPOINT + '/' + datastore.getImage(
                        imageToken)['tag']
                    if __executeCommand__(command, cwd, pidfile) != 0:
                        err = "Error while pushing to registry"
                    else:
                        createFile(
                            os.path.join(dockerfilepath, "push_command"),
                            command)

            # Error case
            if err is not None:
                deleteImage(datastore, imageToken)
                appendInFile(
                    os.path.join(dockerfilepath,
                                 settings.FS_DEF_DOCKER_BUILD_ERR_LOG), err)
                createFile(
                    os.path.join(dockerfilepath,
                                 settings.FS_DEF_DOCKER_BUILD_FLAG), '1')
            else:
                createFile(
                    os.path.join(dockerfilepath,
                                 settings.FS_DEF_DOCKER_BUILD_FLAG), '0')
Exemplo n.º 11
0
def buildImage(datastore,
               contextToken,
               imageName,
               imageToken,
               dk=defaultDockerClient):
    # launch build
    # TODO: In the future, replace commandline docker API with docker-py client. The docker-py api for build do not support context path different from Dockerfile path.
    def __executeCommand__(command, cwd, pidfile):
        p = subprocess.Popen(command,
                             shell=True,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT,
                             cwd=cwd)

        response = ''
        for line in p.stdout.readlines():
            response += line + os.linesep

        createFile(pidfile, str(p.pid))

        return p.wait()

    def __extractBaseImageName__(dockerfilepath):
        """
        Returns the name after the 'FROM' directive in the dockerfile (registry endpoint + provider + image name). Raises exception if not possible to extract the name.
        """
        res = None
        regEndpoint, provider, name = None, None, None
        with open(os.path.join(dockerfilepath, settings.FS_DEF_DOCKERFILE),
                  "r") as f:
            for line in f:
                try:
                    res = re.search(r'^FROM\s*(.*)$', line).group(1)
                except AttributeError:
                    continue
                break
            f.close()

        if res:
            splitted = res.split("/")
            i = 0
            if len(splitted) == 3:
                regEndpoint = ''.join(e for e in splitted[i]
                                      if (e.isalnum() or e == ':' or e == '.'))
                i = i + 1
            if len(splitted) >= 2:
                provider = ''.join(e for e in splitted[i] if e.isalnum())
                i = i + 1
            name = ''.join(e for e in splitted[i]
                           if (e.isalnum() or e == ':' or e == '.'))
        return regEndpoint, provider, name

    def __buildThread__():
        err = None

        cwd = os.path.join(settings.FS_BUILDS, contextToken)

        dockerfilepath = cwd
        dockerfilepath = os.path.join(dockerfilepath,
                                      settings.FS_DEF_DOCKER_IMAGES_FOLDER)
        dockerfilepath = os.path.join(dockerfilepath, imageName)

        pidfile = os.path.join(dockerfilepath,
                               settings.FS_DEF_DOCKER_BUILD_PID)
        try:
            daemon_opts = optionsFromClient(dk)

            # extract base name from dockerfile
            if err is None:
                if settings.DK_RG_SWITCH:
                    dkfRgEnd, dkfProv, dkfName = __extractBaseImageName__(
                        dockerfilepath)
                    if not dkfName:
                        err = "Cannot extract image name from Dockerfile"
                    else:
                        pass

            # Pull image from registry
            if err is None:
                # if registry specified, pull from that. If not, try private reg.
                if dkfProv:
                    completeName = dkfProv + "/" + dkfName
                else:
                    completeName = dkfName
                if settings.DK_RG_SWITCH:
                    output = None
                    try:
                        if not dkfRgEnd and not settings.ENABLE_PULL_FROM_PUBLIC:
                            dkfRgEnd = settings.DK_RG_ENDPOINT
                        output = pullImage(completeName,
                                           registry=dkfRgEnd,
                                           dk=dk,
                                           imageFolder=dockerfilepath)
                        # re-tag pulled image
                        if output:
                            if not tagImage(dkfRgEnd + '/' + completeName,
                                            completeName,
                                            dk=dk,
                                            imageFolder=dockerfilepath):
                                err = "Cannot tag '" + completeName + "'."
                    except Exception, e:
                        pass

                    if dkfRgEnd and dkfRgEnd != settings.DK_RG_ENDPOINT and not output and not err:
                        err = "Cannot pull '" + completeName + "' from registry '" + dkfRgEnd + "'"

                # if not, try with public registry
                # if err is None and not output:
                #     try:
                #         output = pullImage(completeName, registry=None, dk=dk, imageFolder=dockerfilepath)
                #     except Exception, e:
                #         pass
                #
                #     if not output:
                #         err = "Cannot pull '"+completeName+"' from public nor private registry."

            # build
            if err is None:

                command = 'docker ' + daemon_opts + ' build -f ' + dockerfilepath + '/' + settings.FS_DEF_DOCKERFILE + ' -t ' + datastore.getImage(
                    imageToken)['tag'] + ' . ' + '1> ' + os.path.join(
                        dockerfilepath, settings.FS_DEF_DOCKER_BUILD_LOG
                    ) + ' 2> ' + os.path.join(
                        dockerfilepath, settings.FS_DEF_DOCKER_BUILD_ERR_LOG)

                if __executeCommand__(command, cwd, pidfile) != 0:
                    err = "Error while building"
                else:
                    createFile(os.path.join(dockerfilepath, "build_command"),
                               command)

            if settings.DK_RG_SWITCH:
                # tag
                if err is None:
                    command = 'docker ' + daemon_opts + ' tag ' + datastore.getImage(
                        imageToken
                    )['tag'] + ' ' + settings.DK_RG_ENDPOINT + '/' + datastore.getImage(
                        imageToken)['tag']
                    if __executeCommand__(command, cwd, pidfile) != 0:
                        err = "Error tagging image. Maybe an image with the same name for this group already exists."
                    else:
                        pass
                    createFile(os.path.join(dockerfilepath, "tag_command"),
                               command)

                # push
                if err is None:
                    command = 'docker ' + daemon_opts + ' push ' + settings.DK_RG_ENDPOINT + '/' + datastore.getImage(
                        imageToken)['tag']
                    if __executeCommand__(command, cwd, pidfile) != 0:
                        err = "Error while pushing to registry"
                    else:
                        createFile(
                            os.path.join(dockerfilepath, "push_command"),
                            command)

            # Error case
            if err is not None:
                deleteImage(datastore, imageToken)
                appendInFile(
                    os.path.join(dockerfilepath,
                                 settings.FS_DEF_DOCKER_BUILD_ERR_LOG), err)
                createFile(
                    os.path.join(dockerfilepath,
                                 settings.FS_DEF_DOCKER_BUILD_FLAG), '1')
            else:
                createFile(
                    os.path.join(dockerfilepath,
                                 settings.FS_DEF_DOCKER_BUILD_FLAG), '0')

        except Exception, e:
            appendInFile(
                os.path.join(dockerfilepath,
                             settings.FS_DEF_DOCKER_BUILD_ERR_LOG),
                "Unexpected error in build thread.\n" + str(e))
            createFile(
                os.path.join(dockerfilepath,
                             settings.FS_DEF_DOCKER_BUILD_FLAG), '1')