~gnuoy/charm-helpers/services-framework-onchange-action

« back to all changes in this revision

Viewing changes to charmhelpers/contrib/amulet/utils.py

  • Committer: Christopher Glass
  • Date: 2015-08-17 07:47:51 UTC
  • mfrom: (427.1.4 amulet-action-helpers)
  • Revision ID: christopher.glass@canonical.com-20150817074751-mv5lk9jo6uk9psyg
Merge lp:~adam-collard/charm-helpers/amulet-action-helpers [a=adam-collard] [r=tribaal]

Add juju action primitives to amulet.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
17
import io
 
18
import json
18
19
import logging
19
20
import os
20
21
import re
 
22
import subprocess
21
23
import sys
22
24
import time
23
25
 
551
553
            return 'Dicts within list are not identical'
552
554
 
553
555
        return None
 
556
 
 
557
    def run_action(self, unit_sentry, action,
 
558
                   _check_output=subprocess.check_output):
 
559
        """Run the named action on a given unit sentry.
 
560
 
 
561
        _check_output parameter is used for dependency injection.
 
562
 
 
563
        @return action_id.
 
564
        """
 
565
        unit_id = unit_sentry.info["unit_name"]
 
566
        command = ["juju", "action", "do", "--format=json", unit_id, action]
 
567
        self.log.info("Running command: %s\n" % " ".join(command))
 
568
        output = _check_output(command, universal_newlines=True)
 
569
        data = json.loads(output)
 
570
        action_id = data[u'Action queued with id']
 
571
        return action_id
 
572
 
 
573
    def wait_on_action(self, action_id, _check_output=subprocess.check_output):
 
574
        """Wait for a given action, returning if it completed or not.
 
575
 
 
576
        _check_output parameter is used for dependency injection.
 
577
        """
 
578
        command = ["juju", "action", "fetch", "--format=json", "--wait=0",
 
579
                   action_id]
 
580
        output = _check_output(command, universal_newlines=True)
 
581
        data = json.loads(output)
 
582
        return data.get(u"status") == "completed"