def _view():
    if not logged_in():
        return redirect("/")

    cluster_id = request.args.get("cluster_id")

    cluster = Cluster.by_uuid(cluster_id)
    components = pickle.loads(cluster.components)

    return render_template(
        "view.html",
        title="View Cluster",
        cluster_id=cluster_id,
        cluster_state=cluster.cluster_state,
        username=session["username"],
        cluster_name=cluster.cluster_name,
        num_workers=cluster.num_workers,
        cpus=cluster.cpus,
        memory=cluster.memory,
        disk_capacity=cluster.disk_capacity,
        network_speed=cluster.network_speed,
        sl_ssh_key=cluster.sl_ssh_key,
        sl_domain=cluster.sl_domain,
        sl_datacenter=cluster.sl_datacenter,
        master_ip=cluster.master_ip,
        master_password=cluster.master_password,
        components=components,
    )
def set_cluster_state(cluster_id, state):
    logger.debug('setting cluster state {} for id = {}'.format(
        cluster_id, state
    ))
    with app.test_request_context():
        cluster = Cluster.by_uuid(cluster_id)

        cluster.cluster_state = state
        db.session.commit()
def destroy_cluster(cluster_id):
    logger.info('Destroying cluster id = {}'.format(cluster_id))

    async_destroy_cluster(cluster_id)

    logger.debug('removing cluster {} from table Cluster'.format(cluster_id))
    cluster = Cluster.by_uuid(cluster_id)
    db.session.delete(cluster)
    db.session.commit()
def store_master_ip_and_password(master_ip, cluster_id):
    # TODO verify that master_ip is valid ip

    master_password = get_master_password_from_sl(master_ip, cluster_id)

    with app.test_request_context():
        cluster = Cluster.by_uuid(cluster_id)
        cluster.master_ip = master_ip
        cluster.master_password = master_password
        db.session.commit()
def _dashboard():
    if not logged_in():
        return redirect("/")

    return render_template(
        "dashboard.html",
        title="Dashboard",
        username=session["username"],
        clusters=Cluster.by_owner_id(session["username"]),
    )
def _cluster_status():
    cluster_id = request.args.get("cluster_id")

    master_ip, stdout, stderr = get_cluster_status(cluster_id)

    master_ip, master_password = get_master_ip_and_password(cluster_id)

    cluster = Cluster.by_uuid(cluster_id)
    refresh_interval = 1000

    return render_template(
        "cluster_status.html",
        cluster_id=cluster_id,
        cluster_state=cluster.cluster_state,
        refresh_interval=refresh_interval,
        master_ip=master_ip,
        master_password=master_password,
        stdout=stdout,
        stderr=stderr,
        username=session["username"],
    )
def get_master_password_from_sl(master_ip, cluster_id):
    # This function executes one time from Thread and does not have db context

    if master_ip == '':
        return ''

    with app.test_request_context():
        # retrieve sl username and api key by cluster_id
        cluster = Cluster.by_uuid(cluster_id)

        logger.debug(
            'cluster_id={}, sl_username={}, master_ip={}'.format(cluster_id,
                                                                 cluster.sl_username,
                                                                 master_ip))

        client = SoftLayer.Client(username=cluster.sl_username,
                                  api_key=cluster.sl_api_key)

        vs_manager = SoftLayer.managers.VSManager(client)

        try:
            master_details = vs_manager.list_instances(public_ip=master_ip)
            if len(master_details) > 1:
                logger.error(
                    'SoftLayer API returned non-unique instance for ip = {}'.format(
                        master_ip))

            master_id = master_details[0]['id']
            master_instance = vs_manager.get_instance(instance_id=master_id)
            master_password = \
                master_instance['operatingSystem']['passwords'][0]['password']
            # store password in db for faster retrieval in the future
            cluster.master_password = master_password
            db.session.commit()

        except Exception:
            master_password = ''

        return master_password
def get_master_ip_and_password(cluster_id):
    cluster = Cluster.by_uuid(cluster_id)
    if cluster is not None:
        return cluster.master_ip, cluster.master_password
    return '', ''
def _cluster_status_only():
    cluster_id = request.args.get("cluster_id")
    cluster = Cluster.by_uuid(cluster_id)
    return cluster.cluster_state