How Do You Upgrade Python 2.6 to Python 2.7 on CentOS 6?

Problem scenario
On your Linux CentOS 6.x server you run "python --version" and find that Python 2.6 is installed.  You want Python 2.7 to be installed instead.  You do not want to use binaries (e.g., .rpm packages) or yum repositories. What do you do?

Solution
Run these commands:
curl -k https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz > Python-2.7.14.tar.xz
sudo mv Python-2.7.14.tar.xz /usr/bin/
cd /usr/bin/
sudo tar xf Python-2.7.14.tar.xz
cd Python-2.7.14
sudo ./configure --prefix=/usr/local
sudo make && sudo make altinstall

# You must use "altinstall" in the command above.  If you use "make install", you will have two versions of Python installed.

sudo ln -s /usr/local/bin/python2.7 /usr/local/bin/python
# Log off and log back in. You should now be done.

If you want to know how to upgrade Python from a major version (2 to 3) on a Debian/Ubuntu server where you can use binaries or remote repositories (and you do not want to use source media), see this posting.

How Do You Write Your Own Custom, Importable Module in Python?

Problem scenario
You want to build your own Python module.  You want this module to be able to be imported on your Linux server with Python (e.g., with "import contint").  How do you write such a module?

Possible Solution #1
Write a .py file for the module, and place it where Python will look.  To find the location run these three commands:

python

import sys

print(sys.path
)

# This command above will display directory paths for your .py file (the module that will be able to be imported).
# Ignore the .egg files that may be displayed.  (The directory paths will work if they appear without the .egg file.)
# No services need to be stopped or restarted after the file is placed in one of the above locations.   

When you use the "import" command, do NOT include the ".py" extension of the file name.  Use the file name without the ".py" file.

For example, if your module is called contint.py, and you placed it in /usr/lib/python3.7/lib-dynload/, you would run this from a >>> prompt:

import contint

If you want to use a directory location outside of what you found with the >>>  print sys.path command, see this StackOverflow posting.

Possible Solution #2
See this posting.

How Do You Install Jenkins on a CentOS/RedHat/Fedora server?

Problem scenario
You want to install Jenkins on a RedHat derivative Linux server (e.g., CentOS, RHEL, or Fedora).  What do you do?

Solution
1.  Run these commands:

curl https://pkg.jenkins.io/redhat-stable/jenkins.repo > /tmp/jenkins.repo
sudo mv -i /tmp/jenkins.repo /etc/yum.repos.d/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

2.  Run this command:
sudo yum -y install java jenkins

# When you see the "Package configuration" menu that says "Configuring grub-pc" press the tab key (to get to the "Ok" option for "keep the local version currently installed").  Then press enter.  In other words, press the tab key when you see this:

3.  Run these commands:
sudo systemctl start jenkins
curl icanhazip.com

4.a.  From a workstation with a GUI, open a web browser and go to this URL where x.x.x.x is the IP address obtained from the icanhazip.com command above:  http://x.x.x.x:8080

4.b.  Follow the on screen steps.  The web browser must allow cookies from x.x.x.x.  We recommend installing the suggested plugins when prompted.  But the choice is yours.

How Do You Install and Configure SaltStack Master and SaltStack Minion?

Problem scenario
You want to install SaltStack Master and SaltStack Minion.  You want them to be configured to work together.  You have different distributions of Linux.  What should you do?

Solution in Three Parts
Overview
This solution works for any distribution of Linux (e.g., Debian distributions, including Ubuntu, RedHat derivatives, including CentOS, RHEL, and Fedora, or SUSE).  This solution is written as if your Linux servers were in AWS.  If you are using Azure, consider the "Network Security Group" instead of the "Security Group."  If you are deploying SaltStack on-premises, consider the relevant firewall(s) between the servers that will be SaltStack Master and SaltStack Minion instead of the Security Groups.

Part 1  Create a Salt Master Server
1.  Create a Linux server and put it in a Security Group that has access to the internet for this initial setup, and has connectivity to the other server that will be the Salt Minion.  The exception rule should involve the source IP address of the SaltMaster server.  This is the internal IP address of the server (from an "ip addr show" command) and not the external IP address as seen in the EC2 console (one that you would use Putty to connect to).  Now that this server can connect to other servers as far as the Security Group is concerned, move to step #2.

2.  Run the command below that corresponds with your distribution of Linux (as found with cat /etc/*-release | grep -i pretty).

Debian / Ubuntu:  sudo apt-get -y install salt-master
RHEL/CentOS/Fedora: sudo yum -y install salt-master
SUSE: sudo zypper -n install salt-master

3.  Verify it worked:  salt-master --version

4. Start the salt-master service with this command:  sudo systemctl start salt-master
Depending on you distribution of Linux the command to use may be this next one instead:  sudo service salt-master start

Part 2  Create a Salt Minion Server
5.  Create a Linux server with network connectivity to the Salt Master server. If you are not using AWS, go to step #6.

5.a. If you are using AWS put it in a Security Group that has access to the internet for this initial setup, and has connectivity to the other server that will be the Salt Minion.  The exception rule should involve the source IP address of the SaltMinion server.  This is the internal IP address of the server (from an "ip addr show" command) and not the external IP address as seen in the EC2 console (one that you would use Putty to connect to).  Now that this server can connect to other servers as far as the Security Group is concerned, move to step #6.

6.  Find the distribution of Linux below and run the corresponding command (as found with cat /etc/*-release | grep -i pretty):

Debian / Ubuntu:  sudo apt-get -y install salt-minion
RHEL/CentOS/Fedora: sudo yum -y install salt-minion
SUSE: sudo zypper -n install salt-master

7.a.  vi /etc/salt/minion
7.b.  Search for "master: salt"
7.c.  Uncomment this line.  Change "salt" to "saltmaster" or the hostname of the salt master server.

8.  Update the /etc/hosts file to ensure it can resolve the saltmaster hostname via a ping.
9.  Run this command: sudo salt-minion -l debug

Part 3  Configure Salt Master to Communicate With Salt Minion
10.  Log on to the Salt Master server.

11.  Run this: sudo salt-key -L

The output should look something like this:

Accepted Keys:
Denied Keys:
Unaccepted Keys:
salt-minion1.continualintegration.com
Rejected Keys:

12.  Run this command (where salt-minion1.continualintegration.com is an Unnaccepted Key displayed above):
sudo salt-key -a salt-minion1.continualintegration.com
respond to the prompt with "Y" with no quotes and press enter.

13.  You are done.  Now these commands should work (from the Salt Master server):

 sudo salt '*' test.ping
 sudo salt '*' disk.usage
 sudo salt '*' cmd.run 'ls -l /etc'
 sudo salt '*' network.interfaces
 sudo salt '*' test.echo 'foo: bar'

How Do You Install Apache ActiveMQ on Any Distribution of Linux?

Problem scenario
You want a Bash script that will install ActiveMQ on CentOS/RedHat/Fedora, Debian/Ubuntu, and SUSE Linux distributions.  How do you write a script that will work on different distributions of Linux to install Apache ActiveMQ?

Solution
With a "sudo bash" command, run a script with the following content (e.g., call the script below "installactivemq.sh" and run "sudo bash installactivemq.sh"):

#!/bin/bash
# Written by www.continualintegration.com

activemqversion=5.15.12  # Change this version as necessary

distro=$(cat /etc/*-release | grep NAME)

debflag=$(echo $distro | grep -i "ubuntu")
if [ -z "$debflag" ]
then   # If it is not Ubuntu, test if it is Debian.
  debflag=$(echo $distro | grep -i "debian")
  echo "determining Linux distribution..."
else
   echo "You have Ubuntu Linux!"
fi

rhflag=$(echo $distro | grep -i "red*hat")
if [ -z "$rhflag" ]
then   #If it is not RedHat, see if it is CentOS or Fedora.
  rhflag=$(echo $distro | grep -i "centos")
  if [ -z "$rhflag" ]
    then    #If it is neither RedHat nor CentOS, see if it is Fedora.
    echo "It does not appear to be CentOS or RHEL..."
    rhflag=$(echo $distro | grep -i "fedora")
    fi
fi

if [ -z "$rhflag" ]
  then
  echo "...still determining Linux distribution..."
else
  echo "You have a RedHat distribution (e.g., CentOS, RHEL, or Fedora)"
  yum -y install java-1.8.0-openjdk*
  JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk
  echo 'export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk' >> ~/.bashrc
  source ~/.bashrc
  source /etc/environment
fi

if [ -z "$debflag" ]
then
  echo "...still determining Linux distribution..."
else
   echo "You are using either Ubuntu Linux or Debian Linux."
   apt-get -y update # This is necessary on new AWS Ubuntu servers.
   apt -y install openjdk-9-jre-headless
apt -y install openjdk-11-jre-headless
fi

suseflag=$(echo $distro | grep -i "suse")
if [ -z "$suseflag" ]
then
  if [ -z "$debflag" ]
  then
    if [ -z "$rhflag" ]
      then
      echo "*******************************************"
      echo "Could not determine the Linux distribution!"
      echo "Installation aborted. Nothing was done."
      echo "******************************************"
      exit
    fi
  fi
else
   zypper -n install java-1_8_0-openjdk
fi

curl http://ftp.wayne.edu/apache/activemq/$activemqversion/apache-activemq-$activemqversion-bin.tar.gz > /bin/apache-activemq-$activemqversion-bin.tar.gz

cd /bin
tar -xvf apache-activemq-$activemqversion-bin.tar.gz
mkdir -p /usr/local/apache-activemq
mv /bin/apache-activemq-$activemqversion/* /usr/local/apache-activemq

echo 'PATH="$PATH":/usr/local/apache-activemq/bin/' > /etc/profile.d/activemq.sh

echo 'To finish the process, run this command:'
echo 'export PATH="$PATH":/usr/local/apache-activemq/bin/'

echo "Run 'sudo /usr/local/apache-activemq/bin/activemq --version' to see if activemq is installed."

How Do You Install the Latest Version of Apache Ant?

Problem scenario
You want to use the same Bash script to install Apache Ant on CentOS/RedHat/Fedora, Debian/Ubuntu, or Linux SUSE.  How do you install the latest version of Apache Ant on to any distribution of Linux? 

Solution
Updated 4/8/21
With a "sudo bash" command, run a script with the following content (e.g., call the script below "installant.sh" and run "sudo bash installant.sh"):

#!/bin/bash
# Written by www.continualintegration.com

antversion=1.10.9  # Change this version as necessary

distro=$(cat /etc/*-release | grep NAME)

debflag=$(echo $distro | grep -i "ubuntu")
if [ -z "$debflag" ]
then   # If it is not Ubuntu, test if it is Debian.
  debflag=$(echo $distro | grep -i "debian")
  echo "determining Linux distribution..."
else
   echo "You have Ubuntu Linux!"
fi

rhflag=$(echo $distro | grep -i "red*hat")
if [ -z "$rhflag" ]
then   #If it is not RedHat, see if it is CentOS or Fedora.
  rhflag=$(echo $distro | grep -i "centos")
  if [ -z "$rhflag" ]
    then    #If it is neither RedHat nor CentOS, see if it is Fedora.
    echo "It does not appear to be CentOS or RHEL..."
    rhflag=$(echo $distro | grep -i "fedora")
    fi
fi

if [ -z "$rhflag" ]
  then
  echo "...still determining Linux distribution..."
else
  echo "You have a RedHat distribution (e.g., CentOS, RHEL, or Fedora)"
  yum -y install java-1.8.0-openjdk*
  JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk
  echo 'export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk' >> ~/.bashrc
  source ~/.bashrc
  source /etc/environment
fi

if [ -z "$debflag" ]
then
  echo "...still determining Linux distribution..."
else
   echo "You are using either Ubuntu Linux or Debian Linux."
   apt-get -y update # This is necessary on new AWS Ubuntu servers.
   apt -y install openjdk-9-jre-headless
apt -y install openjdk-11-jre-headless
   apt-get -y remove ant
fi

suseflag=$(echo $distro | grep -i "suse")
if [ -z "$suseflag" ]
then
  if [ -z "$debflag" ]
  then
    if [ -z "$rhflag" ]
      then
      echo "*******************************************"
      echo "Could not determine the Linux distribution!"
      echo "Installation aborted. Nothing was done."
      echo "******************************************"
      exit
    fi
  fi
else
   zypper -n install java-1_8_0-openjdk
fi

curl http://ftp.wayne.edu/apache/ant/binaries/apache-ant-$antversion-bin.tar.gz > /bin/apache-ant-$antversion-bin.tar.gz

cd /bin
tar -xvf apache-ant-$antversion-bin.tar.gz
mkdir -p /usr/local/apache-ant
mv /bin/apache-ant-$antversion/* /usr/local/apache-ant

echo 'PATH="$PATH":/usr/local/apache-ant/bin/' > /etc/profile.d/ant.sh

echo 'To finish the process, run this command:'
echo 'export PATH="$PATH":/usr/local/apache-ant/bin/'

echo "Run 'ant -version' to see if ant is installed."

How Do You Deploy a Kubernetes Cluster in Google Cloud Platform?

Updated on 8/18/19

Problem Scenario
How do you deploy a Kubernetes cluster in Google Cloud Platform?

Solution
There are three ways to do this.  (You could use the console in a web browser.  A second way is with a command line interface method.  A third way is with a REST API.)

Possible solution #1
One way is with the console. Log into Google Cloud Platform. From the console search for "kubernetes" and click on the "Kubernetes Engine Option." Click "Create cluster". Adjust the settings as you desire. Click the "Create" button.

Possible solution #2
A second way is with the glcoud command line.  At the top, click the ">_" icon to "Activate Shell" so you can run a gcloud command. In this next example, you will create a cluster of three nodes each with 1 GB of RAM in the us-central1-a zone.  To find out the project names at your disposal, run this command:
gcloud project lists

Assuming you have authenticated, you would run this gloud command (but change "project-name-123456" to your project name, which is often found in the URL when you are using GCP):

gcloud beta container --project "project-name-123456" clusters create "cluster-1" --zone "us-central1-a" --username "admin" --cluster-version "1.12.8-gke.10" --machine-type "custom-1-1024" --image-type "COS" --disk-size "100" --scopes "https://www.googleapis.com/auth/compute","https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" --num-nodes "3" --network "default" --enable-cloud-logging --enable-cloud-monitoring --subnetwork "default"

The cluster-version may need to be changed. The value for cluster-version has (1.12.8-gke.10) is current as of 8/18/19. The machine type value should be changed from "custom-1-1024" to one that is available in your project. To find this value, you may want to use step #1 but do not create the cluster. Step #1 can help you find the cluster-version or the machine type.

Possible solution #3
A third way is with the API.  In this next example, you will create a cluster of three nodes each with 1 GB of RAM in the us-central1-a zone.

POST https://container.googleapis.com/v1/projects/project-name-123456/zones/us-central1-a/clusters
{
  "cluster": {
    "name": "cluster-1",
    "zone": "us-central1-a",
    "network": "default",
    "nodePools": [
      {
        "name": "default-pool",
        "initialNodeCount": 3,
        "config": {
          "machineType": "custom-1-1024",
          "imageType": "COS",
          "diskSizeGb": 100,
          "preemptible": false,
          "oauthScopes": [
            "https://www.googleapis.com/auth/compute",
            "https://www.googleapis.com/auth/devstorage.read_only",
            "https://www.googleapis.com/auth/logging.write",
            "https://www.googleapis.com/auth/monitoring",
            "https://www.googleapis.com/auth/servicecontrol",
            "https://www.googleapis.com/auth/service.management.readonly",
            "https://www.googleapis.com/auth/trace.append"
          ]
        },
        "autoscaling": {
          "enabled": false
        },
        "management": {
          "autoUpgrade": false,
          "autoRepair": false,
          "upgradeOptions": {}
        }
      }
    ],
    "loggingService": "logging.googleapis.com",
    "monitoringService": "monitoring.googleapis.com",
    "initialClusterVersion": "1.12.8-gke.10",
    "masterAuth": {
      "username": "admin",
      "clientCertificateConfig": {
        "issueClientCertificate": true
      }
    },
    "legacyAbac": {
      "enabled": false
    },
    "masterAuthorizedNetworksConfig": {
      "enabled": false,
      "cidrBlocks": []
    },
    "addonsConfig": {
      "kubernetesDashboard": {
        "disabled": false
      },
      "httpLoadBalancing": {
        "disabled": false
      },
      "networkPolicyConfig": {
        "disabled": true
      }
    },
    "networkPolicy": {
      "enabled": false,
      "provider": "CALICO"
    },
    "subnetwork": "default",
    "ipAllocationPolicy": {
      "useIpAliases": false
    }
  }
}

How Do You Get Kubernetes to Work as a Test Instance?

Problem scenario
You want to set up Kubernetes on a server as quickly as possible as a proof-of-concept.  You want to create pods without the best practice security mechanisms because you are behind a firewall and in a development environment.  How do you quickly install and configure Kubernetes as a development deployment on a single host server?

Solution
1.  Install and configure kubelet, kubeadm, kubectl, etcd, kube-proxy, kube-controller-manager, and kube-scheduler.  To do these things on Ubuntu see this link for the first four and this link for the last three.

2.  Run these six commands:

cd ~
mkdir etcd-data
docker run --volume=$PWD/etcd-data:/default.etcd --detach --net=host quay.io/coreos/etcd > etcd-container-id
sudo wget https://storage.googleapis.com/kubernetes-release/release/v1.0.7/bin/linux/amd64/kube-apiserver
sudo chmod +x kube-apiserver
sudo kube-apiserver --etcd-servers=http://127.0.0.1:2379 --service-cluster-ip-range=10.0.0.0/16 --secure-port=0

3.  Create or open a duplicate terminal session.  Run your kubectl commands as you please.

How Do You Add Memory to a Server?

Problem scenario
You want the total available memory to be greater. What can you do?

Solution
You may want to resize your VM, or upgrade the physical server, to have more RAM.

Click here for AWS resizing directions or here for GCP resizing directions.  If you have ample hard disk space but insufficient memory, you may want to see this posting for directions for adding or configuring virtual memory. If you want a Bash script to create swap space, see this posting.

To buy physical RAM chips for your physical server, see this external link. When purchasing physical memory, consider the differences of the bus speed between the varieties you may purchase. This will affect performance.

We are reminded that memory is much more affordable than it used to be by this quote: "…memory rents for about $12 per kilobyte per month…" taken from page 98 to The Mythical Man-Month.