| Philip Joseph | f9c7222 | 2016-10-02 23:31:02 +0530 | [diff] [blame] | 1 | |
| 2 | import subprocess |
| 3 | import paramiko |
| 4 | |
| 5 | from charmhelpers.core.hookenv import ( |
| 6 | config, |
| 7 | status_set, |
| 8 | action_get, |
| 9 | action_set, |
| 10 | action_fail, |
| 11 | ) |
| 12 | |
| 13 | from charms.reactive import ( |
| 14 | when, |
| 15 | when_not, |
| 16 | set_state as set_flag, |
| 17 | remove_state as remove_flag, |
| 18 | ) |
| 19 | |
| 20 | |
| 21 | @when('config.changed') |
| 22 | def test_connection(): |
| 23 | status_set('maintenance', 'configuring ssh connection') |
| 24 | remove_flag('vyos-proxy.ready') |
| 25 | try: |
| 26 | who, _ = run('whoami') |
| 27 | except MgmtNotConfigured as e: |
| 28 | remove_flag('vyos-proxy.configured') |
| 29 | status_set('blocked', str(e)) |
| 30 | except subprocess.CalledProcessError as e: |
| 31 | remove_flag('vyos-proxy.configured') |
| 32 | status_set('blocked', e.output) |
| 33 | else: |
| 34 | set_flag('vyos-proxy.configured') |
| 35 | |
| 36 | |
| 37 | @when('vyos-proxy.configured') |
| 38 | @when_not('vyos-proxy.ready') |
| 39 | def vyos_proxy_ready(): |
| 40 | status_set('active', 'ready') |
| 41 | set_flag('vyos-proxy.ready') |
| 42 | |
| 43 | |
| 44 | @when('actions.ping') |
| 45 | @when_not('vyos-proxy.configured') |
| 46 | def pingme(): |
| 47 | action_fail('proxy is not ready') |
| 48 | |
| 49 | |
| 50 | @when('actions.ping') |
| 51 | @when('vyos-proxy.configured') |
| 52 | def pingme_forreal(): |
| 53 | try: |
| 54 | result, err = run('ping -qc {} {}'.format(action_get('count'), action_get('destination'))) |
| 55 | except: |
| 56 | action_fail('ping command failed') |
| 57 | finally: |
| 58 | remove_flag('actions.ping') |
| 59 | |
| 60 | # Here you can send results back from ping, if you had time to parse it |
| 61 | action_set({'output': result}) |
| 62 | |
| 63 | |
| 64 | |
| 65 | class MgmtNotConfigured(Exception): |
| 66 | pass |
| 67 | |
| 68 | |
| 69 | def run(cmd): |
| 70 | ''' Suddenly this project needs to SSH to something. So we replicate what |
| 71 | _run was doing with subprocess using the Paramiko library. This is |
| 72 | temporary until this charm /is/ the VPE Router ''' |
| 73 | |
| 74 | cfg = config() |
| 75 | |
| 76 | hostname = cfg.get('hostname') |
| 77 | password = cfg.get('pass') |
| 78 | username = cfg.get('user') |
| 79 | |
| 80 | if not (username and password and hostname): |
| 81 | raise MgmtNotConfigured('incomplete remote credentials') |
| 82 | |
| 83 | client = paramiko.SSHClient() |
| 84 | client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 85 | |
| 86 | try: |
| 87 | client.connect(cfg.get('hostname'), port=22, |
| 88 | username=cfg.get('user'), password=cfg.get('pass')) |
| 89 | except paramiko.ssh_exception.AuthenticationException: |
| 90 | raise MgmtNotConfigured('invalid credentials') |
| 91 | |
| 92 | stdin, stdout, stderr = client.exec_command(cmd) |
| 93 | retcode = stdout.channel.recv_exit_status() |
| 94 | client.close() # @TODO re-use connections |
| 95 | if retcode > 0: |
| 96 | output = stderr.read().strip() |
| 97 | raise subprocess.CalledProcessError(returncode=retcode, cmd=cmd, |
| 98 | output=output) |
| 99 | return (''.join(stdout), ''.join(stderr)) |