From: Michael Tremer Date: Sat, 18 Jun 2022 11:36:16 +0000 (+0000) Subject: builders: Implement automatic change of instance type X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7d81ab1701f60ace645607fe739747034cd28af6;p=pbs.git builders: Implement automatic change of instance type Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/builders.py b/src/buildservice/builders.py index 68ab11b6..7ef45389 100644 --- a/src/buildservice/builders.py +++ b/src/buildservice/builders.py @@ -1,6 +1,7 @@ #!/usr/bin/python import asyncio +import botocore.exceptions import datetime import hashlib import logging @@ -444,6 +445,10 @@ class Builder(base.DataObject): def instance_id(self): return self.data.instance_id + @property + def instance_type(self): + return self.data.instance_type + @lazy_property def instance(self): if self.instance_id: @@ -473,6 +478,9 @@ class Builder(base.DataObject): def _start(self): log.info("Starting %s" % self) + # Set correct instance type + self._set_instance_type() + # Send the start signal self.instance.start() @@ -483,6 +491,32 @@ class Builder(base.DataObject): log.debug("%s has been started" % self) + def _set_instance_type(self): + """ + Changes the type of this instance + """ + # Don't try setting instance type if nothing is configured + if not self.instance_type: + return + + # Check if this needs changing at all + if self.instance.instance_type == self.instance_type: + return + + log.debug("Changing instance type of %s to %s" % (self, self.instance_type)) + + # Send the change + try: + self.instance.modify_attribute( + InstanceType={ + "Value" : self.instance_type, + } + ) + + # Log an error if this request wasn't successful + except botocore.exceptions.ClientError as e: + log.warning("Could not change instance type of %s: %s" % (self, e)) + async def stop(self): """ Stops this instance on AWS diff --git a/src/database.sql b/src/database.sql index d079b476..09ede674 100644 --- a/src/database.sql +++ b/src/database.sql @@ -130,7 +130,8 @@ CREATE TABLE public.builders ( time_keepalive timestamp without time zone, online_until timestamp without time zone, cpu_arch text, - instance_id text + instance_id text, + instance_type text );