Browse Source

Merge "Default log location failes for non-root users"

tags/4.2.0^0
Zuul 5 months ago
parent
commit
99ac720bf6
2 changed files with 30 additions and 34 deletions
  1. 10
    34
      paunch/cmd.py
  2. 20
    0
      paunch/utils/common.py

+ 10
- 34
paunch/cmd.py View File

@@ -20,7 +20,6 @@ import yaml
20 20
 
21 21
 import paunch
22 22
 
23
-from paunch import constants
24 23
 from paunch import utils
25 24
 
26 25
 
@@ -69,13 +68,8 @@ class Apply(command.Command):
69 68
         return parser
70 69
 
71 70
     def take_action(self, parsed_args):
72
-
73
-        # takes 1, or 2 if --verbose, or 4 - 5 if --debug
74
-        log_level = (self.app_args.verbose_level +
75
-                     int(self.app_args.debug) * 3)
76
-        log_file = self.app_args.log_file or constants.LOG_FILE
77
-        self.log = utils.common.configure_logging(
78
-            __name__, log_level, log_file)
71
+        (self.log, log_file, log_level) = \
72
+            utils.common.configure_logging_from_args(__name__, self.app_args)
79 73
         labels = collections.OrderedDict()
80 74
         for l in parsed_args.labels:
81 75
             k, v = l.split(('='), 1)
@@ -127,12 +121,8 @@ class Cleanup(command.Command):
127 121
         return parser
128 122
 
129 123
     def take_action(self, parsed_args):
130
-        # takes 1, or 2 if --verbose, or 4 - 5 if --debug
131
-        log_level = (self.app_args.verbose_level +
132
-                     int(self.app_args.debug) * 3)
133
-        log_file = self.app_args.log_file or constants.LOG_FILE
134
-        self.log = utils.common.configure_logging(
135
-            __name__, log_level, log_file)
124
+        (self.log, log_file, log_level) = \
125
+            utils.common.configure_logging_from_args(__name__, self.app_args)
136 126
         paunch.cleanup(
137 127
             parsed_args.config_id,
138 128
             managed_by=parsed_args.managed_by,
@@ -171,12 +161,8 @@ class Delete(command.Command):
171 161
         return parser
172 162
 
173 163
     def take_action(self, parsed_args):
174
-        # takes 1, or 2 if --verbose, or 4 - 5 if --debug
175
-        log_level = (self.app_args.verbose_level +
176
-                     int(self.app_args.debug) * 3)
177
-        log_file = self.app_args.log_file or constants.LOG_FILE
178
-        self.log = utils.common.configure_logging(
179
-            __name__, log_level, log_file)
164
+        (self.log, log_file, log_level) = \
165
+            utils.common.configure_logging_from_args(__name__, self.app_args)
180 166
         paunch.delete(
181 167
             parsed_args.config_id,
182 168
             parsed_args.managed_by,
@@ -277,14 +263,8 @@ class Debug(command.Command):
277 263
         return parser
278 264
 
279 265
     def take_action(self, parsed_args):
280
-
281
-        # takes 1, or 2 if --verbose, or 4 - 5 if --debug
282
-        log_level = (self.app_args.verbose_level +
283
-                     int(self.app_args.debug) * 3)
284
-        # Only log to a file if explicitely set via CLI args
285
-        log_file = self.app_args.log_file
286
-        self.log = utils.common.configure_logging(
287
-            __name__, log_level, log_file)
266
+        (self.log, log_file, log_level) = \
267
+            utils.common.configure_logging_from_args(__name__, self.app_args)
288 268
         labels = collections.OrderedDict()
289 269
         for l in parsed_args.labels:
290 270
             k, v = l.split(('='), 1)
@@ -355,12 +335,8 @@ class List(lister.Lister):
355 335
         return parser
356 336
 
357 337
     def take_action(self, parsed_args):
358
-        # takes 1, or 2 if --verbose, or 4 - 5 if --debug
359
-        log_level = (self.app_args.verbose_level +
360
-                     int(self.app_args.debug) * 3)
361
-        log_file = self.app_args.log_file or constants.LOG_FILE
362
-        self.log = utils.common.configure_logging(
363
-            __name__, log_level, log_file)
338
+        (self.log, log_file, log_level) = \
339
+            utils.common.configure_logging_from_args(__name__, self.app_args)
364 340
         configs = paunch.list(
365 341
             parsed_args.managed_by,
366 342
             cont_cmd=parsed_args.default_runtime,

+ 20
- 0
paunch/utils/common.py View File

@@ -14,8 +14,12 @@
14 14
 # under the License.
15 15
 
16 16
 import logging
17
+import os
17 18
 import sys
18 19
 
20
+from paunch import constants
21
+from paunch import utils
22
+
19 23
 
20 24
 def configure_logging(name, level=3, log_file=None):
21 25
     '''Mimic oslo_log default levels and formatting for the logger. '''
@@ -44,3 +48,19 @@ def configure_logging(name, level=3, log_file=None):
44 48
         log.propagate = False
45 49
 
46 50
     return log
51
+
52
+
53
+def configure_logging_from_args(name, app_args):
54
+    # takes 1, or 2 if --verbose, or 4 - 5 if --debug
55
+    log_level = (app_args.verbose_level +
56
+                 int(app_args.debug) * 3)
57
+
58
+    # if executed as root log to specified file or default log file
59
+    if os.getuid() == 0:
60
+        log_file = app_args.log_file or constants.LOG_FILE
61
+    else:
62
+        log_file = app_args.log_file
63
+
64
+    log = utils.common.configure_logging(
65
+        __name__, log_level, log_file)
66
+    return (log, log_file, log_level)

Loading…
Cancel
Save