]> git.ipfire.org Git - pbs.git/commitdiff
builders: Implement automatic change of instance type
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 18 Jun 2022 11:36:16 +0000 (11:36 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 18 Jun 2022 11:36:16 +0000 (11:36 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/builders.py
src/database.sql

index 68ab11b67cd04076ec01802510593a5f5de34f32..7ef45389ccf5f35c43b323a6c90b346ed2e18a2d 100644 (file)
@@ -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
index d079b476fa488ffc057f718b3e1db1230eaf95b3..09ede6746daf806842ca5916f268795037705153 100644 (file)
@@ -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
 );