Skip to content

Commit f69dd99

Browse files
Switch to oslo.config for CLI parsing
Use oslo.config instead of argparse for CLI parsing. As a side effect, a bunch of logging arguments including --debug become available with this change. Change-Id: Ia8c4e91448f2a8cb15eb570125ac9c236e13274c
1 parent 59d6523 commit f69dd99

File tree

1 file changed

+59
-73
lines changed

1 file changed

+59
-73
lines changed

ironic_python_agent/cmd/agent.py

Lines changed: 59 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,17 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import argparse
15+
from oslo.config import cfg
1616

1717
from ironic_python_agent import agent
1818
from ironic_python_agent.openstack.common import log
1919

20-
21-
log.setup('ironic-python-agent')
22-
LOG = log.getLogger(__name__)
20+
CONF = cfg.CONF
2321

2422

2523
def _get_kernel_params():
26-
try:
27-
with open('/proc/cmdline') as f:
28-
cmdline = f.read()
29-
except Exception as e:
30-
LOG.exception('Could not read /proc/cmdline: {e}'.format(e=e))
31-
return {}
24+
with open('/proc/cmdline') as f:
25+
cmdline = f.read()
3226

3327
options = cmdline.split()
3428
params = {}
@@ -40,68 +34,60 @@ def _get_kernel_params():
4034

4135
return params
4236

37+
KPARAMS = _get_kernel_params()
38+
39+
cli_opts = [
40+
cfg.StrOpt('api-url',
41+
required=('ipa-api-url' not in KPARAMS),
42+
default=KPARAMS.get('ipa-api-url'),
43+
help='URL of the Ironic API'),
44+
45+
cfg.StrOpt('listen-host',
46+
default=KPARAMS.get('ipa-listen-host', '0.0.0.0'),
47+
help='The IP address to listen on.'),
48+
49+
cfg.IntOpt('listen-port',
50+
default=int(KPARAMS.get('ipa-listen-port', 9999)),
51+
help='The port to listen on'),
52+
53+
cfg.StrOpt('advertise-host',
54+
default=KPARAMS.get('ipa-advertise-host', '0.0.0.0'),
55+
help='The host to tell Ironic to reply and send '
56+
'commands to.'),
57+
58+
cfg.IntOpt('advertise-port',
59+
default=int(KPARAMS.get('ipa-advertise-port', 9999)),
60+
help='The port to tell Ironic to reply and send '
61+
'commands to.'),
62+
63+
cfg.IntOpt('lookup-timeout',
64+
default=int(KPARAMS.get('ipa-lookup-timeout', 300)),
65+
help='The amount of time to retry the initial lookup '
66+
'call to Ironic. After the timeout, the agent '
67+
'will exit with a non-zero exit code.'),
68+
69+
cfg.IntOpt('lookup-interval',
70+
default=int(KPARAMS.get('ipa-lookup-timeout', 1)),
71+
help='The initial interval for retries on the initial '
72+
'lookup call to Ironic. The interval will be '
73+
'doubled after each failure until timeout is '
74+
'exceeded.'),
75+
76+
cfg.StrOpt('driver-name',
77+
default=KPARAMS.get('ipa-driver-name', 'agent_ipmitool'),
78+
help='The Ironic driver in use for this node')
79+
]
80+
81+
CONF.register_cli_opts(cli_opts)
82+
4383

4484
def run():
45-
kparams = _get_kernel_params()
46-
47-
parser = argparse.ArgumentParser(
48-
description=('An agent that handles decomissioning and provisioning'
49-
' on behalf of Ironic.'))
50-
51-
api_url = kparams.get('ipa-api-url')
52-
if api_url is None:
53-
parser.add_argument('--api-url',
54-
required=True,
55-
help='URL of the Ironic API')
56-
57-
parser.add_argument('--listen-host',
58-
default=kparams.get('ipa-listen-host', '0.0.0.0'),
59-
type=str,
60-
help='The IP address to listen on.')
61-
62-
parser.add_argument('--listen-port',
63-
default=int(kparams.get('ipa-listen-port', 9999)),
64-
type=int,
65-
help='The port to listen on')
66-
67-
parser.add_argument('--advertise-host',
68-
default=kparams.get('ipa-advertise-host', '0.0.0.0'),
69-
type=str,
70-
help='The host to tell Ironic to reply and send '
71-
'commands to.')
72-
73-
parser.add_argument('--advertise-port',
74-
default=int(kparams.get('ipa-advertise-port', 9999)),
75-
type=int,
76-
help='The port to tell Ironic to reply and send '
77-
'commands to.')
78-
79-
parser.add_argument('--lookup-timeout',
80-
default=int(kparams.get('ipa-lookup-timeout', 300)),
81-
type=int,
82-
help='The amount of time to retry the initial lookup '
83-
'call to Ironic. After the timeout, the agent '
84-
'will exit with a non-zero exit code.')
85-
86-
parser.add_argument('--lookup-interval',
87-
default=int(kparams.get('ipa-lookup-timeout', 1)),
88-
type=int,
89-
help='The initial interval for retries on the initial '
90-
'lookup call to Ironic. The interval will be '
91-
'doubled after each failure until timeout is '
92-
'exceeded.')
93-
94-
parser.add_argument('--driver-name',
95-
default=kparams.get('ipa-driver-name',
96-
'agent_ipmitool'),
97-
type=str,
98-
help='The Ironic driver in use for this node')
99-
100-
args = parser.parse_args()
101-
102-
agent.IronicPythonAgent(api_url or args.api_url,
103-
(args.advertise_host, args.advertise_port),
104-
(args.listen_host, args.listen_port),
105-
args.lookup_timeout,
106-
args.lookup_interval,
107-
args.driver_name).run()
85+
CONF()
86+
log.setup('ironic-python-agent')
87+
88+
agent.IronicPythonAgent(CONF.api_url,
89+
(CONF.advertise_host, CONF.advertise_port),
90+
(CONF.listen_host, CONF.listen_port),
91+
CONF.lookup_timeout,
92+
CONF.lookup_interval,
93+
CONF.driver_name).run()

0 commit comments

Comments
 (0)