The first Chef run cannot update the DNS IP addresses on Red Hat Enterprise Linux

To be able to access URLs on our local network by name from our Red Hat Enterprise Linux ( RHEL ) servers, we use the “resolver” Chef cookbook to configure our systems.

When the “resolver” Chef cookbook tried to store the DNS IP addresses in the /etc/resolv.conf file, during the first Chef run, something magically restored the file to the original content. During the second Chef run, the “resolver” Chef cookbook could successfully update the file.

As we have found out, the Network Manager service restores the file to the original settings. To stop the Network Manager updating the file, we can disable it. Before we call the “resolver” Chef cookbook, we disable and stop the Network Manager service with this code:

bash 'stop_networkmanager' do
  code <<-EOF
    systemctl disable NetworkManager.service
    systemctl stop NetworkManager.service
  EOF
end

include_recipe 'resolver'

 

 

Dynamically set Chef resource attributes

When you need to set a Chef resource attribute based on the current state of the environment, there is a way to dynamically provide the value.

  1. Set the value of a boolean variable with a test,
  2. Declare the Chef resource and assign a reference to it to a variable,
  3. Set the resource attribute based on the value of the boolean variable.
# Set a boolean variable with a test 
def MY_BOOLEAN_VARIABLE?
  "#{node['domain']}" != ""
end 

# Execute a resource and get a reference to it into a variable
t = MY_RESOURCE 'MY_RESOURCE_NAME' do
  ...
end
# Set the attribute value based on the boolean variable
t.MY_RESOURCE_ATTRIBUTE MY_ATTRIBUTE_VALUE if MY_BOOLEAN_VARIABLE?

 

 

How to create a bootable USB drive to install Windows

If the computer you want to install Microsoft WIndows on, does not have a DVD drive, you can install Windows from a USB drive. To start the computer from the USB drive, you need to prepare the drive to make it bootable.

Microsoft has a free tool that can download the edition of the WIndows operating system you need, format the USB drive, make it bootable, and place the installer file on it.

  1. Using a web browser navigate to https://www.microsoft.com/en-us/software-download/windows10,
  2. Click the Download tool now button to install the Microsoft Media Creation Tool,
  3. Start the downloaded MediaCreationTool.exe program and follow the prompts.

Setup failed: Failed to copy slug dir: lstat /Users: no such file or directory error in Terraform Enterprise

When you try to execute a Terraform configuration in Terraform Enterprise (Atlas) you may get the error message:

Setup failed: Failed to copy slug dir: lstat /Users: no such file or directory

Cause:

The Git repository contains the .terraform/modules directory, and the Terraform Enterprise server cannot get the latest modules from GitHub.

Solution:

  1. Create a .gitignore file in the repository and add these lines:
    */.terraform/modules/
    
    # Ignore DS_Store if working on a mac
    .DS_Store
  2. Delete the .terraform directory and push the changes to GitHub

 

 

Install a Python package

To install a Python package on Macintosh

  1. Download the Python package to your workstation,
  2. Unpack the package,
  3. Open a terminal window,
  4. Start sudo,
    sudo -i
  5. Change to the directory of the package,
  6. Install the package.
    pip install PACKAGE_NAME

If you don’t install the package in sudo mode, you will get “Permission denied” errors during the package installation.

 

PyCharm configuration

When you install PyCharm, the default Python interpreter is may not the version you want to use for your new projects. When you create a new project, and the project interpreter is not set, you may get the following error message:

Executed command:
/var/folders/_x/_f0zzs1s4sx6bk4s8n78l2jsbn9r3z/T/tmpBoQLx_pycharm-management/pip-9.0.1/setup.py install

Error occurred:
40:357: execution error: /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/distutils/dist.py:266: UserWarning: Unknown distribution option: ‘python_requires’

Command output:
40:357: execution error: /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/distutils/dist.py:266: UserWarning: Unknown distribution option: ‘python_requires’
warnings.warn(msg)
warning: build_py: byte-compiling is disabled, skipping.
warning: install_lib: byte-compiling is disabled, skipping.
error: byte-compiling is disabled.
(1)

Solution:

Set the default Python interpreter to the latest installed version before you create your project:

  1. Start PyCharm
  2. In the File menu select Default Settings
  3. On the left side select Project Interpreter,
  4. On the right side click the arrow of the Project Interpreter drop-down
  5. Select the Python interpreter you want to use by default, and click the OK button.

Python ImportError: No module named …

Symptom:

from MY_FOLDER.MY_FILE import MY_CLASS causes an error:
ImportError: No module named MY_FOLDER.MY_FILE

Debugging:

  1. Execute the program in debug mode,
  2. In the Variables window expand Special Variables,
  3. Read the __warningregistry__ value.

Possible cause:

The __init__.py file is missing in the directory of the referenced class.

Solution:

Create an empty __init__.py file in the directory where the class is referenced from.

Python basics

Great Python introductory video for C# developers
https://www.youtube.com/watch?v=jj60geqaP08

__  (double underscore) Starting the attribute name with double underscore attribute makes it private. To expose the private attributes and methods

print(dir(MY_CLASS))

 

Code hierarchy

In Python indentation denotes the hierarchy of the instructions, not curly braces, like in C# or Java.

Directories

When you create a directory in your project, create an empty __init__.py file in the new directory to be able to reference objects from that directory, and avoid

from MY_FOLDER.MY_FILE import MY_CLASS causes an error:
ImportError: No module named MY_FOLDER.MY_FILE

Classes

Predefined methods

__init__(self)  Initializer method (constructor)

__repr__(self)  Representation

__iter__(self)  Method to define hot to iterate through the object

Decorators

@property  Exposes a method as a property of the class

from _04_anonymous.anonobject import AnonObject
ImportError: No module named _04_anonymous.anonobject

{(“Not importing directory ‘/Users/pinterl/Git/devops-scripts/Python_Tutorial/Python/Python_for_C_sharp_developers/_04_anonymous’: missing __init__.py”, <type ‘exceptions.ImportWarning’>, 6): True}

Working with PyCharm

Project management

Create a new project in PyCharm

  1. In the File menu select New Project…,
  2. On the left side select the Python framework you want to use,
  3. Enter the name for your project to the end of the Location path,
  4. If you have selected a framework that asks for more information
    1. Click the More Settings link and enter the additional values,
    2. Click the Create button,
    3. PyCharm generates folders for your project and application,
    4. When PyCharm asks you about compatibility inspection, enable it, to make sure your code is compatible with future versions of Python.
    5. PyCharm will enable Code compatibility inspection. Click OK to save the setting.
    6. PyCharm will still set your project interpreter to 2.6, so set it to 2.7. In the PyCharm menu select Preferences…,
    7. On the left side select Project:…, Project Interpreter,
    8. In the Project Interpreter drop-down select 2.7 and click the OK button.

Delete a project in PyCharm

The PyCharm project explorer does not have a delete option. To delete a PyCharm project

  1. Open the project location in Finder,
  2. Move the project folder to the Trash,
  3. The PyCharm project explorer window will reflect the change.

Shortcuts, tips, and tricks

 Auto generate

Auto generate functions, classes, attributes

  1. Enter the name of the undefined element where you want to use it,
  2. Click the light bulb, or press ⌥⏎ (Alt-Enter) to pop up the Intention Action alert,
  3. Select the action to generate the definition.

Auto generate fields for classes

  1. Enter the __init__ function of the class with the field names,
  2. Place the cursor on the field you want to generate the definition for,
  3. Click the light bulb, or press ⌥⏎ (Alt-Enter) to pop up the Intention Action alert,
  4. Select Add field ‘…’ to class …,
  5. Press Enter to select the option,
  6. Press Enter at the end of the new line to move the cursor to the next line, otherwise, the Intention Action alert does not pop up when you press ⌥⏎ (Alt-Enter)

 

Code completion

  • Press Ctrl-Space to complete the name of the element.
  • Press Ctrl-Space twice to complete the name of any class, even if it is not yet imported. The import statement will be automatically added.

Quick documentation

To get a short documentation on an element under the cursor, press F1. (By default on a Macintosh you need to press fn-F1, because the F1 button is programmed to adjust the brightness.)

For more information

Auto completing code, see https://www.jetbrains.com/help/pycharm/auto-completing-code.html

Keyboard shortcuts
https://www.jetbrains.com/help/pycharm/keyboard-shortcuts-and-mouse-reference.html