def initialize_environments():
    """Initializes a global variable (module level) environments which
    is a python dict containing a hash of environment names with a list
    of the corresponding appliances."""
    global environments
    config = get_config('environments.conf')
    environments = {}
    for section in config.sections():
        environments[section] = config.get(section, 'appliances').split()
def initialize_environments():
    """Initializes a global variable (module level) environments which
    is a python dict containing a hash of environment names with a list
    of the corresponding appliances."""
    global environments
    config = get_config('environments.conf')
    environments = {}
    for section in config.sections():
        environments[section] = config.get(section, 'appliances').split()
Esempio n. 3
0
def call_method(plugin, form):
    """Gather the arguments and function name from form then invoke
    _call_method. Wrap the results in html and return them."""
    t = Timestamp()
    name = form.get("callable")
    arguments, func = _get_arguments(plugin, name)
    kwargs = {}
    for arg, default in arguments:
        if isinstance(default, bool):
            if arg == "web":
                kwargs[arg] = True
                continue
            value = form.get(arg)
            if value == 'true':
                kwargs[arg] = True
            else:
                kwargs[arg] = False
        elif isinstance(default, list):
            # TODO: This needs to implement a selection feature
            if arg == 'appliances':
                kwargs[arg] = form.getlist(arg + '[]')
            elif arg == 'credentials':
                kwargs[arg] = [
                    xordecode(
                        _,
                        key=xorencode(
                            flask.request.cookies["9x4h/mmek/j.ahba.ckhafn"]))
                    for _ in form.getlist(arg + '[]')
                ]
            else:
                kwargs[arg] = form.getlist(arg + '[]')
        elif isinstance(default, basestring):
            if arg == 'out_dir':
                kwargs[arg] = os.path.join('tmp', 'web', name, t.timestamp)
            elif arg == 'out_file' and default is not None:
                kwargs[arg] = os.path.join(
                    "tmp", "web", name,
                    "{}-{}{}".format(t.timestamp, name,
                                     os.path.splitext(default)[1])).replace(
                                         os.path.sep, "/")
            else:
                kwargs[arg] = form.get(arg) or default
        elif isinstance(default, int):
            kwargs[arg] = int(form.get(arg)) or default
        elif default is None:
            kwargs[arg] = form.get(arg) or default
    out, history_id = _call_method(func, kwargs)
    link = ""
    if 'out_dir' in kwargs:
        config = get_config("server.conf")
        static_dir = config.get('dirs', 'static')

        fname = ""
        for appliance in kwargs['appliances']:
            fname = "{}-{}".format(fname, appliance)
        fname = "{}-{}{}.zip".format(t.timestamp, name, fname)
        zip_filename = os.path.join(static_dir, 'tmp', fname)
        zip_file = zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED)
        _zipdir(kwargs['out_dir'], zip_file)
        zip_file.close()
        #filename = '%s-%s.zip' % (t.timestamp, name)
        link = flask.Markup(flask.render_template('link.html', filename=fname))
    if 'out_file' in kwargs and kwargs["out_file"] is not None:
        import shutil
        config = get_config("server.conf")
        static_dir = config.get('dirs', 'static')
        dst = os.path.join(static_dir, "tmp",
                           os.path.basename(kwargs["out_file"]))
        shutil.copyfile(kwargs["out_file"], dst)

        link = flask.Markup(
            flask.render_template('link.html',
                                  filename=os.path.basename(
                                      kwargs["out_file"])))
    out = flask.render_template('output.html',
                                output=out,
                                callable=name,
                                timestamp=str(t),
                                history_id=history_id,
                                link=link)
    if 'out_dir' in kwargs:
        out = out
    return out
def call_method(plugin, form):
    """Gather the arguments and function name from form then invoke
    _call_method. Wrap the results in html and return them."""
    t = Timestamp()
    name = form.get("callable")
    arguments, func = _get_arguments(plugin, name)
    kwargs = {}
    for arg, default in arguments:
        if isinstance(default, bool):
            if arg == "web":
                kwargs[arg] = True
                continue
            value = form.get(arg)
            if value == 'true':
                kwargs[arg] = True
            else:
                kwargs[arg] = False
        elif isinstance(default, list):
            # TODO: This needs to implement a selection feature
            if arg == 'appliances':
                kwargs[arg] = form.getlist(arg + '[]')
            elif arg == 'credentials':
                kwargs[arg] = [
                    xordecode(
                        _, key=xorencode(
                            flask.request.cookies["9x4h/mmek/j.ahba.ckhafn"]))
                            for _ in form.getlist(arg + '[]')]
            else:
                kwargs[arg] = form.getlist(arg + '[]')
        elif isinstance(default, basestring):
            if arg == 'out_dir':
                kwargs[arg] = os.path.join('tmp', 'web', name, t.timestamp)
            elif arg == 'out_file' and default is not None:
                kwargs[arg] = os.path.join("tmp",
                                           "web",
                                           name,
                                           "{}-{}{}".format(t.timestamp,
                                                            name,
                                                            os.path.splitext(default)[1])
                ).replace(os.path.sep, "/")
            else:
                kwargs[arg] = form.get(arg) or default
        elif isinstance(default, int):
            kwargs[arg] = int(form.get(arg)) or default
        elif default is None:
            kwargs[arg] = form.get(arg) or default
    out, history_id = _call_method(func, kwargs)
    link = ""
    if 'out_dir' in kwargs:
        config = get_config("server.conf")
        static_dir = config.get('dirs', 'static')

        fname = ""
        for appliance in kwargs['appliances']:
            fname = "{}-{}".format(fname, appliance)
        fname = "{}-{}{}.zip".format(t.timestamp, name, fname)
        zip_filename = os.path.join(
            static_dir,
            'tmp',
            fname)
        zip_file = zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED)
        _zipdir(kwargs['out_dir'], zip_file)
        zip_file.close()
        #filename = '%s-%s.zip' % (t.timestamp, name)
        link = flask.Markup(flask.render_template('link.html', filename=fname))
    if 'out_file' in kwargs and kwargs["out_file"] is not None:
        import shutil
        config = get_config("server.conf")
        static_dir = config.get('dirs', 'static')
        dst = os.path.join(static_dir,
                           "tmp",
                           os.path.basename(kwargs["out_file"]))
        shutil.copyfile(kwargs["out_file"], dst)

        link = flask.Markup(flask.render_template('link.html',
                                                  filename=os.path.basename(kwargs["out_file"])))
    out = flask.render_template(
        'output.html',
        output=out,
        callable=name,
        timestamp=str(t),
        history_id=history_id,
        link=link)
    if 'out_dir' in kwargs:
        out = out
    return out
Esempio n. 5
0
def main(credentials=[],          timeout=120,
         no_check_hostname=False, environment="",
         vcs_creds="",            vcs_uri="",
         vcs_dir="tmp",           quiesce_appliance=False,
         quiesce_domain=False,    no_quiesce_service=False,
         backup_dir="tmp"):

    logger = make_logger("mast.datapower.deploy")
    check_hostname = not no_check_hostname
    quiesce_service = not no_quiesce_service

    # Make sure deployment is not in progress
    create_inprogress_file(environment)

    ensure_config_file_exists()
    config = get_config("deploy.conf")
    ensure_environment_is_configured(config, environment)

    appliances = config.get(environment, "appliances").split()
    domain = config.get(environment, "domain").strip()

    env = datapower.Environment(
        appliances, credentials, timeout, check_hostname=check_hostname)

    # Clone fresh copy of deployment from VCS
    vcs_details = [config.get("VCS", x) for x in
                   ["type", "server", "base_uri"]]
    export_dir = os.path.abspath(vcs_dir)
    vcs_details.extend([vcs_creds, vcs_uri, export_dir])
    repo_path = clone_repo_from_vcs(vcs_details)

    cwd = os.getcwd()
    os.chdir(repo_path)


    # Get list of files to copy to DataPower appliances

    # gather anything below ./local/.

    # gather anything below ./$environment/local/.



    # Get list of zip files to deploy

    # get a list of any xml or zip files under ./config/.


    # Get deployment policy from ./$environment/deploymentPolicy/.


    for appliance in env.appliances:
        # optionally quiesce appliance

        # optionally quiesce domain

        # optionally quiesce service

        # Backup domain on DataPower
        backup = appliance.get_normal_backup(
            domain=domain,
            format="ZIP",
            comment="deployment")
        filename = os.path.join(backup_dir, "{}.zip".format(appliance.hostname))

        # Copy all files to DataPower

        # Deploy deployment policy to current domain

        # Deploy config zip files to DataPower apply deployment policy

        # Save the config
        pass

    # Clean-up, check-out and exit
    delete_inprogress_file(environment)
    os.chdir(cwd)
    os.rmdir(repo_path)