def install(cls, data_provider_options={}): """ Installs the launchd agent into ~/Library/LaunchAgents Args: data_provider_options (dict): A dict containing the following key/value pairs: 'provider': Name of the blockchain data provider to use. 'api_key_id': The API key to use (if required by the provider). 'api_key_secret': The API secret to use (if required) Returns: bool: True if the daemon was successfully installed, False otherwise. """ dpo = data_provider_options rv = False if cls.installed(): rv = True else: # Figure out data provider stuff env_vars = {} if dpo.get('provider', None) == 'chain': env_vars["CHAIN_API_KEY_ID"] = dpo['api_key_id'] env_vars["CHAIN_API_KEY_SECRET"] = dpo['api_key_secret'] try: cls._create_plist_file(env_vars) rv = cls.installed() except WalletError as e: raise DaemonizerError("Couldn't install launchd agent: %s" % e) return rv
def _write_service_file(cls, text): """ Writes a file containing the parameters required for systemd to daemonize the process. Args: text (str): The complete text that should be written to the file. Returns: bool: True if the file was successfully created and written to, False otherwise. """ rv = False with tempfile.NamedTemporaryFile() as tf: tf.write(text.encode()) tf.flush() try: subprocess.check_output( ["sudo", "cp", tf.name, cls.SERVICE_FILE_PATH]) subprocess.check_output( ["sudo", "chmod", "644", cls.SERVICE_FILE_PATH]) rv = cls.SERVICE_FILE_PATH.exists() except subprocess.CalledProcessError as e: raise DaemonizerError("Couldn't create service file: %s" % (e)) return rv
def _write_env_file(cls, text): """ Writes a file containing environment variables that should be set to run the daemon. Args: text (str): The complete text that should be written to the file. Returns: bool: True if the file was successfully created and written to, False otherwise. """ rv = False with tempfile.NamedTemporaryFile() as tf: tf.write(text.encode()) tf.flush() try: subprocess.check_output( ["sudo", "mkdir", "-p", cls.ENV_FILE_PATH.dirname()]) subprocess.check_output( ["sudo", "cp", tf.name, cls.ENV_FILE_PATH]) subprocess.check_output( ["sudo", "chmod", "644", cls.ENV_FILE_PATH]) rv = cls.ENV_FILE_PATH.exists() except subprocess.CalledProcessError as e: raise DaemonizerError("Couldn't create environment file: %s" % (e)) return rv
def disable(cls): """ Disables the daemon within systemd. Returns: bool: True if the daemon was successfully disabled, False otherwise. """ rv = False try: subprocess.check_output( ["systemctl", "--user", "disable", cls.SERVICE_NAME], stderr=subprocess.STDOUT) rv = not cls.enabled() except subprocess.CalledProcessError as e: raise DaemonizerError("Couldn't disable service: %s" % e) return rv
def stop(cls): """ Stops the agent. Returns: bool: True if the agent was successfully stopped, False otherwise. """ rv = True if cls.started(): try: subprocess.check_output( ['launchctl', 'unload', '-w', cls.PLIST_FILE_PATH], stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: raise DaemonizerError( "Couldn't disable daemon using launchctl: %s" % e) return rv
def stop(cls): """ Stops the daemon. Returns: bool: True if the daemon was successfully stopped, False otherwise. """ rv = False if cls.started(): try: subprocess.check_output( ["systemctl", "--user", "stop", cls.SERVICE_NAME]) rv = not cls.started() except subprocess.CalledProcessError as e: raise DaemonizerError("Couldn't stop service: %s" % e) return rv