Esempio n. 1
0
    def handle_instances(self, args):
        """
        Describe instances in each region
        """
        instances = self.regions.instances()
        if args.debug:
            print(to_json(instances, indent=2))

        table = [[
            "State",
            "Region",
            "Zone",
            "Instance",
            "Name",
            "Type",
            "IP Addr",
        ]]

        for instance in instances:
            table.append([
                instance.state_light(),
                instance.region.name,
                instance.zone,
                str(instance),
                instance.name,
                instance.vm_type,
                instance.ipaddr,
            ])

        print(tabulate(table, tablefmt=args.format, headers='firstrow'))
Esempio n. 2
0
    def handle_availability_zones(self, args):
        """
        Describe availability zones in each region
        """
        zones = self.regions.zones()
        if args.debug:
            print(to_json(zones, indent=2))
            return

        table = [["Region", "Name", "State", "Messages"]]
        for zone in zones:
            table.append([
                zone.region.name, zone.name, zone.state,
                ", ".join(list(zone.messages()))
            ])

        print(tabulate(table, tablefmt=args.format, headers='firstrow'))
Esempio n. 3
0
    def handle_images(self, args):
        """
        Describe AMI images available in each region
        """
        images = self.regions.images()
        if args.debug:
            print(to_json(images, indent=2))
            return

        table = [["Region", "AMI", "Name", "Size", "Disk"]]
        for image in images:
            table.append([
                image.region.name,
                str(image), image.name, image.size, image.disk
            ])

        print(tabulate(table, tablefmt=args.format, headers='firstrow'))
Esempio n. 4
0
    def handle_volumes(self, args):
        """
        Describe volumes in each region
        """
        volumes = self.regions.volumes()
        if args.debug:
            print(to_json(volumes, indent=2))

        table = [["State", "Region", "Volume", "Name", "Attached"]]

        for volume in volumes:
            table.append([
                volume.state_light(), volume.region.name,
                str(volume), volume.name, ", ".join(list(volume.attached_to()))
            ])

        print(tabulate(table, tablefmt=args.format, headers='firstrow'))
Esempio n. 5
0
    def handle_key_pairs(self, args):
        """
        Describe key pairs available in each region
        """
        pairs = self.regions.key_pairs()
        if args.debug:
            print(to_json(pairs, indent=2))
            return

        table = [["Region", "Name", "Fingerprint", "Valid Local"]]

        for key in pairs:
            table.append([
                key.region.name, key.name, key.fingerprint,
                CHECKS[key.has_valid_key()]
            ])

        print(tabulate(table, tablefmt=args.format, headers='firstrow'))
Esempio n. 6
0
    def pprint(self, report, headers, args):
        """
        Pretty print the regions data for the command line.
        """

        # Data serialization
        if args.format == 'json':
            print(to_json(report, indent=2))
            return

        # Make human readable
        for item in report:
            item['Used'] = CHECKMARK if item['Used'] else ""

        # Tabulate the report and print
        table = [headers]
        table += [[row[key] for key in headers] for row in report]

        print(tabulate(table, tablefmt=args.format, headers='firstrow'))
Esempio n. 7
0
    def handle_security_groups(self, args):
        """
        Describe security groups in each region
        """
        groups = self.regions.security_groups()
        if args.debug:
            print(to_json(groups, indent=2))
            return

        table = [["Region", "Group", "Name", "Ports"]]

        for group in groups:
            ports = ", ".join([
                "{}-{}".format(*p) if isinstance(p, tuple) else str(p)
                for p in group.open_ports()
            ])

            table.append([group.region.name, str(group), group.name, ports])

        print(tabulate(table, tablefmt=args.format, headers='firstrow'))
Esempio n. 8
0
    def handle_placement_groups(self, args):
        """
        Describe placement groups in each region
        """
        groups = self.regions.placement_groups()
        if args.debug:
            print(to_json(groups, indent=2))
            return

        table = [["Region", "Name", "State", "Strategy", "Partition Count"]]

        for group in groups:
            table.append([
                group.region.name,
                group.name,
                group.state,
                group.strategy,
                group.partition_count(),
            ])

        print(tabulate(table, tablefmt=args.format, headers='firstrow'))
Esempio n. 9
0
    def handle_launch_templates(self, args):
        """
        Describe launch templates in each region
        """
        templates = self.regions.launch_templates()
        if args.debug:
            print(to_json(templates, indent=2))
            return

        table = [["Region", "Template", "Name", "Version"]]
        for template in templates:
            version = "v{} (r{})".format(template.default_version,
                                         template.latest_version)

            table.append([
                template.region.name,
                str(template),
                template.name,
                version,
            ])

        print(tabulate(table, tablefmt=args.format, headers='firstrow'))
Esempio n. 10
0
        otherwise this will take a really long time and return many results.
        """
        return Images.collect(
            wait((region.images for region in self), kwargs=kwargs))

    def security_groups(self, **kwargs):
        """
        Returns the security groups associated with the region.
        """
        return SecurityGroups.collect(
            wait((region.security_groups for region in self), kwargs=kwargs))

    def placement_groups(self, **kwargs):
        """
        Returns the placement groups associated with the region.
        """
        return PlacementGroups.collect(
            wait((region.placement_groups for region in self), kwargs=kwargs))


if __name__ == '__main__':
    from geonet.utils.serialize import to_json

    regions = Regions.load()
    region = regions[settings.aws.aws_region]

    # print(to_json(region.key_pairs(), indent=2))
    print(to_json(region.instances(), indent=2))
    # print(to_json(region.images(), indent=2))
    # print(to_json(region.security_groups(), indent=2))