Open Kilda Java Documentation
core.py
Go to the documentation of this file.
1 # Copyright 2017 Telstra Open Source
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 
16 import os
17 import signal
18 import sys
19 
20 import click
21 
22 from kilda.traffexam import common
23 from kilda.traffexam import const
24 from kilda.traffexam import context as context_module
25 from kilda.traffexam import exc
26 from kilda.traffexam import model
27 from kilda.traffexam import proc
28 from kilda.traffexam import service
29 from kilda.traffexam import system
30 from kilda.traffexam import rest
31 
32 
33 @click.command()
34 @click.option(
35  '--root',
36  default=context_module.Context.root,
37  type=click.Path(file_okay=False, dir_okay=True))
38 @click.option('--logging-config')
39 @click.option('--debug', is_flag=True)
40 @click.argument('iface')
41 @click.argument('bind')
42 def main(iface, bind, **args):
43  try:
45 
46  iface = model.TargetIface.new_from_cli(iface)
47  bind = model.BindAddress.new_from_cli(bind)
48 
49  context = context_module.Context(iface, bind)
50  try:
51  context.set_root(args['root'])
52 
53  logging_config = args['logging_config']
54  if logging_config:
55  try:
56  context.set_logging_config(logging_config)
57  except KeyError:
58  pass
59 
60  context.set_debug_mode(args['debug'])
61 
62  with proc.PidFile(context.make_lock_file_name()):
63  setup_environment(context)
64 
65  context.set_service_adapter(service.Adapter(context))
66  SigCHLD(context.children)
67 
68  rest.init(bind, context)
69  finally:
70  context.close()
71  except exc.InputDataError as e:
72  click.echo(str(e), err=True)
73  sys.exit(const.CLI_ERROR_CODE_COMMON)
74 
75 
77  if 0 < os.geteuid():
79 
80 
81 def setup_environment(context):
82  context.acquire_resources(system.IPDBRoot)
83 
84  iface_info_getter = system.RootIfaceInfo(context)
85  target_iface = iface_info_getter(context.iface.index)
86 
87  context.acquire_resources(
90 
91  if target_iface.kind != 'bridge':
92  context.acquire_resources(
95  else:
96  context.acquire_resources(system.JoinTargetBridge)
97 
98  context.acquire_resources(
103 
104 
106  def __init__(self, children):
107  super().__init__(signal.SIGCHLD)
108  self.children = children
109 
110  def handle(self):
111  for child in self.children:
112  child.poll()
def main(iface, bind, args)
Definition: core.py:42
def __init__(self, children)
Definition: core.py:106
def check_privileges()
Definition: core.py:76
def setup_environment(context)
Definition: core.py:81