Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Add logging setup for receiverd and assassind
Browse files Browse the repository at this point in the history
Since all services inside containers are now started via systemd we need
add logging handling which in past was done by supervisord. Thus such
settings are added for rpc receiver and assassin services. Loggers are
set up independently from nailgun root logger as writing to files are
required for mentioned components.

Change-Id: I21136108d426531f572940dfc9f75fe153111d41
Partial-Bug: #1523476
  • Loading branch information
Artem Roma committed Jan 28, 2016
1 parent 66ad02e commit 31b3dbb
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 19 deletions.
9 changes: 6 additions & 3 deletions nailgun/nailgun/assassin/assassind.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@

from nailgun.db import db
from nailgun.db.sqlalchemy.models import Node
from nailgun.logger import logger
from nailgun.settings import settings

from nailgun.utils import logs

def update_nodes_status(timeout):

def update_nodes_status(timeout, logger):
to_update = db().query(Node).filter(
not_(Node.status == 'provisioning')
).filter(
Expand All @@ -54,10 +55,12 @@ def update_nodes_status(timeout):


def run():
logger = logs.prepare_submodule_logger('assassin',
settings.ASSASSIN_LOG_PATH)
logger.info('Running Assassind...')
try:
while True:
update_nodes_status(settings.KEEPALIVE['timeout'])
update_nodes_status(settings.KEEPALIVE['timeout'], logger)
time.sleep(settings.KEEPALIVE['interval'])
except (KeyboardInterrupt, SystemExit):
logger.info('Stopping Assassind...')
Expand Down
24 changes: 18 additions & 6 deletions nailgun/nailgun/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@
'[%(thread)x] (%(module)s) %(message)s'
formatter = logging.Formatter(LOGFORMAT, DATEFORMAT)

# NOTE(aroma): the logging level for nailgun is set up inside
# nailgun settings parsing object in nailgun.settings module hence
# following option will be in effect only when the settings are not
# available for some reason
LOG_LEVEL = logging.DEBUG


def make_nailgun_logger():
"""Make logger for nailgun app writes logs to stdout"""
logger = logging.getLogger("nailgun")
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(formatter)
logger.addHandler(handler)
set_logger(logger, handler)
return logger


Expand All @@ -46,12 +50,20 @@ def make_api_logger():

logger = logging.getLogger("nailgun-api")
log_file = WatchedFileHandler(settings.API_LOG)
log_file.setFormatter(formatter)
logger.setLevel(logging.DEBUG)
logger.addHandler(log_file)
set_logger(logger, log_file)
return logger


def set_logger(logger, handler, level=None):
if level is None:
level = LOG_LEVEL

handler.setFormatter(formatter)

logger.setLevel(level)
logger.addHandler(handler)


logger = make_nailgun_logger()


Expand Down
9 changes: 8 additions & 1 deletion nailgun/nailgun/rpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import logging
import six
import functools

Expand All @@ -23,10 +24,16 @@
from oslo_serialization import jsonutils
from kombu import Queue

from nailgun.logger import logger
from nailgun.settings import settings
from nailgun.rpc import utils

from nailgun.utils import logs


logger = logs.prepare_submodule_logger('receiverd',
settings.RPC_CONSUMER_LOG_PATH)


creds = (
("userid", "guest"),
("password", "guest"),
Expand Down
5 changes: 4 additions & 1 deletion nailgun/nailgun/rpc/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import copy
import datetime
import itertools
import logging
import os
import six
import traceback
Expand All @@ -36,7 +37,6 @@
from nailgun.db.sqlalchemy.models import IPAddr
from nailgun.db.sqlalchemy.models import Node
from nailgun.db.sqlalchemy.models import Release
from nailgun.logger import logger
from nailgun.network import connectivity_check
from nailgun.network import utils as net_utils
from nailgun.objects.plugin import ClusterPlugins
Expand All @@ -45,6 +45,9 @@
from nailgun.utils import reverse


logger = logging.getLogger('receiverd')


class NailgunReceiver(object):

@classmethod
Expand Down
5 changes: 4 additions & 1 deletion nailgun/nailgun/rpc/receiverd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import logging
import os
import sys

Expand All @@ -30,12 +31,14 @@

from nailgun.db import db
from nailgun.errors import errors
from nailgun.logger import logger
import nailgun.rpc as rpc
from nailgun.rpc.receiver import NailgunReceiver
from nailgun.rpc import utils


logger = logging.getLogger('receiverd')


class RPCConsumer(ConsumerMixin):

def __init__(self, connection, receiver):
Expand Down
5 changes: 4 additions & 1 deletion nailgun/nailgun/rpc/threaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import logging
import threading
import traceback

Expand All @@ -22,11 +23,13 @@

from nailgun.db import db
from nailgun.errors import errors
from nailgun.logger import logger
import nailgun.rpc as rpc
from nailgun.rpc.receiver import NailgunReceiver


logger = logging.getLogger('receiverd')


class RPCConsumer(ConsumerMixin):

def __init__(self, connection, receiver):
Expand Down
4 changes: 3 additions & 1 deletion nailgun/nailgun/rpc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
# License for the specific language governing permissions and limitations
# under the License.

import logging
import six

from nailgun.logger import logger

logger = logging.getLogger('receiverd')


def delete_entities(conn, *entities):
Expand Down
4 changes: 4 additions & 0 deletions nailgun/nailgun/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ APP_LOG: &nailgun_log "/var/log/nailgun/app.log"
API_LOG: &api_log "/var/log/nailgun/api.log"
SYSLOG_DIR: &remote_syslog_dir "/var/log/remote/"

RPC_CONSUMER_LOG_PATH: "/var/log/nailgun/receiverd.log"

ASSASSIN_LOG_PATH: "/var/log/nailgun/assassind.log"

MIRANTIS_REGISTRATION_URL: "https://software.mirantis.com/wp-content/themes/mirantis_responsive_v_1_0/scripts/fuel_forms_api/"

COLLECTOR_ACTION_LOGS_URL: "https://{collector_server}/api/v1/action_logs/"
Expand Down
13 changes: 8 additions & 5 deletions nailgun/nailgun/test/integration/test_assassin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,34 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock

from nailgun.assassin import assassind
from nailgun.test.base import BaseIntegrationTest


@mock.patch('nailgun.utils.logs.prepare_submodule_logger')
class TestKeepalive(BaseIntegrationTest):

VERY_LONG_TIMEOUT = 60 * 60 # 1 hour
ZERO_TIMEOUT = 0

def test_node_becomes_offline(self):
def test_node_becomes_offline(self, m_logger):
node = self.env.create_node(
status="discover",
roles=["controller"],
name="Dead or alive"
)
assassind.update_nodes_status(self.VERY_LONG_TIMEOUT)
assassind.update_nodes_status(self.VERY_LONG_TIMEOUT, m_logger)
self.assertEqual(node.online, True)
assassind.update_nodes_status(self.ZERO_TIMEOUT)
assassind.update_nodes_status(self.ZERO_TIMEOUT, m_logger)
self.assertEqual(node.online, False)

def test_provisioning_node_not_becomes_offline(self):
def test_provisioning_node_not_becomes_offline(self, m_logger):
node = self.env.create_node(
status="provisioning",
roles=["controller"],
name="Dead or alive"
)
assassind.update_nodes_status(self.ZERO_TIMEOUT)
assassind.update_nodes_status(self.ZERO_TIMEOUT, m_logger)
self.assertEqual(node.online, True)
16 changes: 16 additions & 0 deletions nailgun/nailgun/utils/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
# License for the specific language governing permissions and limitations
# under the License.

import logging
import os
import shutil
import sys

from nailgun import consts
from nailgun.db import db
from nailgun.db.sqlalchemy.models import IPAddr
from nailgun.db.sqlalchemy.models import NetworkGroup
from nailgun.db.sqlalchemy.models import Node
from nailgun.logger import logger
from nailgun.logger import set_logger
from nailgun import objects
from nailgun.settings import settings
from nailgun.utils import remove_silently
Expand Down Expand Up @@ -113,3 +116,16 @@ def delete_node_logs(node, prefix=settings.SYSLOG_DIR):
if os.path.lexists(log_path):
logger.debug('delete_node_logs log_path="%s"', log_path)
remove_silently(log_path)


def prepare_submodule_logger(submodule_name, file_path=None):
logger = logging.getLogger(submodule_name)

if file_path is None:
handler = logging.FileHandler(file_path)
else:
handler = logging.StreamHandler(sys.stdout)

set_logger(logger, handler)

return logger
2 changes: 2 additions & 0 deletions nailgun/tools/env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ DATABASE:
passwd: "${NAILGUN_DB_PW}"
API_LOG: "${NAILGUN_LOGS}/api.log"
APP_LOG: "${NAILGUN_LOGS}/app.log"
RPC_CONSUMER_LOG_PATH: "${NAILGUN_LOGS}/receiverd.log"
ASSASSIN_LOG_PATH: "${NAILGUN_LOGS}/assassind.log"
EOL
}

Expand Down

0 comments on commit 31b3dbb

Please sign in to comment.