Exemplo n.º 1
0
def deleteBase(datastore, name):
    '''
    Delete a base image
    '''
    try:
        # get base
        base = datastore.getBase(name)
        if base == None:
            raise errors.NotFoundError("Base does not exist.")

        # check if building and stop in case
        docker.stopBaseBuild(name)

        # delete docker image
        if docker.deleteBaseImage(datastore, name) == False:
            raise errors.ControllerError("Can't delete docker base image.")

        # remove from filesystem
        files.deleteBaseDir(name)

        # remove from datastore
        datastore.delBase(name)

        base['status'] = 'deleted'
        base['description'] = ''

        return base, 200

    except errors.NotFoundError, e:
        return e.getResponse()
Exemplo n.º 2
0
def deleteImage(datastore, imageToken):
    '''
    Deletes image and stops the build process.
    '''
    try:
        # get image
        image = datastore.getImage(imageToken)
        if image == None:
            raise errors.NotFoundError("Image does not exist.")

        # check if building and stop in case
        docker.stopBuild(image['context'], image['imageName'])

        # delete docker image
        if docker.deleteImage(datastore, imageToken) == False:
            raise errors.ControllerError("Can't delete docker image.")

        # remove from filesystem
        files.deleteImageDir(image['context'], image['imageName'])

        # remove from datastore
        datastore.delImage(imageToken)

        image['status'] = 'deleted'
        image['description'] = ''

        return image, 200

    except errors.NotFoundError, e:
        return e.getResponse()
Exemplo n.º 3
0
def deleteCluster(datastore, token):
    '''
    Deletes the cluster and all reference with it.
    '''
    try:
        # Retrieve cluster information in datastore
        cluster = datastore.getCluster(token)
        if cluster == None:
            raise errors.NotFoundError("Cluster does not exist.")

        # stop cluster (depends on the type of cluster)
        if cluster['type'] is 'simple-preprovisioned':
            pass
        else:
            raise errors.ControllerError(
                "Don't know how to remove a cluster of this type (" +
                cluster['type'] + ").")

        # delete folder
        files.deleteClusterDir(token)
        # delete from datastore
        datastore.delCluster(token)
        # fake cluster just to return a response
        cluster = {}
        cluster['status'] = 'deleted'
        cluster[
            'description'] = 'Cluster has been removed and will not be accesible anymore.'
        return cluster
    except errors.NotFoundError, e:
        return e.getResponse()
Exemplo n.º 4
0
def newComposition(datastore, composefile, clusterReference):
    '''
    Saves docker compose file and runs it.
    '''
    cluster = None
    try:
        token = tokens.newCompositionToken(datastore)
        # retrieve cluster information from datastore
        cluster = datastore.getCluster(clusterReference)
        if cluster == None:
            raise errors.ControllerError("Cluster does not exist.")
        # Check docker
        endpoint = cluster['nodes'][0]['endpoint']
        dk = docker.dockerClient(endpoint,
                                 settings.DK_DEFAULT_MASTER_CLIENT_CERTS)

        dockercheck = docker.checkDocker(dk)
        if dockercheck is not True:
            raise errors.ControllerError("Error in cluster. Docker error: " +
                                         dockercheck)

        # Create composition in datastore
        datastorecomposition = {
            'token': token,
            'cluster': clusterReference,
            'status': 'providing',
            'description': 'Providing data'
        }
        datastore.addComposition(token, datastorecomposition)

        # Save compose file
        try:
            files.saveComposeFile(token, composefile)
        except os.error:
            files.deleteComposeFile(token)
            datastore.delComposition(token)
            raise errors.OperationError(
                "Couldn't create composition in the filesystem")

        # Launch composition
        docker.runComposition(datastore, token, dk=dk)

        return datastorecomposition
    except dataStore.DataStoreError, e:
        aux = errors.ControllerError("Runtime error: " + e.message)
        return aux.getResponse()
Exemplo n.º 5
0
def getDockerInfo(dockerClient):
    '''
    Retrieve docker system information
    '''
    try:
        return docker.dockerInfo(dockerClient)
    except Exception, e:
        aux = errors.ControllerError("Unknown error: " + e.message)
        return aux.getResponse()
Exemplo n.º 6
0
def __obtainDockerHostInfo__(endpoint):
    # Obtain information about docker daemon listening on endpoint
    info = None

    try:
        dk = docker.dockerClient(
            base_url=endpoint,
            cert_path=settings.DK_DEFAULT_MASTER_CLIENT_CERTS)
        info = docker.dockerInfo(dk)
    except Exception, e:
        raise errors.ControllerError(
            "Error while connecting with provided docker host. Endpoint: '" +
            str(endpoint) + "' exception:" + str(e))
Exemplo n.º 7
0
def getAllTokens(datastore):
    '''
    Get all tokens in datastore
    '''
    try:
        content = {}
        # get tokens from datastore
        try:
            content = datastore.getTokens()
        except:
            raise errors.ControllerError("Error in datastore.")

        return content
    except errors.ControllerError, e:
        return e.getResponse()
Exemplo n.º 8
0
def newImage(datastore, contextReference, imageName, puppetmanifest=None, base=None, dockerfile=None):
    '''
    Saves the files in the filesystem, and launch the build process
    '''
    try:
        imageName=''.join(ch for ch in imageName if ch.isalnum())
        token = tokens.newImageToken(datastore, contextReference, imageName)

        # Check if context exists and if it is completed
        contextInfo, statusCode = checkContext(datastore, contextReference)
        if statusCode == 404:
            raise errors.OperationError("Context does not exist")
        if not statusCode == 200:
            raise errors.OperationError("Error while inspecting context")
        if not contextInfo['status']=='finished':
            raise errors.OperationError("Context is not ready")

        # Create image in datastore
        datastoreimage = {'token':token, 'context':contextReference, 'imageName':imageName, 'status':'building', 'description':'Under creation', 'tag':datastore.getContext(contextReference)['group']+'/'+imageName.lower()}
        datastore.addImage(contextReference, token, datastoreimage)

        # Create image in filesystem and save Dockerfile and Puppetmanifest
        try:
            files.createImageDir(contextReference, imageName)
            if puppetmanifest!=None:
                files.savePuppetManifest(contextReference, imageName, puppetmanifest)
            if dockerfile!=None:
                files.saveDockerfile(contextReference, imageName, dockerfile)
            if base!=None:
                baseImageName=settings.DK_DEFAULT_BASE_PROVIDER+'/'+base
                if docker.imageInRegistry(baseImageName) == False:
                    raise errors.OperationError("Base image '"+baseImageName+"' does not exist in private registry")
                files.createBaseDockerfile(contextReference, imageName, settings.DK_RG_ENDPOINT+'/'+baseImageName)

        except os.error:
            files.deleteImageDir(contextReference, imageName)
            datastore.delImage(token)
            raise errors.OperationError("Couldn't create image in the filesystem")

        # Launch build operation
        docker.buildImage(datastore, contextReference, imageName, token)

        return datastoreimage
    except dataStore.DataStoreError, e:
        aux = errors.ControllerError("Runtime error: "+ e.message)
        return aux.getResponse()
Exemplo n.º 9
0
from toolbox import files
from threading import Thread


def contextList(datastore):
    '''
    Returns a list of contexts.
    '''
    try:
        listOfContexts = datastore.getContexts()
        return {'contexts': [] if listOfContexts == None else listOfContexts}

    except errors.ControllerError, e:
        return e.getResponse()
    except Exception, e:
        aux = errors.ControllerError("Unknown error: " + e.message)
        return aux.getResponse()


def newContext(puppetfile, datastore, group='default'):
    '''
    Reads and checks puppetfile, creates the directory in the filesystem and launch puppetfile processing.
    '''
    try:
        token = tokens.newContextToken(datastore)

        # Create context in filesystem
        try:
            files.createContextDir(token)
        except os.error:
            files.deleteContextDir(token)
Exemplo n.º 10
0
def __obtainDockerHostInfo__(endpoint):
    # Obtain information about docker daemon listening on endpoint
    info = None

    try:
        dk = docker.dockerClient(
            base_url=endpoint,
            cert_path=settings.DK_DEFAULT_MASTER_CLIENT_CERTS)
        info = docker.dockerInfo(dk)
    except Exception, e:
        raise errors.ControllerError(
            "Error while connecting with provided docker host. Endpoint: '" +
            str(endpoint) + "' exception:" + str(e))

    if not info:
        raise errors.ControllerError(
            "Could not validate docker daemon on provided endpoint.")

    return info


def newSingleProvisionedMachineCluster(datastore, endpoint, apiVersion=None):
    '''
    Set a preprovisioned single-machine cluster. 'endpoint' is the Docker host api endpoint.
    '''
    try:
        info = __obtainDockerHostInfo__(endpoint)

        # Generate token
        token = tokens.newClusterToken(datastore)

        # Add to filesystem: for this simple already provisioned machine, only the folder will be created
Exemplo n.º 11
0
        # Create composition in datastore
        datastorecomposition = {
            'token': token,
            'cluster': clusterReference,
            'status': 'providing',
            'description': 'Providing data'
        }
        datastore.addComposition(token, datastorecomposition)

        # Save compose file
        try:
            files.saveComposeFile(token, composefile)
        except os.error:
            files.deleteComposeFile(token)
            datastore.delComposition(token)
            raise errors.OperationError(
                "Couldn't create composition in the filesystem")

        # Launch composition
        docker.runComposition(datastore, token, dk=dk)

        return datastorecomposition
    except dataStore.DataStoreError, e:
        aux = errors.ControllerError("Runtime error: " + e.message)
        return aux.getResponse()
    except errors.ControllerError, e:
        return e.getResponse()
    except Exception, e:
        aux = errors.ControllerError("Unknown error: " + str(e))
        return aux.getResponse()