src/buildservice/__init__.py \
src/buildservice/__version__.py \
src/buildservice/arches.py \
+ src/buildservice/aws.py \
src/buildservice/base.py \
src/buildservice/bugtracker.py \
src/buildservice/builders.py \
import pakfire
from . import arches
+from . import aws
from . import bugtracker
from . import builders
from . import builds
self.settings = settings.Settings(self)
self.arches = arches.Arches(self)
+ self.aws = aws.AWS(self)
self.builds = builds.Builds(self)
self.cache = cache.Cache(self)
self.jobs = jobs.Jobs(self)
--- /dev/null
+#!/usr/bin/python3
+###############################################################################
+# #
+# Pakfire - The IPFire package management system #
+# Copyright (C) 2022 Pakfire development team #
+# #
+# This program is free software: you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program. If not, see <http://www.gnu.org/licenses/>. #
+# #
+###############################################################################
+
+import boto3
+import botocore.config
+
+from . import base
+from .decorators import *
+
+class AWS(base.Object):
+ @property
+ def session(self):
+ return boto3.session.Session(
+ region_name=self.settings.get("aws-region"),
+ aws_access_key_id=self.settings.get("aws-access-key"),
+ aws_secret_access_key=self.settings.get("aws-access-secret"),
+ )
+
+ @property
+ def ec2(self):
+ return self.session.resource("ec2")
#!/usr/bin/python
+import asyncio
import datetime
import hashlib
import logging
from .decorators import *
+log = logging.getLogger("pakfire.builders")
+
ACTIVE_STATES = [
"dispatching",
"running",
return entries
+ async def sync(self, *args, **kwargs):
+ """
+ Synchronize any state with AWS
+ """
+ log.info("Syncing state with AWS")
+
+ # Sync all builders
+ await asyncio.gather(*(builder.sync() for builder in self))
+
class Builder(base.DataObject):
table = "builders"
# Looks like we are ready
return True
+ # AWS
+
+ @property
+ def instance_id(self):
+ return self.data.instance_id
+
+ @lazy_property
+ def instance(self):
+ if self.instance_id:
+ return self.backend.aws.ec2.Instance(self.instance_id)
+
+ async def sync(self):
+ log.info("Syncing AWS state for %s" % self)
+
+ if not self.instance:
+ log.debug("%s does not have an instance ID" % self)
+ return
+
+ # This callback is being executed in a separate thread
+ # because boto3 is not thread-safe
+ def callback():
+ log.debug("%s is currently in state: %s" % (self, self.instance.state))
+
+ # Launch in a separate thread
+ return await asyncio.to_thread(callback)
+
def generate_password_hash(password, salt=None, algo="sha512"):
"""
updated_at timestamp without time zone,
time_keepalive timestamp without time zone,
online_until timestamp without time zone,
- cpu_arch text
+ cpu_arch text,
+ instance_id text
);
logging.info("Successfully initialied application")
- def _run_task(self, callback, t):
+ # Perform some initial tasks
+ self._run_task(self.backend.builders.sync)
+
+ def _run_task(self, callback, t=None):
"""
- Runs the callback every t seconds in the background
+ Runs the callback every t seconds in the background or once if t is None
"""
# Pass backend to the function
callback = functools.partial(callback, self.backend)
# Create a periodic callback object
- task = tornado.ioloop.PeriodicCallback(callback, t * 1000)
+ if t:
+ task = tornado.ioloop.PeriodicCallback(callback, t * 1000)
+ task.start()
- # Start the task
- task.start()
+ else:
+ ioloop = tornado.ioloop.IOLoop.current()
+ ioloop.add_callback(callback)