2 # Copyright 2016 Canonical Ltd.
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
18 from charmhelpers
.core
.hookenv
import (
24 from charms
.reactive
import (
29 import charms
.sshproxy
33 @when('config.changed')
35 """ Checks to see if the charm is configured with SSH credentials. If so,
36 set a state flag that can be used to execute ssh-only actions.
40 @when('sshproxy.configured')
41 def run_remote_command(cmd):
44 @when_not('sshproxy.configured')
45 def run_local_command(cmd):
49 if all(k
in cfg
for k
in ['ssh-hostname', 'ssh-username',
50 'ssh-password', 'ssh-private-key']):
51 set_state('sshproxy.configured')
53 remove_state('sshproxy.configured')
59 Run an arbitrary command, either locally or over SSH with the configured
63 cmd
= action_get('command')
64 output
, err
= charms
.sshproxy
._run
(cmd
)
66 action_fail("Command '{}' returned error code {}".format(cmd
, err
))
68 action_set({'output': output
})
69 except subprocess
.CalledProcessError
as e
:
70 action_fail('Command failed: %s (%s)' %
71 (' '.join(e
.cmd
), str(e
.output
)))
73 remove_state('actions.run')