from ..OpsiServer.ApplyOpsiRepositories import ApplyOpsiRepositories
from ..OpsiServer.ApplyRepositoryKey import ApplyRepositoryKey
from ..OpsiServer.SetRepositoryUrl import SetRepositoryUrl
from ...SetupSteps.Playbooks.Playbook import Playbook

REPO_BASE_DIR = '/etc/apt/sources.list.d/'
REPO_FILE_NAME_OPSI = f'{REPO_BASE_DIR}opsi.list'
REPO_FILE_NAME_GRAFANA = f'{REPO_BASE_DIR}grafana.list'

OPSI_REPO_URL = 'https://download.opensuse.org/repositories/home:/uibmz:/opsi:/4.3:/stable/Univention_5.2'
OPSI_KEY_URL = 'https://download.opensuse.org/repositories/home:/uibmz:/opsi:/4.3:/stable/Univention_5.2/Release.key'
OPSI_KEY_PATH = '/usr/local/share/keyrings/opsi.gpg'
OPSI_REPOSITORY = '/'
GRAFANA_REPO_URL = 'https://packages.grafana.com/oss/deb'
GRAFANA_KEY_URL = 'https://packages.grafana.com/gpg.key'
GRAFANA_KEY_PATH = '/usr/local/share/keyrings/grafana.gpg'
GRAFANA_REPOSITORY = 'stable main'

class UpdateOpsiRepositories(Playbook):

    def start(self):
        self.run_steps(ApplyRepositoryKey(OPSI_KEY_URL, OPSI_KEY_PATH),
                       SetRepositoryUrl(OPSI_REPO_URL, OPSI_KEY_PATH, OPSI_REPOSITORY, REPO_FILE_NAME_OPSI),
                       ApplyRepositoryKey(GRAFANA_KEY_URL, GRAFANA_KEY_PATH),
                       SetRepositoryUrl(GRAFANA_REPO_URL, GRAFANA_KEY_PATH, GRAFANA_REPOSITORY, REPO_FILE_NAME_GRAFANA),
                       ApplyOpsiRepositories()
                       )