mercoledì 2 maggio 2018

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 | sudo apt-key add -
sudo apt-get install apt-transport-https
echo "deb https://artifacts.elastic.co/packages/6.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-6.x.list
sudo apt-get update && sudo apt-get install elasticsearch
edit the elasticsearch.yml file:
sudo vi /etc/elasticsearch/elasticsearch.yml
Change network.host to 0.0.0.0
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable elasticsearch.service
sudo /bin/systemctl start elasticsearch.service
so, now Elasticsearch should be running!
Check if it running at this address: http://127.0.0.1:9200/
http://127.0.0.1:9200/
At this address you should see something as this:
{
  "name" : "ktJu_0M",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "-zyJ1svGQu-nU5iKa23Z2w",
  "version" : {
    "number" : "6.2.4",
    "build_hash" : "ccec39f",
    "build_date" : "2018-04-12T20:37:28.497551Z",
    "build_snapshot" : false,
    "lucene_version" : "7.2.1",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

setting SSH connection to Ubuntu on Virtualbox

How to setting an ssh connection on a Virtualbox Ubuntu.
Create an Ubuntu Virtualbox
Create a new Virtual Machine and check Create a new virtual disk
Name your virtualmachine, select Linux as OS type and Ubuntu (64 bit) as linux version.
Set the virtual RAM at half of the your real RAM, in my case I have 8gb on my laptop, so i give to virtual machine 4gb
Create a new virtual disk:
Select the source for install your Ubuntu OS, select VDI (VirtualBox Disk Image)
Select Dynamic allocation:
Set the size of your virtual Hard Disk and click on create:
Start your Machine:
Select the iso image of ubuntu (You must download it from ubuntu official web site).
Install Ubuntu following the installation wizard:
After have installed Ubuntu, login your virtualmachine ubuntu with username and password inserted during the installation:
Install openssh-server
sudo apt-get update
sudo apt-get install openssh-server
stop your virtualmachine.
Set the net of your virtualmachine:
click on settings & net:
Select NAT as connection and click on advanced, and set as in the picture below.
Click on port forwarding:
and set the SSH parameter:
Name: SSH
Protocol: TCP
Host IP: 127.0.0.1
Host Port: 2222
IP Guest: Empty
Port Guest: 22
Start your Virtual Machine:
When your VM is started, open your terminal and try to connect:
ssh yourusername@127.0.0.1 -p 2222
now you shoud be inside your virtualmachine.

Problems and Possible Solutions:

If you can’t connect try to disable or change settings to ubuntu firewall:
sudo ufw disable
or try to connect to the ip of VM:
click on global tools:
Click on create:
Back to Settings > Network and select adapter 1
Select Adapter only Host.
go inside the VM and get the ip number with ifconfig and get ip address:
ifconfig
In my case the ip is 192.168.56.101
now from terminal I try to connect in ssh to the ip 192.168.56.101
ssh yourusername@192.168.56.101

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

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