Moshe Zadka
DevOps in Python
Infrastructure as Python
2nd ed.
The Apress logo.
Moshe Zadka
Belmont, CA, USA
ISBN 978-1-4842-7995-3 e-ISBN 978-1-4842-7996-0
https://doi.org/10.1007/978-1-4842-7996-0
Moshe Zadka 2022
Apress Standard
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
This Apress imprint is published by the registered company APress Media, LLC, part of Springer Nature.
The registered company address is: 1 New York Plaza, New York, NY 10004, U.S.A.
Dedicated to A and N, my favorite two projects
Introduction
Python began as a language to automate an operating system: the Amoeba. A typical Unix shell would be ill-suited since it had an API, not just textual file representations. The Amoeba OS is a relic now. However, Python continues to be a useful tool for automation of operationsthe heart of typical DevOps work.
It is easy to learn and easy to write readable code is a necessity when a critical part of the work is responding to a 4 a.m. alert and modifying some misbehaving program.
It has powerful bindings to C and C++, the universal languages of the operating systemand yet is natively memory-safe, leading to few crashes at the automation layer.
Finally, although not true when it was created, Python is one of the most popular languages. This means that it is relatively easy to hire people with Python experience and easy to get training materials and courses for people who need to learn on the job.
This book guides you through how to take advantage of Python to automate operations.
To get the most out of the book, you need to be somewhat familiar with Python. If you are new to Python, there are many great resources to learn it, including the official Python tutorial at docs.python.org. You also need to be somewhat familiar with Unix-like operating systems like Linux, especially how to use the command line.
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub (github.com/apress). For more detailed information, please visit http://www.apress.com; https://github.com/Apress/DevOps-in-Python-2nd-ed
Acknowledgments
Thanks to my wife, Jennifer Zadka, without whose support I could not have written this book.
Thanks to my parents, Yaacov and Pnina Zadka, who taught me how to learn.
Thanks to my advisor, Yael Karshon, who taught me how to write.
Thanks to Mahmoud Hashemi for inspiration and encouragement.
Thanks to Mark Williams for being there for me.
Thanks to Glyph Lefkowitz for teaching me about Python, programming, and being a good person.
Thanks to Brennon Church and Andrea Ross, who supported my personal growth and learning journey.
Table of Contents
About the Author
Moshe Zadka
A photo of Moshe Zadka.
has been involved in the Linux community since 1998, helping in Linux installation parties. He has been programming Python since 1999 and has contributed to the core Python interpreter. Moshe has been a DevOps/SRE since before those terms existed, caring deeply about software reliability, build reproducibility, and more. He has worked in companies as small as three people and as big as tens of thousandsand usually in a position where software meets system administration.
About the Technical Reviewer
Martyn Bristow
A photo of Martyn Bristow.
is a software developer in the United Kingdom. He began using Python as a researcher but is now an experienced broad user of Python for data analysis, software test automation, and DevOps. He currently builds data analysis web apps in Python deployed to Kubernetes.
You can find Martyn on GitHub @martynbristow.
The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
M. Zadka DevOps in Python https://doi.org/10.1007/978-1-4842-7996-0_1
1. Installing Python
Before you can use Python, you need to install it. Some operating systems, such as macOS and some Linux variants, have Python preinstalled. Those versions of Python, colloquially called system Python, often make poor defaults for people who want to develop in Python.
The version of Python installed is often behind the latest practices. System integrators often patch Python in ways that can lead to surprises. For example, Debian-based Python is often missing modules like venv and ensurepip. macOS Python links against a Mac shim around its native SSL library. Those things mean, especially when starting and consuming FAQs and web resources, it is better to install Python from scratch.
This chapter covers a few ways to do so and the pros and cons of each.
1.1 OS Packages
Volunteers have built ready-to-install packages for some of the more popular operating systems.
The most famous is the deadsnakes PPA (Personal Package Archives). The dead in the name refers to the fact that those packages are already builtwith the metaphor that sources are alive. Those packages are built for Ubuntu and usually support all the versions of Ubuntu that are still supported upstream. Getting those packages is done with a simple
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt update
On macOS, the Homebrew third-party package manager has up-to-date Python packages. An introduction to Homebrew is beyond the scope of this book. Homebrew being a rolling release, the Python version is upgraded from time to time. While this means that it is a useful way to get an up-to-date Python, it makes it a poor target for reliably distributing tools.
It also has some downsides for doing day-to-day development. Since it upgrades quickly after a new Python release, development environments can break quickly and without warning. It also means that sometimes code can stop working; even if you are careful to watch upcoming Python versions for breaking changes, not all packages will. Homebrew is a good fit when needing a well-built up-to-date Python interpreter for a one-off task. Writing a quick script to analyze data, or automate some APIs, is a good use of Homebrew Python.