from rolekit.server.io.systemd import SystemdContainerServiceUnit img = 'testimage' cont = 'testcontainer' desc = 'this test container' env = {'var_y': 'value_y', 'var_x': 'value_x'} ports = [8080, 443] s = SystemdContainerServiceUnit(image_name=img, container_name=cont, desc=desc, env=env, ports=ports) s.write()
def do_deploy_async(self, values, sender=None): log.debug9("TRACE: do_deploy_async") # Run whatever series of actions are needed to deploy # this role in a meaningful way. # import docker # Get the default cache size # Find out how much RAM is available on the system if 'cache_size' not in values: # Do a late import of psutil. This will only get # used during a deployment, so we don't need to # have it as a dependency for rolekit itself import psutil # Get the total number of bytes in local system memory total_ram = psutil.virtual_memory().total # If 25% of the available memory is less than 1GB, use # that for the cache. if total_ram / 4 < GiB_SIZE: # Set cache_size in MiB values['cache_size'] = int(total_ram / 4 / MiB_SIZE) else: # Cap the default size at 1 GB in MiB values['cache_size'] = int(GiB_SIZE / MiB_SIZE) # Set defaults if "connections" not in values: values["connections"] = self._DEFAULTS["connections"] if "threads" not in values: values["threads"] = self._DEFAULTS["threads"] # Create a container for memcached and launch that log.debug2("Enabling the Docker container manager") # Enable and start the docker service enable_units(['docker.service']) log.debug2("Starting the Docker container manager") with SystemdJobHandler() as job_handler: job_path = job_handler.manager.StartUnit("docker.service", "replace") job_handler.register_job(job_path) job_results = yield job_handler.all_jobs_done_future() if any([x for x in job_results.values() if x not in ("skipped", "done")]): details = ", ".join(["%s: %s" % item for item in job_results.items()]) raise RolekitError(COMMAND_FAILED, "Starting docker.service failed: %s" % details) log.debug2("Pulling %s image from Docker Hub" % MEMCACHED_DOCKER_IMAGE) dockerclient = docker.Client(base_url=docker.utils.utils.DEFAULT_UNIX_SOCKET, version='auto') # First, pull down the latest version of the memcached container dockerclient.pull(MEMCACHED_DOCKER_IMAGE, tag="latest") log.debug2("Creating systemd service unit") # Generate a systemd service unit for this container container_unit = SystemdContainerServiceUnit( image_name = MEMCACHED_DOCKER_IMAGE, container_name = "memcached_%s" % self.get_name(), desc="memcached docker container - %s" % self.get_name(), env = { "MEMCACHED_CACHE_SIZE": str(values['cache_size']), "MEMCACHED_CONNECTIONS": str(values['connections']), "MEMCACHED_THREADS": str(values['threads']) }, ports = ("{0}:{0}/tcp".format(MEMCACHED_DEFAULT_PORT), "{0}:{0}/udp".format(MEMCACHED_DEFAULT_PORT)) ) container_unit.write() # Make systemd load this new unit file log.debug2("Running systemd daemon-reload") with SystemdJobHandler() as job_handler: job_handler.manager.Reload() # Return the target information target = RoleDeploymentValues(self.get_type(), self.get_name(), "Memory Cache") target.add_required_units(['memcached_%s.service' % self.get_name()]) log.debug9("TRACE: exiting do_deploy_async") yield target
def do_deploy_async(self, values, sender=None): log.debug9("TRACE: do_deploy_async") # Run whatever series of actions are needed to deploy # this role in a meaningful way. # import docker # Get the default cache size # Find out how much RAM is available on the system if 'cache_size' not in values: # Do a late import of psutil. This will only get # used during a deployment, so we don't need to # have it as a dependency for rolekit itself import psutil # Get the total number of bytes in local system memory total_ram = psutil.virtual_memory().total # If 25% of the available memory is less than 1GB, use # that for the cache. if total_ram / 4 < GiB_SIZE: # Set cache_size in MiB values['cache_size'] = int(total_ram / 4 / MiB_SIZE) else: # Cap the default size at 1 GB in MiB values['cache_size'] = int(GiB_SIZE / MiB_SIZE) # Set defaults if "connections" not in values: values["connections"] = self._DEFAULTS["connections"] if "threads" not in values: values["threads"] = self._DEFAULTS["threads"] # Create a container for memcached and launch that log.debug2("Enabling the Docker container manager") # Enable and start the docker service with SystemdJobHandler() as job_handler: job_handler.manager.EnableUnitFiles(["docker.service"], False, True) log.debug2("Starting the Docker container manager") with SystemdJobHandler() as job_handler: job_path = job_handler.manager.StartUnit("docker.service", "replace") job_handler.register_job(job_path) job_results = yield job_handler.all_jobs_done_future() if any([x for x in job_results.values() if x not in ("skipped", "done")]): details = ", ".join(["%s: %s" % item for item in job_results.items()]) raise RolekitError(COMMAND_FAILED, "Starting docker.service failed: %s" % details) log.debug2("Pulling %s image from Docker Hub" % MEMCACHED_DOCKER_IMAGE) dockerclient = docker.Client(base_url=docker.utils.utils.DEFAULT_UNIX_SOCKET, version='auto') # First, pull down the latest version of the memcached container dockerclient.pull(MEMCACHED_DOCKER_IMAGE, tag="latest") log.debug2("Creating systemd service unit") # Generate a systemd service unit for this container container_unit = SystemdContainerServiceUnit( image_name = MEMCACHED_DOCKER_IMAGE, container_name = "memcached_%s" % self.name, desc="memcached docker container - %s" % self.name, env = { "MEMCACHED_CACHE_SIZE": str(values['cache_size']), "MEMCACHED_CONNECTIONS": str(values['connections']), "MEMCACHED_THREADS": str(values['threads']) }, ports = ("{0}:{0}/tcp".format(MEMCACHED_DEFAULT_PORT), "{0}:{0}/udp".format(MEMCACHED_DEFAULT_PORT)) ) container_unit.write() # Make systemd load this new unit file log.debug2("Running systemd daemon-reload") with SystemdJobHandler() as job_handler: job_handler.manager.Reload() # Return the target dictionary target = {'Role': 'memcache', 'Instance': self.get_name(), 'Description': "Memory Cache Role - %s" % self.get_name(), 'Wants': ['memcached_%s.service' % self.name], 'After': ['network.target']} log.debug9("TRACE: exiting do_deploy_async") yield target