Skip to content

Commit

Permalink
cephadm: split main into an init function
Browse files Browse the repository at this point in the history
Signed-off-by: Joao Eduardo Luis <[email protected]>
  • Loading branch information
jecluis committed Jan 20, 2021
1 parent da309f2 commit 3afec2a
Showing 1 changed file with 41 additions and 38 deletions.
79 changes: 41 additions & 38 deletions src/cephadm/cephadm
Original file line number Diff line number Diff line change
Expand Up @@ -7361,65 +7361,68 @@ def _parse_args(av):
return args


def main():
def cephadm_init(args: List[str]) -> Optional[CephadmContext]:

global logger

# root?
if os.geteuid() != 0:
sys.stderr.write('ERROR: cephadm should be run as root\n')
sys.exit(1)
ctx = CephadmContext()
ctx.args = _parse_args(args)

# Logger configuration
if not os.path.exists(LOG_DIR):
os.makedirs(LOG_DIR)
dictConfig(logging_config)
logger = logging.getLogger()

# allow argv to be injected
try:
av = injected_argv # type: ignore
except NameError:
av = sys.argv[1:]
logger.debug("%s\ncephadm %s" % ("-" * 80, av))
args = _parse_args(av)

# More verbose console output
if args.verbose:
if ctx.args.verbose:
for handler in logger.handlers:
if handler.name == "console":
handler.setLevel(logging.DEBUG)
if handler.name == "console":
handler.setLevel(logging.DEBUG)

if 'func' not in args:
sys.stderr.write('No command specified; pass -h or --help for usage\n')
sys.exit(1)

container_path = ""
if "func" not in ctx.args:
sys.stderr.write("No command specified; pass -h or --help for usage\n")
return None

# podman or docker?
if args.func != command_check_host:
if args.docker:
container_path = find_program('docker')
ctx.container_path = ""
if ctx.args.func != command_check_host:
if ctx.args.docker:
ctx.container_path = find_program("docker")
else:
for i in CONTAINER_PREFERENCE:
try:
container_path = find_program(i)
ctx.container_path = find_program(i)
break
except Exception as e:
logger.debug('Could not locate %s: %s' % (i, e))
if not container_path and args.func != command_prepare_host\
and args.func != command_add_repo:
sys.stderr.write('Unable to locate any of %s\n' % CONTAINER_PREFERENCE)
sys.exit(1)
logger.debug("Could not locate %s: %s" % (i, e))
if not ctx.container_path and ctx.args.func != command_prepare_host\
and ctx.args.func != command_add_repo:
sys.stderr.write("Unable to locate any of %s\n" %
CONTAINER_PREFERENCE)
return None

ctx = CephadmContext()
ctx.args = args
ctx.container_path = container_path
return ctx


def main():

# root?
if os.geteuid() != 0:
sys.stderr.write('ERROR: cephadm should be run as root\n')
sys.exit(1)

av: List[str] = []
try:
av = injected_argv # type: ignore
except NameError:
av = sys.argv[1:]

ctx = cephadm_init(av)
if not ctx: # error, exit
sys.exit(1)

try:
r = args.func(ctx)
r = ctx.args.func(ctx)
except Error as e:
if args.verbose:
if ctx.args.verbose:
raise
sys.stderr.write('ERROR: %s\n' % e)
sys.exit(1)
Expand Down

0 comments on commit 3afec2a

Please sign in to comment.