From f69dd99a79ac7848b24f2034a98ff6fdcdc037cd Mon Sep 17 00:00:00 2001
From: Russell Haering <russellhaering@gmail.com>
Date: Wed, 28 May 2014 23:11:47 -0700
Subject: [PATCH] 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
---
 ironic_python_agent/cmd/agent.py | 130 ++++++++++++++-----------------
 1 file changed, 58 insertions(+), 72 deletions(-)

diff --git a/ironic_python_agent/cmd/agent.py b/ironic_python_agent/cmd/agent.py
index 770d6f17e..553a9ef31 100644
--- a/ironic_python_agent/cmd/agent.py
+++ b/ironic_python_agent/cmd/agent.py
@@ -12,23 +12,17 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import argparse
+from oslo.config import cfg
 
 from ironic_python_agent import agent
 from ironic_python_agent.openstack.common import log
 
-
-log.setup('ironic-python-agent')
-LOG = log.getLogger(__name__)
+CONF = cfg.CONF
 
 
 def _get_kernel_params():
-    try:
-        with open('/proc/cmdline') as f:
-            cmdline = f.read()
-    except Exception as e:
-        LOG.exception('Could not read /proc/cmdline: {e}'.format(e=e))
-        return {}
+    with open('/proc/cmdline') as f:
+        cmdline = f.read()
 
     options = cmdline.split()
     params = {}
@@ -40,68 +34,60 @@ def _get_kernel_params():
 
     return params
 
+KPARAMS = _get_kernel_params()
+
+cli_opts = [
+    cfg.StrOpt('api-url',
+                  required=('ipa-api-url' not in KPARAMS),
+                  default=KPARAMS.get('ipa-api-url'),
+                  help='URL of the Ironic API'),
+
+    cfg.StrOpt('listen-host',
+                  default=KPARAMS.get('ipa-listen-host', '0.0.0.0'),
+                  help='The IP address to listen on.'),
+
+    cfg.IntOpt('listen-port',
+               default=int(KPARAMS.get('ipa-listen-port', 9999)),
+               help='The port to listen on'),
+
+    cfg.StrOpt('advertise-host',
+                  default=KPARAMS.get('ipa-advertise-host', '0.0.0.0'),
+                  help='The host to tell Ironic to reply and send '
+                       'commands to.'),
+
+    cfg.IntOpt('advertise-port',
+               default=int(KPARAMS.get('ipa-advertise-port', 9999)),
+               help='The port to tell Ironic to reply and send '
+                    'commands to.'),
+
+    cfg.IntOpt('lookup-timeout',
+               default=int(KPARAMS.get('ipa-lookup-timeout', 300)),
+               help='The amount of time to retry the initial lookup '
+                    'call to Ironic. After the timeout, the agent '
+                    'will exit with a non-zero exit code.'),
+
+    cfg.IntOpt('lookup-interval',
+               default=int(KPARAMS.get('ipa-lookup-timeout', 1)),
+               help='The initial interval for retries on the initial '
+                    'lookup call to Ironic. The interval will be '
+                    'doubled after each failure until timeout is '
+                    'exceeded.'),
+
+    cfg.StrOpt('driver-name',
+                  default=KPARAMS.get('ipa-driver-name', 'agent_ipmitool'),
+                  help='The Ironic driver in use for this node')
+]
+
+CONF.register_cli_opts(cli_opts)
+
 
 def run():
-    kparams = _get_kernel_params()
+    CONF()
+    log.setup('ironic-python-agent')
 
-    parser = argparse.ArgumentParser(
-        description=('An agent that handles decomissioning and provisioning'
-                     ' on behalf of Ironic.'))
-
-    api_url = kparams.get('ipa-api-url')
-    if api_url is None:
-        parser.add_argument('--api-url',
-                            required=True,
-                            help='URL of the Ironic API')
-
-    parser.add_argument('--listen-host',
-                        default=kparams.get('ipa-listen-host', '0.0.0.0'),
-                        type=str,
-                        help='The IP address to listen on.')
-
-    parser.add_argument('--listen-port',
-                        default=int(kparams.get('ipa-listen-port', 9999)),
-                        type=int,
-                        help='The port to listen on')
-
-    parser.add_argument('--advertise-host',
-                        default=kparams.get('ipa-advertise-host', '0.0.0.0'),
-                        type=str,
-                        help='The host to tell Ironic to reply and send '
-                             'commands to.')
-
-    parser.add_argument('--advertise-port',
-                        default=int(kparams.get('ipa-advertise-port', 9999)),
-                        type=int,
-                        help='The port to tell Ironic to reply and send '
-                             'commands to.')
-
-    parser.add_argument('--lookup-timeout',
-                        default=int(kparams.get('ipa-lookup-timeout', 300)),
-                        type=int,
-                        help='The amount of time to retry the initial lookup '
-                             'call to Ironic. After the timeout, the agent '
-                             'will exit with a non-zero exit code.')
-
-    parser.add_argument('--lookup-interval',
-                        default=int(kparams.get('ipa-lookup-timeout', 1)),
-                        type=int,
-                        help='The initial interval for retries on the initial '
-                             'lookup call to Ironic. The interval will be '
-                             'doubled after each failure until timeout is '
-                             'exceeded.')
-
-    parser.add_argument('--driver-name',
-                        default=kparams.get('ipa-driver-name',
-                                            'agent_ipmitool'),
-                        type=str,
-                        help='The Ironic driver in use for this node')
-
-    args = parser.parse_args()
-
-    agent.IronicPythonAgent(api_url or args.api_url,
-                            (args.advertise_host, args.advertise_port),
-                            (args.listen_host, args.listen_port),
-                            args.lookup_timeout,
-                            args.lookup_interval,
-                            args.driver_name).run()
+    agent.IronicPythonAgent(CONF.api_url,
+                            (CONF.advertise_host, CONF.advertise_port),
+                            (CONF.listen_host, CONF.listen_port),
+                            CONF.lookup_timeout,
+                            CONF.lookup_interval,
+                            CONF.driver_name).run()