Visualizzazione post con etichetta automation. Mostra tutti i post
Visualizzazione post con etichetta automation. Mostra tutti i post

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!

venerdì 30 marzo 2018

A basic NGINX cookbook for CHEF

Ok, this is how create a simple nginx cookbook for Chef.
For this example I don’t use chef-server, but i work directly on the machine. In my case the machine is a Vagrant VM based on Ubuntu/xenial64, but i think that you can use a VPS or Cloud service and others Linux OS.
As first thing to do, we must install ChefDK on our machine:
curl https://omnitruck.chef.io/install.sh | sudo bash -s -- -P chefdk -c stable
Then we have to create a folder called cookbooks where we will go to insert our cookbooks:
mkdir cookbooks
inside the cookbooks folder we create our first cookbook named mynginx:
cd cookbooks
chef generate cookbook mynginx
Edit the file default.rb in in mynginx/recipes/default.rb
package 'git'
package 'tree'

package 'nginx' do
  action :install
end


service 'nginx' do
  action [ :enable, :start ]
end


cookbook_file "/var/www/html/index.html" do
  source "index.html"
  mode "0644"
end
template "/etc/nginx/nginx.conf" do   
  source "nginx.conf.erb"
  notifies :reload, "service[nginx]"
end
from inside the mynginx folder I must generate a file index.html:
chef generate file index.html
edit the file index.html in files/default/index.html
<html>
  <head>
    <title>Hello there</title>
  </head>
  <body>
    <h1>This is a test</h1>
    <p>Please work!</p>
  </body>
</html>
create a templates named nginx.conf:
chef generate template nginx.conf
edit the file nginx.conf.erb in templates/nginx.conf.erb and insert your custom configuration of nginx.
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
 worker_connections 768;
 # multi_accept on;
}
http {
 ##
 # Basic Settings
 ##
 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 keepalive_timeout 65;
 types_hash_max_size 2048;
 # server_tokens off;
 # server_names_hash_bucket_size 64;
 # server_name_in_redirect off;
 include /etc/nginx/mime.types;
 default_type application/octet-stream;
 ##
 # SSL Settings
 ##
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
 ssl_prefer_server_ciphers on;
 ##
 # Logging Settings
 ##
 access_log /var/log/nginx/access.log;
 error_log /var/log/nginx/error.log;
 ##
 # Gzip Settings
 ##
 gzip on;
 gzip_disable "msie6";
 # gzip_vary on;
 # gzip_proxied any;
 # gzip_comp_level 6;
 # gzip_buffers 16 8k;
 # gzip_http_version 1.1;
 # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
 ##
 # Virtual Host Configs
 ##
 include /etc/nginx/conf.d/*.conf;
 include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
# server {
#  listen     localhost:110;
#  protocol   pop3;
#  proxy      on;
# }
# 
# server {
#  listen     localhost:143;
#  protocol   imap;
#  proxy      on;
# }
#}
now you can launch chef-client in local mode for install or update your machine:
sudo chef-client -z --runlist "mynginx"
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...