Sunday, April 12, 2015

Puppet::java_oracle_centos configuration

#puppet hostname is puppet
#centos7 hostname is ct7
#Goal: installing oracle java 7 from tar.gz to centos7 distro
#Using local web server

root@puppet:/etc/puppet/modules/java/manifests# cat oracle_jdk7.pp
class java::oracle_jdk7 {
  case $::operatingsystem {
    'RedHat', 'CentOS', 'Scientific', 'OracleLinux', 'OEL': {
     $java_install_path="/usr/local/java"
     $jdk_file="jdk-7u75-linux-x64.tar.gz"
     $jdk_dir="jdk1.7.0_75"
     $local_web="http://10.0.0.100"
     # do something CentOS specific
     # execute 'yum update'
     exec { 'yum-update':
       command => '/bin/yum -y update',
     }
     exec { 'wget-install':
       path => ['/bin','/usr/bin'],
       command => '/bin/yum -y install wget',
       unless => 'ls /usr/bin/wget',
       require => Exec['yum-update'],
     }

     exec { 'jdk7-get-untar':
       path => ['/bin','/usr/bin'],
       cwd => '/tmp',
       #when using oracle web site
       #command => "wget --no-cookies --no-check-certificate --header \"Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie\" \"http://download.oracle.com/otn-pub/java/jdk/7u75-b13/jdk-7u75-linux-x64.tar.gz\" ",
       #when using local web
       #command => "wget $local_web/$jdk_file",
       unless => "ls /tmp/$jdk_file",
       timeout => 0,
       require => Exec['wget-install'],
      }

     exec { 'jdk7-install-to-path':
       cwd => '/tmp',
       path => ['/bin','/usr/bin'],
       command => "tar xvzf $jdk_file && mv $jdk_dir $java_install_path",
       unless => "ls $java_install_path/bin",
       require => Exec['jdk7-get-untar'],
     }
     include java::centos_config

   }

   'ubuntu': {
    #On ubuntu use ppa
    class { 'apt': }

    # Add ppa
    apt::ppa { 'ppa:webupd8team/java':} ->
    # Prepare response file
    file { "/tmp/oracle-java7-installer.preseed":
         source => 'puppet:///modules/java/java.response',
         mode => 600,
         backup => false,
     } ->
    # Install java
    package { "oracle-java7-installer":
         ensure => "installed",
         responsefile => '/tmp/oracle-java7-installer.preseed'
    } ->
    package { "oracle-java7-set-default": ensure => "installed" }
  }
 }
}

root@puppet:/etc/puppet/modules/java/manifests# cat centos_config.pp
class java::centos_config {
      file { "/etc/profile":
        ensure => present,
        group => 'root',
        owner => 'root',
        mode => 0644,
        source => "puppet:///modules/java/java.oracle.centos.profile",
      }
}

#profile java path insert
#root@puppet:/etc/puppet/modules/java/manifests# cat ../files/java.oracle.centos.profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`id -u`
        UID=`id -ru`
    fi
    USER="`id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge
export PATH=/usr/local/java/bin:$PATH



root@puppet:/etc/puppet/modules/java/manifests# cat init.pp
class java {
 include java::oracle_jdk7
}

#site.pp
root@puppet:/etc/puppet/modules/java/manifests# cat /etc/puppet/manifests/site.pp
node default { include ssh }

node 'ct7' {
 include java
}

#Test on centos7(ct7) node
[root@ct7 ~]# puppet agent --test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for ct7
Info: Applying configuration version '1428899877'
Notice: /Stage[main]/Java::Oracle_jdk7/Exec[jdk7-install-to-path]/returns: executed successfully
Notice: /Stage[main]/Java::Centos_config/File[/etc/profile]/content:
--- /etc/profile    2015-04-13 04:37:47.095816707 +0000
+++ /tmp/puppet-file20150413-5363-w912dg    2015-04-13 04:38:00.971816547 +0000
@@ -74,3 +74,4 @@

 unset i
 unset -f pathmunge
+export PATH=/usr/local/java/bin:$PATH

Info: Computing checksum on file /etc/profile
Info: FileBucket got a duplicate file {md5}b3fb9b49834801ac9532447053dab36f
Info: /Stage[main]/Java::Centos_config/File[/etc/profile]: Filebucketed /etc/profile to puppet with sum b3fb9b49834801ac9532447053dab36f
Notice: /Stage[main]/Java::Centos_config/File[/etc/profile]/content: content changed '{md5}b3fb9b49834801ac9532447053dab36f' to '{md5}4e76aeb186c5c0f9c841df324f24b877'
Notice: Finished catalog run in 3.80 seconds

No comments:

Post a Comment