Skip to content

Commit

Permalink
Feat register no machine-id (#3554)
Browse files Browse the repository at this point in the history
* Registration mechanism looks for machine-id file and if is not registered insights-client fails executing
* Test to check this new logic

Signed-off-by: ahitacat <[email protected]>
  • Loading branch information
ahitacat authored Dec 5, 2022
1 parent 9ed8f4a commit 1857a42
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 60 deletions.
10 changes: 9 additions & 1 deletion insights/client/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import print_function
from __future__ import absolute_import
from os.path import isfile
import sys
import json
import logging
Expand Down Expand Up @@ -126,11 +127,18 @@ def _legacy_handle_registration(config, pconn):
write_to_disk(constants.machine_id_file, delete=True)
logger.debug('Re-register set, forcing registration.')

logger.debug('Machine-id: %s', generate_machine_id(new=config.reregister))
machine_id_present = isfile(constants.machine_id_file)

# check registration with API
check = get_registration_status(config, pconn)

if machine_id_present and check['status'] is False:
logger.info("Machine-id found, insights-client can not be registered."
" Please, unregister insights-client first: `insights-client --unregister`")
return False

logger.debug('Machine-id: %s', generate_machine_id(new=config.reregister))

for m in check['messages']:
logger.debug(m)

Expand Down
58 changes: 35 additions & 23 deletions insights/client/phase/v1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import print_function
import functools
from os.path import isfile
import json
import logging
import os
Expand Down Expand Up @@ -135,9 +136,7 @@ def update(client, config):

@phase
def post_update(client, config):
# create a machine id first thing. we'll need it for all uploads
logger.debug('Machine ID: %s', client.get_machine_id())
logger.debug("CONFIG: %s", config)

print_egg_versions()

if config.list_specs:
Expand Down Expand Up @@ -179,12 +178,14 @@ def post_update(client, config):
else:
sys.exit(constants.sig_kill_bad)

if config.offline:
logger.debug('Running client in offline mode. Bypassing registration.')
return

if config.no_upload:
logger.debug("Running client without uploading. Bypassing registration.")
if config.offline or config.no_upload:
# create a machine id first thing. we'll need it for all uploads
logger.debug('Machine ID: %s', client.get_machine_id())
logger.debug("CONFIG: %s", config)
if config.offline:
logger.debug('Running client in offline mode. Bypassing registration.')
else:
logger.debug("Running client without uploading. Bypassing registration.")
return

if config.display_name and not config.register:
Expand Down Expand Up @@ -212,23 +213,25 @@ def post_update(client, config):
return
# -------delete everything above this line-------

if config.offline:
logger.debug('Running client in offline mode. Bypassing registration.')
return

if config.no_upload:
logger.debug("Running client without uploading. Bypassing registration.")
return

# --payload short circuits registration check
if config.payload:
logger.debug('Uploading a specified archive. Bypassing registration.')
if config.offline or config.no_upload or config.payload:
# create a machine id first thing. we'll need it for all uploads
logger.debug('Machine ID: %s', client.get_machine_id())
logger.debug("CONFIG: %s", config)
if config.offline:
logger.debug('Running client in offline mode. Bypassing registration.')
elif config.no_upload:
logger.debug("Running client without uploading. Bypassing registration.")
else:
logger.debug('Uploading a specified archive. Bypassing registration.')
return

# check registration status before anything else
reg_check = client.get_registration_status()
if reg_check is None:
sys.exit(constants.sig_kill_bad)
if isfile(constants.machine_id_file):
reg_check = client.get_registration_status()
if reg_check is None:
sys.exit(constants.sig_kill_bad)
else:
reg_check = False

# --status
if config.status:
Expand Down Expand Up @@ -266,6 +269,11 @@ def post_update(client, config):
if config.register:
# don't actually need to make a call to register() since
# system creation and upload are a single event on the platform
if reg_check is False and isfile(constants.machine_id_file):
# Do not register if a machine_id file is found
logger.info("Machine-id found, insights-client can not be registered."
" Please, unregister insights-client first: `insights-client --unregister`")
sys.exit(constants.sig_kill_bad)
if reg_check:
logger.info('This host has already been registered.')
if not config.disable_schedule:
Expand All @@ -290,6 +298,10 @@ def post_update(client, config):
else:
sys.exit(constants.sig_kill_bad)

# create a machine id first thing. we'll need it for all uploads
logger.debug('Machine ID: %s', client.get_machine_id())
logger.debug("CONFIG: %s", config)


@phase
def collect_and_output(client, config):
Expand Down
21 changes: 18 additions & 3 deletions insights/tests/client/phase/test_LEGACY_post_update.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: UTF-8 -*-

from insights.client.phase.v1 import post_update
from mock.mock import patch
from mock.mock import patch, MagicMock
from pytest import raises


Expand Down Expand Up @@ -46,12 +45,12 @@ def test_post_update_legacy_upload_on(insights_config, insights_client):
Registration is processed in legacy_upload=True
"""
insights_config.return_value.load_all.return_value.legacy_upload = True
insights_client.return_value.register.return_value = False
try:
post_update()
except SystemExit:
pass
insights_client.return_value.register.assert_called_once()
insights_client.return_value.get_machine_id.assert_called_once()


@patch("insights.client.phase.v1.InsightsClient")
Expand Down Expand Up @@ -79,3 +78,19 @@ def test_post_update_no_upload(insights_config, insights_client):
pass
insights_client.return_value.register.assert_not_called()
insights_client.return_value.get_machine_id.assert_called_once()


@patch("insights.client.phase.v1.InsightsClient")
@patch_insights_config
def test_post_update_register_machineid(insights_config, insights_client):
"""
Client run with --register.
If machine-id found, exit with 0 exit code (don't kill parent)
Also enable scheduling.
"""
insights_config.return_value.load_all.return_value.register = True
insights_client.return_value.get_registration_status = MagicMock(return_value=False)
insights_client.return_value.register = MagicMock(return_value=False)
with raises(SystemExit) as exc_info:
post_update()
assert exc_info.value.code == 101
Loading

0 comments on commit 1857a42

Please sign in to comment.