def _get_unit_name(name, client): try: name = Systemd._get_name(name, client, '/opt/OpenvStorage/config/templates/systemd/') except ValueError: try: name = Systemd._get_name(name, client) except ValueError: name = 'ovs-{0}'.format(name) client_ip = FleetCtl._get_client_ip(client) fleet_name = "{0}@{1}.service".format(name, client_ip) return fleet_name
def _get_unit_name(name, client): try: name = Systemd._get_name( name, client, '/opt/OpenvStorage/config/templates/systemd/') except ValueError: try: name = Systemd._get_name(name, client) except ValueError: name = 'ovs-{0}'.format(name) client_ip = FleetCtl._get_client_ip(client) fleet_name = "{0}@{1}.service".format(name, client_ip) return fleet_name
def get_service_status(name, client): if FleetCtl._has_service(name, client): fleet_name = FleetCtl._get_unit_name(name, client) unit = FleetCtl._get_unit(fleet_name) FleetCtl._logger.debug('Fleet unit {0} status {1}'.format(fleet_name, unit.as_dict()['currentState'])) return unit.as_dict()['currentState'] == 'launched' return Systemd.get_service_status(name, client)
def is_enabled(name, client): if FleetCtl._has_service(name, client): fleet_name = FleetCtl._get_unit_name(name, client) output = client.run( 'systemctl is-enabled {0} || true'.format(fleet_name)) return 'enabled' in output return Systemd.is_enabled(name, client)
def get_service_status(name, client): if FleetCtl._has_service(name, client): fleet_name = FleetCtl._get_unit_name(name, client) unit = FleetCtl._get_unit(fleet_name) FleetCtl._logger.debug('Fleet unit {0} status {1}'.format( fleet_name, unit.as_dict()['currentState'])) return unit.as_dict()['currentState'] == 'launched' return Systemd.get_service_status(name, client)
def add_service(name, client, params=None, target_name=None, additional_dependencies=None): """ This will generate a .service (temporary) file to feed to fleet to start a service service will become "name@<client.ip>.service" """ if params is None: params = {} if additional_dependencies is None: additional_dependencies = [] client_ip = FleetCtl._get_client_ip(client) if FleetCtl.has_service(name, client): logger.info('Not re-adding service {0} to machine {1}'.format( name, client_ip)) return name = Systemd._get_name( name, client, '/opt/OpenvStorage/config/templates/systemd/') template_service = '/opt/OpenvStorage/config/templates/systemd/{0}.service' if not client.file_exists(template_service.format(name)): # Given template doesn't exist so we are probably using system # init scripts return template_file = client.file_read(template_service.format(name)) for key, value in params.iteritems(): template_file = template_file.replace('<{0}>'.format(key), value) if '<SERVICE_NAME>' in template_file: service_name = name if target_name is None else target_name template_file = template_file.replace('<SERVICE_NAME>', service_name.lstrip('ovs-')) dependencies = '' for service in additional_dependencies: dependencies += '{0}.service '.format(service) template_file = template_file.replace('<ADDITIONAL_DEPENDENCIES>', dependencies) template_file += "\n[X-Fleet]\nMachineID={0}".format( FleetCtl._get_id_from_ip(client_ip)) fleet_name = "{0}@{1}.service".format(name, client_ip) logger.debug('Creating fleet unit {0} {1}'.format( fleet_name, template_file)) unit = FleetCtl._create_unit(fleet_name, template_file) time.sleep(1) FLEET_CLIENT.set_unit_desired_state(unit, 'loaded') time.sleep(1) unit = FleetCtl._get_unit(fleet_name) logger.info('Created unit {0}'.format(unit.as_dict()))
def add_service(name, client, params=None, target_name=None, additional_dependencies=None): """ This will generate a .service (temporary) file to feed to fleet to start a service service will become "name@<client.ip>.service" """ if params is None: params = {} if additional_dependencies is None: additional_dependencies = [] client_ip = FleetCtl._get_client_ip(client) if FleetCtl.has_service(name, client): FleetCtl._logger.info('Not re-adding service {0} to machine {1}'.format(name, client_ip)) return name = Systemd._get_name(name, client, '/opt/OpenvStorage/config/templates/systemd/') template_service = '/opt/OpenvStorage/config/templates/systemd/{0}.service' if not client.file_exists(template_service.format(name)): # Given template doesn't exist so we are probably using system # init scripts return template_file = client.file_read(template_service.format(name)) for key, value in params.iteritems(): template_file = template_file.replace('<{0}>'.format(key), value) if '<SERVICE_NAME>' in template_file: service_name = name if target_name is None else target_name template_file = template_file.replace('<SERVICE_NAME>', service_name.lstrip('ovs-')) template_file = template_file.replace('<_SERVICE_SUFFIX_>', '@{0}'.format(client_ip)) dependencies = '' for service in additional_dependencies: dependencies += '{0}@{1}.service '.format(service, client_ip) template_file = template_file.replace('<ADDITIONAL_DEPENDENCIES>', dependencies) template_file += "\n[X-Fleet]\nMachineID={0}".format(FleetCtl._get_id_from_ip(client_ip)) if target_name is not None: name = target_name fleet_name = "{0}@{1}.service".format(name, client_ip) FleetCtl._logger.debug('Creating fleet unit {0} {1}'.format(fleet_name, template_file)) unit = FleetCtl._create_unit(fleet_name, template_file) time.sleep(1) FLEET_CLIENT.set_unit_desired_state(unit, 'loaded') time.sleep(1) unit = FleetCtl._get_unit(fleet_name) FleetCtl._logger.info('Created unit {0}'.format(unit.as_dict()))
def remove_service(name, client): if FleetCtl._has_service(name, client): fleet_name = FleetCtl._get_unit_name(name, client) unit = FleetCtl._get_unit(fleet_name) FleetCtl.stop_service(name, client) result = FLEET_CLIENT.destroy_unit(unit) start = time.time() FleetCtl._logger.debug('Fleet destroy unit {0} {1}'.format(fleet_name, result)) while time.time() - start < 60: time.sleep(1) if FleetCtl._has_service(name, client) is False: return FleetCtl._logger.warning('Failed to remove unit {0} after 60 seconds'.format(fleet_name)) else: return Systemd.remove_service(name, client)
def stop_service(name, client): if FleetCtl._has_service(name, client): fleet_name = FleetCtl._get_unit_name(name, client) start = time.time() while time.time() - start < 60: unit = FleetCtl._get_unit(fleet_name) if unit.as_dict()['currentState'] != 'loaded': FLEET_CLIENT.set_unit_desired_state(unit, 'loaded') time.sleep(1) else: break unit = FleetCtl._get_unit(fleet_name) if unit['currentState'] != 'loaded': FleetCtl._logger.warning('Failed to stop unit {0}'.format(unit.as_dict())) FleetCtl._logger.debug('Fleet stop unit {0} {1}'.format(fleet_name, unit.as_dict()['currentState'])) return unit.as_dict()['currentState'] return Systemd.stop_service(name, client)
def remove_service(name, client): if FleetCtl._has_service(name, client): fleet_name = FleetCtl._get_unit_name(name, client) unit = FleetCtl._get_unit(fleet_name) FleetCtl.stop_service(name, client) result = FLEET_CLIENT.destroy_unit(unit) start = time.time() FleetCtl._logger.debug('Fleet destroy unit {0} {1}'.format( fleet_name, result)) while time.time() - start < 60: time.sleep(1) if FleetCtl._has_service(name, client) is False: return FleetCtl._logger.warning( 'Failed to remove unit {0} after 60 seconds'.format( fleet_name)) else: return Systemd.remove_service(name, client)
def stop_service(name, client): if FleetCtl._has_service(name, client): fleet_name = FleetCtl._get_unit_name(name, client) start = time.time() while time.time() - start < 60: unit = FleetCtl._get_unit(fleet_name) if unit.as_dict()['currentState'] != 'loaded': FLEET_CLIENT.set_unit_desired_state(unit, 'loaded') time.sleep(1) else: break unit = FleetCtl._get_unit(fleet_name) if unit['currentState'] != 'loaded': FleetCtl._logger.warning('Failed to stop unit {0}'.format( unit.as_dict())) FleetCtl._logger.debug('Fleet stop unit {0} {1}'.format( fleet_name, unit.as_dict()['currentState'])) return unit.as_dict()['currentState'] return Systemd.stop_service(name, client)
def enable_service(name, client): if FleetCtl._has_service(name, client): fleet_name = FleetCtl._get_unit_name(name, client) client.run('systemctl enable {0}'.format(fleet_name)) else: Systemd.enable_service(name, client)
def get_service_pid(name, client): return Systemd.get_service_pid(name, client)
def restart_service(name, client): if not FleetCtl.has_service(name, client): return Systemd.restart_service(name, client) FleetCtl.stop_service(name, client) FleetCtl.start_service(name, client) return FleetCtl.get_service_status(name, client)
def enable_service(name, client): if FleetCtl._has_service(name, client): fleet_name = FleetCtl._get_unit_name(name, client) client.run('systemctl enable {0}'.format(fleet_name)) else: Systemd.enable_service(name, client)
def has_service(name, client): fleet_has_service = FleetCtl._has_service(name, client) if not fleet_has_service: return Systemd.has_service(name, client) return fleet_has_service
def prepare_template(base_name, target_name, client): return Systemd.prepare_template(base_name, target_name, client)
def send_signal(name, signal, client): return Systemd.send_signal(name, signal, client)
def is_enabled(name, client): if FleetCtl._has_service(name, client): fleet_name = FleetCtl._get_unit_name(name, client) output = client.run('systemctl is-enabled {0} || true'.format(fleet_name)) return 'enabled' in output return Systemd.is_enabled(name, client)
def restart_service(name, client): if not FleetCtl.has_service(name, client): return Systemd.restart_service(name, client) FleetCtl.stop_service(name, client) FleetCtl.start_service(name, client) return FleetCtl.get_service_status(name, client)
def has_service(name, client): fleet_has_service = FleetCtl._has_service(name, client) if not fleet_has_service: return Systemd.has_service(name, client) return fleet_has_service
def send_signal(name, signal, client): return Systemd.send_signal(name, signal, client)
def get_service_pid(name, client): return Systemd.get_service_pid(name, client)
def is_enabled(name, client): return Systemd.is_enabled(name, client)
def enable_service(name, client): fleet_name = FleetCtl._get_unit_name(name, client) return Systemd.enable_service(fleet_name, client)
def _get_unit_name(name, client): name = Systemd._get_name( name, client, '/opt/OpenvStorage/config/templates/systemd/') client_ip = FleetCtl._get_client_ip(client) fleet_name = "{0}@{1}.service".format(name, client_ip) return fleet_name