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

domenica 15 aprile 2018

Dockerize an existing GOLANG web server

in the golang project folder create a file named Dockerfile and a file named docker-compose.yml

Dockerile

FROM golang:latest
LABEL maintainer "Pierangelo Orizio <pierangelo1982@gmail.com>"
# for install go packages RUN go get /path
RUN go get github.com/go-sql-driver/mysql
# Copy the local package files to the container's workspace.
ADD . /go/src/github.com/pierangelo1982/myproject
# build executable
RUN go install github.com/pierangelo1982/myproject
# execute 
ENTRYPOINT /go/bin/myproject
# Document that the service listens on port 8080.
EXPOSE 8080
N.B: in the paths you can substitute pierangelo1982 with your github username.
For add other GO packages add in dockerfile this command (see line 5 in Dockerfile for see an example):
RUN go get github.com/<nameofthepackages>

docker-compose.yml

version: '2'
services:
  app:
    build: .
    volumes:
      - .:/go/src/github.com/pierangelo1982/myproject
    expose:
      - "8080"
    ports:
      - 8080:8080
From the terminal, from inside the project folder launch this commands:
docker-compose build
and
docker-compose up -d
check with the browser if the app run at the address http://0.0.0.0:8080

mercoledì 11 aprile 2018

My Django Docker image


A description, step by step of how I builded my docker django image, how i loaded it on docker hub and how can be use it and customized.

STEP 1 — Create Dockerfile and push it on Docker Hub

In a folder I create a file named Dockerfile and an another named requirements.txt
Dockerfile
FROM python:2.7
LABEL maintainer "Pierangelo Orizio <pierangelo1982@gmail.com>"
RUN apt-get update -qq && apt-get install build-essential g++ flex bison gperf ruby perl \
  mysql-client \ 
  libsqlite3-dev libmysqlclient-dev libfontconfig1-dev libicu-dev libfreetype6 libssl-dev \
  postgresql-client \
  libpng-dev libjpeg-dev python libx11-dev libxext-dev -y
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
COPY . /code
RUN pip install -r requirements.txt
#ADD . /code/
RUN django-admin.py startproject djangoproject
RUN mkdir /code/djangoproject/media && mkdir /code/djangoproject/static
VOLUME /code
WORKDIR /code/djangoproject
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
requirements.txt
Django==1.9.8
django-filer==1.2.5
django-filter==1.0.0
django-image-cropping==1.0.4
django-mptt==0.8.6
from terminal inside the folder, I build the local image:
docker build -t django .
tag the local image:
docker tag django pierangelo1982/django
sign in to your docker account:
docker login
push the image on your Docker Hub account:
docker push pierangelo1982/django
P.S: pierangelo1982 is my docker account username, change it with yours.

STEP 2 — create and implement container

pull the image:
docker pull pierangelo1982/django
For work and implement our project, we have need to share the project folders of the container on our local host. For do that we have need to build a volume and copy the file.
create a volume:
docker volume create --name django-volume
connect the volume to the container for can copy the project folder
docker run --name django-test \
 -v django-volume:/code \
 -p 8000:8000 \
 -d pierangelo1982/django
copy project folders in your host
docker cp django-test:/code ~/my/local/path/myfolder
remove the container
docker rm -f django-test
recreate the container with the volume that point to our local folder where before we have copy the folders of the project.
docker run --name django-test \
 -v ~/my/local/path/myfolder:/code \
 -p 8000:8000 \
 -d pierangelo1982/django

Using django and python commands:

docker exec django-test python manage.py

how install new packages:

docker exec django-pierangelo pip install
Source code:

Docker Hub:

Subscribe my weekly newsletter:

Bye!

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!

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...