<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import os <EOL> from salt . ext . six import string_types <EOL> from salt . ext . six . moves import configparser <EOL> import salt . utils <EOL> from salt . exceptions import CommandExecutionError , CommandNotFoundError <EOL> def __virtual__ ( ) : <EOL> return True <EOL> def _get_supervisorctl_bin ( bin_env ) : <EOL> '''<STR_LIT>''' <EOL> cmd = '<STR_LIT>' <EOL> if not bin_env : <EOL> which_result = __salt__ [ '<STR_LIT>' ] ( [ cmd ] ) <EOL> if which_result is None : <EOL> raise CommandNotFoundError ( <EOL> '<STR_LIT>' . format ( cmd ) <EOL> ) <EOL> return which_result <EOL> if os . path . isdir ( bin_env ) : <EOL> cmd_bin = os . path . join ( bin_env , '<STR_LIT>' , cmd ) <EOL> if os . path . isfile ( cmd_bin ) : <EOL> return cmd_bin <EOL> raise CommandNotFoundError ( '<STR_LIT>' . format ( cmd ) ) <EOL> return bin_env <EOL> def _ctl_cmd ( cmd , name , conf_file , bin_env ) : <EOL> '''<STR_LIT>''' <EOL> ret = [ _get_supervisorctl_bin ( bin_env ) ] <EOL> if conf_file is not None : <EOL> ret += [ '<STR_LIT:-c>' , conf_file ] <EOL> ret . append ( cmd ) <EOL> if name : <EOL> ret . append ( name ) <EOL> return ret <EOL> def _get_return ( ret ) : <EOL> if ret [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> return ret [ '<STR_LIT>' ] <EOL> else : <EOL> return '<STR_LIT>' <EOL> def start ( name = '<STR_LIT:all>' , user = None , conf_file = None , bin_env = None ) : <EOL> '''<STR_LIT>''' <EOL> if name . endswith ( '<STR_LIT>' ) : <EOL> name = name [ : - <NUM_LIT:1> ] <EOL> ret = __salt__ [ '<STR_LIT>' ] ( <EOL> _ctl_cmd ( '<STR_LIT:start>' , name , conf_file , bin_env ) , <EOL> runas = user , <EOL> python_shell = False , <EOL> ) <EOL> return _get_return ( ret ) <EOL> def restart ( name = '<STR_LIT:all>' , user = None , conf_file = None , bin_env = None ) : <EOL> '''<STR_LIT>''' <EOL> if name . endswith ( '<STR_LIT>' ) : <EOL> name = name [ : - <NUM_LIT:1> ] <EOL> ret = __salt__ [ '<STR_LIT>' ] ( <EOL> _ctl_cmd ( '<STR_LIT>' , name , conf_file , bin_env ) , <EOL> runas = user , <EOL> python_shell = False , <EOL> ) <EOL> return _get_return ( ret ) <EOL> def stop ( name = '<STR_LIT:all>' , user = None , conf_file = None , bin_env = None ) : <EOL> '''<STR_LIT>''' <EOL> if name . endswith ( '<STR_LIT>' ) : <EOL> name = name [ : - <NUM_LIT:1> ] <EOL> ret = __salt__ [ '<STR_LIT>' ] ( <EOL> _ctl_cmd ( '<STR_LIT>' , name , conf_file , bin_env ) , <EOL> runas = user , <EOL> python_shell = False , <EOL> ) <EOL> return _get_return ( ret ) <EOL> def add ( name , user = None , conf_file = None , bin_env = None ) : <EOL> '''<STR_LIT>''' <EOL> if name . endswith ( '<STR_LIT::>' ) : <EOL> name = name [ : - <NUM_LIT:1> ] <EOL> elif name . endswith ( '<STR_LIT>' ) : <EOL> name = name [ : - <NUM_LIT:2> ] <EOL> ret = __salt__ [ '<STR_LIT>' ] ( <EOL> _ctl_cmd ( '<STR_LIT>' , name , conf_file , bin_env ) , <EOL> runas = user , <EOL> python_shell = False , <EOL> ) <EOL> return _get_return ( ret ) <EOL> def remove ( name , user = None , conf_file = None , bin_env = None ) : <EOL> '''<STR_LIT>''' <EOL> if name . endswith ( '<STR_LIT::>' ) : <EOL> name = name [ : - <NUM_LIT:1> ] <EOL> elif name . endswith ( '<STR_LIT>' ) : <EOL> name = name [ : - <NUM_LIT:2> ] <EOL> ret = __salt__ [ '<STR_LIT>' ] ( <EOL> _ctl_cmd ( '<STR_LIT>' , name , conf_file , bin_env ) , <EOL> runas = user , <EOL> python_shell = False , <EOL> ) <EOL> return _get_return ( ret ) <EOL> def reread ( user = None , conf_file = None , bin_env = None ) : <EOL> '''<STR_LIT>''' <EOL> ret = __salt__ [ '<STR_LIT>' ] ( <EOL> _ctl_cmd ( '<STR_LIT>' , None , conf_file , bin_env ) , <EOL> runas = user , <EOL> python_shell = False , <EOL> ) <EOL> return _get_return ( ret ) <EOL> def update ( user = None , conf_file = None , bin_env = None , name = None ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( name , string_types ) : <EOL> if name . endswith ( '<STR_LIT::>' ) : <EOL> name = name [ : - <NUM_LIT:1> ] <EOL> elif name . endswith ( '<STR_LIT>' ) : <EOL> name = name [ : - <NUM_LIT:2> ] <EOL> ret = __salt__ [ '<STR_LIT>' ] ( <EOL> _ctl_cmd ( '<STR_LIT>' , name , conf_file , bin_env ) , <EOL> runas = user , <EOL> python_shell = False , <EOL> ) <EOL> return _get_return ( ret ) <EOL> def status ( name = None , user = None , conf_file = None , bin_env = None ) : <EOL> '''<STR_LIT>''' <EOL> all_process = { } <EOL> for line in status_raw ( name , user , conf_file , bin_env ) . splitlines ( ) : <EOL> if len ( line . split ( ) ) > <NUM_LIT:2> : <EOL> process , state , reason = line . split ( None , <NUM_LIT:2> ) <EOL> else : <EOL> process , state , reason = line . split ( ) + [ '<STR_LIT>' ] <EOL> all_process [ process ] = { '<STR_LIT:state>' : state , '<STR_LIT>' : reason } <EOL> return all_process <EOL> def status_raw ( name = None , user = None , conf_file = None , bin_env = None ) : <EOL> '''<STR_LIT>''' <EOL> ret = __salt__ [ '<STR_LIT>' ] ( <EOL> _ctl_cmd ( '<STR_LIT:status>' , name , conf_file , bin_env ) , <EOL> runas = user , <EOL> python_shell = False , <EOL> ) <EOL> return _get_return ( ret ) <EOL> def custom ( command , user = None , conf_file = None , bin_env = None ) : <EOL> '''<STR_LIT>''' <EOL> ret = __salt__ [ '<STR_LIT>' ] ( <EOL> _ctl_cmd ( command , None , conf_file , bin_env ) , <EOL> runas = user , <EOL> python_shell = False , <EOL> ) <EOL> return _get_return ( ret ) <EOL> def _read_config ( conf_file = None ) : <EOL> '''<STR_LIT>''' <EOL> if conf_file is None : <EOL> paths = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for path in paths : <EOL> if os . path . exists ( path ) : <EOL> conf_file = path <EOL> break <EOL> if conf_file is None : <EOL> raise CommandExecutionError ( '<STR_LIT>' ) <EOL> config = configparser . ConfigParser ( ) <EOL> try : <EOL> config . read ( conf_file ) <EOL> except ( IOError , OSError ) as exc : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( conf_file , exc ) <EOL> ) <EOL> return config <EOL> def options ( name , conf_file = None ) : <EOL> '''<STR_LIT>''' <EOL> config = _read_config ( conf_file ) <EOL> section_name = '<STR_LIT>' . format ( name ) <EOL> if section_name not in config . sections ( ) : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( name ) ) <EOL> ret = { } <EOL> for key , val in config . items ( section_name ) : <EOL> val = salt . utils . str_to_num ( val . split ( '<STR_LIT:;>' ) [ <NUM_LIT:0> ] . strip ( ) ) <EOL> if isinstance ( val , string_types ) : <EOL> if val . lower ( ) == '<STR_LIT:true>' : <EOL> val = True <EOL> elif val . lower ( ) == '<STR_LIT:false>' : <EOL> val = False <EOL> ret [ key ] = val <EOL> return ret </s>
