How Do You Create a List of Strings in Golang?

Problem scenario
Lists have a fixed length number of discrete items. You want the items in the list to be strings. How do you create a list of strings in Golang?

Solution
1. Use this program called animal.go:

package main

import "fmt"

func main() {
  var x [10]string
  x[0] = "dog"
  x[1] = "cat"
  x[2] = "hamster"
  x[3] = "rabbit"
  x[4] = "chicken"

  for i := 0; i < 5; i++ {
    fmt.Println(x[i])
  }
}

2. Run it like this: go run animal.go

How Do You Get SonarQube to Use a New Database Connection and Change It from the Previous One It Was Working with?

Problem scenario
You try to start an old SonarQube instance but with a new database connection. In the web.log file you see a message like this: "Can not connect to database. Please check connectivity and settings." What should you do?

Solution
Go to sonar.properties. Find the connection string for the new database. It should look something like this:

sonar.jdbc.url=jdbc:oracle://FQDN:1521:XE

The default port for Oracle databases is 1521. Are the last three characters "/XE"? Change the "/" to ":". XE stands for Express Edition. Some sources indicate that the format should be hostname:portnumber:sid

The "sid" (or service name) for Oracle is often "XE" (which stands for Express Edition). The character separating the TCP port number and the sid should be a colon ":"

Details and reference information can be found in these links:
https://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html
https://docs.oracle.com/cd/B25329_01/doc/appdev.102/b25320/getconn.htm

How Do You Delete Browser Data from Chrome when Your First Attempt Failed?

Problem scenario
In Chrome you deleted browsing data including cookies and autofill form data. You closed the browser and reopen it, but old URLs still show up. What should you do?

Solution
Go to History -> Clear browsing data -> and go to "Time Range" (the drop down menu). Choose "All time" and then go to "Clear data".

How Do You Find Out if a GitHub Repository’s Branch is Protected or Not?

Problem scenario
Using the API you want to know if a given GitHub repository's branch is protected. What do you do?

Solution
Draft the four commands below. Replace "jdoe" with the GitHub username. Replace "organizationname" with the organization name in your repo. Replace "repositoryname" with the repository name. Replace "master" with the branch you want to check. Then run the four commands below. You will be prompted to enter a password.

ORGNAME=organizationname
REPONAME=repositoryname
BRANCHNAME=master
curl -u jdoe https://api.github.com/repos/$ORGNAME/$REPONAME/branches/$BRANCHNAME/protection

How Do You Create Your Own RPM Package (Binary File) for Your Own C Program?

Problem scenario
You want to know more about RPM packaging so you can create your own .rpm file (a custom RPM package file).  In other words you want to create your own RPM package (a binary file).  You want it for installing an uncompiled C program you wrote.  The uncompiled C program is one .c file and very simple.  You don't know how to use a spec (specification) file to create your own RPM package file.  You don't know how to use a Makefile to install the C program.  How do you create an rpm package for your own program?  More specifically how do you create your own stand-alone RPM file that will install an executable C program from a source (.c) file?

Solution
1.  Log into the Red Hat server as any user except root.

2.  Run this command:
sudo yum -y install gcc rpm-build rpm-devel rpmlint make python bash coreutils diffutils patch rpmdevtools

3.  Run this command:  mkdir /tmp/contint-1.0

4.  Run this command:  cd /tmp/contint-1.0

5.  Create a C program named contint.c with this as the content (the first line is "#include <stdio.h>"):

#include <stdio.h>

int main(void) {
    printf("ContinualIntegration.com says Hello!\n");
    return 0;
}

6.a.  Create a file named "Makefile" in the same directory as the contint.c.  The Makefile should have the following content, but remember that indented, non-blank lines need to be indented pressing the Tab key before the text on the right:

contint:
        gcc -o contint contint.c 
clean:
        rm contint
install:
        mkdir -p $(DESTDIR)/usr/bin
        install -m 0755 contint $(DESTDIR)/usr/bin/contint

6.b.  Clarification: The step above will not work if you just copy and paste the content; you can copy and paste it, but re-indent the indented lines using the Tab key.

7.  Create a LICENSE file in the same directory as the two files above.  The LICENSE file should have the following content:

This is just a test.  For information about real GNU licenses, see this link: http://www.gnu.org/licenses/

8.  Run these several commands:  


cd /tmp/
tar -cvzf contint-1.0.tar.gz contint-1.0
cd ~
rpmdev-setuptree
mv /tmp/contint-1.0.tar.gz ~/rpmbuild/SOURCES/
cd /tmp/contint-1.0
cp contint.c contint.c.orig
diff -Naur contint.c.orig contint.c > ~/rpmbuild/SOURCES/contint-output-first-patch.patch

9.  Create contint.spec in ~/rpmbuild/SPECS/ with the content below:

Name:           contint
Version:        1.0
Release:        1%{?dist}
Summary:        A Hello World type example using the C programming language

License:        MadeUpLicense
URL:            https://www.contintualintegration.com/%{name}
Source0:        https://www.continualintegration.com/%{name}/releases/%{name}-%{version}.tar.gz

Patch0:         contint-output-first-patch.patch

BuildRequires:  gcc
BuildRequires:  make

%description
The verbose description for this Continual Integration example.
You can put lines here.

%prep
%setup -q

%patch0

%build
make %{?_smp_mflags}

%install
%make_install

%files
%license LICENSE
%{_bindir}/%{name}

%changelog
* Wed Nov 29 2017 Continual Integration <buildengineer@continualintegration.com> - 1.0-1
- A basic contint package

10.  Run this command:   rpmbuild -bb ~/rpmbuild/SPECS/contint.spec

11.   Now you should have an .rpm file in this directory: ~/rpmbuild/RPMS/x86_64/.  If you did not change any of the above directions, the file will be called "contint-1.0-1.el7.x86_64.rpm".  Test installing it by running these four commands:

contint # This command should fail
cd ~/rpmbuild/RPMS/x86_64/
sudo rpm -i contint-1.0-1.el7.x86_64.rpm # *
contint  # This command should work
# * This command could work if you do not want to use "rpm" to install it:
# sudo yum localinstall /path/to/contint-1.0-1.el7.x86_64.rpm

12.  The .rpm can now be used on other servers running a RedHat distribution of Linux.  If you want to learn more about creating .spec files and .rpm packages, see this link.

How Do You Get curl to Work when the Web Page is Working?

Problem scenario
You can download a file with Lynx and wget. But curl is not working. What should you do?

Solution
First, try to curl the web page without redirecting the output. If the curl command creates a file, it does not necessarily give you an accurate depiction. You may want to examine the file with a text editor. What you see without redirecting the output to a file (that is allowing the output to echo to the screen) may provide a clue. If the the word "redirect" is displayed (referring to an HTTP), you need the -L flag (with no quotes). This way curl can follow the redirection and obtain the file.

For example, try this command (where www.continualintegration.com is the URL you are trying to download with curl):

curl -L www.continualintegration.com > /tmp/local.html

How Do You Save a Linux Machine where You Overwrote the PATH Variables Such That No Command Will Work?

Problem scenario
The server seems to be in a hopeless condition. You want to recover the server, but no commands in Linux are working. You get "command not found" with simple commands such as ls. You cannot use sudo, cat, rm, or reboot. What should you do?

Solution
Use absolute paths. For example, use this command:

/bin/sudo reboot

If there is a new file that is causing the problem, you can use /bin/vi to modify the file or /bin/rm to delete it.

In Python, How Do You Call a Function as a New Thread with the threading Module?

Problem scenario
(The old problem scenario was "You want to write a program to call a unbound function in a new thread with the threading module. How do you do this?")

You want to write a program to call a function in a new thread with the threading module. How do you do this?

Solution
Run this program (e.g., python foobar.py):

from threading import Thread
from time import sleep

def threaded_invoker():
    for i in range(7):
        print("the function is processing")
        sleep(1)

if __name__ == "__main__":
    thread = Thread(target = threaded_invoker)
    thread.start()
    thread.join()
    print("Threading completed.")

How Do You Examine an .xz File in the /var/log Directory (with No “tar” in Its Extension)?


Problem scenario
You have an mail.xz file in /var/log/ that has nor "tar" in its name. You want to view its contents. What do you do to view or use an .xz file with no "tar" in its name?

Solution
If you are new at this, copy the log to the /tmp/ directory (assuming this .xz file has no sensitive data or no one else uses this server). Then run these commands (where logfilename is the name of the file you want to view without its ".xz" extension):

cd /tmp/
mkdir delete
mv logfilename.xz delete
cd delete
sudo xz --decompress logfilename.xz
sudo view logfilename

How Do You Create and Store an Application-Level Secret on the AWS Cloud?

Problem scenario
You want to create and save an application-level secret in AWS. What do you do?

Solution
Use Secrets Manager.

Procedures

  1. Log into the AWS console.
  2. Go to "Secrets Manager"
  3. Click "Store a new secret"
  4. For the secret type choose "Other type of secrets".
  5. For the left-most field, enter the username. For the right-most field enter the password.
  6. Choose the encryption key of your choice. Click "Next."
  7. Enter a "Secret" name. Enter text into any of the optional fields if you want. Click "Next".
  8. You may want to keep automatic rotation disabled. If you are to use automatic rotation, you have to have a Lambda function created. If you need assistance, see this posting. Click "Next".
  9. You may or may not want to copy some of the sample code to retrieve the secret in your application.
  10. Click "Store".