Visualizzazione post con etichetta chef-client. Mostra tutti i post
Visualizzazione post con etichetta chef-client. Mostra tutti i post

lunedì 16 aprile 2018

PhpMyAdmin in a Chef lamp cookbook

How add phpmyadmin to a Chef lamp (linux, apache, mysql, php) cookbook in 10 steps

1 — Create the cookbook in your chef-repo

Create a folder named cookbooks if not exist already in your chef-repo
Generate a cookbook named lamp
chef generate cookbook cookbooks/lamp

2 — Create the recipes

PhpMyAdmin have need of an Apache or NGINIX (in our case APACHE) web server, a mysql database and php.
In /cookbooks/lamp generate 4 recipes, named apache, mysql, php and phpmyadmin:
chef generate recipe apache
chef generate recipe mysql
chef generate recipe php
chef generate recipe phpmyadmin
Now in your recipes folder should appear 5 file:
  • default.rb
  • apache.rb
  • mysql.rb
  • php.rb
  • phpmyadmin.rb

3 — Default

In default.rb we put useful standard tools (git etc…) and the command apt-get update for keep updated our package manager:
default.rb
apt_update 'Update the apt cache daily' do
    frequency 86_400
    action :periodic
end
package 'git'
package 'tree'
package 'curl'

4 — APACHE:

Rdit apache.rb
package "apache2" do
    action :install
end
service "apache2" do
    action [:enable, :start]
end

5 — MySql:

Edit mysql.rb
# Configure the MySQL client.
mysql_client 'default' do
  action :create
end
mysql_service 'default' do
  version '5.5'
  bind_address '0.0.0.0'
  port '3306'
  data_dir '/data'
  initial_root_password "password123"
  action [:create, :start]
end

6 — Create a data_bags for store mysql password:

In root folder of the chef-repo create a folder named data_bags, with inside another folder named passwords
mkdir data_bags
mkdir data_bags/passwords
inside passwords folder create a json file named mysql.json
mysql.json
{
    "id": "mysql",
    "root_password": "mypassword"
}
in metadata.rb, in the lamp cookbooks folder add this:
depends 'mysql', '~> 8.5.1'

7 — PHP

Now we must install php library
Edit php.rb in recipes folder:
# install php.
package "php" do
    action :install
end
package "php-pear" do
    action :install
end
package 'libapache2-mod-php' do
  action :install
  notifies :restart, "service[apache2]"
end
# Install php-mysql.
package 'php-mysql' do
    action :install
    notifies :restart, "service[apache2]"
end

8 — PhpMyAdmin

Edit phpmyadmin.rb in recipes folder:
package "phpmyadmin" do
    action :install
end
execute "link_in_www" do
  command "sudo ln -s /usr/share/phpmyadmin/ /var/www/html"
  user "root"
  not_if { ::File.exist?('/var/www/html/phpmyadmin') }
end
# call passwords databags
passwords = data_bag_item('passwords', 'mysql')
template '/etc/phpmyadmin/config.inc.php' do
    variables(
      'phpmyadmin_password': passwords['root_password']
    )
    source 'phpmyadmin.config.inc.php.erb'
    action :create
end

9 — Now we must generate a template file with our custom configuration of phpmyadmin.

Inside your cookbook lamp generate a template file named phpmyadmin.config.inc.php:
chef generate template phpmyadmin.config.inc.php
Now in your cookbooks/lamp/templates folder you find a file named phpmyadmin.config.inc.php.rb
In phpmyadmin.config.inc.php.rb add this code:
<?php
/**
 * Debian local configuration file
 *
 * This file overrides the settings made by phpMyAdmin interactive setup
 * utility.
 *
 * For example configuration see
 *   /usr/share/doc/phpmyadmin/examples/config.sample.inc.php
 * or
 *   /usr/share/doc/phpmyadmin/examples/config.manyhosts.inc.php
 *
 * NOTE: do not add security sensitive data to this file (like passwords)
 * unless you really know what you're doing. If you do, any user that can
 * run PHP or CGI on your webserver will be able to read them. If you still
 * want to do this, make sure to properly secure the access to this file
 * (also on the filesystem level).
 */
if (!function_exists('check_file_access')) {
    function check_file_access($path)
    {
        if (is_readable($path)) {
            return true;
        } else {
            error_log(
                'phpmyadmin: Failed to load ' . $path
                . ' Check group www-data has read access and open_basedir restrictions.'
            );
            return false;
        }
    }
}
// Load secret generated on postinst
if (check_file_access('/var/lib/phpmyadmin/blowfish_secret.inc.php')) {
    require('/var/lib/phpmyadmin/blowfish_secret.inc.php');
}
// Load autoconf local config
if (check_file_access('/var/lib/phpmyadmin/config.inc.php')) {
    require('/var/lib/phpmyadmin/config.inc.php');
}
/**
 * Server(s) configuration
 */
$i = 0;
// The $cfg['Servers'] array starts with $cfg['Servers'][1].  Do not use $cfg['Servers'][0].
// You can disable a server config entry by setting host to ''.
$i++;
/**
 * Read configuration from dbconfig-common
 * You can regenerate it using: dpkg-reconfigure -plow phpmyadmin
 */
if (check_file_access('/etc/phpmyadmin/config-db.php')) {
    require('/etc/phpmyadmin/config-db.php');
}
/* Authentication type */
//$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = '0.0.0.0';
//$cfg['Servers'][$i]['connect_type'] = 'tcp';
//$cfg['Servers'][$i]['compress'] = false;
/* Select mysqli if your server has it */
$cfg['Servers'][$i]['extension'] = 'mysql';
/* Optional: User for advanced features */
$cfg['Servers'][$i]['controluser'] = 'root';
$cfg['Servers'][$i]['controlpass'] = '<%= @phpmyadmin_password %>';
/* Storage database and tables */
$cfg['Servers'][$i]['bookmarktable'] = 'pma__bookmark';
$cfg['Servers'][$i]['relation'] = 'pma__relation';
$cfg['Servers'][$i]['table_info'] = 'pma__table_info';
$cfg['Servers'][$i]['table_coords'] = 'pma__table_coords';
$cfg['Servers'][$i]['pdf_pages'] = 'pma__pdf_pages';
$cfg['Servers'][$i]['column_info'] = 'pma__column_info';
$cfg['Servers'][$i]['history'] = 'pma__history';
$cfg['Servers'][$i]['table_uiprefs'] = 'pma__table_uiprefs';
$cfg['Servers'][$i]['tracking'] = 'pma__tracking';
$cfg['Servers'][$i]['designer_coords'] = 'pma__designer_coords';
$cfg['Servers'][$i]['userconfig'] = 'pma__userconfig';
$cfg['Servers'][$i]['recent'] = 'pma__recent';
$cfg['Servers'][$i]['favorite'] = 'pma__favorite';
$cfg['Servers'][$i]['users'] = 'pma__users';
$cfg['Servers'][$i]['usergroups'] = 'pma__usergroups';
$cfg['Servers'][$i]['navigationhiding'] = 'pma__navigationhiding';
$cfg['Servers'][$i]['savedsearches'] = 'pma__savedsearches';
/* Uncomment the following to enable logging in to passwordless accounts,
 * after taking note of the associated security risks. */
// $cfg['Servers'][$i]['AllowNoPassword'] = TRUE;
/*
 * End of servers configuration
 */
/*
 * Directories for saving/loading files from server
 */
$cfg['UploadDir'] = '';
$cfg['SaveDir'] = '';
/* Support additional configurations */
foreach (glob('/etc/phpmyadmin/conf.d/*.php') as $filename)
{
    include($filename);
}

10 — test

Associate a node (in my case an UBUNTU vagrant vps) to your chef server
knife bootstrap 127.0.0.1 --ssh-port 2222 --ssh-user ubuntu --sudo --identity-file /my/path/chefworkspace/devops-config/chef-repo/.vagrant/machines/default/virtualbox/private_key -N vpsdev
Associate the recipes to your run list:
knife node run_list add myvps "recipe[lamp::default],recipe[lamp::apache],recipe[lamp::mysql],recipe[lamp::php],recipe[lamp::phpmyadmin]"
Inside your VPS run sudo chef-client
sudo chef-client
Check on your ip if run: http://my-ip/phpmyadmin
Source Code:
├── cookbooks
│ ├── chefignore
│ ├── lamp
│ │ ├── Berksfile
│ │ ├── Berksfile.lock
│ │ ├── chefignore
│ │ ├── metadata.rb
│ │ ├── README.md
│ │ ├── recipes
│ │ │ ├── apache.rb
│ │ │ ├── default.rb
│ │ │ ├── mysql.rb
│ │ │ ├── phpmyadmin.rb
│ │ │ └── php.rb
│ │ ├── spec
│ │ │ ├── spec_helper.rb
│ │ │ └── unit
│ │ │ └── recipes
│ │ │ ├── apache_spec.rb
│ │ │ ├── default_spec.rb
│ │ │ ├── mysql_spec.rb
│ │ │ ├── phpmyadmin_spec.rb
│ │ │ └── php_spec.rb
│ │ ├── templates
│ │ │ ├── default
│ │ │ └── phpmyadmin.config.inc.php.erb
│ │ ├── test
│ │ │ └── smoke
│ │ │ └── default
│ │ │ ├── apache_test.rb
│ │ │ ├── default_test.rb
│ │ │ ├── mysql_test.rb
│ │ │ ├── phpmyadmin_test.rb
│ │ │ └── php_test.rb
│ │ └── ubuntu-xenial-16.04-cloudimg-console.log
│ └── starter
│ ├── attributes
│ │ └── default.rb
│ ├── files
│ │ └── default
│ │ └── sample.txt
│ ├── metadata.rb
│ ├── recipes
│ │ └── default.rb
│ └── templates
│ └── default
│ └── sample.erb
├── data_bags
│ └── passwords
│ └── mysql.json
├── README.md
├── roles
│ └── starter.rb

martedì 10 aprile 2018

How to install a Jenkins Docker image with Chef

Inside the cookbooks folder in our chef-repo (read here for more info about chef-repo and here for open a free account on chef-server) we must create a cookbook named mydocker:
chef generate cookbook mydocker
in /mydocker folder we edit metadata.rb file and insert this:
depends 'docker', '~> 2.0'
edit default.rb in /mydocker/recipe/ and add this:
## install and start docker
docker_service 'default' do
  action [:create, :start]
end
## create a folder for volumes docker jenkins
execute "run a script" do
  user "root"
  command <<-EOH
  mkdir -p /home/jenkins_home/secrets/ /
  chmod -R 777 /home/jenkins_home/
  EOH
end
## pull the jenkins image
docker_image 'jenkins' do
    tag 'latest'
    action :pull
end

## run container
docker_container 'my_jenkins' do
    repo 'jenkins'
    tag 'latest'
    port '50000:50000'
    port '8081:8080'
    volumes "/home/jenkins_home/secrets/:/var/jenkins_home/secrets/"
end
with the terminal, inside the mydocker folder upload the cookbook on your chef-server:
berks install
berks upload
create a node relative to your machine (vps, vagrant etc…), for more info about this step read here:
knife bootstrap 127.0.0.1 --ssh-port 2200 --ssh-user myuser --sudo --identity-file /my/path/private_key -N nameofYourMachine
now add the mydocker cookbook to your node:
knife node run_list add nameOfYourNode "recipe[mydocker]"
From terminal, connected in ssh at your machine, launch chef-client command:
sudo chef-client
Now if we connect to the ip of the machine, at the port :8081 we shoud must see Jenkins run and ask us to insert the password:
we can find the password in our docker volumes; from terminal launch this command:
less /home/jenkins_home/secrets/initialAdminPassword
and we can see the password for jenkins installation:
Good Luck!

lunedì 9 aprile 2018

cooking Docker Nginx with Chef Server


from pexels.com
In the last couple of weeks, I started to study Chef; install Docker with Chef was one of my first steps into the wide world of server automation.
From your chef-server you must download your chef-repo.
Go inside the chef-repo folder and generate a cookbook named mydocker:
generate cookbook cookbooks/mydocker
edit the file metadata.rb in cookbooks/mydocker/ and add this:
depends 'docker', '~> 2.0'
in cookbooks/mydocker/recipes edit the file default.rb and add this:
docker_service 'default' do
  action [:create, :start]
end


# Pull latest image
docker_image 'nginx' do
  tag 'latest'
  action :pull
end


# Run container exposing ports
docker_container 'my_nginx' do
  repo 'nginx'
  tag 'latest'
  port '80:80'
  volumes "/home/docker/default.conf:/etc/nginx/conf.d/default.conf:ro"
  volumes "/home/docker/html:/usr/share/nginx/html"
end

# create file default.conf for volumes doccker
template "/home/docker/default.conf" do
  source "default.conf.erb"
  #notifies :reload, "service[default]"
end

# create file index.html for volumes docker
template '/home/docker/html/index.html' do
  source 'index.html.erb'
  variables(
    :ambiente => node.chef_environment
  )
  action :create
  #notifies :restart, 'service[httpd]', :immediately
end
Now we must create two template named index.html.erb and default.conf.erb
chef generate template index.html
chef generate template default.conf
edit index.html.erb in /cookbooks/mydocker/templates
<html>
  <body>
    <h1>Hello, World!</h1>
    <h3>HOSTNAME: <%= node['hostname'] %></h3>
    <h3>IPADDRESS: <%= node['ipaddress'] %></h3>
    <p><hr></p>
    <h3>ENVIRONMENT: <%= @ambiente %></h3>
</body>
</html>
edit default.conf in /cookbooks/mydocker/templates:
server {
    listen       80;
    server_name  localhost;
    #ssl
    # ssl    on;
    #ssl_certificate    /etc/nginx/ssl/nginx.crt;
    #ssl_certificate_key    /etc/nginx/ssl/nginx.key;
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
    }
link the cookbook to the node (your machine…)
knife node run_list add docker1 "recipe[mydocker]"
Now from inside the mydocker (/cookbooks/mydocker) folder we can upload the new cookbook on the chef-server:
berks install
berks upload
Now, you can go on your server, and launch chef-client for update your machine with the new configuration.
sudo chef-client
Good Luck!

Install Elasticsearch on Ubuntu

ELK STACK install Java sudo apt-get install default-jdk Elasticsearch 6 wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch...