Moshe Zadka
DevOps in Python Infrastructure as Python
Moshe Zadka
Belmont, CA, USA
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/978-1-4842-4432-6 . For more detailed information, please visit http://www.apress.com/source-code .
ISBN 978-1-4842-4432-6 e-ISBN 978-1-4842-4433-3
https://doi.org/10.1007/978-1-4842-4433-3
Moshe Zadka 2019
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
To A and N, my favorite two projects even when they need immediate maintenance at 4 a.m.
Introduction
Python was started as a language to automate an operating system: the Amoeba. Since it had an API, not just textual files representations, a typical UNIX shell would be ill suited. The Amoeba OS is now a relic. However, Python continues to be a useful tool for automation of operations the heart of typical DevOps work. It is easy to learn and easy to write readable code in 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 operating systems and yet is natively memory safe, leading to few crashes at the automation layer.
Finally, although not true when it was originally 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 will guide you through how to take advantage of Python to automate operations.
What to Expect in the Book
There are many sources that teach Python, the language: books, tutorials, and even free videos online. Basic familiarity with the language will be assumed here. However, for the typical SRE/DevOps person, there are a lot of aspects of Python that few sources cover, except for primary documentation and various blogs. We cover those early on in the book.
The first step in using Python is not writing a hello world program. The first step is installing it. There, already, we are faced with various choices, with various trade-offs between them. We will cover using the preinstalled version of Python, using ready-made, third-party prebuilt packages, installing Python from sources, and other alternatives. One of Pythons primary strengths, which any program slightly longer than hello world will take advantage of, is its rich third-party library ecosystem. We will cover how to use these packages, how to develop a workflow around using specific versions of packages and when to upgrade, and what tools are used to manage that. We will also cover packaging our own code, whether for open source distribution or for internal dissemination.
Finally, Python was built for exploration. Coming from the Lisp tradition of the REPL (Read-Eval-Print Loop), using Python interactively is a primary way to explore the language, the libraries, and even the world around us. Whether the world is made of planets and atoms, or virtual machines and containers, the same tools can be used to explore it. Being a DevOps-oriented book, we cover it more from the perspective of exploring a world of virtual machines, services, and containers.
Acknowledgments
Thanks to my wife, Jennifer Zadka, without whose support I could not have done it.
Thanks to my parents, Yaacov and Pnina Zadka, who taught me how to learn.
Thanks to my advisor, Yael Karshon, for teaching me how to write.
Thanks to Mahmoud Hashemi for inspiration and encouragement.
Thanks to Mark Williams for always being there for me.
Thanks to Glyph Lefkowitz for teaching me things about Python, about programming, and about being a good person.
Table of Contents
About the Author and About the Technical Reviewer
About the Author
Moshe Zadka
has been part of the open source community since 1995 and has been involved with DevOps since before the term became mainstream. One of two collaborators in the Facebook bootcamp Python class, he made his first core Python contributions in 1998, and is a founding member of the Twisted open source project. He has also given tutorials and talks at several recent PyCon conferences and is a co-author of Expert Twisted (Apress, 2019).
About the Technical Reviewer
Paul Ganssle
is a software developer at Bloomberg and frequent contributor to open source projects. Among other projects, he maintains the Python libraries dateutil and setuptools. He holds a PhD in Physical Chemistry from the University of California, Berkeley; and a BS in Chemistry from the University of Massachusetts, Amherst.
Moshe Zadka 2019
Moshe Zadka DevOps in Python https://doi.org/10.1007/978-1-4842-4433-3_1
1. Installing Python
Before we start using Python, we need to install it. Some operating systems, like Mac OS X 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.