5 from charmhelpers
.core
.hookenv
import (
13 from charms
.reactive
import (
16 set_state
as set_flag
,
17 remove_state
as remove_flag
,
21 @when('config.changed')
22 def test_connection():
23 status_set('maintenance', 'configuring ssh connection')
24 remove_flag('vyos-proxy.ready')
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
)
34 set_flag('vyos-proxy.configured')
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')
45 @when_not('vyos-proxy.configured')
47 action_fail('proxy is not ready')
51 @when('vyos-proxy.configured')
54 result
, err
= run('ping -qc {} {}'.format(action_get('count'), action_get('destination')))
56 action_fail('ping command failed')
58 remove_flag('actions.ping')
60 # Here you can send results back from ping, if you had time to parse it
61 action_set({'output': result
})
65 class MgmtNotConfigured(Exception):
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 '''
76 hostname
= cfg
.get('hostname')
77 password
= cfg
.get('pass')
78 username
= cfg
.get('user')
80 if not (username
and password
and hostname
):
81 raise MgmtNotConfigured('incomplete remote credentials')
83 client
= paramiko
.SSHClient()
84 client
.set_missing_host_key_policy(paramiko
.AutoAddPolicy())
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')
92 stdin
, stdout
, stderr
= client
.exec_command(cmd
)
93 retcode
= stdout
.channel
.recv_exit_status()
94 client
.close() # @TODO re-use connections
96 output
= stderr
.read().strip()
97 raise subprocess
.CalledProcessError(returncode
=retcode
, cmd
=cmd
,
99 return (''.join(stdout
), ''.join(stderr
))