Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Using Falcon to cleanup Satellite host records that belong to terminated OSP instances

October 6, 2017
Simeon Debreceni
Related topics:
Python

Share:

    Overview

    In an environment where OpenStack instances are automatically subscribed to Satellite, it is important that Satellite is notified of terminated instances so that is can safely delete its host record. Not doing so will:

    • Exhaust the available subscriptions, leading to unsubscribed hosts not being able to apply updates and security errata.
    • In the event that an emergency security errata needs to be deployed across the organization, Satellite administrators would be unable to determine if a host was either off or terminated, leading to uncertainty with their security posture.

    In smaller environments, where one team is responsible for both OSP and Satellite, it's possible to have one system administrator do this by using their administrator level access across both systems to determine which host records can be safely deleted in Satellite when the corresponding instance no longer exists.

    This approach, however, does not scale as the number of instances is launched and terminated daily increases across the environment. Larger environments also lead to different teams being responsible for different software suites, and administrator level credentials would seldom be granted to a single person.

    One approach to solving this problem in an automated manner is to have Satellite periodically poll OpenStack to determine if a given instance's UUID still exists and should it not, remove the host record.

    Some assumptions before we begin:

    • Instances launched are automatically subscribed to Satellite via rhsm.
    • UUID of the instance is passed to Satellite during the registration process, found by default under the hosts virt::uuid fact on Satellite.
    • An instance/VM/physical box that can connect to the Keystone/nova endpoints and that can be polled by Satellite.

    Designing the API

    Falcon is a remarkably simple Python web API framework that can be quickly deployed with minimal effort. The API was designed to return status codes depending on the status of the instance's UUID being checked using the call http://hostname/check/, where the following return codes are used:

    200 = instance exists, don't delete host record

    400 = bad UUID request - UUID not formatted correctly

    404 = instance does not exist, delete host record

    500 = unable to reach OSP

    With the API designed, it's now a simple coding exercise to have the API:

    1. Check that the UUID provided is valid.
    2. Contact OSP and search for the provided UUID.
    3. Return a status code based on the search result.

    Using Keystone v3

    The keystoneauth session Python API is used to create a token that is then passed to Nova via the novaclient Python API. The following functions will be used later to query OSP:

    
    from novaclient import client as nova_client
    from keystoneclient.v3 import client as keystone_client
    from keystoneauth1.identity import v3
    from keystoneauth1 import session
    import sys
    
    def get_osp_token():
      try:
        auth = v3.Password(user_domain_name=default, username=admin, password=XXXX, auth_url=https://osp.endpoint:35357, project_domain_name=default, project_name=admin)
        sess = session.Session(auth=auth, verify="./cacert.pem")
        return sess
    
      except session.exceptions.http.Unauthorized:
        print ("Credentials incorrect")
    
      except session.excpetions.connection.ConnectFailure:
        print ("Unable to reach OSP Server")
    
      except:
        print ("Unexpected error:, sys.exc_info()[0])

    Using the token generated by the above get_osp_token() function, the following generate_id_list() function will generate a list of all the instance UUID's that exist in OSP:

    
    def generate_id_list(token):
      nova = nova_client.Client('2', session=token)
      instance_list = nova.servers.list(detailed=True, search_opts= {'all_tenants': 1,})
      instance_id_list = [instance.id.lower() for instance in instance_list]
      return instance_id_list
    

    The Falcon API

    Beginning with Falcon's simple Learning by Example, we utilize the above functions to create our API:

    
    #instanceapi.py
    import falcon
    
    class CheckUUID(object):
    
      def on_get(self, req, resp, uuid):
        if not uuid_valid(uuid):
          resp.status = falcon.HTTP_400
          resp.body = (uuid+' is not a valid UUID that can be parsed\n')
          return
    
        osptoken = get_osp_token()
        id_list = generate_id_list(osptoken)
    
        if not id_list:
          resp.status = falcon.HTTP_500
          resp.body = ('Server Down\n')
          return
    
        uuid = uuid.lower()
    
        if uuid in id_list:
          resp.status=falcon.HTTP_200
          resp.body =('The UUID '+uuid+' exists in OSP\n')
          return
    
        # no match found
        resp.status = falcon.HTTP_404
        resp.body = ('The UUID '+uuid+' does not exist in OSP\n')
    
    # main block
    app = falcon.API()
    check = UUIDCheck()
    app.add_route('/check/{uuid}', check)
    

    Using Gunicorn to serve the API

    With the working code, it's now a simple matter of deploying the code to a host that can be polled by Satellite.

    Using Gunicorn, a simple WSGI server is started which serves the Python code.

    # gunicorn --workers=4 --bind=0.0.0.0:80 check:app

    A simple systemd service file will allow the API to start in the event the host is restarted:

    #/usr/lib/systemd/system/instanceapi.service
    
    [Unit]
    Description=Instance API Frontend
    After=network.target
    
    [Service]
    Environment="PATH=/root"
    WorkingDirectory=/root
    ExecStart=/usr/bin/gunicorn instanceapi:app -b 0.0.0.0:80 -w 4 --access-logfile /var/log/access.log
    
    [Install]
    WantedBy=multi-user.target
    # systemctl enable instanceapi.service; systemctl start instanceapi.service

    Checking Satellite Hosts

    Satellite can now check iterate over the host records and determine if it can be safely removed. To get the UUID of a specific host from Satellite, you can use the hammer CLI:

    # hammer fact list --search "host=<hostname> and fact=virt::uuid"

    The UUID returned by hammer can then be passed to API, where the return code will indicate whether the instance still exists.

    Conclusion

    Without requiring the sharing of administrator credentials between Satellite and OpenStack deployments, it's now possible with the use of a simple API to determine if an instance on OSP still exists.


    Whether you are new to Linux or have experience, downloading this cheat sheet can assist you when encountering tasks you haven’t done lately.

    Last updated: October 5, 2017

    Recent Posts

    • Red Hat build of Quarkus 3.27: Release highlights for developers

    • ActiveMQ Artemis or Apache Kafka? What you need to know

    • Multimodal AI at the edge: Deploy vision language models with RamaLama

    • SDG Hub: Building synthetic data pipelines with modular blocks

    • AI accelerator selection for inference: A stage-based framework

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue