Ask Your Question
1

How to use two files into same directory?

asked 2014-08-17 10:04:03 -0500

Cesar Sturion gravatar image

Hi people,

I'm trying to install a java/tomcat module and I use /usr/local/ with directory base. But when execute puppet agent -t in puppet client server is generating the error below:

Error: Failed to apply catalog: Cannot alias File[tomcat_path] to ["/usr/local"] at /etc/puppet/environments/production/modules/tomcat/manifests/init.pp:124; resource ["File", "/usr/local"] already declared at /etc/puppet/environments/production/modules/tomcat/manifests/init.pp:76

Below the file init.pp:

#  Class: tomcat
#
#  Install and configure tomcat with java  
#
#  Parameters
#
#  [*$source_path*]
#  The is the directory from  where the tomcat module will get its setup files and configurations
# 
#  [*$java_version*]
#  The parameter checks version of java i.e java 1.7  or java 1.8 
# 
#  [*$java_dir*]
#  The is the base directory on which java is deployed.
# 
#  [*$platform*]
#  This parameter takes input from the user to check machine platform i.e x86 bit or x64 bit.
# 
#  [*$env_path*]
#  The is the parameter which sets java path.
# 
#  [*$tomcat_version*]
#  Specify tomcat version which you want to install  
# 
#  [*$tomcat_path*]
#  Specify tomcat path 
# 
#  [*$javaDownloadURI*]
#  Specify java download url  
#  
#  Authors
#
#  Boxupp Team <http://boxupp.com/>
#
#  Copyright
#  Copyright © 2014-2015 Paxcel Technologies (p) Ltd
#  site : http://paxcel.net/   http://boxupp.com/

 class tomcat ( #Define java_version here 
        $java_version   = '7u65',
            #Define java_dir here 
        $java_dir       = '/usr/local',
        #Define OS platform here
        $platform       = 'x64',
        # Define your environment variable diectory path here.
        $env_path       = '/root/.bash_profile',
        # Define  tomcat diectory path here.
        $tomcat_path    = '/usr/local/',
        # Define  tomcat version.
        $tomcat_version = 'tomcat-7', ) {

  # Setting default exec path for this module
  #Exec { path => [ "/bin/", "/sbin/" , "/usr/bin/", "/usr/sbin/", ] }

  case $platform {
    'x64': { $plat_filename = 'x64'  }
    'x86': { $plat_filename = 'i586' }
    default: { fail("Unsupported platform: ${platform}.  Implement me?") }
  }

  case $java_version {
    '8u11':  { $java_home = "${java_dir}/jdk1.8.0_11"   }
    '7u65':  { $java_home = "${java_dir}/jdk1.7.0_65"   }
    default: { fail("Unsupported java_version: ${java_version}.  Implement me?")    }
  }

  $java_file_name = "jdk-${java_version}-linux-${plat_filename}.tar.gz"
  notify {'Install ${java_file_name}':}

  file { 'java_dir':
      path => $java_dir,
      mode => "0755",
      owner => "root",
      group => "root",
      source  => "puppet:///modules/tomcat/${plat_filename}/${java_file_name}",
  }

  if ( $java_version in [ '7u65', '8u11' ] ) {
    exec { 'extract_jdk':
           cwd     => "${java_dir}/",
           command => "tar -xzf ${java_file_name}",
           require => File["${java_dir}"],
           unless => "test -d ${java_dir}/${java_file_name}",
    }
  }

  notify {'Extract JDK Complete':}

  if ( $java_version in [ '7u65', '8u11' ] ) {
    exec { "set_java_home":
      command => "echo 'export JAVA_HOME=${java_home}'>> ${env_path}",
      unless  => "grep 'JAVA_HOME=${java_home}' ${env_path}",
      require => Exec["extract_jdk"],
    }
  }

  exec { "java_path":
    command => "echo 'export PATH=\$JAVA_HOME/bin:\$PATH' >> ${env_path}",
    unless  => "grep 'export PATH=\$JAVA_HOME/bin:\$PATH' ${env_path}",
    require => Exec["set_java_home"],
  }

  exec { "set_env":
    command => "bash -c 'source ${env_path}'",
    require => Exec["java_path"];
  }

  case $tomcat_version { 
    'tomcat-7':  { notify {'Install apache-tomcat-7.0.54.tar.gz':} }
    'tomcat-8':  { notify {'Install apache-tomcat-8.0.9.tar.gz':} }
    default:     { notify {'Install apache-tomcat-7.0.53.tar.gz':} }
  }

  case $tomcat_version {
    'tomcat-7':  { $tomcat_name = 'apache-tomcat-7.0.54.tar.gz' }
    'tomcat-8':  { $tomcat_name = 'apache-tomcat-8.0.9.tar.gz'  }
    default:     { $tomcat_name = 'apache-tomcat-7.0.53.tar.gz' }
  }

  file { 'tomcat_path':
      path    => $tomcat_path,
      source  => "puppet:///modules/tomcat/${tomcat_name}",
      require => Exec["set_env"],
  }

  exec { "extract_tomcat":
    command => "tar xzf $tomcat_file -C ${tomcat_path}",
    require => File["${tomcat_path}"],
  } 

  service { "iptables":
    enable => true,
    ensure => stopped,
    require => Exec["extract_tomcat"],
  }

  case $tomcat_version {
    'tomcat-7':  { $tomcat_service_name = 'tomcat7' }
    'tomcat-8':  { $tomcat_service_name = 'tomcat8'  }
    default:     { $tomcat_service_name = 'tomcat7' }
  }

  file { 'tomcat_service_path':
      path => "/etc/init.d/${tomcat_service_name}",
      mode => "0755",
      owner => "root",
      group => "root",
      source ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2014-08-18 04:14:22 -0500

Gary Steven gravatar image

updated 2014-08-18 12:08:43 -0500

ramindk gravatar image

You can't re-declare or duplicate a resource. In this case you have 2 file resources of /usr/local as you are setting your java_home and tomcat_home to the same location. In essence this should be a install_dir or install_base as you are not managing /usr/local and are really creating something like /usr/local/tomcat.

It looks like you are copying the distributions over and extracting to /usr/local. As this is your base directory you could do something like this.

class tomcat (
  $ensure => present,
  $exec_path      = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin",
  $install_dir    = '/usr/local',
  $java_version   = '7u65',
  $java_home      = '/usr/local/java',
  $source_dir     = '/usr/src',
  $tomcat_home    = '/usr/local/tomcat',
  $tomcat_version = '7.0.54', )
) {

  # Copy Tomcat tarball to hosts
  file { 'tomcat_tarball':
    ensure => $ensure,
    path   => "${source_dir}/apache-tomcat-${tomcat_version}.tar.gz",
    source => 'puppet:///modules/tomcat/apache-tomcat-${tomcat_version}.tar.gz'
  }

  # Copy java tarball to host
  file { 'java_tarball':
     ensure  => $ensure,
     path    => "${source_dir}/jdk-${java_version}-linux-${
     source  => "puppet:///modules/tomcat/jdk-${java_version}-linux-x64.tar.gz"
  }

  # Extract Tomcat tarball on host 1x only
  exec { 'extract tomcat tarball':
    cwd     => $install_dir,
    command => "tar zxf ${source_dir}/apache-tomcat-${tomcat_version}.tar.gz",
    creates => $tomcat_home,
    path    => $exec_path,
    require => File['tomcat_tarball']
  }

  # Extract Java tarball on host 1x only
  exec { 'extract java tarball':
    cwd     => $install_dir,
    command => "tar zxf ${source_dir}/jdk-${java_version}-linux-x64.tar.gz",
    creates => $java_home,
    path    => $exec_path,
    require => File['java_tarball']
  }

}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-08-17 10:04:03 -0500

Seen: 1,744 times

Last updated: Aug 18 '14