Stop checking config if ssh verification fails
[osm/devops.git] / juju-charms / layers / pingpong / reactive / pingpong.py
old mode 100644 (file)
new mode 100755 (executable)
index b5a5db9..ae18076
@@ -10,20 +10,42 @@ from charms.reactive import (
     remove_state as remove_flag,
     set_state as set_flag,
     when,
+    when_not,
 )
 import charms.sshproxy
-from subprocess import (
-    Popen,
-    CalledProcessError,
-    PIPE,
-)
+from subprocess import (
+    Popen,
+    CalledProcessError,
+    PIPE,
+)
 
 
 cfg = config()
 
 
-@when('config.changed')
+@when_not('pingpong.configured')
+def not_configured():
+    """Check the current configuration.
+
+    Check the current values in config to see if we have enough
+    information to continue.
+    """
+    config_changed()
+
+
+@when('config.changed', 'sshproxy.configured')
 def config_changed():
+    """Verify the configuration.
+
+    Verify that the charm has been configured
+    """
+    status_set('maintenance', 'Verifying configuration data...')
+    (validated, output) = charms.sshproxy.verify_ssh_credentials()
+    if not validated:
+        status_set('blocked', 'Unable to verify SSH credentials: {}'.format(
+            output
+        ))
+        return
     if all(k in cfg for k in ['mode']):
         if cfg['mode'] in ['ping', 'pong']:
             set_flag('pingpong.configured')
@@ -32,6 +54,13 @@ def config_changed():
     status_set('blocked', 'Waiting for configuration')
 
 
+@when('config.changed')
+@when_not('sshproxy.configured')
+def invalid_credentials():
+    status_set('blocked', 'Waiting for SSH credentials.')
+    pass
+
+
 def is_ping():
     if cfg['mode'] == 'ping':
         return True
@@ -63,16 +92,16 @@ def start():
         remove_flag('actions.start')
         return
 
+    # Attempt to raise the non-mgmt interface, but ignore failures if
+    # the interface is already up.
     try:
-        cmd =  "sudo timeout 30 /sbin/ifup eth1"
+        cmd = "sudo timeout 30 /sbin/ifup eth1"
         result, err = charms.sshproxy._run(cmd)
     except Exception as e:
-        action_fail('command failed: {}, errors: {}'.format(e, e.output))
-        remove_flag('actions.start')
-        return
+        pass
 
     try:
-        cmd =  "sudo timeout 30 /usr/bin/systemctl start {}". \
+        cmd = "sudo timeout 30 /usr/bin/systemctl start {}". \
               format(cfg['mode'])
         result, err = charms.sshproxy._run(cmd)
     except Exception as e: