Beispiel #1
0
    def terminate(self, instance_id):
        logs.info(
            self.__class__.__name__,
            "terminate instance {}".format(instance_id),
        )
        if not instance_id:
            raise Exception("Missing id")

        instance = self.account_provider.get_instance(instance_id)

        try:
            self.account_provider.remove_instance_dns_records(instance)
        except Exception as error:
            logs.warning("Model", "remove_dns_records failed")
            pass

        self.account_provider.terminate()

        try:
            self.dispatcher.send("info", {
                "event": "terminate",
                "resource": instance
            })
        except Exception as error:
            logs.warning("Model", "notify failed")
            pass
Beispiel #2
0
    def stop(self, instance_id):
        logs.info(self.__class__.__name__,
                  "stop instance {}".format(instance_id))
        if not instance_id:
            raise Exception("Missing id")
        instance = self.account_provider.get_instance(instance_id)
        if instance.get("state") != "running":
            raise Exception("Instance is not running")

        self.account_provider.stop()
        instance = self.account_provider.get_instance()

        try:
            self.account_provider.remove_instance_dns_records(instance)
        except Exception as error:
            logs.warning("Model", "remove_dns_record failed")
            pass
        finally:
            try:
                self.dispatcher.send("info", {
                    "event": "stop",
                    "resource": instance
                })
            except Exception as error:
                logs.warning("Model", "notify failed")
                pass
Beispiel #3
0
    def start(self, instance_id):
        logs.info(self.__class__.__name__,
                  "start instance {}".format(instance_id))

        if not instance_id:
            raise Exception("Missing id")

        instance = self.account_provider.get_instance(instance_id)

        if instance.get("state") not in ("stopped"):
            raise Exception("Wrong state, Instance must be state stopped")

        self.account_provider.start()

        instance = self.account_provider.get_instance()

        try:
            self.account_provider.create_instance_dns_records(instance)
        except Exception as error:
            logs.warning(
                "Model",
                "create_dns_record failed because {}".format(error.args[0]),
            )
            raise exception.DnsRecordsTagsMissing(
                "create_dns_record failed because {}".format(error.args[0]))
        finally:
            try:
                self.dispatcher.send("info", {
                    "event": "start",
                    "resource": instance
                })
            except Exception as error:
                logs.warning("Model", "notify failed")
                pass
Beispiel #4
0
    def start_tagged_instances(self, filters):
        filters.append({"Name": "instance-state-name", "Values": ["stopped"]})

        tagged_instances = self.account_provider.list_instances(filters)
        logs.info(
            self.__class__.__name__,
            "Info: start_tagged_instances tagged_instances: {}".format(
                tagged_instances),
        )

        if not len(tagged_instances):
            return

        ids = []
        for instance in tagged_instances:
            ids.append(instance.get("id"))

        self.account_provider.start_instances(ids)

        logs.info(
            self.__class__.__name__,
            "Info: start_tagged_instances sleeping 60 sec to get public ips",
        )
        time.sleep(60)
        logs.info(
            self.__class__.__name__,
            "Info: start_tagged_instances wake up from sleep",
        )

        for instance_id in ids:
            instance = self.account_provider.get_instance(instance_id)
            logs.info(
                self.__class__.__name__,
                "Info: start_tagged_instances instance: {}".format(instance),
            )

            try:
                self.account_provider.create_instance_dns_records(instance)
            except Exception as error:
                logs.warning("Model", "create_dns_record failed")
                pass

        self.dispatcher.send("info", {
            "event": "start_tagged_instances",
            "resources": ids
        })
Beispiel #5
0
    def stop_tagged_instances(self, filters):
        filters.append({"Name": "instance-state-name", "Values": ["running"]})

        tagged_instances = self.account_provider.list_instances(filters)

        if not len(tagged_instances):
            return

        ids = []
        for instance in tagged_instances:
            ids.append(instance.get("id"))

            if instance.get("public_ip"):
                try:
                    self.account_provider.remove_public_dns_record(instance)
                except Exception as error:
                    logs.warning(
                        "Model",
                        "remove_public_dns_record failed, {}".format(
                            error.args[0]),
                    )
                    pass

            try:
                self.account_provider.remove_private_dns_record(instance)
            except Exception as error:
                logs.warning(
                    "Model",
                    "remove_private_dns_record failed, {}".format(
                        error.args[0]),
                )
                pass

            print("{0} ({1}) has public ip {2}, private_ip {3}".format(
                instance.get("name"),
                instance.get("id"),
                instance.get("public_ip"),
                instance.get("private_ip"),
            ))

        print("Total tagged matches:", len(ids))
        self.account_provider.stop_instances(ids)
        self.dispatcher.send("info", {
            "event": "stop_tagged_instances",
            "resources": ids
        })