To get the last characters of a string in Ruby, use the following instructions. If the string is shorter than the selected number of characters, the entire string is returned.
a = '12345' b = a[-3..-1] || a puts b
Returns
345
Knowledge Base for IT Professionals, Teachers and Astronauts
To get the last characters of a string in Ruby, use the following instructions. If the string is shorter than the selected number of characters, the entire string is returned.
a = '12345' b = a[-3..-1] || a puts b
Returns
345
If you work on a Windows computer and create a new GitHub repository, you can copy the code from the GitHub page to set the remote address and push the existing code to the GitHub server. You may get the following error message:
$ git push -u origin master Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
Make sure you have selected the HTTPS button to see the correct lines for Windows.
To change to address of the remote server execute the following
git remote set-url origin https://github.com/.....
Set up your account in the AWS console
If you are not an AWS account administrator, ask your AWS account administrator to do the following for you
brew install awscli
choco install awscli
or
PATH=$PATH:/c/Program Files/AWSCLI/bin
If you do not configure the AWS CLI you will get the following error message: ‘NoneType’ object has no attribute ‘get_frozen_credentials’
aws configure
AWS Access Key ID [None]: YOUR AWS KEY AWS Secret Access Key [None]: YOUR AWS SECRET KEY Default region name [None]: us-east-1 Default output format [None]: HIT ENTER FOR NONE
Close and reopen all open Bash and Command windows to reload the changed Path environment variable.
The configure command created the .aws folder with two files.
Your AWS configuration folder is located at ~/.aws
Set the permissions on the folder
chmod -R 700 ~/.aws
Your AWS configuration folder is located at C:\Users\YOUR_USER_NAME\.aws
Your credentials file will look like this
[default] aws_access_key_id = MY_ACCESS_KEY aws_secret_access_key = MY_SECRET_KEY
Test Kitchen will use the keys from the [default] section to connect to AWS when we launch instances.
When you work with multiple AWS accounts you can add all of your keys to the credentials file. To use a specific key, add the profile option to your command line instructions. If you don’t specify the profile in your AWS commands, the AWS Command Line Interface will use the key from the default section.
[aws01] aws_access_key_id = MY_ACCESS_KEY_FOR_AWS01 aws_secret_access_key = MY_SECRET_KEY_FOR_AWS01 [aws02] aws_access_key_id = MY_ACCESS_KEY_FOR_AWS02 aws_secret_access_key = MY_SECRET_KEY_FOR_AWS02 [default] aws_access_key_id = MY_ACCESS_KEY_FOR_AWS01 aws_secret_access_key = MY_SECRET_KEY_FOR_AWS01
The config file will look like this. If you don’t specify the region in the AWS command, the AWS Command Line utility will use the region from this file.
[default] region = us-east-1
Keep your private keys in a folder in your home directory. You will need them to launch servers, and log into Linux servers with SSH, or retrieve the administrator password for Windows servers.
Create the folder ~/aws_keys
Set the permissions on the folder key folder too
chmod -R 700 ~/aws_keys
If you copy key files to the folder you may get the following error message:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: UNPROTECTED PRIVATE KEY FILE! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Permissions 0755 for ‘/Users/YOUR_USERNAME/.aws/KEY_FILE_NAME.pem’ are too open. It is required that your private key files are NOT accessible by others. This private key will be ignored. Load key “/Users/YOUR_USERNAME/.aws/KEY_FILE_NAME.pem”: bad permissions USERNAME@SERVER_IP’s password: Permission denied, please try again.
To satisfy the security requirements set the permission on the key files to 700
chmod -R 700 ~/.aws/KEY_FILE_NAME.pem
Create the folder C:\Users\YOUR_USER_NAME\aws_keys
For more info visit Amazon EC2 Key Pairs
When you have created your cookbook, Chef already added a .kitchen.yml file that tells Test Kitchen how to launch a server instance on your workstation.
The default .kitchen.yml file only contains driver and provisioner information for Vargrant. To launch a server instance in AWS we need to add AWS specific instructions to the file. First modify the .kitchen.yml file to be ready for other drivers:
--- provisioner: name: chef_zero verifier: name: inspec platforms: - name: ubuntu-16.04 driver: name: vagrant - name: centos-7.2 driver: name: vagrant suites: - name: default run_list: - recipe[test::default] verifier: inspec_tests: - test/recipes attributes:
Make sure the empty lines are really empty. The .kitchen.yml file should not have white space in a seemingly empty line.
Connect to the Chef server in Beginner’s Guide to DevOps Engineering part 4.
to the Tutorials page
When you set up a new Windows server and want to make sure all necessary Windows features are installed on it, you can list them in text format. If you want to replicate the configuration of an existing server, just list the features of both servers and compare them in a comparison tool, like Araxis Merge.
To list the installed Windows features, execute the following in the PowerShell prompt
get-windowsfeature
The result follows the layout of the checkboxes of the graphical user interface in text form.
[X] Web Server (IIS) Web-Server Installed [X] Web Server Web-WebServer Installed [X] Common HTTP Features Web-Common-Http Installed [X] Default Document Web-Default-Doc Installed [X] Directory Browsing Web-Dir-Browsing Installed [X] HTTP Errors Web-Http-Errors Installed [X] Static Content Web-Static-Content Installed [X] HTTP Redirection Web-Http-Redirect Installed [ ] WebDAV Publishing Web-DAV-Publishing Available
To produce a simpler output execute
Dism /online /Get-Features
This will return
Deployment Image Servicing and Management tool Version: 6.3.9600.17031 Image Version: 6.3.9600.17031 Features listing for package : Microsoft-Windows-ServerCore-Package~31bf3856ad364e35~amd64~~6.3.9600.16384 Feature Name : NetFx4ServerFeatures State : Enabled Feature Name : NetFx4 State : Enabled Feature Name : NetFx4Extended-ASPNET45 State : Disabled
When you test your cookbook in Chef Test Kitchen and get the following error, delete the “Policyfile.rb” from your Chef cookbook directory.
$$$$$$ You must set your run_list in your policyfile instead of kitchen config. The run_list your config will be ignored. $$$$$$ Ignored run_list: ["recipe[...::...]"] Preparing dna.json Exporting cookbook dependencies from Policyfile /tmp/... Error: Invalid lockfile data Reason: (ChefDK::DependencyConflict) Cookbook ... (...) has dependency constraints that cannot be met by the existing cookbook set: Cookbook ... isn't included in the existing cookbook set.
There are secrets in most of the Chef cookbooks that we want to protect. We don’t want to give out user names, passwords and AWS keys. In Chef the best place to hide these secrets is the Encrypted Data Bag.
A Data Bag is a JSON file that we can encrypt, so we can store it in version control with the rest of the cookbook.
To make continuous integration and delivery (CI/CD) easier, store the Encrypted Data Bags in the cookbook folder structure and commit them together with the rest of the cookbook into version control (Git)
To make sure the unencrypted secret is not committed into version control, add the following line to the .gitignore file
# Ignore the unencrypted Data Bags
data_bags_unencrypted/
The structure of Chef folder should look like this. Store the unencrypted Data Bags with the original values in the data_bags_unencrypted folder
Create a folder for the unencrypted Data Bag and create a file for the Data Bag Item. The name of the file and the value of the id element should be the same.
Enter the Data Bag Item values and save the file.
{
"id": "access_key",
"AccessKey": "XXXXX",
"SecretKey": "YYYYY"
}
Create the following script and name it encrypt_databag.sh. This script
Replace >>>MY_ENCRYPTED_DATABAG_SECRET<<< with the name of the encrypted data bag secret file.
#!/bin/bash
if [ -z $1 ] || [ -z $2 ]
then
echo "Please supply the arguments: DATABAG_NAME ITEM_NAME"
echo "../devops-chef-scripts/encrypt_databag.sh [DATA_BAG_NAME/NAME OF THE FOLDER] [ITEM_NAME/ID]"
else
echo -- knife data bag create $1
knife data bag create $1
# Encrypt the databag and upload it to the Chef server
echo -- knife data bag from file $1 $1/$2.json --secret-file ../data_bags_unencrypted/>>>MY_ENCRYPTED_DATABAG_SECRET<<<
knife data bag from file $1 $1/$2.json --secret-file ../data_bags_unencrypted/>>>MY_ENCRYPTED_DATABAG_SECRET<<<
# Create a directory for the encrypted databag on the workstation
echo -- mkdir -p ../data_bags/$1
mkdir -p ../data_bags/$1
# Download the encrypted data bag
echo -- knife data bag show $1 $2 -F json 'to' ../data_bags/$1/$2.json
knife data bag show $1 $2 -F json > ../data_bags/$1/$2.json
echo "Encrypted data bag has been created at ../data_bags/"$1"/"$2".json"
fi
echo -n "Press a key to exit" #'-n' means do not add \n to end of string
read # No arg means dump next line of input
Add execution right to the file
chmod +x ./encrypt_databag.sh
Open a Bash window in the data_bags_unencrypted folder
Execute the following command, where
DATA_BAG_NAME is the name of the data bag folder
ITEM_NAME is the value of the id element and the item file name without the ‘.json’ extension,
../devops-chef-scripts/encrypt_databag.sh DATA_BAG_NAME ITEM_NAME
The script will create a folder for the Data Bag in the “data_bags” folder and save the encrypted Data Bag file in it.
The following warning is normal. We did not want to unencrypt the data bag, just download the encrypted version.
WARNING: Encrypted data bag detected, but no secret provided for decoding. Displaying encrypted data.
If you get the error message
ERROR: The object you are looking for could not be found
Response: Cannot load data bag item … for data bag …
make sure you set the name of the data bag item file without the .json extension and the value of the id element the same.
When Berkshelf in the Chef Test Kitchen cannot resolve the cookbook dependencies, it displays the following error message during converge:
Unable to satisfy the following requirements
To help Berks to start a fresh calculation, delete the Berksfile.lock file and enter the following into the command window:
berks install
In the first part of the series, Beginner’s Guide to DevOps Engineering Part 1. we have already installed the DevOps development tools.
chef generate cookbook test
file '/Users/YOUR_USERNAME/Desktop/helloworld.txt' do content 'Hello world' end
file 'C:\\Users\\YOUR_USERNAME\\Desktop\\helloworld.txt' do content 'Hello world' end
cd test
chef-client --local-mode recipes/default.rb
We will use Test Kitchen and Vagrant to launch virtual machines. Currently, only Linux images are available for Vagrant, so we will modify our recipe to select between Linux and Windows and act accordingly.
Open the default.rb recipe and update it to look like this
case node['os']
when 'linux'
file "/tmp/helloworld.txt" do
content 'This file was created by Chef!'
end
when 'windows'
file "C:\\Chef\\helloworld.txt" do
content 'This file was created by Chef!'
end
end
Enter the following commands into the Bash window
kitchen list
kitchen converge ubuntu
kichen login ubuntu
kitchen destroy ubuntu
To test your cookbook on a Windows virtual machine locally, create one for Vagrant. See Launch Windows instances locally with Chef Test Kitchen for the details.
Chef has a steep learning curve. Chef is not just scripting or programming, but you have to understand how Chef works to be able to use it to configure servers. There are many ways to do the same thing and there is not much documentation to recommend the best way. If you search Google, the problems usually have multiple solutions, and many times the “best” answer is selected based on personal preference. To get started, you should familiarize yourself with the tools, because you will use most of them during the development process.
In this guide I will use a Windows computer as a workstation, but all tools work on Mac and Linux computers.
Learn Vagrant to understand how Test Kitchen manages the test servers on your local machine or at AWS. You will not use vagrant directly, but Test Kitchen uses it to launch servers.
Working with AWS in Beginner’s Guide to DevOps Engineering part 3.
to the Tutorials page
DevOps engineering (Release engineering) is a relatively new profession. There is a very high demand for experienced DevOps engineers, because more and more large corporations want to migrate their data centers into the Cloud. In this series I will introduce you to the tools of the trade through the development of a Chef cookbook.
The tools of the DevOps engineers are new and most of them are in the active development phase with frequent releases. Some of the new versions have show stopping bugs in them, so it is a good idea to test all new versions of the tools before you uninstall the old one.
To work as a DevOps engineer you need a development environment with multiple tools. Luckily all of them are available for free and easy to set up.
You can do all development and testing on your workstation for free, but to see your scripts running in a real cloud, you can set up an account at a cloud provider. Amazon Web Services (AWS) offers a free tier where you can launch small server instances for free.
In this guide I will use a Windows computer as a workstation, but all tools work on Mac and Linux computers.
If you don’t have an Apple ID create one. You can obtain one without a credit card:
Create the opt folder in the root of the harddisk.
The Windows operating system already has a Remote Desktop Client
Download the Microsoft Remote Desktop app from the App Store
Install a good text editor. If you do not have a favorite, I recommend Notepad ++
Install iTerm2, a smart terminal emulator to issue Bash commands and log into Linux servers.
https://ruigomes.me/blog/perfect-iterm-osx-terminal-installation/
https://gist.github.com/kevin-smets/8568070
Certain commands need elevated rights to run, so we will set up the Bash window to run as administrator.
Download Virtual Box from https://www.virtualbox.org/wiki/Downloads and follow the instructions to install it.
At the time of writing this was the section where the installer files were referenced
Run the downloaded installer file and accept all default values.
The installer starts the application. You can close it, we will use other tools to start it in the future.
The default credentials of a Vagrant server are:
The Ruby Development Kit is need for certain Ruby gems, so install it.
ruby dk.rb init
ruby dk.rb install
Ruby is already a part of the operating system.
;C:\HashiCorp\Terraform
For more details see https://www.terraform.io/intro/getting-started/install.html
cd ~
ls .bash_profile
# PATH Export PATH=/opt/terraform:$PATH export PATH unset DYLD_LIBRARY_PATH
We will use this utility to display the Terraform graphs.
;C:\Program Files (x86)\GraphvizX.XX\bin
Set up the DevOps development environment in Beginner’s Guide to DevOps Engineering part 2.
to the Tutorials page
When you create an EC2 instance in Amazon Web Services (AWS) the security group (firewall) blocks all ports that are not explicitly opened. To make file sharing possible on Windows servers, open the following ports in the security group of the server: