af6644bdd6ae44847f319abfc348faaab173d0c0
[osm/N2VC.git] / tests / charms / layers / simple / reactive / simple.py
1 from charmhelpers.core.hookenv import (
2 action_get,
3 action_fail,
4 action_set,
5 status_set,
6 )
7 from charms.reactive import (
8 clear_flag,
9 set_flag,
10 when,
11 when_not,
12 )
13 import charms.sshproxy
14 import os
15
16
17 @when('sshproxy.configured')
18 @when_not('simple.installed')
19 def install_simple_proxy_charm():
20 """Post-install actions.
21
22 This function will run when two conditions are met:
23 1. The 'sshproxy.configured' state is set
24 2. The 'simple.installed' state is not set
25
26 This ensures that the workload status is set to active only when the SSH
27 proxy is properly configured.
28 """
29 set_flag('simple.installed')
30 status_set('active', 'Ready!')
31
32
33 @when('actions.touch')
34 def touch():
35 if not in_action_context():
36 clear_flag('actions.touch')
37 return
38
39 err = ''
40 try:
41 filename = action_get('filename')
42 cmd = ['touch {}'.format(filename)]
43 result, err = charms.sshproxy._run(cmd)
44 except Exception:
45 action_fail('command failed:' + err)
46 else:
47 action_set({'output': result})
48 finally:
49 clear_flag('actions.touch')
50
51
52 def in_action_context():
53 """Determine whether we're running on an action context."""
54 return 'JUJU_ACTION_UUID' in os.environ