Giter VIP home page Giter VIP logo

backend.ai-client-py's Introduction

Backend.AI Client

Warning

Deprecation Notice This repository is deprecated and no longer maintained. The code has been migrated to our semi-mono repository at backend.ai. Please use the new repository for any future development or issue tracking.


PyPI version Python Versions SDK Documentation Build Status (Linux) Build Status (Windows) Code Coverage

The official API client library for Backend.AI

Usage (KeyPair mode)

You should set the access key and secret key as environment variables to use the API. Grab your keypair from cloud.backend.ai or your cluster admin.

On Linux/macOS, create a shell script as my-backend-ai.sh and run it before using the backend.ai command:

export BACKEND_ACCESS_KEY=...
export BACKEND_SECRET_KEY=...
export BACKEND_ENDPOINT=https://my-precious-cluster
export BACKEND_ENDPOINT_TYPE=api

On Windows, create a batch file as my-backend-ai.bat and run it before using the backend.ai command:

chcp 65001
set PYTHONIOENCODING=UTF-8
set BACKEND_ACCESS_KEY=...
set BACKEND_SECRET_KEY=...
set BACKEND_ENDPOINT=https://my-precious-cluster
set BACKEND_ENDPOINT_TYPE=api

Note that you need to switch to the UTF-8 codepage for correct display of special characters used in the console logs.

Usage (Session mode)

Change BACKEND_ENDPOINT_TYPE to "session" and set the endpoint to the URL of your console server.

export BACKEND_ENDPOINT=https://my-precious-cluster
export BACKEND_ENDPOINT_TYPE=session
$ backend.ai login
User ID: [email protected]
Password:
✔ Login succeeded!

$ backend.ai ...  # run any command

$ backend.ai logout
✔ Logout done.

The session expiration timeout is set by the console server.

Command-line Interface

backend.ai command is the entry point of all sub commands. (Alternatively you can use a verbosely long version: python -m ai.backend.client.cli)

Highlight: run command

The run command execute a code snippet or code source files on a Backend.AI compute session created on-the-fly.

To run the code specified in the command line directly, use -c option to pass the code string (like a shell).

$ backend.ai run python:3.6-ubuntu18.04 -c "print('hello world')"
∙ Client session token: d3694dda6e5a9f1e5c718e07bba291a9
✔ Kernel (ID: zuF1OzMIhFknyjUl7Apbvg) is ready.
hello world

By default, you need to specify language with full version tag like python:3.6-ubuntu18.04. Depending on the Backend.AI admin's language alias settings, this can be shortened just as python. If you want to know defined language aliases, contact the admin of Backend.AI server.

You can even run a C code on-the-fly. (Note that we put a dollar sign before the single-quoted code argument so that the shell to interpret '\n' as actual newlines.)

$ backend.ai run gcc:gcc6.4-alpine3.8 -c $'#include <stdio.h>\nint main() {printf("hello world\\n");}'
∙ Client session token: abc06ee5e03fce60c51148c6d2dd6126
✔ Kernel (ID: d1YXvee-uAJTx4AKYyeksA) is ready.
hello world

For larger programs, you may upload multiple files and then build & execute them. The below is a simple example to run a sample C program.

$ git clone https://gist.github.com/achimnol/df464c6a3fe05b21e9b06d5b80e986c5 c-example
Cloning into 'c-example'...
Unpacking objects: 100% (5/5), done.
$ cd c-example
$ backend.ai run gcc:gcc6.4-alpine3.8 main.c mylib.c mylib.h
∙ Client session token: 1c352a572bc751a81d1f812186093c47
✔ Kernel (ID: kJ6CgWR7Tz3_v2WsDHOwLQ) is ready.
✔ Uploading done.
✔ Build finished.
myvalue is 42
your name? LABLUP
hello, LABLUP!

Please refer the --help manual provided by the run command.

Highlight: start and app command

backend.ai start is simliar to the run command in that it creates a new compute session, but it does not execute anything there. You can subsequently call backend.ai run -t <sessionId> ... to execute codes snippets or use backend.ai app command to start a local proxy to a container service such as Jupyter which runs inside the compute session.

$ backend.ai start -t mysess -r cpu=1 -r mem=2g lablup/python:3.6-ubuntu18.04
∙ Session ID mysess is created and ready.
∙ This session provides the following app services: ipython, jupyter, jupyterlab
$ backend.ai app mysess jupyter
∙ A local proxy to the application "jupyter" provided by the session "mysess" is available at: http://127.0.0.1:8080

Highlight: ps and rm command

You can see the list of currently running sessions using your API keypair.

$ backend.ai ps
Session ID    Lang/runtime              Tag    Created At                        Terminated At    Status      CPU Cores    CPU Used (ms)    Total Memory (MiB)    Used Memory (MiB)    GPU Cores
------------  ------------------------  -----  --------------------------------  ---------------  --------  -----------  ---------------  --------------------  -------------------  -----------
88ee10a027    lablup/python:3.6-ubuntu         2018-12-11T03:53:14.802206+00:00                   RUNNING             1            16314                  1024                 39.2            0
fce7830826    lablup/python:3.6-ubuntu         2018-12-11T03:50:10.150740+00:00                   RUNNING             1            15391                  1024                 39.2            0

If you set -t option in the run command, it will be used as the session ID—you may use it to assign a human-readable, easy-to-type alias for your sessions. These session IDs can be reused after the current session using the same ID terminates.

To terminate a session, you can use terminate or rm command.

$ backend.ai rm 5baafb2136029228ca9d873e1f2b4f6a
✔ Done.

Highlight: proxy command

To use API development tools such as GraphiQL for the admin API, run an insecure local API proxy. This will attach all the necessary authorization headers to your vanilla HTTP API requests.

$ backend.ai proxy
∙ Starting an insecure API proxy at http://localhost:8084

More commands?

Please run backend.ai --help to see more commands.

Troubleshooting (FAQ)

  • There are error reports related to simplejson with Anaconda on Windows. This package no longer depends on simplejson since v1.0.5, so you may uninstall it safely since Python 3.5+ offers almost identical json module in the standard library.

    If you really need to keep the simplejson package, uninstall the existing simplejson package manually and try reinstallation of it by downloading a pre-built binary wheel from here.

backend.ai-client-py's People

Contributors

achimnol avatar adrysn avatar fregataa avatar gofeel avatar hephaex avatar inureyes avatar jil8885 avatar kmkwon94 avatar ksy9164 avatar kyujin-cho avatar leksikov avatar lizable avatar miraliahmadli avatar nokchalatte avatar rlatjcj avatar sonminwoo avatar tink-expo avatar vesselofgod avatar yongkyunlee avatar zeniuus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

backend.ai-client-py's Issues

Commands for agent watcher

Let's add commands for managing agent through agent watcher.

  • Get agent/watcher status.
  • Start agent.
  • Stop agent.
  • Restart agent.

Handle redirects in API requests

This is for forward compatibility.
Currently requests module automatically handles redirects but the authentication header is not updated and causes authorization failures.

Automatic legacy support

Automatically use --legacy option when server API is v3.

  • Automatically use --legacy option when server API is v3.

Windows-specific issues

  • aiohttp is not installed automatically when pip install backend.ai-client.
  • CLI command's colored outputs are not compatible with command prompt.
    image
  • PowerShell doesn't allow me to execute virtualenv script.
    image
    • PowerShell security exception can be solved if the user runs Set-ExecutionPolicy RemoteSigned and choose "Y" in a PowerShell run as administrator. The user manual will include this case.

Config displays the latest API version always

backend.ai config command displays current settings including API endpoint and API version. However, the API version displays always the latest version (v5.20191215 for current master). For example, https://api.backend.ai endpoint currently serves v4.20181215 as shown below:

image

When running backend.ai config, you'll see API version of v5.20191215.

image

I think we need to display the correct API version of the endpoint by fetching the information from the endpoint. Or, at least we need to provide users to be able to set API version by environment variables, such as BACKEND_API_VERSION.

버츄얼 폴더에 get 메소드 버그

session = Session()
vf = session.VFolder
name = "first_vf"
vf.create(name)
vf.get(name)

을 할경우 아래와 같이 에러가 발생합니다.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-586f79b79e3d> in <module>
----> 1 vf.get('first_vf')

~/code/brgg/CodeLion/.venv/lib/python3.6/site-packages/ai/backend/client/base.py in _caller(cls, *args, **kwargs)
     63                    'You must use API wrapper functions via a Session object.'
     64             gen = meth(*args, **kwargs)
---> 65             resp = cls._make_request(gen)
     66             return cls._handle_response(resp, gen)
     67 

~/code/brgg/CodeLion/.venv/lib/python3.6/site-packages/ai/backend/client/base.py in _make_request(gen)
     49     @staticmethod
     50     def _make_request(gen):
---> 51         rqst = next(gen)
     52         resp = rqst.fetch()
     53         return resp

TypeError: 'VFolder' object is not an iterator

get메소드는 다른 메소드와 달리 별도의 처리가 되지 않아 발생한 상황인듯 하여 확인 부탁드립니다.

Local insecure API proxy

Let's add a command python -m ai.backend.client.cli proxy which opens an insecure HTTP server where all requests are proxied to the original API server with configured authorization headers.
This will allow use of many convenient API development tools such as GraphiQL.

Provide shortcut / alias

  • Provide shortcut or aliases for intuitive understanding about the sorna system

Suggestion

  • lcc : C/C++ on lablup.ai cloud.
  • lpython : python on lablup.ai cloud.

Make a debian package

Let's apt-get install backend.ai-client.

Top priority: 18.04
Second priorities: 18.10, 19.04
Dropped: 16.04 due to Python 3.5.1

Tunneling proxy to access service ports (e.g., TensorBoard)

Configuring a network is often painful for end-users.
Let's provide a better way in the CLI of sorna client, and let the users just open "localhost:8081" to magically connect to a web page served by the kernel session.
We could use the SSH socks proxy or just some custom-built ones via WebSockets.

AttributeError: 'ComputeSession' object has no attribute 'id'

After removing no longer needed argument when using client.request.Request instances, AttributeError: 'ComputeSession' object has no attribute 'id' was occured in src/ai/backend/client/versioning.py:33 with the command, backend.ai app ID jupyterlab.

def get_id_or_name(api_version: Tuple[int, str], obj: ComputeSession) -> str:
    if api_version[0] <= 4:
        return obj.name
    return obj.id

As shown in the picture below, the variable api_version[0] was printed 6, but ComputeSession had no attribute 'id'. When the version check was removed, it worked normally again. Should the version check be removed from the source code?
image

Drop Python 3.5 support

It's September 2019.

  • Even Python 3.6 has reached the "security fix only" phase. (PEP-494)
  • Ubuntu 18.04 LTS: Python 3.6
  • Debian "Buster" 10 stable: Python 3.6
  • CentOS: Anyway the users need to install Python 3 separately

python client 문서 갱신 요청드립니다.

Description

http://docs.backend.ai/en/latest/gsg/clientlib.html 현재 sorn-client는 pip에서 0.9.3버전이라
pip install backend.ai-client로 변경 및 해당문서의 샘플 코드도 갱신이 필요해 보입니다.

샘플코드의 경우 아래와 같이 변경하여 연동을 시도해봤습니다.

from ai.backend.client.request import Request
req = Request('GET', '/authorize', {'echo': 'test'})
rep = request.send()
print(rep.status)
print(rep.json())

하지만 새로이 변경된 클라이언트는 메소드 대신 세션을 요구하는데 세션 생성에 관련한 가이드라인 문서가 없어
연동에 어려움이 있습니다.

Expected / actual behavior

How to reproduce?

Your environment

  • OS Version:
  • Python:
  • Docker:
  • Accelerator:
  • Backend.AI:
    • Using cloud.backend.ai
    • Using a privately installed cluster
    • Using a demo container/VMs

Python Client Library

Let's write a basic Python client library that conforms with the API specification.

Command line interface

  • Make it available via python -m sorna or python -m sorna.client
  • Provide batch-mode access via CLI.
    • --build, --exec, --lang, --mount, --limit, --gpu params. If some params are omitted, infer them with some heuristics: e.g., choose the only script file in the arguments as --exec target; if the argument contains a Makefile then use it as --build target.)
    • --sess-id to manually specify client-side session ID (i.e., unique identifier of the session). If omitted, auto-create a new session and show an arbitrary ID so that the user can refer it in subsequent executions.
    • Add -d / --detach option Add --rm option (like Docker).
  • Provide list/control of active kernel sessions for the configured keypair.
    • Let's provide this as "ps" command (an alias of "admin sessions")
  • Show resource statistics of the configured keypair.

Make client accept humanized memory resource unit

Currently, the memory unit is GB: 0.5 == 512 MiB, 1 == 1024MiB. Let use can deliver humanized unit. For example, -r ram=512mb, -r ram=2.5GB.

Comments by Achimnol

I think stripping "b" would be better though we could optionally support it. (e.g., -r ram=512m)
Also, how about using -r mem=... instead of -r ram=...? (or maybe we could treat ram, mem, memory as the same key)

Cannot download file(s) from compute session

backend.ai download <sess-id> <filepath> command is broken with following traceback:

Traceback (most recent call last):c51362187f653ee5745d6906b...
  File "/Users/adrysn/.pyenv/versions/playground/bin/backend.ai", line 11, in <module>
    sys.exit(main())
  File "/Users/adrysn/.pyenv/versions/3.7.0/envs/playground/lib/python3.7/site-packages/ai/backend/client/cli/__init__.py", line 104, in main
    args.function(args)
  File "/Users/adrysn/.pyenv/versions/3.7.0/envs/playground/lib/python3.7/site-packages/ai/backend/client/cli/__init__.py", line 40, in wrapped
    handler(args)
  File "/Users/adrysn/.pyenv/versions/3.7.0/envs/playground/lib/python3.7/site-packages/ai/backend/client/cli/files.py", line 44, in download
    kernel.download(args.files, show_progress=True)
  File "/Users/adrysn/.pyenv/versions/3.7.0/envs/playground/lib/python3.7/site-packages/ai/backend/client/base.py", line 79, in _caller
    return self._handle_response(resp, gen)
  File "/Users/adrysn/.pyenv/versions/3.7.0/envs/playground/lib/python3.7/site-packages/ai/backend/client/base.py", line 37, in _handle_response
    meth_gen.send(resp)
  File "/Users/adrysn/.pyenv/versions/3.7.0/envs/playground/lib/python3.7/site-packages/ai/backend/client/kernel.py", line 191, in _download
    total=resp.stream_reader.total_bytes,
AttributeError: 'Response' object has no attribute 'stream_reader'

Meanwhile, downloading from vfolders is working.

Add X-Method-Override support

New Sorna gateway will support custom HTTP methods using X-Method-Override header.
Let's add a configuration option to enable to use this instead of directly sending custom HTTP methods.

Make optional parameters keyword-only

The current Python API has many optional parameters (with default values) but they can be passed as either positional or keyword arguments.

Let's restrict them to be passed as only keyword arguments, so that adding new arguments does not break existing Python API-based codes elsewhere.

Show username in config for session mode

It would be better to have a way to check username for logged in user.

  • Store config.json file in appdir in login with username information.
  • Delete config.json when logout.

쿼리 요청이 불가능합니다.

from ai.backend.client import Kernel로 되있으나 실제로는
from ai.backend.client.kernel import Kernel해야 작동이 됩니다.
또한 위와 같이 변경하여 아래와 같이 요청을 시도하여도

from ai.backend.client.kernel import Kernel

kern = Kernel.get_or_create('python', client_token='abcdef')
result = kern.execute('print("hello world")', mode='query')
print(result['console'])
kern.destroy()

AssertionError: You must use API wrapper functions via a Session object. 에러가 발생합니다.
get_or_create가 클래스 메소드이다보니 세션을 어느시점에 어떻게 넣어줘야 하는지 궁금합니다.

마지막으로 사소한 부분이긴 하나 실세 샘플 코드에서는 클라이언트 토큰이 'abc'로 적혀있는데
실제로 그대로 돌려볼경우
assert 4 <= len(client_token) <= 64, 'Client session token should be 4 to 64 characters long.'
에러가 막히지 않을까 싶습니다. 그래서 4자 이상으로 변경하시면 처음 시도하시는 분이 에러없이 테스트 해보실수 있지 않을까 싶습니다.

Basic API specification

  • Basic API specification
  • Requirements
    • Authentication method
    • Policy for anonymous users
    • Specification
    • REST API
    • XML-RPC API?
    • GET / POST simple API (for traditional users?)
    • WebSocket API for web terminals

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.