[RUBY] Creating an execution environment for Chef-Zero / Solo and SoftLayer CUI

SoftLayer Cooking LABO

Chef is written in Ruby and slcli commands are written in Python. You may want to change the version of Ruby or Python to avoid problems with these programming languages. However, the versions of these programming languages are not easy because they are determined by your Linux distribution. Getting the source code from the internet, compiling it and installing it under / usr / local / can be a daunting task.

As a workaround, install multiple versions of Python and Ruby, and use a convenient tool to change the version of ruby and python for a limited range of logged-in users, not for the entire server. Here, I will write about how to use the slcli command and Chef in Python under the pyenv environment and Ruby under the rbenv environment.

Preparing a virtual server to develop Chef recipes

This virtual server can be either VirtualBox on your computer or SoftLayer's virtual server. If you have a vagrant and VirtualBox environment on your computer, it may be cheaper than setting up a new SoftLayer virtual server. Here, we will proceed on the premise that a virtual server for recipe development will be set up in SoftLayer.

Prepare the following server as the minimum environment to use Chef-Solo / Chef-Zero.

CPU: 1 core RAM: 2GB OS: Ubuntu 14.04 64bit Disk: 25GB

If you have 1GB of RAM, you'll need to keep it at 2GB only when you install Chef, as it will run out of memory and stop halfway through. It seems to work even if it is reduced to 1 giga after the introduction.

If you have already created the execution environment for the slcli command, it is convenient because you only need to execute the following shell.

#!/bin/bash
slcli vs create \
--hostname ChefWs --domain softlayer.com \
--datacenter tok02 \
--cpu 1 --memory 2 \
--os UBUNTU_LATEST_64 \
--billing hourly \
--public \
--postinstall https://raw.githubusercontent.com/takara9/ProvisioningScript/master/ubuntu_basic_config \
--key 355919 \
--network 100

Here, postinstall is a script that is executed only once at the first startup, and it is acquired from GitHub and executed. Since the setting contents are published on https://github.com/takara9/ProvisioningScript, it is recommended to develop a dedicated provisioning script by referring to it.

Then key is the ID number of the ssh login key. You can get the comparison between the label name and ID number of the customer portal with the following command.

$ slcli sshkey list

This provisioning script is designed to block access from the Internet by an OS firewall, so go to "Support"-> "SSL VPN Login" from the customer portal to make a VPN connection, and then private address. You will be able to log in with ssh.

Creating a working user

In SoftLayer, there is only a root user in the initial state, so create a working user. Here, I want to proceed with the theme of automating server settings using Chef, so I will create a user called chef.

root@ChefWs:~# adduser chef
user`chef'Is adding...
New group`chef' (1000)Is adding...
New user`chef' (1000)Group`chef'Is adding to...
Home directory`/home/chef'Is creating...
`/etc/skel'Copying files from...
Please enter a new UNIX password: 
Please re-enter your new UNIX password: 
passwd: password updated successfully
Changing the user information for chef
Enter the new value, or press ENTER for the default
	Full Name []: chef
	Room Number []: 
	Work Phone []: 
	Home Phone []: 
	Other []: 
Is this correct?? [Y/n] Y

Make a copy of the root login key so that chef users can log in with the same key.

root@ChefWs:~# cp -r .ssh /home/chef/
root@ChefWs:~# chown -R chef:chef /home/chef/.ssh/

Then add chef to / etc / sudoers.

/etc/sudoers


chef    ALL=(ALL) NOPASSWD: ALL

SoftLayer CUI environment creation

SoftLayer's command line user interface is slcli, which warns you depending on the Python version, so you can use pyenv to select the version you need, regardless of the Python version of your Linux distribution. Then, the command execution example will proceed using Ubuntu 14.04.

Installation and configuration of pyenv

Detailed installation method and setting explanation can be found at https://github.com/yyuu/pyenv, so we will introduce it as a reference. As a caveat, run the installation as a chef user instead of root.

$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

If you log out, log back in, and check with the following command, you'll see that it only contains Python for the Linux Distribution.

chef@ChefWs:~$ pyenv versions
* system (set by /home/chef/.pyenv/version)

Before deploying Python, gcc and zlib are required for the build, so install them as root.

root@ChefWs:~$ sudo apt-get install git gcc make openssl libssl-dev libbz2-dev libreadline-dev libsqlite3-dev

Next, use the Python slcli command to install the version that does not come out with a warning.

chef@ChefWs:~$ pyenv install 2.7.10

As a confirmation, from the result of the following command, you can see that it is installed but not selected.

chef@ChefWs:~$ pyenv versions
* system (set by /home/chef/.pyenv/version)
  2.7.10

Select the version introduced this time and reconfirm.

chef@ChefWs:~$ pyenv global 2.7.10
chef@ChefWs:~$ pyenv versions
  system
* 2.7.10 (set by /home/chef/.pyenv/version)

Confirmation of reminders once again

chef@ChefWs:~$ python --version
Python 2.7.10

This completes the creation of Python.

Install slcli using pip

The following command completes the installation of the slcli command.

chef@ChefWs:~$ pip install softlayer
Collecting softlayer
  Downloading SoftLayer-4.1.1-py2.py3-none-any.whl (248kB)
    100% |████████████████████████████████| 249kB 1.3MB/s 
Collecting prompt-toolkit (from softlayer)
  Downloading prompt_toolkit-0.52-py2-none-any.whl (188kB)
    100% |████████████████████████████████| 188kB 1.7MB/s 
Collecting click>=5 (from softlayer)
  Downloading click-5.1-py2.py3-none-any.whl (65kB)
    100% |████████████████████████████████| 65kB 3.9MB/s 
Collecting six>=1.7.0 (from softlayer)
  Downloading six-1.9.0-py2.py3-none-any.whl
Collecting prettytable>=0.7.0 (from softlayer)
  Downloading prettytable-0.7.2.tar.bz2
Collecting requests>=2.7.0 (from softlayer)
  Downloading requests-2.7.0-py2.py3-none-any.whl (470kB)
    100% |████████████████████████████████| 471kB 756kB/s 
Collecting pygments (from prompt-toolkit->softlayer)
  Downloading Pygments-2.0.2-py2-none-any.whl (672kB)
    100% |████████████████████████████████| 675kB 554kB/s 
Collecting wcwidth (from prompt-toolkit->softlayer)
  Downloading wcwidth-0.1.5-py2.py3-none-any.whl
Installing collected packages: six, pygments, wcwidth, prompt-toolkit, click, prettytable, requests, softlayer
  Running setup.py install for prettytable
Successfully installed click-5.1 prettytable-0.7.2 prompt-toolkit-0.52 pygments-2.0.2 requests-2.7.0 six-1.9.0 softlayer-4.1.1 wcwidth-0.1.5

Get and set user ID and API key

Set the user name and API key with the following command. This username and API key can be found by clicking View in the API Key column at the location of the desired user ID in Account-> Users, copy and enter.

chef@ChefWs:~$ slcli config setup
Username []: *********
API Key or Password []: 
Endpoint (public|private|custom) [public]: 
Timeout [0]: 
:..............:..................................................................:
:         Name : Value                                                            :
:..............:..................................................................:
:     Username : *********                                                        :
:      API Key : **************************************************************** :
: Endpoint URL : https://api.softlayer.com/xmlrpc/v3.1/                           :
:      Timeout : not set                                                          :
:..............:..................................................................:
Are you sure you want to write settings to "/home/chef/.softlayer"? [Y/n]: Y
Configuration Updated Successfully

To check the operation, execute the following command, and if no error occurs, the process is complete.

chef@ChefWs:~$ slcli summary

Install Ruby version control environment

There are rbenv and rvm, but I think that you can use it if you are familiar with it. Since pyenv was installed here, I would like to introduce rbenv. The difficulty is that the installation method is a little complicated, but the installation procedure follows this URL https://github.com/sstephenson/rbenv.

chef@ChefWs:~$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
Cloning into '/home/chef/.rbenv'...
remote: Counting objects: 2079, done.
remote: Total 2079 (delta 0), reused 0 (delta 0), pack-reused 2079
Receiving objects: 100% (2079/2079), 353.65 KiB | 237.00 KiB/s, done.
Resolving deltas: 100% (1267/1267), done.
Checking connectivity... done.

Run the following command to add the shell settings.

chef@ChefWs:~$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
chef@ChefWs:~$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

Log out and log back in to see if your environment is set up correctly. If the following message is displayed in type rbenv, the environment is ready.

chef@ChefWs:~$Log out
Connection to 10.132.253.38 closed.
imac:~ maho$ ssh [email protected] -i key/takara3
Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-55-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
Last login: Wed Sep 30 17:37:00 2015 from 10.2.224.229
chef@ChefWs:~$ type rbenv
rbenv is a function
rbenv () 
{ 
    local command;
    command="$1";
    if [ "$#" -gt 0 ]; then
        shift;
    fi;
    case "$command" in 
        rehash | shell)
            eval "`rbenv "sh-$command" "$@"`"
        ;;
        *)
            command rbenv "$command" "$@"
        ;;
    esac
}

Get ruby-build from GitHUB.

chef@ChefWs:~$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
--Omitted on the way--
Checking connectivity... done.

You need to have a package to build Ruby, so do the following:

chef@ChefWs:~$ sudo apt-get install -y libssl-dev libreadline-dev zlib1g-dev

The following command will download and build. It will take some time if 1 core has 2 giga memory.

chef@ChefWs:~$ rbenv install 2.2.3
--Omitted on the way--
Installed ruby-2.2.3 to /home/chef/.rbenv/versions/2.2.3

To confirm, do the following: It is not selected in this state.

chef@ChefWs:~$ rbenv versions
  2.2.3

To select Ruby 2.2.3 and make it available, run the following command.

chef@ChefWs:~$ rbenv global 2.2.3
chef@ChefWs:~$ ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]

Install Chef Solo / Zero

If you think Solo will not disappear, the following link will be helpful. (http://www.sawanoboly.net/blog/2014/11/25/chef-solo-zero-knife-solo-zero) In a super-summary, it means "it won't disappear".

Add the following modules to build Chef.

chef@ChefWs:~/chef-repo$ sudo apt-get install g++ autoconf

Create a Chef repository.

chef@ChefWs:~$ pwd
/home/chef
chef@ChefWs:~$ mkdir chef-repo
chef@ChefWs:~$ cd chef-repo/

Install bunlder to use Gemfile.

chef@ChefWs:~/chef-repo$ gem install bundler
--Omitted on the way--
1 gem installed

Create a Gemfile template in the repository folder with the following command.

chef@ChefWs:~/chef-repo$ bundle init
Writing new Gemfile to /home/chef/chef-repo/Gemfile

Edit with an editor to add the module you want to install.

/home/chef/chef-repo/Gemfile


# A sample Gemfile
source "https://rubygems.org"

gem 'chef'
gem 'knife-zero'
gem 'knife-solo'
gem 'berkshelf'
gem 'knife-softlayer'

Introduce the required set of modules for Chef.

chef@ChefWs:~/chef-repo$ bundle install
Fetching gem metadata from https://rubygems.org/.........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies.................
Using rake 10.4.2
--Omitted on the way--
Using bundler 1.10.6
Bundle complete! 5 Gemfile dependencies, 96 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
Post-install message from knife-solo:
Thanks for installing knife-solo!

If you run into any issues please let us know at:
  https://github.com/matschaffer/knife-solo/issues

If you are upgrading knife-solo please uninstall any old versions by
running `gem clean knife-solo` to avoid any errors.

See http://bit.ly/CHEF-3255 for more information on the knife bug
that causes this.

The current repository is in the following state.

chef@ChefWs:~/chef-repo$ pwd
/home/chef/chef-repo
chef@ChefWs:~/chef-repo$ ls -la
20 in total
drwxrwxr-x 2 chef chef 4096 September 30 18:48 .
drwxr-xr-x 8 chef chef 4096 September 30 18:22 ..
-rw-r--r--1 chef chef 133 September 30 18:22 Gemfile
-rw-rw-r--1 chef chef 6402 September 30 18:48 Gemfile.lock

Summary

We have created a Chef recipe development environment for automating server construction with SoftLayer. By using pyenv and rbenv, you can be free from binding to Python and Ruby versions of Linux distributions. From the next time onward, we will proceed to develop specific recipes.

Recommended Posts

Creating an execution environment for Chef-Zero / Solo and SoftLayer CUI
Tips for using Selenium and Headless Chrome in a CUI environment
Studying Python Part.1 Creating an environment
An app for smart people and smart people
Created an environment for Anaconda & Jupyter
Explanation of creating an application for displaying images and drawing with Python
Creating an Ubuntu 18.04 + MAAS2.4 environment starting from 0
Creating amateur python environment settings (for MAC)
Creating a virtual environment in an Anaconda environment
Creating a development environment for machine learning
Building an environment for "Tello_Video" on Raspbian
Building an environment for "Tello_Video" on Windows
Creating an environment for OSS-DB Silver # 1_Create a Linux environment (CentOS7 virtual environment) with VirtualBox/Vagrant