Beispiel #1
0
def save_scripts():
    '''
    curl -F pre=@art1_pre.sh -F deploy=@art1_deploy.sh\
    'localhost:8081/scripts?artifact=art1&env=dev1'
    '''
    artifact, env = request.query.get('art'), request.query.get('env')
    if None in (artifact, env): raise HTTPResponse(
        'Please provide artifact and environment name\n', 400)
    company = request.creds.company
    model = utils.get_sds_model(env, company)
    if model:
        artifacts = model['arts_to_nodes'].keys()
    else:
        raise HTTPResponse('Couldn\'t get model %s\n' % env, 500)
    if artifact not in artifacts: raise HTTPResponse(
        'Artifact %s is not supported in model %s\n' % (artifact, env), 400)
    script_types = [s for s in request.files]
    if len(script_types) == 0: raise HTTPResponse(
        'You need to upload at least one script type\n', 400)
    try:
        sds = pymongo.Connection().sds
    except(pymongo.errors.AutoReconnect):
        raise HTTPResponse('Couldn\'t connect to SDS db\n', 500)
    scripts = sds.scripts
    script_dict = {}
    
    allowed_types = ['pre', 'deploy', 'selftest', 'version']
    for t in script_types:
        if t not in allowed_types:
            raise HTTPResponse(
                'Sorry, script type %s is not supported\n' % t, 400)
        script_object = {'company': company, 'env': env, 'art': artifact,
            'str': request.files[t].file.read(),
            'fn': request.files[t].filename, 'type': t}
        script_template = {'company': company, 'env': env, 'art': artifact,
            'type': t}
        script_id = scripts.update(script_template, script_object, upsert=True,
            safe=True)
        if script_id:
            script_dict[t] = request.files[t].filename
        else:
            raise HTTPResponse('Couldn\'t save the script %s\n' %
                request.files[t].filename, 500)

    if not model.has_key('arts_to_scripts'): # Never attached any scripts to model
        a2s = {artifact: script_dict} # Initializing with what we have
    elif not model['arts_to_scripts'].has_key(artifact): # No scripts for current art
        a2s = model['arts_to_scripts'] # Copying the existing scripts... 
        a2s[artifact] = script_dict # And adding the new ones.
    else: # Current art has scripts
        a2s = model['arts_to_scripts']
        for k in script_dict:
            a2s[artifact][k] = script_dict[k]
    
    model['arts_to_scripts'] = a2s
    models = sds.models
    models.update({'company': company, 'env': env}, {'$set': {'model': model}})
Beispiel #2
0
def deploy_env():
    '''Extracts at least 1 file with an artifact to deploy from request.files
    and an ssh key from request.forms.key.
    Starts a deployment process for the environment specified in the 'env' URL
    parameter and returns a url with the deployment status or an error message
    if the deployment couldn't start.

    curl -F "art2=@curlMan" -F "key=<.ssh/id_dsa_" localhost:8081/deploy?env=dev4
    '''
    env = request.query.get('env')
    if not env:
        raise HTTPResponse('Please provide your environment name\n', 400)
    company = request.creds.company
    model = utils.get_sds_model(env, company)
    if not model:
        raise HTTPResponse('Couldn\'t find environment named %s\n' % env, 404)
    if len(request.files) == 0:
        raise HTTPResponse('Please upload at least 1 artifact\n', 400)
    if not request.forms.key:
        raise HTTPResponse(
            'Please upload the contents of your ssh private key in a "key" form field\n',
            400)
    pkey = utils.str_to_key(request.forms.key)
    if not pkey:
        raise HTTPResponse("Couldn't decode your SSH key.\n", 400)
    dry_run = request.query.get('dry_run')

    token = utils.update_status({
        'Error occured': False,
        'Deployment finished': False
    })
    log_obj = utils.update_logs({'_id': token})

    arts_dict = {}
    for art in request.files:
        fs = request.files[art]
        if fs.name not in model['arts_to_nodes'].keys():
            raise HTTPResponse(
                'Artifact %s is not supported in your model.\n' % fs.name, 400)
        # We need to save the artifacts in real temp files so that fapi.put
        # could work with them
        dn = '/tmp/deployment_arts/%s/%s/' % (company, str(token))
        fn = fs.name
        if not os.path.exists(dn):
            os.makedirs(dn)
        try:
            fh = open(dn + fn, 'wb')
            fh.write(fs.file.read())
            fh.close()
        except:
            raise HTTPResponse('Error occured while saving %s \n' % art, 500)
        arts_dict[fs.name] = {
            'type': fs.name,
            'filename': fs.filename,
            'file_path': dn + fn
        }

    # Saving everything needed for generating and running steps in the db
    keys = pymongo.Connection().sds.keys
    args = pymongo.Connection().sds.args
    gen_step_args = {
        'model': model,
        'token': token,
        'company': company,
        'arts': arts_dict
    }

    steps = utils.generate_steps(model, pkey, token, company, **arts_dict)
    if steps:
        if dry_run:
            shutil.rmtree(dn)
            return utils.run_steps(steps, company, token, dry_run=True)
        else:
            # This runs the deployment in a separate daemonized process:
            keys.save({'_id': token, 'key': request.forms.key})
            args.save({'_id': token, 'args': gen_step_args})
            p = subprocess.Popen(
                ['/home/simplds/sds/srv/executor.py',
                 str(token)])

            serverpart = 'https://sds.simplcloud.com'
            status_url = '"%s/status?token=%s&human_readable=true"' % (
                serverpart, str(token))
            log_url = '%s/logs/%s' % (serverpart, str(token))
            #return '%s\n%s\n' % (status_url, log_url)
            return (
                '"https://sds.simplcloud.com/status?token=%s&human_readable=true"\n'
                % str(token))
    else:
        raise HTTPResponse(
            'Error occured while generating deployment steps.\n', 500)
Beispiel #3
0
def save_scripts():
    '''
    curl -F pre=@art1_pre.sh -F deploy=@art1_deploy.sh\
    'localhost:8081/scripts?artifact=art1&env=dev1'
    '''
    artifact, env = request.query.get('art'), request.query.get('env')
    if None in (artifact, env):
        raise HTTPResponse('Please provide artifact and environment name\n',
                           400)
    company = request.creds.company
    model = utils.get_sds_model(env, company)
    if model:
        artifacts = model['arts_to_nodes'].keys()
    else:
        raise HTTPResponse('Couldn\'t get model %s\n' % env, 500)
    if artifact not in artifacts:
        raise HTTPResponse(
            'Artifact %s is not supported in model %s\n' % (artifact, env),
            400)
    script_types = [s for s in request.files]
    if len(script_types) == 0:
        raise HTTPResponse('You need to upload at least one script type\n',
                           400)
    try:
        sds = pymongo.Connection().sds
    except (pymongo.errors.AutoReconnect):
        raise HTTPResponse('Couldn\'t connect to SDS db\n', 500)
    scripts = sds.scripts
    script_dict = {}

    allowed_types = ['pre', 'deploy', 'selftest', 'version']
    for t in script_types:
        if t not in allowed_types:
            raise HTTPResponse('Sorry, script type %s is not supported\n' % t,
                               400)
        script_object = {
            'company': company,
            'env': env,
            'art': artifact,
            'str': request.files[t].file.read(),
            'fn': request.files[t].filename,
            'type': t
        }
        script_template = {
            'company': company,
            'env': env,
            'art': artifact,
            'type': t
        }
        script_id = scripts.update(script_template,
                                   script_object,
                                   upsert=True,
                                   safe=True)
        if script_id:
            script_dict[t] = request.files[t].filename
        else:
            raise HTTPResponse(
                'Couldn\'t save the script %s\n' % request.files[t].filename,
                500)

    if not model.has_key(
            'arts_to_scripts'):  # Never attached any scripts to model
        a2s = {artifact: script_dict}  # Initializing with what we have
    elif not model['arts_to_scripts'].has_key(
            artifact):  # No scripts for current art
        a2s = model['arts_to_scripts']  # Copying the existing scripts...
        a2s[artifact] = script_dict  # And adding the new ones.
    else:  # Current art has scripts
        a2s = model['arts_to_scripts']
        for k in script_dict:
            a2s[artifact][k] = script_dict[k]

    model['arts_to_scripts'] = a2s
    models = sds.models
    models.update({'company': company, 'env': env}, {'$set': {'model': model}})
Beispiel #4
0
def deploy_env():
    '''Extracts at least 1 file with an artifact to deploy from request.files
    and an ssh key from request.forms.key.
    Starts a deployment process for the environment specified in the 'env' URL
    parameter and returns a url with the deployment status or an error message
    if the deployment couldn't start.

    curl -F "art2=@curlMan" -F "key=<.ssh/id_dsa_" localhost:8081/deploy?env=dev4
    '''
    env = request.query.get('env')
    if not env:
        raise HTTPResponse('Please provide your environment name\n', 400)
    company = request.creds.company
    model = utils.get_sds_model(env, company)
    if not model:
        raise HTTPResponse('Couldn\'t find environment named %s\n' % env, 404)
    if len(request.files) == 0:
        raise HTTPResponse('Please upload at least 1 artifact\n', 400)
    if not request.forms.key:
        raise HTTPResponse(
            'Please upload the contents of your ssh private key in a "key" form field\n', 400)
    pkey = utils.str_to_key(request.forms.key)
    if not pkey:
        raise HTTPResponse("Couldn't decode your SSH key.\n", 400)
    dry_run = request.query.get('dry_run')
    
    token = utils.update_status({'Error occured': False, 'Deployment finished': False})
    log_obj = utils.update_logs({'_id': token})

    arts_dict = {}
    for art in request.files:
        fs = request.files[art]
        if fs.name not in model['arts_to_nodes'].keys():
            raise HTTPResponse('Artifact %s is not supported in your model.\n' %
                fs.name, 400)
        # We need to save the artifacts in real temp files so that fapi.put
        # could work with them
        dn = '/tmp/deployment_arts/%s/%s/' % (company, str(token))
        fn = fs.name
        if not os.path.exists(dn):
            os.makedirs(dn)
        try:
            fh = open(dn+fn, 'wb')
            fh.write(fs.file.read())
            fh.close()
        except:
            raise HTTPResponse('Error occured while saving %s \n' % art, 500)
        arts_dict[fs.name] = {'type': fs.name, 'filename': fs.filename, 'file_path': dn+fn } 

    # Saving everything needed for generating and running steps in the db
    keys = pymongo.Connection().sds.keys
    args = pymongo.Connection().sds.args
    gen_step_args = {'model': model, 'token': token, 'company': company, 'arts': arts_dict}


    steps = utils.generate_steps(model, pkey, token, company, **arts_dict)
    if steps:
        if dry_run:
            shutil.rmtree(dn)
            return utils.run_steps(steps, company, token, dry_run=True)
        else:
            # This runs the deployment in a separate daemonized process:
            keys.save({'_id': token, 'key': request.forms.key})
            args.save({'_id': token, 'args': gen_step_args})
            p = subprocess.Popen(['/home/simplds/sds/srv/executor.py', str(token)])

            serverpart = 'https://sds.simplcloud.com'
            status_url = '"%s/status?token=%s&human_readable=true"' % (serverpart,
                str(token))
            log_url = '%s/logs/%s' % (serverpart, str(token))
            #return '%s\n%s\n' % (status_url, log_url)
            return ('"https://sds.simplcloud.com/status?token=%s&human_readable=true"\n' %
                str(token))
    else:
         raise HTTPResponse('Error occured while generating deployment steps.\n',
            500)