~bcsaller/pyjuju/makefile-meta

« back to all changes in this revision

Viewing changes to ensemble/providers/ec2/connect.py

merge handling of invalid stored zk instance id and resulting ec2 exception. [r=niemeyer][f=659930]

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
from twisted.internet.defer import succeed, inlineCallbacks, returnValue
4
4
 
 
5
from txaws.ec2.exception import EC2Error
 
6
 
5
7
from ensemble.state.sshclient import SSHClient
6
8
from ensemble.errors import (
7
9
    NoConnection, EnvironmentNotFound, EnvironmentPending)
13
15
class EC2ZookeeperAware(object):
14
16
 
15
17
    def _verify_zookeeper_instances(self, state):
16
 
        """Verify which zookeeper nodes are running."""
17
 
        d = succeed([])
 
18
        """Verify which zookeeper nodes are running.
 
19
 
 
20
        Returns the first valid instance found as a single element list,
 
21
        or returns an empty list. If
 
22
        """
 
23
        instances = None
18
24
 
19
25
        if state:
20
 
            instance_info = state.get("zookeeper-instances", None)
21
 
            if instance_info:
22
 
                d = self.ec2.describe_instances(*[i for i in instance_info])
23
 
 
24
 
        def filter_running(instances):
25
 
            running = []
26
 
            has_pending = False
27
 
            for instance in instances:
28
 
                if instance.instance_state == "running":
29
 
                    running.append(instance)
30
 
                elif instance.instance_state == "pending":
31
 
                    has_pending = True
32
 
            if not running:
33
 
                if has_pending:
34
 
                    raise EnvironmentPending(
35
 
                        "Started machine is still pending.")
36
 
                else:
37
 
                    raise EnvironmentNotFound(
38
 
                        "No Ensemble machines found. "
39
 
                        "Is the environment bootstrapped?")
40
 
            return running
41
 
 
42
 
        d.addCallback(filter_running)
 
26
            instances = state.get("zookeeper-instances", None)
 
27
 
 
28
        if not instances:
 
29
            d = succeed([])
 
30
        else:
 
31
            instance_id = instances.pop(0)
 
32
            d = self.ec2.describe_instances(instance_id)
 
33
 
 
34
        d.addCallback(self._cb_filter_running_zookeeper_instances)
 
35
        d.addErrback(self._eb_invalid_instance_id, instances)
 
36
 
43
37
        return d
44
38
 
 
39
    def _eb_invalid_instance_id(self, failure, instances):
 
40
        # Trap these failures to continue processing instances
 
41
        failure.trap(
 
42
            EC2Error, EnvironmentPending, EnvironmentNotFound)
 
43
 
 
44
        # Reformat invalid ids ec2 errors into an appros ensemble error.
 
45
        if not instances and failure.check(EC2Error):
 
46
            error_code = failure.value.get_error_codes()
 
47
            if error_code == "InvalidInstanceID.NotFound":
 
48
                raise EnvironmentNotFound(
 
49
                    "No Ensemble machines found. "
 
50
                    "Is the environment bootstrapped?")
 
51
 
 
52
        # Process next instance
 
53
        if instances:
 
54
            instance_id = instances.pop(0)
 
55
            d = self.ec2.describe_instances(instance_id)
 
56
 
 
57
            d.addCallback(self._cb_filter_running_zookeeper_instances)
 
58
            d.addErrback(self._eb_invalid_instance_id, instances)
 
59
            return d
 
60
 
 
61
        # Return the original failure
 
62
        return failure
 
63
 
 
64
    def _cb_filter_running_zookeeper_instances(self, instances):
 
65
        # instances is either a single element list or an empty list.
 
66
        assert len(instances) <= 1, "Should only process single instance %r"
 
67
        for instance in instances:
 
68
            if instance.instance_state == "running":
 
69
                return [instance]
 
70
            elif instance.instance_state == "pending":
 
71
                raise EnvironmentPending(
 
72
                    "Started machine is still pending.")
 
73
        raise EnvironmentNotFound(
 
74
            "No Ensemble machines found. "
 
75
            "Is the environment bootstrapped?")
 
76
 
45
77
 
46
78
class EC2Connect(EC2Operation, EC2ZookeeperAware):
47
79