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)
            raise errors.OperationError("Couldn't create context in the filesystem")

        # Save puppetfile
        files.savePuppetfile(token, puppetfile)

        # Check puppetfile
        aux = puppet.checkPuppetfile(token)
        if not aux==True:
            files.deleteContextDir(token)
            raise errors.OperationError("Syntax error in provided puppetfile: " + aux)

        # Launch build operation
        puppet.buildContext(token)

        # Create context in datastore
        datastorecontext = {'token':token, 'group':group, 'status':'building', 'description':'Under creation', 'images':[]}
        datastore.addContext(token, datastorecontext)

        return datastorecontext

    except errors.ControllerError, e:
        return e.getResponse()
def newBase(datastore, name, dockerfile):
    '''
    Build a new base image.
    '''
    try:
        # Create base in filesystem
        try:
            files.createBaseDir(name)
        except os.error:
            raise errors.OperationError(
                "Couldn't create base in the filesystem. It may already exists."
            )

        # Save Dockerfile
        files.saveBaseDockerfile(name, dockerfile)

        # Launch build operation
        docker.buildBase(datastore, name)

        # Create base in datastore
        datastorebase = {
            'name': name,
            'status': 'building',
            'description': 'Under creation'
        }
        datastore.addBase(name, datastorebase)

        return datastorebase

    except errors.ControllerError, e:
        return e.getResponse()
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()
Exemple #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()
Exemple #5
0
def newProvisionedMachineCluster(datastore, endpoint, apiVersion=None):
    '''
    Set a preprovisioned single-machine cluster. 'endpoint' is the Docker host api endpoint.
    '''
    try:
        info = __obtainDockerHostInfo__(endpoint)
        dk = docker.dockerClient(
            base_url=endpoint,
            cert_path=settings.DK_DEFAULT_MASTER_CLIENT_CERTS)

        # Generate token
        token = tokens.newClusterToken(datastore)

        # Add to filesystem: for this simple already provisioned machine, only the folder will be created
        try:
            files.createClusterDir(token)
        except os.error:
            files.deleteClusterDir(token)
            raise errors.OperationError(
                "Couldn't create cluster reference in the filesystem")

        # Add to datastore
        datastorecluster = {
            'token': token,
            'status': 'joining',
            'description': 'Ready to use',
            'numberOfNodes': 1,
            'type': 'swarm',
            'nodes': [{
                'endpoint': endpoint,
                'status': 'joining'
            }]
        }
        datastore.addCluster(token, datastorecluster)

        docker.createOneNodeSwarm(datastore, token, dk)

        return datastorecluster, 200

    except errors.ControllerError, e:
        return e.getResponse()