Skip to content

Commit

Permalink
Merge pull request #65 from tvansteenburgh/sync-watch-juju2
Browse files Browse the repository at this point in the history
Make sync-watch work on juju2 also
  • Loading branch information
Charles Butler committed May 9, 2016
2 parents 63961da + c480e32 commit 32a2417
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions juju-sync-watch
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ from datetime import datetime
import jujuclient


JUJU_ENV = os.environ.get('JUJU_ENV', '')
JUJU_REPOSITORY = os.environ.get('JUJU_REPOSITORY', '')


def current_environment():
return subprocess.check_output(['juju', 'switch']).strip()


class ShowDesc(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
super(ShowDesc, self).__init__(option_strings, dest, nargs=0, **kwargs)
Expand All @@ -39,8 +42,9 @@ def parse_args():
parser.add_argument('unit')
parser.add_argument('charm_path', nargs='?')
parser.add_argument('-d', '--description', action=ShowDesc, help='show description')
parser.add_argument('-e', '--environment', dest='env', default=JUJU_ENV,
help='juju environment to operate in')
parser.add_argument('-e', '--environment', dest='env', default=current_environment(),
help='Juju environment or model name. Defaults to the value '
'obtained from `juju switch`')
parser.add_argument('-R', '--no-retry', '--no-restart', dest='retry', action='store_false',
help='do not automatically retry failed hooks when a change is made')
parser.add_argument('-q', '--quiet', action='store_true', help='suppress output')
Expand Down Expand Up @@ -139,7 +143,11 @@ def unit_in_error(unit_name):
unit = filter(None, [unit['Subordinates'].get(unit_name) for unit in service['Units'].values()])[0]
else:
unit = service['Units'][unit_name]
return unit['AgentState'] == 'error'
unit_status = (
unit.get('AgentState') or # juju1
unit.get('WorkloadStatus', {}).get('Status') # juju2
)
return unit_status == 'error'


def restart_errored_unit(unit_name):
Expand Down Expand Up @@ -193,10 +201,10 @@ def prompt_for_upgrade(unit_name):
if __name__ == '__main__':
opts = parse_args()
env = get_env(opts.env)
series, charm_name = get_charm_info(env, opts.unit)
if not hasattr(opts, 'charm_dir'):
opts.charm_dir = find_charm(series, charm_name)
changes = watch(opts.unit, opts.charm_dir, opts.retry, opts.quiet)
if not hasattr(opts, 'charm_path'):
series, charm_name = get_charm_info(env, opts.unit)
opts.charm_path = find_charm(series, charm_name)
changes = watch(opts.unit, opts.charm_path, opts.retry, opts.quiet)

# this doesn't work with Juju <= 1.23 due to way plugins are subprocessed :(
#if changes:
Expand Down

0 comments on commit 32a2417

Please sign in to comment.