File: //proc/thread-self/root/usr/sbin/osms
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
# Licensed under the Universal Permissive License v 1.0 as shown at https://opensource.org/licenses/UPL.
import glob
import os
import re
import shutil
import socket
import sys
sys.path.insert(0, '/usr/share/oracle-cloud-agent/plugins/osms')
from osms import cli
from osms import config
from osms import distro
from osms import transaction
from osms import utils
from osms.i18n import sstr
from osms.server import OsmsServer
def get_distro_information():
ts = transaction.initReadOnlyTransaction()
for release_package in ["oraclelinux-release", "redhat-release"]:
for h in ts.dbMatch('Providename', release_package):
sysrelver = 'system-release(releasever)'
version = sstr(h['version'])
release = sstr(h['release'])
if sysrelver in h['providename']:
provides = dict(zip(h['providename'], h['provideversion']))
release = '%s-%s' % (version, release)
version = provides[sysrelver]
return sstr(h['name']), version, release
raise Exception('Could not get OS distro informaton')
def disable_plugin():
utils.update_file('/etc/yum/pluginconf.d/osmsplugin.conf', 'enabled = 1', 'enabled = 0')
def enable_plugin():
utils.update_file('/etc/yum/pluginconf.d/osmsplugin.conf', 'enabled = 0', 'enabled = 1')
def backup_yum_repos():
ignored_repo_files = []
try:
with open('/etc/oracle-cloud-agent/plugins/osms/ignored_repos.conf') as fobj:
ignored_repo_files = fobj.read().splitlines()
except IOError:
# Ignore all errors, ie, fall back to normal operations
pass
for repo in glob.glob('/etc/yum.repos.d/*.repo'):
if os.path.basename(repo) not in ignored_repo_files:
shutil.move(repo, repo + '.osms-backup')
def restore_yum_repos():
for repo in glob.glob('/etc/yum.repos.d/*.repo.osms-backup'):
shutil.move(repo, re.sub(r'\.osms-backup$', '', repo))
def self_test(args=None):
endpoint = None if not hasattr(args, 'server_url') else args.server_url
self_tester = cli.SelfTest.create(endpoint)
self_tester.run()
def check(args=None):
backup_yum_repos()
enable_plugin()
cli.Checker().run()
def register(args):
cfg = config.initUp2dateConfig()
cfg.set('serverURL', args.server_url)
package_list = distro.get_installed_package_list()
instance_ocid = utils.get_instance_ocid()
profile_name = socket.gethostname() or instance_ocid
release_name, os_release, _ = get_distro_information()
is_alx = utils.is_alx()
if is_alx:
autonomous_linux = '1'
else:
autonomous_linux = '0'
auth_dict = {
'profile_name': profile_name,
'os_release': os_release,
'release_name': release_name,
'architecture': os.uname()[4],
'ocid': instance_ocid,
'autonomous_linux': autonomous_linux,
}
if args.activation_key:
auth_dict['token'] = args.activation_key
server = OsmsServer()
system_id = server.registration.new_system(auth_dict)
server.registration.add_packages(system_id, package_list)
utils.create_file(cfg['systemIdPath'], sstr(system_id))
cfg.save()
backup_yum_repos()
enable_plugin()
def unregister(args):
utils.remove_system_id_file()
utils.set_default_server_url()
disable_plugin()
restore_yum_repos()
CMDS = {
'check': {
'func': check,
'help': 'Check for actions',
},
'register': {
'func': register,
'help': 'Register a system',
'args': [
[['--server-url'], {'help': 'server url', 'required': True}],
[['--activation-key'], {'help': 'activation key'}],
],
},
'unregister': {
'func': unregister,
'help': 'Unregister a system',
},
'test': {
'func': self_test,
'help': 'perform a self test to check that OSMS is correctly configured',
'args': [
[['--server-url'], {'help': 'server url to use for service'}],
]
},
}
if __name__ == '__main__':
cli.main(CMDS)