def get_magma_services_status(): """ Get health for all the running services """ # DBus Unit objects: https://www.freedesktop.org/wiki/Software/systemd/dbus/ chan = ServiceRegistry.get_rpc_channel('magmad', ServiceRegistry.LOCAL) client = MagmadStub(chan) configs = client.GetConfigs(common_pb2.Void()) services_health_summary = [] for service_name in configs.configs_by_key: unit = Unit('magma@{}.service'.format(service_name), _autoload=True) active_state = ActiveState.dbus2state[unit.Unit.ActiveState] sub_state = str(unit.Unit.SubState, 'utf-8') if active_state == ActiveState.ACTIVE: pid = unit.Service.MainPID process = subprocess.Popen( 'ps -o etime= -p {}'.format(pid).split(), stdout=subprocess.PIPE) time_running, error = process.communicate() else: time_running = b'00' services_health_summary.append( ServiceHealth(service_name=service_name, active_state=active_state, sub_state=sub_state, time_running=str(time_running, 'utf-8').strip())) return services_health_summary
def ping(self, host, num_packets=4): chan = ServiceRegistry.get_rpc_channel('magmad', ServiceRegistry.LOCAL) client = MagmadStub(chan) response = client.RunNetworkTests( magmad_pb2.NetworkTestRequest(pings=[ magmad_pb2.PingParams(host_or_ip=host, num_packets=num_packets) ])) return response.pings
def _restart_service(service): print('Restarting %s...' % service) if service == 'magmad': _restart_all() return chan = ServiceRegistry.get_rpc_channel('magmad', ServiceRegistry.LOCAL) client = MagmadStub(chan) client.RestartServices(RestartServicesRequest(services=[service]))
def get_gateway_hw_id(): """ Get the hardware ID of the gateway. Is blocking. Returns: hw_id (str): hardware ID of the gateway specified by env variable GATEWAY_IP """ magmad_stub = MagmadStub(get_rpc_channel('magmad')) stub_response = magmad_stub.GetGatewayId(Void()) gateway_hw_id = stub_response.gateway_id return gateway_hw_id
def get_gateway_service_mconfigs(services: List[str]) -> Dict[str, Any]: """ Get the managed configurations of some gateway services. Args: services: List of service names to fetch configs for Returns: service mconfigs keyed by name """ ret = {} magmad_stub = MagmadStub(get_rpc_channel('magmad')) stub_response = magmad_stub.GetConfigs(Void()) for srv in services: ret[srv] = unpack_mconfig_any(stub_response.configs_by_key[srv]) return ret
class MagmadServiceGrpc(MagmadServiceClient): """ Handle magmad actions by making service calls over gRPC. """ def __init__(self): self._magmad_stub = MagmadStub(get_rpc_channel("magmad")) def restart_services(self, services): request = RestartServicesRequest() for s in services: request.services.append(s) try: self._magmad_stub.RestartServices(request) except grpc.RpcError as error: err_code = error.exception().code() if err_code == grpc.StatusCode.FAILED_PRECONDITION: logging.info("Ignoring FAILED_PRECONDITION exception") else: raise
def reset_gateway_mconfigs(): """ Delete the stored mconfigs from the gateway. """ magmad_stub = MagmadStub(get_rpc_channel('magmad')) magmad_stub.SetConfigs(mconfigs_pb2.GatewayConfigs())
def __init__(self): self._magmad_stub = MagmadStub(get_rpc_channel("magmad"))