Exemple #1
0
    def Run(self):
        response = None
        if self.tags is None:
            response = BotoClient.Get(self.region).describe_instances()
        else:
            filters = []
            for k, v in self.tags.iteritems():
                # job_id:None can be specified to kill all instances without job_id
                if v == "None":
                    continue
                d = {}
                d["Name"] = ("tag:%s" % k)
                d["Values"] = [v]
                filters.append(d)
            response = BotoClient.Get(
                self.region).describe_instances(Filters=filters)
        #Cons.P(pprint.pformat(response, indent=2, width=100))

        self.inst_ids_to_term = []

        for r in response["Reservations"]:
            for r1 in r["Instances"]:
                if "Name" in r1["State"]:
                    # Terminate only running intances
                    if r1["State"]["Name"] == "running":
                        if _job_id_none_requested:
                            #Cons.P(pprint.pformat(r1))
                            if "Tags" not in r1:
                                self.inst_ids_to_term.append(r1["InstanceId"])
                        else:
                            self.inst_ids_to_term.append(r1["InstanceId"])

        #Cons.P("There are %d instances to terminate." % len(self.inst_ids_to_term))
        #Cons.P(pprint.pformat(self.inst_ids_to_term, indent=2, width=100))

        if len(self.inst_ids_to_term) > 0:
            self.response = BotoClient.Get(self.region).terminate_instances(
                InstanceIds=self.inst_ids_to_term)

        with _regions_processed_lock:
            global _regions_processed
            _regions_processed += 1
            if _regions_processed == 1:
                pass
            elif _regions_processed % 6 == 1:
                #                        Terminating running instances:
                Cons.sys_stdout_write("\n                              ")
            Cons.sys_stdout_write(" %s" % self.region)
    def Run(self):
        bc = boto3.session.Session().client("ec2", region_name=self.region)
        response = bc.describe_account_attributes(AttributeNames=[
            "max-instances",
        ])
        #Cons.P(pprint.pformat(response, indent=2))
        for r in response["AccountAttributes"]:
            if r["AttributeName"] != "max-instances":
                continue
            if len(r["AttributeValues"]) != 1:
                raise RuntimeError("len(r[\"AttributeValues\"])=%d" %
                                   len(r["AttributeValues"]))
            self.max_inst = int(r["AttributeValues"][0]["AttributeValue"])
            #Cons.P(self.max_inst)

        Cons.sys_stdout_write(" %s" % self.region)
	def Run(self):
		bc = boto3.session.Session().client("ec2", region_name = self.region)
		response = bc.describe_account_attributes(
				AttributeNames=[
					"max-instances",
					]
				)
		#Cons.P(pprint.pformat(response, indent=2))
		for r in response["AccountAttributes"]:
			if r["AttributeName"] != "max-instances":
				continue
			if len(r["AttributeValues"]) != 1:
				raise RuntimeError("len(r[\"AttributeValues\"])=%d" % len(r["AttributeValues"]))
			self.max_inst = int(r["AttributeValues"][0]["AttributeValue"])
			#Cons.P(self.max_inst)

		Cons.sys_stdout_write(" %s" % self.region)
Exemple #4
0
	def Run(self):
		try:
			# http://boto3.readthedocs.io/en/latest/guide/session.html
			session = boto3.session.Session()
			bc = BotoClient.Get(self.region)

			if self.tags is None:
				self.response = bc.describe_instances()
			else:
				filters = []
				for k, v in self.tags.iteritems():
					d = {}
					d["Name"] = ("tag:%s" % k)
					d["Values"] = [v]
					filters.append(d)
				self.response = bc.describe_instances(Filters = filters)

		except Exception as e:
			Cons.P("%s\n%s\nregion=%s" % (e, traceback.format_exc(), self.region))
			os._exit(1)

		Cons.sys_stdout_write(" %s" % self.region)
Exemple #5
0
    def Run(self):
        try:
            # http://boto3.readthedocs.io/en/latest/guide/session.html
            session = boto3.session.Session()
            bc = BotoClient.Get(self.region)

            if self.tags is None:
                self.response = bc.describe_instances()
            else:
                filters = []
                for k, v in self.tags.iteritems():
                    d = {}
                    d["Name"] = ("tag:%s" % k)
                    d["Values"] = [v]
                    filters.append(d)
                self.response = bc.describe_instances(Filters=filters)

        except Exception as e:
            Cons.P("%s\n%s\nregion=%s" %
                   (e, traceback.format_exc(), self.region))
            os._exit(1)

        Cons.sys_stdout_write(" %s" % self.region)
Exemple #6
0
    def Run(self):
        response = None
        if self.tags is None:
            response = BotoClient.Get(self.region).describe_instances()
        else:
            filters = []
            for k, v in self.tags.iteritems():
                # job_id:None can be specified to kill all instances without job_id
                if v == "None":
                    continue
                d = {}
                d["Name"] = ("tag:%s" % k)
                d["Values"] = [v]
                filters.append(d)
            response = BotoClient.Get(
                self.region).describe_instances(Filters=filters)
        #Cons.P(pprint.pformat(response, indent=2, width=100))

        inst_ids_to_term_self = []
        inst_ids_to_term_others = []
        self.inst_ids_to_term = []

        for r in response["Reservations"]:
            for r1 in r["Instances"]:
                if "Name" in r1["State"]:
                    # Terminate only running intances
                    if r1["State"]["Name"] == "running":
                        inst_id = r1["InstanceId"]
                        if _TermInst._job_id_none_requested:
                            #Cons.P(pprint.pformat(r1))
                            if "Tags" not in r1:
                                if _TermInst._term_by_job_id_self_last:
                                    if inst_id == Ec2Util.InstId():
                                        inst_ids_to_term_self.append(inst_id)
                                    else:
                                        inst_ids_to_term_others.append(inst_id)
                                self.inst_ids_to_term.append(inst_id)
                        else:
                            if _TermInst._term_by_job_id_self_last:
                                if inst_id == Ec2Util.InstId():
                                    inst_ids_to_term_self.append(inst_id)
                                else:
                                    inst_ids_to_term_others.append(inst_id)
                            self.inst_ids_to_term.append(inst_id)

        #Cons.P("There are %d instances to terminate." % len(self.inst_ids_to_term))
        #Cons.P(pprint.pformat(self.inst_ids_to_term, indent=2, width=100))

        if _TermInst._term_by_job_id_self_last:
            if len(inst_ids_to_term_others) > 0:
                self.term_inst_response = BotoClient.Get(
                    self.region).terminate_instances(
                        InstanceIds=inst_ids_to_term_others)

            if len(inst_ids_to_term_self) > 0:
                # Wait for others to terminate
                time.sleep(5)
                self.term_inst_response = BotoClient.Get(
                    self.region).terminate_instances(
                        InstanceIds=inst_ids_to_term_self)
        else:
            if len(self.inst_ids_to_term) > 0:
                self.term_inst_response = BotoClient.Get(
                    self.region).terminate_instances(
                        InstanceIds=self.inst_ids_to_term)

        # Note: below is not even reached when you kill yourself
        with _TermInst._regions_processed_lock:
            _TermInst._regions_processed += 1
            if _TermInst._regions_processed == 1:
                pass
            elif _TermInst._regions_processed % 6 == 1:
                #                        Terminating running instances:
                Cons.sys_stdout_write("\n                              ")
            Cons.sys_stdout_write(" %s" % self.region)