How Should You Automate Linux Tasks with Python 2 vs Python 3?

Problem scenario
You have been given a task to automate tasks in Python. You were advised to never use "import os" in a previous position. You were told that this time speed is very important. What should you do?

Possible Solution #1
If you are doing basic file manipulations such as copying or moving files, or changing files' permissions, you may want to looking into "import shutil". See this documentation for more information.

Possible Solution #2
This solution examines the situation wherein you make thousands of calls to shell (the Linux command prompt). In reality you may be automating a few big shell tasks that are time consuming. Therefore this article may not apply to you.

This Python program below does not use "import os". In our testing, it takes approximately 9 seconds to run with Python 2.x and 8.5 seconds to run with Python 3.x.

import datetime, subprocess
t1 = datetime.datetime.now()
for i in range(10000):
   f = subprocess.Popen("exit 0", shell=True)
   f.wait()
t2 = datetime.datetime.now()
t3 = t2 - t1
print "Time format is in hours:minutes:seconds:seconds_decimals"
print t3

This Python program does use "import os". In our testing, it takes 5 seconds to run with Python 2.x and 9.7 seconds with Python 3.

import datetime, os
t1 = datetime.datetime.now()
for i in range(10000):
    f = os.popen("exit 0")
    f.close()
t2 = datetime.datetime.now()
t3 = t2 - t1
print "Time format is in hours:minutes:seconds:seconds_decimals"
print t3

The time difference between the "import subprocess" and "import os" versions is drastic holding constant for Python 2.x. While we know it is generally not recommended to use "import os" for Linux tasks, we found it was much faster with Python 2.x.

This Python program does not use "import os". In our testing, it takes approximately 8.5 seconds to run with Python 3.x.

import datetime, subprocess
t1 = datetime.datetime.now()
for i in range(10000):
   f = subprocess.Popen("exit 0", shell=True)
   f.wait()
t2 = datetime.datetime.now()
t3 = t2 - t1
print ("Time format is in hours:minutes:seconds:seconds_decimals")
print (t3)

This Python program does use "import os". In our testing, it takes 9.7 seconds to run with Python 3.

import datetime, os
t1 = datetime.datetime.now()
for i in range(10000):
    f = os.popen("exit 0")
    f.close()
t2 = datetime.datetime.now()
t3 = t2 - t1
print ("Time format is in hours:minutes:seconds:seconds_decimals")
print (t3)

The time difference between the "import subprocess" and "import os" versions is significant holding constant for Python 3.x.

Python 3 is starting to be more widely used. If you are using Python 3, you will enjoy a performance benefit to using "import subprocess" while keeping with the recommended practice of avoiding "import os" for infrastructure tasks.

(Thanks to Aaron Sherman who wrote this external posting in 2011.)

If you want to learn about the vulnerabilities in different versions of Python, you may want to see this external article.

Python 2.x is not going to be maintained after 1/1/2020. That is less than six months away! Here is more information about Python 2.x not being maintained indefinitely.

How Do You Get Oracle VirtualBox Guest Servers to Communicate with Each Other?

Problem scenario
You want to allow for TCP/IP connectivity between two VMs running Linux in Oracle VirtualBox. What do you do?

Solution
The network settings for the VMs should be as follows:

  • The "Attached to:" setting hould be "Host-only Adapter"
  • The "Adapter type:" setting should be "Paravirtualized Network"
  • The "Cable connected" option should be checked.

Optional Reading
How Do You Connect to a VM in Oracle VirtualBox from a Desktop?

How Do You Troubleshoot the nmap Result “Host seems down. If it is really up, but blocking our ping probes”?

Problem scenario
You use nmap and get this error from one server but not another:

Note: Host seems down. If it is really up, but blocking our ping probes, try -Pn
Nmap done: 1 IP address (0 hosts up) scanned in 3.05 seconds

There seems to be no intermediate firewall rule stopping one server. What should you do?

Solution
The error message suggests using "-Pn". We recommend you try that: nmap -Pn x.x.x.x

# (where x.x.x.x is the IP address of the server with the problem)

You may want to try "sudo " before your nmap command; the results may differ. It is possible that ICMP packets have been turned off in the intermediate networking devices.

Which Log File Shows a Record of Web Traffic on an Nginx Web Server Running on a Linux?

Problem scenario
You configured Nginx on a Linux server. You want to see if there has been activity on the website (e.g., via a web browser). What log file, visible on the back-end, has records of web browsing activity?

Solution
It is often here: /var/log/nginx/access.log

For general Nginx purposes, the nginx.conf file specifies the location of the Nginx logs and the names of the log files themselves. To find this file, run this command: sudo find / -name nginx.conf
View it and look for "access_log". This stanza should have what you are looking for (if the access.log file is somewhere other than the default location). For Apache web server logs, see this posting.

What is a Continuous Application?

Question
What is a Continuous Application?

Answer
Databricks website says this "[w]e define a continuous application as an end-to-end application that reacts to data in real-time."

The proper term should probably be "continual application" as there may be discrete moments where no data is coming in. Many streams can be interrupted. In fact, Structured Streaming, an aspect of Spark that is used in Databricks' "continuous applications" is based on microbatching (according to this site). A series of microbatches would be a continual, not a continuous process.

In their article that defines "continuous application," Databricks refers to "Structured Streaming" as an integral component of such an application. They say this: "Our long-term vision for streaming in Spark is ambitious: we want every library in Spark to work in an incremental fashion on Structured Streaming" (which can be found here). This "incremental fashion" clearly favors the word "continual" and not "continuous."

We believe Databricks meant by the somewhat vague phrase "end-to-end" as a source of data to its destination. We believe the application itself must adapt to data in real-time. This is different from the data the application presents for it to be considered a "continuous application" (or "continual application").

We like the term "adapt" because Databricks refers to "[o]nline machine learning" as an example in their discussion of a "continuous application" (as you can read here).

Pactpub.com defines "continuous applications" in a way that is distinguished from microbatches. They also refer to such a "continuous application" being centered around the pipeline of data.

"[T]he notion of continuous applications--in contrast to batch processing--emerged, and basically means the composite of batch processing and real-time stream processing with a clear focus of the streaming part being the main driver of the application, and just accessing the data created or processed by batch processes for further augmentation. Continuous applications never stop and continuously produce data as new data arrives." (The source of this quote is here.)

Slide 9 of this slide deck show presents a graphic of a continuous application. We take away from the above picture that the "continuous application" embodies the streaming process and it incorporates static data the whole time. We also find that the "continuous application" is consistent with individual batch jobs. Thus the term should be "continual application."

We find some semblance of a definition of "continuous application" from a source outside of Databricks with the www.oreilly.com site:

"In addition to the venerable Lambda Architecture, emerging systems like Apache Flink, Kafka, Apache Beam and Spark 2.0’s upcoming Structured Streaming offer new ways to provide more principled implementations of continuous applications. These systems hold promise for applications where the analytics can be pre-computed, often involving simple aggregations and specialized “checkpoint” models of application state. In general, these systems are based on low-level programming primitives.

But what about continuous applications that need to support complex analytics, including joins, data science models, even analysis over various periods of history? We look at options for building complex analytic big data applications including tradeoffs for simplicity, completeness, and changing semantics over time backed by rich query engines."

The above paragraphs were taken from oreilly.com.

We add this note to help define "continuous application": "A Continuous Application is an end-to-end application that reacts to data in real-time. But it is more than a typical event-based streaming app. Continuous applications capture input streams, blend them when static/offline data and sometimes apply machine learning to the combined data before serving the results back out. These modern applications support quick ad-hoc queries along with long running batch queries." (The source of this quote is linked here.)

How Do You Use the AWS CLI to List Aurora Databases?

Problem scenario
You have an Amazon Aurora database in you AWS account. You want to see the status of this database with the AWS CLI. How do you list information about your Aurora databases?

Solution
Prerequisites

You must have the AWS CLI installed and configured with a user that has the ability to view Aurora databases (e.g., an administrator of your AWS account). If you need assistance, see this posting.

Procedures
Run this command: aws rds describe-db-instances

How Do You Install Composer on Debian/Ubuntu Linux?

Problem scenario
You want to install Composer (a dependency manager for PHP) on your Debian/Ubuntu Linux server. What do you do?

Solution

  1. Run these commands:

sudo apt -y update
sudo apt -y install php-cli unzip
cd ~
sudo curl -sS https://getcomposer.org/installer -o composer-setup.php
HASH=curl -sS https://composer.github.io/installer.sig
echo $HASH
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

  1. The last command above should have printed out "Installer verified". Now run these two commands:

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
composer

  1. You are done. The above directions were adapted from this external page.
  2. The above directions were tested to work on Debian 10.

How Do You Set up SSH on a Windows 2016 Server with Cygwin?

Problem scenario
You have a heterogeneous enterprise network of servers. You want your Linux machines to communicate and transfer files with your Windows machines. You do not want to use Active Directory. What do you do?

Solution
1. Install Cygwin on the Windows server if it has not been set up already. If you need assistance with this, see this posting.

2. Open Cygwin and and set up SSH keys as you normally would. See this article How Do You Set up Passwordless SSH from One Server to Another? if you do not know how.

How Do You Find the URL (or HTTP Endpoint) of a Kubernetes Cluster Running in Amazon EKS?

Problem scenario
You have a Kubernetes cluster in AWS. You have the AWS CLI installed. How do you find the URL for the Kubernetes cluster?

Solution
1. Run this command: aws eks list-clusters

2. Run this command: aws eks describe-cluster foobar # where "foobar" was name determined in above command