Skip to content

Commit

Permalink
[sonic-package-manager] add generated service to /etc/sonic/generated…
Browse files Browse the repository at this point in the history
…_services.conf

Signed-off-by: Stepan Blyschak <stepanb@nvidia.com>
  • Loading branch information
stepanblyschak committed Nov 3, 2023
1 parent bf9c07c commit 47777c8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
36 changes: 36 additions & 0 deletions sonic_package_manager/service_creator/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import contextlib
import os
import sys
import shutil
import stat
import subprocess
import tempfile
from collections import defaultdict
from typing import Dict, Type, List

Expand Down Expand Up @@ -32,6 +34,8 @@

SYSTEMD_LOCATION = '/usr/lib/systemd/system'

GENERATED_SERVICES_CONF_FILE = '/etc/sonic/generated_services.conf'

SERVICE_MGMT_SCRIPT_TEMPLATE = 'service_mgmt.sh.j2'
SERVICE_MGMT_SCRIPT_LOCATION = '/usr/local/bin'

Expand Down Expand Up @@ -163,6 +167,7 @@ def create(self,
self.generate_service_mgmt(package)
self.update_dependent_list_file(package)
self.generate_systemd_service(package)
self.update_generated_services_conf_file(package)
self.generate_dump_script(package)
self.generate_service_reconciliation_file(package)
self.install_yang_module(package)
Expand Down Expand Up @@ -199,6 +204,7 @@ def remove(self,
remove_if_exists(os.path.join(DEBUG_DUMP_SCRIPT_LOCATION, f'{name}'))
remove_if_exists(os.path.join(ETC_SONIC_PATH, f'{name}_reconcile'))
self.update_dependent_list_file(package, remove=True)
self.update_dependent_list_file(package, remove=True)

if deregister_feature and not keep_config:
self.remove_config(package)
Expand Down Expand Up @@ -320,6 +326,36 @@ def generate_systemd_service(self, package: Package):
render_template(template, output_file, template_vars)
log.info(f'generated {output_file}')

def update_generated_services_conf_file(self, package: Package, remove=False):
""" Updates generated_services.conf file.
Args:
package: Package to update generated_services.conf with.
remove: True if update for removal process.
Returns:
None.
"""
name = package.manifest['service']['name']
asic_service= package.manifest['service']['asic-service']

with open(GENERATED_SERVICES_CONF_FILE, 'r') as generated_services_conf_file:
list_of_services = set(generated_services_conf_file.read().split())

if not remove:
list_of_services.add(f'{name}.service')
if asic_service:
list_of_services.add(f'{name}@.service')
else:
list_of_services.discard(f'{name}.service')
list_of_services.discard(f'{name}@.service')

# Write to tmp file and replace the original file with it
with tempfile.NamedTemporaryFile('w', delete=False) as tmp:
tmp.write('\n'.join(list_of_services))
tmp.flush()

shutil.move(tmp.name, GENERATED_SERVICES_CONF_FILE)

def update_dependent_list_file(self, package: Package, remove=False):
""" This function updates dependent list file for packages listed in "dependent-of"
(path: /etc/sonic/<service>_dependent file).
Expand Down
1 change: 1 addition & 0 deletions tests/sonic_package_manager/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ def sonic_fs(fs):
fs.create_dir(SYSTEMD_LOCATION)
fs.create_dir(DOCKER_CTL_SCRIPT_LOCATION)
fs.create_dir(SERVICE_MGMT_SCRIPT_LOCATION)
fs.create_file(GENERATED_SERVICES_CONF_FILE)
fs.create_file(os.path.join(TEMPLATES_PATH, SERVICE_FILE_TEMPLATE))
fs.create_file(os.path.join(TEMPLATES_PATH, TIMER_UNIT_TEMPLATE))
fs.create_file(os.path.join(TEMPLATES_PATH, SERVICE_MGMT_SCRIPT_TEMPLATE))
Expand Down
2 changes: 2 additions & 0 deletions tests/sonic_package_manager/test_service_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def service_creator(mock_feature_registry,

def test_service_creator(sonic_fs, manifest, service_creator, package_manager):
entry = PackageEntry('test', 'azure/sonic-test')
manifest['service']['asic-service'] = True
package = Package(entry, Metadata(manifest))
installed_packages = package_manager._get_installed_packages_and(package)
service_creator.create(package)
Expand All @@ -112,6 +113,7 @@ def read_file(name):
assert read_file('warm-reboot_order') == 'swss teamd test syncd'
assert read_file('fast-reboot_order') == 'teamd test swss syncd'
assert read_file('test_reconcile') == 'test-process test-process-3'
assert set(read_file('generated_services.conf').split()) == set(['test.service', 'test@.service'])


def test_service_creator_with_timer_unit(sonic_fs, manifest, service_creator):
Expand Down

0 comments on commit 47777c8

Please sign in to comment.