How Do You Use Python to Create a Server in AWS?

Problem scenario
You want to be able to create EC-2 instances in AWS with a Python script.  How do you do this?

Solution
Prerequisites

This assumes you have installed Boto3.  If you do not know how, see this posting.

Procedures
As a reference, you may go here to learn more about Boto's features:  http://boto3.readthedocs.io/en/latest/guide/migrationec2.html
Use this program:

""" Usage instructions
  1. replace "aaa111" with the aws_access_key_id of your account
  2. replace "bbb222" with the aws_secret_access_key of your account
     If you are not sure how to find these credentials do the following:
     To find the AWS Access Key ID and AWS Secret Access Key, in the AWS console, click on your name in the upper right hand corner.
     Then click on "My Security Credentials."  Click "Create New Access Key"  Click "Show Access Key."
  3.  Replace us-west-1 with the region that you want to list Ec-2 instances for.
  4.  Change the "t2.micro" to be the flavor of server that you want to create.
  5.  Change the "3" in MaxCount to the number of servers you want to be created.
  6.  Change the "ami-0b1e356e" to the AMI you want.  One way to browse is to manually log into the AWS console and click "Launch Instance".  The first step will allow you to see IDs of AMIs.
  7.  To run it, call it test3.py and run it with a command like this: "python test3.py"

 Warning: THIS WILL CREATE THREE SERVERS in AWS!
 Warning:  This could be more expensive than you are used to.
"""

import boto3

ec2 = boto3.resource('ec2', aws_access_key_id='aaa111', aws_secret_access_key='bbb222', region_name='us-west-1')

ec2.create_instances(ImageId='ami-0b1e356e', MinCount=1, MaxCount=3, InstanceType='t2.micro')

How Do You Get a Head Set (Or Pair of Ear Buds) to Work with Your Windows Machine?

Problem scenario
You insert 3.5 mm jack for your headphones (audio cable) that is known good into your laptop.  You cannot hear sound out of it.  You go to different parts of the Control Panel, but you cannot see evidence that your Windows computer (laptop or desktop) sees the device.  How do you get your headphones to work?

Root cause
Some I.T. policies disable the 3.5 mm audio jack on the Windows computers.  Sometimes the 3.5 mm jack is defective or loses its connectivity to the motherboard.

Possible Solution #1
Use the docking station.    If you have a docking station with an audio outlet (3.5 mm) use that.  

Possible Solution #2
Use a headset with a USB connector or a USB adapter to 3.5 mm convertor.  This may not be disabled.

How Do You Solve the Error “failed to push some refs” after a “git push origin master”?

Problem scenario
You have two Linux servers (server A and server B).  You have a Git repository on server A.  You are logged into server B as jdoe. You cloned a Git repository from server A (with a "git clone git@gitlabFQDN:/path/to/nameOfRepo.git" command).  You added some files and used "git commit" on server B.  From server B you run this command as the Linux user jdoe: git push origin master

You get this problem:

remote: GitLab: You are not allowed to upload code for this project.
To GitLabServerFQDN:/var/opt/gitlab/git-data/repositories/root/nameOfRepo.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@GitLabServerFQDN:/var/opt/gitlab/git-data/repositories/root/nameOfRepo.git'

How do you upload the new files?

Solution
1.  Log into the web UI for GitLab.  
2.  Click on the Project that has the repository you want.
3.  On the left click on "Settings".
4.  Click on "Repository" under "Settings."  
5.  Click on the "Expand" button for "Deploy Keys".
6.  Provide an arbitrary phrase in the "Title" field.
7.  For the "Key" field input the content of the id_rsa.pub file for the jdoe user (e.g., /home/jdoe/.ssh/id_rsa.pub).  If one does not exist, go to the back-end of the Linux server, log in as jdoe, run 'ssh-keygen -t rsa -P ""' and press enter to the next prompt.
8.  Check the option for "Write access allowed"
9.  Click the green "Add key" button.
10.  Now go back to server B and run: git push origin master

(This solution could potentially work with BitBucket (but probably not step-by-step).  You may need to check the branch name; it may not be "master" but "development" or something else.)

How Do You Use a Dockerfile with Reserved Words ENTRYPOINT and CMD to Create an Image and Test It Out?

Problem scenario
You want to use the ENTRYPOINT and CMD keywords in a Dockerfile.  You know that the value of CMD amounts to a parameter for the ENTRYPOINT value which is executed.  You want to verify that the value of ENDPOINT is used in an execution.  You want the Dockerfile to be simple to illustrate these reserved words.  What do you do?

Solution
Prerequisite
Install Docker.  See this posting if you need directions.

Procedures
1.  Create a file called Dockerfile.  Have these three lines be the content:

FROM ubuntu:latest
ENTRYPOINT ["echo"]
CMD ["parameter1 parameter2"]

2.  Run this command:  docker build .

3.  The above command should say something like this:  "Successfully build abcd1234"
The "abcd1234" is the image ID.  Run this next command, but replace "abcd1234" with the image ID that you just created:

docker run abcd1234

# This command above should print "parameter1 parameter2".  

You are done.  That should demonstrate how ENTRYPOINT values use the CMD values as parameters.  In the Dockerfile when you use "docker run" with an image that was the result of a Dockerfile with ENTRYPOINT and CMD stanzas, there will be execution of ENTRYPOINT with one or more additional parameters if CMD is also used.  

How Do You Configure Oracle 12 to Work with JIRA 7.x when Writing a dbconfig.xml File for JIRA?

Problem scenario
You have configured dbconfig.xml for JIRA 7.x to use Oracle 12.  But JIRA is not working correctly when you try to start it.  What should you do with your dbconfig.xml file?

Solution
Atlassian's website, as of 8/30/18, recommends having "oracle10g", an unsupported version, in the dbconfig.xml file.  This is despite the fact that Oracle 10 and Oracle 11 are not supported for JIRA 7.x according to Atlassian's site here.

The XML tag for the database type in dbconfig.xml (a file for JIRA) should specify version 10 -- even though it is not supported!  Atlassian's website shows the correct tag in the dbconfig.xml file on their website as this:

<database-type>oracle10g</database-type>

The tag should not say "oracle11g" or "oracle12g".  It should say a version that is not supporte: "oracle10g".

How Do You Monitor The Disk Activity of a Linux Server with a Command Similar to top?

Problem scenario
You think that there is disk contention with the various processes on your Linux server.  You think that the I/O activity is too high and potentially creating a bottleneck.  What do you do to learn more about disk utilization?

Solution
1.  To learn the names of the underlying disks on your Linux system, run this command:

ls -lh /dev/disk/by-path

2.  Now run this command:

ls -lh /dev/disk/by-path | awk '{print $11}'

3.  Then based on the output of the above command, mentally replace the "../../" with "/dev/" when you compose the next command.  (So instead of using "../../sda1" use "/dev/sda1".)

To find the activity with five-second intervals of each disk found in the above command, compose a command such as this:

iostat -dz /dev/sda /dev/sda1 /dev/sda2 5

# The arguments between the "-dz" and "5" are the names of the disks.  Run the command you compose in the format above (e.g., with the interval you want and the disks you want).  The "z" flag omits disks that have no activity during the reporting period.

Using Ansible How Do You Search The Contents of a File on Linux for “{{” or “}}”?

Problem scenario
You want to see if an Ansible playbook is injecting (or leaving) two braces, "{{" or "}}", in a specific file on Linux.  These symbols can be injected accidentally with little notification.  Variable substitution could fail during the course of a playbook run.  This is a subtle problem that may have no alert.  You want some debugging to check for these symbols.  How do you know a file on a managed node does not have a "{{" or "}}"?

Solution
Root cause

What makes the double braces difficult to place into a shell module in Ansible is that Ansible views them (as a pair) as a reserved symbol; variable expansion in Ansible is done via these very symbols in quotes.  

Procedure
You need to re-write the playbook to use the "shell" module. To search for lines that have both types of double braces, "{{" and "}}", with no quotes, put these two lines in your playbook.
shell:  /usr/bin/cat /path/to/file.txt | grep "\{\{" | grep "\}\}"
register: foundplaceholder

The backslash "\" (without quotes) acts as an escape character.  The variable foundplaceholder will have a value if a single line had both types of double braces, "{{" and "}}" (with no quotes).

How Do You Use a Jenkinsfile?

Problem scenario
You read about what Jenkinsfiles are.  Coveros.com defines them as "Jenkinsfiles, using a domain specific language based on the Groovy programming language, are persistent files that model delivery pipelines “as code", containing the complete set of encoded steps (steps, nodes, and stages) necessary to define the entire application life-cycle."

How do you use a Jenkinsfile with Declarative pipeline syntax?

Solution
Overview

A Declarative pipeline syntax has four mandatory sections with each of these reserved words: agent, stages, stage and step.  For more information about this, you may want to view this Blazemeter.com site.

Prerequisites
i.  You must have Jenkins installed.  See the posting for your type of Linux below for assistance installing Jenkins:

CentOS/RHEL/Fedora
Debian/Ubuntu

Pipeline jobs require more memory than typical Jenkins jobs.  If you need to resize your server (i.e., give it more RAM), click here if you have a server in AWS, and click here if you have a server in GCP.

If you are not using a public cloud, you may want to add physical RAM.  You may want to create virtual memory to help with performance.  To do that, see this posting.

ii.  Install the Pipeline plugin.  Log into the web UI of Jenkins as an administrator.  Go to Manage Jenkins -> Manage Plugins -> Available.  Search for "Build Plugin."  Check the box and click "Install without restart".  Next, click the option for "Restart Jenkins..." at the bottom of the screen.

Procedures
1.  Log into the web UI for Jenkins as an administrator.
2.a.  If you see "New Item" do 2.b, otherwise do 2.c.
2.b.  Click on "New Item".
2.c.  Click on "Dashboard" if you do not see "New Item."  Then click "New Item".
3.  Enter a name in the field and the top, then click on "Pipeline".  Then click "OK".
4.a.  Scroll down to the "Pipeline" section.  For a very simple choice go to step 4.b., but if you want something slightly more complex, go to option 4.c.
4.b.  In the upper righthand portion of the "Script" click on the drop down menu that says "try sample pipeline...", and click "Hello World."  Skip step 4.c. and go to step 5.
4.c.  Enter this text (which is declarative syntax according to this external site):

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building..'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing..'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....'
            }
        }
    }
}

5.  (You can uncheck the "Use Groovy Sandbox" or leave it checked.)  Click "Save".

6.a.  You can test it by clicking on "Build Now."

6.b.  You are done.  The Jenkinsfile should be checked into a source control repository.

6.c.  Here is optional reading if you want to learn more.

How Do You Clone Down a Specific Branch of a Repository from Git?

Problem scenario
You want to clone a specific branch from Git.  But you do not want to clone the master branch.  How do you clone the branch named "develop"?

Solution
1.  Clone down the repository as normal with a command like this (but replace <SSH or HTTP constructor of repo> with the remote repository):
git clone <SSH or HTTP constructor of repo>

2.  Run this command (you could replace "develop" with the name of the branch you created):  git checkout -b develop

3.  Run this command (you could replace "develop" with the name of the branch you created):  git pull origin develop

How Do You Troubleshoot the Message “pycurl error 22”?

Problem scenario
You are trying to install a new package or apply yum updates on a CentOS/RHEL/Fedora server.  But you get 404 errors or "pycurl error 22" as the result of your yum commands.  How do you use yum to install a new package or apply a new update?

Solution
Run this command:  sudo yum clean all

Now your yum commands should work.