def get_running_environment(self, env_name, env_template_name, all_credentials):
        nodes = self.list(all_credentials, lambda x: node_predicates.running_in_env(env_name, env_template_name)(x))
        region_nodes_map = {}
        for node in nodes:
            if not node.region().name in region_nodes_map.keys():
                region_nodes_map.update({node.region().name:[node]})
            else:
                region_nodes_map[node.region().name].append(node)

        locations = self.get_locations(region_nodes_map)
        return phoenix.environment_description.EnvironmentDescription(env_name, locations)
 def get_running_environment(self, env_name, env_template_name, all_credentials):
     nodes = self.list(all_credentials, lambda x: node_predicates.running_in_env(env_name, env_template_name)(x))
     locations = []
     if not nodes is None and len(nodes) != 0:
         locations.append(Location(self.host_name, nodes))
     return phoenix.environment_description.EnvironmentDescription(env_name, locations)
    def delta_defs_with_running_nodes(self, node_definitions):
        # Given a list of node definitions, determines if a running node matches. If so,
        # that node is removed from the returned node definitons.
        # Returns a tuple of nodes still to provision, a mapping from service to running nodes for those
        # nodes that match, and list of nodes in the environment that are no longer needed
        pre_existing_nodes = defaultdict(lambda: set())
        node_defs_to_provision = []

        nodes_in_environment = self.node_provider.list(self.all_credentials, lambda x: node_predicates.running_in_env(self.name, self.env_def_name)(x))

        for node_def in node_definitions:
            matching_running_node = next((n for n in nodes_in_environment if n.matches_definition(node_def)), None)

            if matching_running_node:
                for service in node_def.services:
                    pre_existing_nodes[service].add(matching_running_node)

                # We don't want to match the same node again!
                nodes_in_environment.remove(matching_running_node)
            else:
                node_defs_to_provision.append(node_def)

        # At this point, if there are any nodes still in 'nodes_in_environment' then they haven't matched, and need
        # therefore to be terminated
        nodes_to_terminate = []
        nodes_to_terminate.extend(nodes_in_environment)

        return node_defs_to_provision, pre_existing_nodes, nodes_to_terminate
 def list_nodes(self):
     return self.node_provider.list(self.all_credentials, lambda x: node_predicates.running_in_env(self.name, self.env_def_name)(x))
    def test_full_end_to_end_integration_test(self):
        with settings(credentials_dir="credentials"):
            environment_definition = launch(env_template="lxc_hello_world", env_name="auto", config_dir="samples")

            node_provider = environment_definition.node_provider

            nodes = node_provider.list(environment_definition.all_credentials, lambda x: node_predicates.running_in_env('auto','lxc_hello_world')(x))

            hello_world_node = [x for x in nodes if 'hello_world' in x.get_services()].pop()

            status, output = commands.getstatusoutput("curl -s %s/healthcheck" % hello_world_node.address().get_service_address('hello_world')[1])

            self.assertEqual(status, 0)
            self.assertEqual(output, "* Apache: OK\n* deadlocks: OK")
    def test_can_create_elb_for_a_service(self):
        with settings(credentials_dir="credentials"):
            self.remove_existing_hello_world_elb('elb_integration', 'hello_world')

            dynamic_env_name = "test_can_create_a_load_balancer_for_a_service"+str(int(round(time.time() * 1000)))
            env_template_name = "elb_integration"
            self.env_to_shut_down.update({env_template_name: dynamic_env_name})

            environment_definition = launch(env_template=env_template_name, env_name=dynamic_env_name, config_dir="samples", property_file="build_credentials/phoenix.ini")
            node_provider = environment_definition.node_provider
            nodes = node_provider.list(environment_definition.all_credentials, lambda x: node_predicates.running_in_env(dynamic_env_name, env_template_name)(x))
            node_provider = environment_definition.node_provider
            node = [x for x in nodes if 'hello_world' in x.get_services()].pop()

            conn = self.get_connection_for_region(node.region().name, node_provider.public_api_key, node_provider.private_api_key)

            hook_elb_name = environment_definition.service_lifecycle_hooks['hello_world'][0].elb_name
            load_balancer = conn.get_all_load_balancers(load_balancer_names=[hook_elb_name])[0]

#            self.assertEqual(load_balancer.dns_name, node.address().get_dns_name()) TODO: make sure the node address dns matches with the elb dns

            status, output = commands.getstatusoutput("curl -s %s:8081/healthcheck" % load_balancer.dns_name)

            self.assertEqual(status, 0)
            self.assertEqual(output, "* Apache: OK\n* deadlocks: OK")
    def test_should_have_a_functioning_hello_world(self):
        with settings(credentials_dir="credentials"):
            dynamic_env_name = "test_should_have_a_functioning_hello_world_"+str(int(round(time.time() * 1000)))
            env_template_name = "integration"
            self.env_to_shut_down.update({env_template_name: dynamic_env_name})
            environment_definition = launch(env_template=env_template_name, env_name=dynamic_env_name, config_dir="samples", property_file="build_credentials/phoenix.ini")

            node_provider = environment_definition.node_provider

            nodes = node_provider.list(environment_definition.all_credentials, lambda x: node_predicates.running_in_env(dynamic_env_name, env_template_name)(x))

            hello_world_node = [x for x in nodes if 'hello_world' in x.get_services()].pop()

            status, output = commands.getstatusoutput("curl -s %s:8081/healthcheck" % hello_world_node.address().dns_name)

            self.assertEqual(status, 0)
            self.assertEqual(output, "* Apache: OK\n* deadlocks: OK")
 def get_running_node(self, environment_definition, env_def_name, environment_name, node_provider):
     nodes = node_provider.list(environment_definition.all_credentials,
         lambda x: node_predicates.running_in_env(environment_name, env_def_name)(x))
     self.assertEquals(len(nodes), 1)
     return nodes[0]