<s> import json <EOL> from distutils . version import LooseVersion <EOL> from dcos import http , util <EOL> from dcos . errors import DCOSException , DCOSHTTPException <EOL> from six . moves import urllib <EOL> logger = util . get_logger ( __name__ ) <EOL> def create_client ( config = None ) : <EOL> """<STR_LIT>""" <EOL> if config is None : <EOL> config = util . get_config ( ) <EOL> marathon_url = _get_marathon_url ( config ) <EOL> timeout = config . get ( '<STR_LIT>' , http . DEFAULT_TIMEOUT ) <EOL> logger . info ( '<STR_LIT>' , marathon_url ) <EOL> return Client ( marathon_url , timeout = timeout ) <EOL> def _get_marathon_url ( config ) : <EOL> """<STR_LIT>""" <EOL> marathon_url = config . get ( '<STR_LIT>' ) <EOL> if marathon_url is None : <EOL> dcos_url = util . get_config_vals ( [ '<STR_LIT>' ] , config ) [ <NUM_LIT:0> ] <EOL> marathon_url = urllib . parse . urljoin ( dcos_url , '<STR_LIT>' ) <EOL> return marathon_url <EOL> def _to_exception ( response ) : <EOL> """<STR_LIT>""" <EOL> if response . status_code == <NUM_LIT> : <EOL> msg = '<STR_LIT>' . format ( <EOL> response . request . method , <EOL> response . request . url , <EOL> response . status_code , <EOL> response . reason ) <EOL> try : <EOL> json_msg = response . json ( ) <EOL> msg += '<STR_LIT>' + json . dumps ( json_msg , <EOL> indent = <NUM_LIT:2> , <EOL> sort_keys = True , <EOL> separators = ( '<STR_LIT:U+002C>' , '<STR_LIT>' ) ) <EOL> except ValueError : <EOL> pass <EOL> return DCOSException ( msg ) <EOL> elif response . status_code == <NUM_LIT> : <EOL> return DCOSException ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> try : <EOL> response_json = response . json ( ) <EOL> except Exception : <EOL> logger . exception ( <EOL> '<STR_LIT>' , <EOL> response ) <EOL> return DCOSException ( <EOL> '<STR_LIT>' . format ( <EOL> response . request . url , response . status_code , response . reason ) ) <EOL> message = response_json . get ( '<STR_LIT:message>' ) <EOL> if message is None : <EOL> errs = response_json . get ( '<STR_LIT>' ) <EOL> if errs is None : <EOL> logger . error ( <EOL> '<STR_LIT>' , <EOL> response_json ) <EOL> return DCOSException ( _default_marathon_error ( ) ) <EOL> msg = '<STR_LIT:\n>' . join ( error [ '<STR_LIT:error>' ] for error in errs ) <EOL> return DCOSException ( _default_marathon_error ( msg ) ) <EOL> return DCOSException ( '<STR_LIT>' . format ( message ) ) <EOL> def _http_req ( fn , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return fn ( * args , ** kwargs ) <EOL> except DCOSHTTPException as e : <EOL> raise _to_exception ( e . response ) <EOL> class Client ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , marathon_url , timeout = http . DEFAULT_TIMEOUT ) : <EOL> self . _base_url = marathon_url <EOL> self . _timeout = timeout <EOL> min_version = "<STR_LIT>" <EOL> version = LooseVersion ( self . get_about ( ) [ "<STR_LIT:version>" ] ) <EOL> self . _version = version <EOL> if version < LooseVersion ( min_version ) : <EOL> msg = ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) . format ( <EOL> version , <EOL> min_version ) <EOL> raise DCOSException ( msg ) <EOL> def _create_url ( self , path ) : <EOL> """<STR_LIT>""" <EOL> return urllib . parse . urljoin ( self . _base_url , path ) <EOL> def get_version ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _version <EOL> def get_about ( self ) : <EOL> """<STR_LIT>""" <EOL> url = self . _create_url ( '<STR_LIT>' ) <EOL> response = _http_req ( http . get , url , timeout = self . _timeout ) <EOL> return response . json ( ) <EOL> def get_app ( self , app_id , version = None ) : <EOL> """<STR_LIT>""" <EOL> app_id = self . normalize_app_id ( app_id ) <EOL> if version is None : <EOL> url = self . _create_url ( '<STR_LIT>' . format ( app_id ) ) <EOL> else : <EOL> url = self . _create_url ( <EOL> '<STR_LIT>' . format ( app_id , version ) ) <EOL> response = _http_req ( http . get , url , timeout = self . _timeout ) <EOL> if version is None : <EOL> return response . json ( ) [ '<STR_LIT>' ] <EOL> else : <EOL> return response . json ( ) <EOL> def get_groups ( self ) : <EOL> """<STR_LIT>""" <EOL> url = self . _create_url ( '<STR_LIT>' ) <EOL> response = _http_req ( http . get , url , timeout = self . _timeout ) <EOL> return response . json ( ) [ '<STR_LIT>' ] <EOL> def get_group ( self , group_id , version = None ) : <EOL> """<STR_LIT>""" <EOL> group_id = self . normalize_app_id ( group_id ) <EOL> if version is None : <EOL> url = self . _create_url ( '<STR_LIT>' . format ( group_id ) ) <EOL> else : <EOL> url = self . _create_url ( <EOL> '<STR_LIT>' . format ( group_id , version ) ) <EOL> response = _http_req ( http . get , url , timeout = self . _timeout ) <EOL> return response . json ( ) <EOL> def get_app_versions ( self , app_id , max_count = None ) : <EOL> """<STR_LIT>""" <EOL> if max_count is not None and max_count <= <NUM_LIT:0> : <EOL> raise DCOSException ( <EOL> '<STR_LIT>' . format ( max_count ) <EOL> ) <EOL> app_id = self . normalize_app_id ( app_id ) <EOL> url = self . _create_url ( '<STR_LIT>' . format ( app_id ) ) <EOL> response = _http_req ( http . get , url , timeout = self . _timeout ) <EOL> if max_count is None : <EOL> return response . json ( ) [ '<STR_LIT>' ] <EOL> else : <EOL> return response . json ( ) [ '<STR_LIT>' ] [ : max_count ] <EOL> def get_apps ( self ) : <EOL> """<STR_LIT>""" <EOL> url = self . _create_url ( '<STR_LIT>' ) <EOL> response = _http_req ( http . get , url , timeout = self . _timeout ) <EOL> return response . json ( ) [ '<STR_LIT>' ] <EOL> def get_apps_for_framework ( self , framework_name ) : <EOL> """<STR_LIT>""" <EOL> return [ app for app in self . get_apps ( ) <EOL> if app . get ( '<STR_LIT>' , { } ) . get ( <EOL> '<STR_LIT>' ) == framework_name ] <EOL> def add_app ( self , app_resource ) : <EOL> """<STR_LIT>""" <EOL> url = self . _create_url ( '<STR_LIT>' ) <EOL> if hasattr ( app_resource , '<STR_LIT>' ) : <EOL> app_json = json . load ( app_resource ) <EOL> else : <EOL> app_json = app_resource <EOL> response = _http_req ( http . post , url , <EOL> json = app_json , <EOL> timeout = self . _timeout ) <EOL> return response . json ( ) <EOL> def _update ( self , resource_id , payload , force = None , url_endpoint = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> resource_id = self . normalize_app_id ( resource_id ) <EOL> if not force : <EOL> params = None <EOL> else : <EOL> params = { '<STR_LIT>' : '<STR_LIT:true>' } <EOL> url = self . _create_url ( '<STR_LIT>' . format ( url_endpoint , resource_id ) ) <EOL> response = _http_req ( http . put , url , <EOL> params = params , <EOL> json = payload , <EOL> timeout = self . _timeout ) <EOL> return response . json ( ) . get ( '<STR_LIT>' ) <EOL> def update_app ( self , app_id , payload , force = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _update ( app_id , payload , force ) <EOL> def update_group ( self , group_id , payload , force = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _update ( group_id , payload , force , "<STR_LIT>" ) <EOL> def scale_app ( self , app_id , instances , force = None ) : <EOL> """<STR_LIT>""" <EOL> app_id = self . normalize_app_id ( app_id ) <EOL> if not force : <EOL> params = None <EOL> else : <EOL> params = { '<STR_LIT>' : '<STR_LIT:true>' } <EOL> url = self . _create_url ( '<STR_LIT>' . format ( app_id ) ) <EOL> response = _http_req ( http . put , <EOL> url , <EOL> params = params , <EOL> json = { '<STR_LIT>' : int ( instances ) } , <EOL> timeout = self . _timeout ) <EOL> deployment = response . json ( ) [ '<STR_LIT>' ] <EOL> return deployment <EOL> def scale_group ( self , group_id , scale_factor , force = None ) : <EOL> """<STR_LIT>""" <EOL> group_id = self . normalize_app_id ( group_id ) <EOL> if not force : <EOL> params = None <EOL> else : <EOL> params = { '<STR_LIT>' : '<STR_LIT:true>' } <EOL> url = self . _create_url ( '<STR_LIT>' . format ( group_id ) ) <EOL> response = http . put ( url , <EOL> params = params , <EOL> json = { '<STR_LIT>' : scale_factor } , <EOL> timeout = self . _timeout ) <EOL> deployment = response . json ( ) [ '<STR_LIT>' ] <EOL> return deployment <EOL> def stop_app ( self , app_id , force = None ) : <EOL> """<STR_LIT>""" <EOL> return self . scale_app ( app_id , <NUM_LIT:0> , force ) <EOL> def remove_app ( self , app_id , force = None ) : <EOL> """<STR_LIT>""" <EOL> app_id = self . normalize_app_id ( app_id ) <EOL> if not force : <EOL> params = None <EOL> else : <EOL> params = { '<STR_LIT>' : '<STR_LIT:true>' } <EOL> url = self . _create_url ( '<STR_LIT>' . format ( app_id ) ) <EOL> _http_req ( http . delete , url , params = params , timeout = self . _timeout ) <EOL> def remove_group ( self , group_id , force = None ) : <EOL> """<STR_LIT>""" <EOL> group_id = self . normalize_app_id ( group_id ) <EOL> if not force : <EOL> params = None <EOL> else : <EOL> params = { '<STR_LIT>' : '<STR_LIT:true>' } <EOL> url = self . _create_url ( '<STR_LIT>' . format ( group_id ) ) <EOL> _http_req ( http . delete , url , params = params , timeout = self . _timeout ) <EOL> def kill_tasks ( self , app_id , scale = None , host = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> app_id = self . normalize_app_id ( app_id ) <EOL> if host : <EOL> params [ '<STR_LIT:host>' ] = host <EOL> if scale : <EOL> params [ '<STR_LIT>' ] = scale <EOL> url = self . _create_url ( '<STR_LIT>' . format ( app_id ) ) <EOL> response = _http_req ( http . delete , url , <EOL> params = params , <EOL> timeout = self . _timeout ) <EOL> return response . json ( ) <EOL> def restart_app ( self , app_id , force = None ) : <EOL> """<STR_LIT>""" <EOL> app_id = self . normalize_app_id ( app_id ) <EOL> if not force : <EOL> params = None <EOL> else : <EOL> params = { '<STR_LIT>' : '<STR_LIT:true>' } <EOL> url = self . _create_url ( '<STR_LIT>' . format ( app_id ) ) <EOL> response = _http_req ( http . post , url , <EOL> params = params , <EOL> timeout = self . _timeout ) <EOL> return response . json ( ) <EOL> def get_deployment ( self , deployment_id ) : <EOL> """<STR_LIT>""" <EOL> url = self . _create_url ( '<STR_LIT>' ) <EOL> response = _http_req ( http . get , url , timeout = self . _timeout ) <EOL> deployment = next ( <EOL> ( deployment for deployment in response . json ( ) <EOL> if deployment_id == deployment [ '<STR_LIT:id>' ] ) , <EOL> None ) <EOL> return deployment <EOL> def get_deployments ( self , app_id = None ) : <EOL> """<STR_LIT>""" <EOL> url = self . _create_url ( '<STR_LIT>' ) <EOL> response = _http_req ( http . get , url , timeout = self . _timeout ) <EOL> if app_id is not None : <EOL> app_id = self . normalize_app_id ( app_id ) <EOL> deployments = [ <EOL> deployment for deployment in response . json ( ) <EOL> if app_id in deployment [ '<STR_LIT>' ] <EOL> ] <EOL> else : <EOL> deployments = response . json ( ) <EOL> return deployments <EOL> def _cancel_deployment ( self , deployment_id , force ) : <EOL> """<STR_LIT>""" <EOL> if not force : <EOL> params = None <EOL> else : <EOL> params = { '<STR_LIT>' : '<STR_LIT:true>' } <EOL> url = self . _create_url ( '<STR_LIT>' . format ( deployment_id ) ) <EOL> response = _http_req ( http . delete , url , <EOL> params = params , <EOL> timeout = self . _timeout ) <EOL> if force : <EOL> return None <EOL> else : <EOL> return response . json ( ) <EOL> def rollback_deployment ( self , deployment_id ) : <EOL> """<STR_LIT>""" <EOL> return self . _cancel_deployment ( deployment_id , False ) <EOL> def stop_deployment ( self , deployment_id ) : <EOL> """<STR_LIT>""" <EOL> self . _cancel_deployment ( deployment_id , True ) <EOL> def get_tasks ( self , app_id ) : <EOL> """<STR_LIT>""" <EOL> url = self . _create_url ( '<STR_LIT>' ) <EOL> response = _http_req ( http . get , url , timeout = self . _timeout ) <EOL> if app_id is not None : <EOL> app_id = self . normalize_app_id ( app_id ) <EOL> tasks = [ <EOL> task for task in response . json ( ) [ '<STR_LIT>' ] <EOL> if app_id == task [ '<STR_LIT>' ] <EOL> ] <EOL> else : <EOL> tasks = response . json ( ) [ '<STR_LIT>' ] <EOL> return tasks <EOL> def get_task ( self , task_id ) : <EOL> """<STR_LIT>""" <EOL> url = self . _create_url ( '<STR_LIT>' ) <EOL> response = _http_req ( http . get , url , timeout = self . _timeout ) <EOL> task = next ( <EOL> ( task for task in response . json ( ) [ '<STR_LIT>' ] <EOL> if task_id == task [ '<STR_LIT:id>' ] ) , <EOL> None ) <EOL> return task <EOL> def stop_task ( self , task_id , wipe = None ) : <EOL> """<STR_LIT>""" <EOL> if not wipe : <EOL> params = None <EOL> else : <EOL> params = { '<STR_LIT>' : '<STR_LIT:true>' } <EOL> url = self . _create_url ( '<STR_LIT>' ) <EOL> response = _http_req ( http . post , <EOL> url , <EOL> params = params , <EOL> json = { '<STR_LIT>' : [ task_id ] } , <EOL> timeout = self . _timeout ) <EOL> task = next ( <EOL> ( task for task in response . json ( ) [ '<STR_LIT>' ] <EOL> if task_id == task [ '<STR_LIT:id>' ] ) , <EOL> None ) <EOL> return task <EOL> def get_app_schema ( self ) : <EOL> """<STR_LIT>""" <EOL> version = self . get_version ( ) <EOL> schema_version = LooseVersion ( "<STR_LIT>" ) <EOL> if version < schema_version : <EOL> return None <EOL> url = self . _create_url ( '<STR_LIT>' ) <EOL> response = _http_req ( http . get , url , timeout = self . _timeout ) <EOL> return response . json ( ) <EOL> def normalize_app_id ( self , app_id ) : <EOL> """<STR_LIT>""" <EOL> return urllib . parse . quote ( '<STR_LIT:/>' + app_id . strip ( '<STR_LIT:/>' ) ) <EOL> def create_group ( self , group_resource ) : <EOL> """<STR_LIT>""" <EOL> url = self . _create_url ( '<STR_LIT>' ) <EOL> if hasattr ( group_resource , '<STR_LIT>' ) : <EOL> group_json = json . load ( group_resource ) <EOL> else : <EOL> group_json = group_resource <EOL> response = _http_req ( http . post , url , <EOL> json = group_json , <EOL> timeout = self . _timeout ) <EOL> return response . json ( ) <EOL> def get_leader ( self ) : <EOL> """<STR_LIT>""" <EOL> url = self . _create_url ( '<STR_LIT>' ) <EOL> response = _http_req ( http . get , url , timeout = self . _timeout ) <EOL> return response . json ( ) [ '<STR_LIT>' ] <EOL> def _default_marathon_error ( message = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> return ( "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( <EOL> message ) </s>
