How Do You Fix the Problem of Being Prompted for the Root Password When You Want to sudo as a Given User?

Problem scenario
You try to run a command with sudo and you are prompted for the root user password (not the password for the username who issued the sudo command). You want to be prompted for the user (e.g., jdoe) not root.  What should you do?

Solution
Root cause
sudo is not configured properly.

Procedures
1.  Use the visudo command (with no arguments): visudo

2.  Comment the stanzas (place leading "#" symbols) in front of these stanzas:

Defaults targetpw
ALL    ALL=(ALL) ALL

They will look like this:

#Defaults targetpw
#ALL    ALL=(ALL) ALL

3.  Add the user to the "User privilege specification":

jdoe ALL=(ALL) ALL

4.  Save (e.g., press ZZ).

5.  You are done.  If you want to change the interval of how frequently you are prompted to enter a password (the time duration before being challenged again) when you use the sudo command, see this posting.

How Do You Write a Hello World Program in Node.js?

Problem scenario
You want to write a "Hello World" program using Node.js.  What do you do?

Solution
Prerequisite
Identify the external IP address of the server (e.g., run "curl icanhazip.com" if you have access to the internet).

Procedures
1.  Install node and npm on a Linux server.  If you need assistance with this, see this posting.
2.  Run these two commands:
mkdir contint
cd contint

3.  Run this command:

npm init  # respond to the interactive prompts.  You probably will want to accept the defaults, but for the "Entry point" one with the suggestion of "index.js" change it to "contint.js"

For "Is it ok?" type "yes" (with no quotes) and press "Enter".

4.  Create a file called contint.js and have the following as content:

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World');
});

app.listen(2987, function () {
  console.log('Example webpage application listening on port 2987!');
});

5.  Run these two commands:

npm install express --save
node contint.js

6.  Open a web browser on your workstation.  Go to x.x.x.x:2987 where x.x.x.x is the external IP address of the Linux server.

How Do You Create a Node.js Application to Be Presentable and Usable via a Docker Container?

Problem scenario
You want to create a basic "Hello World" web page with Node.js running in a Docker container.  How do you do this?

Solution
Prerequisite

Install Docker.  If you need assistance, see this posting.

Procedures
1.  Create three files in the same directory on a Linux server.  A possible location would be the home directory of the user that will run the "docker" commands.

1.a.  Dockerfile should have this as its content (which was modeled on what was here):

FROM node:argon

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE 8080
CMD [ "npm", "start" ]

1.b.  package.json should have this as its content (which was taken from https://nodejs.org/en/docs/guides/nodejs-docker-webapp/):

{
  "name": "docker_web_app",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "First Last <first.last@example.com>",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.16.1"
  }
}

1.c.  server.js should have this as its content (which was mostly taken from https://nodejs.org/en/docs/guides/nodejs-docker-webapp/):

'use strict';

const express = require('express');

// Constants
const PORT = 8080;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello world. This is from Continual integration\n');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

2.  Run these two commands:

docker build .
docker images
    # Mentally identify in the output your new image.

3.  Run this command but substitute 44444 with the port you want to access the Node.js web page on and replace abcd1234 with the image ID found above:

docker run -p 44444:8080 -d abcd1234 # where abcd1234 is an Image ID

4.  Go to web browser.  Go to x.x.x.x:49160 where x.x.x.x is the external IP address of the Docker host; to find this IP address you can go to the Docker host and run "icanhazip.com" if you have internet access on the host.

How Do You, in Python, Make a Tuple from a List?

Problem scenario
You are writing code in Python.  You have a list that you want to have a copy of in the form of a tuple.  What do you do to convert the content to a different data type?

Solution
Assuming you have a list called "contintlist", this line would create a tuple with the content of "contintlist":

cooltuple = tuple(contintlist)

Here is a Python program that generates a list, then creates a tuple with the same content:

import random
pretuplelist = []
for i in range(1000):
   pretuplelist.append(random.randint(1,100))
cool_tuple = tuple(pretuplelist)
print(cool_tuple)

How Do You Generate 10,000 Random Numbers in a List in Python?

Problem scenario
You want to generate 10,000 random numbers in a tuple.  That is, you want a tuple with 10,000 numbers.  The numbers can be chosen at random.  How do you do this?

Solution
Run this program:
import random
coollist = []
for i in range(1000000):
   coollist.append(random.randint(1,100000))

How Do You Install kubeadm on Any Type of Linux?

Problem scenario
You want a quick, generic way to install kubeadm on any type of Linux.  What should you do?

Solution
Run these commands:

cd /tmp

curl -Lo kubeadm https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubeadm && chmod +x kubeadm

sudo mv kubeadm /usr/bin/

Test it by running this command: kubeadm version

(If you would prefer to use apt commands, because you are using a Debian/Ubuntu distribution of Linux, to install kubeadm, see this posting.)

How Do You Find the Resource Group Names in your Azure Account?

Problem scenario
You want to use an existing Resource Group name.  How do you find which ones you have created?

Solution
1.  Log into the web UI for the Azure Portal.
2.  On the left there often will be an item "Resource Groups" (under favorites).  Click on it.  If it is not there, search for "Resource Groups"

How Do You Write Tic-Tac-Toe in JavaScript?

Problem scenario
You want to write a program using JavaScript. You want to play Tic-Tac-Toe. What do you do?

Solution

1. Save the file below as contint.html to your computer.
2. Open a web browser, preferably Firefox.
3. View the HTML file on your computer via the web browser. (e.g., c:\path\to\contint.html)

<!DOCTYPE html>
<html>
<body>

<h2>Play Tic-Tac-Toe with Anyone You Can Share a Keyboard With</h2>
<p> </p>
<p> ***************************************************************</p>
<p> To make your mark, use the legend below for your position: </p>
<p> ltc is left-top-corner, tm is top-middle, rtc is right-top-corner</p>
<p> lm is left-column-middle-row, c is center, mr is middlerow-right-column</p>
<p> lbc is left-bottom-corner, bm is bottom-middle, rbc is right-bottom-corner</p>
<p> ***************************************************************</p>
<p> This works best in Firefox. </p>

<p id="demo"></p>

<script>

curboard = [ " ", " ", " ", " ", " ", " ", " ", " ", " " ]
gameover = 0
leftcol = 0
midcol = 0
rightcol = 0

function printBoard(board) {
  var x = board[0] + board[1] + board[2] + board[3] + board[4] + board[5] + board[6] + board[7] + board[8] 
  return x;   // The function returns the product of p1 and p2
}

function mainControl(board, player) {
  var place = prompt("Player " + player + ", please enter a place to make your mark");
  var possiblepos = ["ltc", "tm", "rtc", "lm", "c", "mr", "lbc", "bm", "rbc"];
  var validpos = (possiblepos.indexOf(place));
  if (validpos < 0) { 
    alert("Warning: That was not a valid place on the board! See the legend. You will now be prompted to enter a place on the board again.")
    mainControl(board, player) 
  }
  
  moveMaker(board, place, player)

  var heading = "Player " + player + " has just made a move.  Here is the current board:"
  
  widthcount = leftcol + midcol + rightcol
  
  // when board is empty, it is never displayed.
	
  if (widthcount == 1) {
    var line1 = "____"
	var line6 = "-------"
	}
	
  if (widthcount == 2) {
    var line1 = "_____"
	var line6 = "--------"
	}
	
  if (widthcount == 3) {
    var line1 = "_____"
	var line6 = "---------"
	}  
  
  var line2 = "|"+ board[0] + "|" + board[1] + "|" + board[2] + "|"
  var line4 = "|"+ board[3] + "|" + board[4] + "|" + board[5] + "|"
  var line5 = "|"+ board[6] + "|" + board[7] + "|" + board[8] + "|"
  
  var line7 = " "
  var line8 = "Press 'Enter' or click 'OK' to continue."
  var z = [
    heading,
    line1,  
    line2, 
    line6,  
    line4, 
    line6,  
    line5, 
    line6,
	line7,
	line8
  ].join("\n");
  return z
}

function moveMaker(board, pos, player) {
    if (pos == 'ltc') {
      if (board[0] == ' ') {
        board[0] = player
		leftcol = 1
      } else {
        alert("That square has already been marked. Please try again.")
        mainControl(board, player) }
    } if (pos == 'tm') {
        if (board[1] == ' ') {
		  midcol = 1
          board[1] = player
        } else {
           alert("That square has already been marked. Please try again.")
           mainControl(board, player) }
    } if (pos == 'rtc') {
        if (board[2] == ' ') {
		 rightcol = 1
         board[2] = player
        } else { 
          alert("That square has already been marked. Please try again.")
          mainControl(board, player) }
    } if (pos == 'lm') {
        if (board[3] == ' ') {
         board[3] = player 
		 leftcol = 1
        } else {
          alert("That square has already been marked. Please try again.")
          mainControl(board, player) }
    } if (pos == 'c') { 
        if (board[4] == ' ') { 
		 midcol = 1
         board[4] = player
        } else {
          alert("That square has already been marked. Please try again.")
          mainControl(board, player) }
    } if (pos == 'mr') {
        if (board[5] == ' ') {
         board[5] = player
		 rightcol = 1
        } else {
          alert("That square has already been marked. Please try again.")
          mainControl(board, player) }
    } if (pos == 'lbc') {
        if (board[6] == ' ') {
          board[6] = player
		  leftcol = 1
        } else {
          alert("That square has already been marked. Please try again.")
          mainControl(board, player) }
    } if (pos == 'bm') { 
        if (board[7] == ' ') {
		 midcol = 1
         board[7] = player
        } else {
          alert("That square has already been marked. Please try again.")
          mainControl(board, player) }
    } if (pos == 'rbc') {
        if (board[8] == ' ') { 
          board[8] = player 
		  rightcol = 1
		  } else {
          alert("That square has already been marked. Please try again.")
          mainControl(board, player) }
		}   
    return board
}


function boardwinchecker(board) {
  if (board[0] == board[4]) { // check the diagonal from left top to right bottom corner
    if (board[4] == board[8]) {
      if (board[4] != ' ') { 
        lastmessage = "Player " + board[0] + " wins! Do not press 'OK'. Please refresh the web page to play again."
        alert(lastmessage)
		gameover = 1
		return
      }
    }
  }
  if (board[2] == board[4]) { // check the diagonal from right top to left bottom corner
    if (board[4] == board[6]) {
      if (board[6] != ' ') {
        lastmessage = "Player " + board[2] + " wins! Do not press 'OK'. Please refresh the web page to play again."
        alert(lastmessage)
		gameover = 1
		return
      }
    }
  } 
  if (board[0] == board[3]) { // check the left column
    if (board[3] == board[6]) {
      if (board[6] != ' ') {
        lastmessage = "Player " + board[0] + " wins! Do not press 'OK'. Please refresh the web page to play again."
        alert(lastmessage)
		gameover = 1
		return
      }
    }
  } 
  if (board[0] == board[1]) { // check the top row
    if (board[1] == board[2]) {
      if (board[0] != ' ') {
        lastmessage = "Player " + board[0] + " wins! Do not press 'OK'. Please refresh the web page to play again."
        alert(lastmessage)
		gameover = 1
		return
      }
    }
  } 
  if (board[2] == board[5]) { // check right column
    if (board[5] == board[8]) {
      if (board[2] != ' ') {
        lastmessage = "Player " + board[2] + " wins! Do not press 'OK'. Please refresh the web page to play again."
        alert(lastmessage)
		gameover = 1
		return
      }
    }
  }
  if (board[6] == board[7]) { // check bottom row
    if (board[7] == board[8]) {
      if (board[8] != ' ') {
        lastmessage = "Player " + board[6] + " wins! Do not press 'OK'. Please refresh the web page to play again."
        alert(lastmessage)
		gameover = 1
		return
      }
    }
  }
  if (board[1] == board[4]) { // check middle column
    if (board[4] == board[7]) {
      if (board[1] != ' ') {
        lastmessage = "Player " + board[1] + " wins! Do not press 'OK'. Please refresh the web page to play again."
        alert(lastmessage)
		gameover = 1
		return
      }
    }
  }
  if (board[3] == board[4]) { // check middle row
    if (board[4] == board[5]) {
      if (board[3] != ' ') {
        lastmessage = "Player " + board[3] + " wins! Do not press 'OK'. Please refresh the web page to play again."
        alert(lastmessage)
		gameover = 1
		return
      }
    }
  } 
}

function boardcatchecker(board) {
    if (board[0] != ' ') {
      if (board[1] != ' ') {
        if (board[2] != ' ') {
          if (board[3] != ' ') {
            if (board[4] != ' ') {
              if (board[5] != ' ') {
                if (board[6] != ' ') {
                  if (board[7] != ' ') {
                    if (board[8] != ' ') {
                      alert("Game over. Cat's game. It was a draw!")
                      alert("Please refresh the web page to play again.")
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
}


last = printBoard(curboard);

var1 = mainControl(curboard, "X")
alert(var1)
var2 = mainControl(curboard, "O")
alert(var2)
var3 = mainControl(curboard, "X")
alert(var3)
var4 = mainControl(curboard, "O")
alert(var4)
var5 = mainControl(curboard, "X")
alert(var5)
boardwinchecker(curboard)
  
if (gameover == 0) {
    var6 = mainControl(curboard, "O")
    alert(var6) 
    boardwinchecker(curboard)  }
  
if (gameover == 0) {
    var7 = mainControl(curboard, "X")
    alert(var7)
    boardwinchecker(curboard)  }
  
if (gameover == 0) {
    var8 = mainControl(curboard, "O")
    alert(var8)
    boardwinchecker(curboard)  }
  
if (gameover == 0) {
    var9 = mainControl(curboard, "X")
    alert(var9)
    boardwinchecker(curboard)	}
	
if (gameover == 0) {
     boardcatchecker(curboard)   }

</script> 

</body>
</html>

How Do You Use an Unbound Method in Python?

Problem scenario
You want to write a Python program with a unbound method.  What should you do?

Solution
The definition of an unbound method is a method or function that is not part of a class.  Run this program and you will invoke an unbound method:

#!/usr/bin/env python

def unboundexample():
  print("This function is not bound to a class.  Therefore it will work without an object being created")

unboundexample()