Ask Your Question
0

Puppet Module - Execute only when package doesn't exist

asked 2016-05-09 16:04:44 -0500

RyboCollier gravatar image

Good Afternoon,

Let me preface this by saying I am new to puppet. With that said, I am in the process of writing a module that will install a piece of software on all of our linux servers. The installation of this software is different than most because it requires a shell script to be ran.

There are several files involved with the installation, they are:

  • RPM
  • Installation script
  • Configuration file

I've written a class that successfully installs the software on the first puppet run. My problem is that puppet will try to install the software on subsequent puppet runs due to the way I've written my class (remember, I am a noob) which results in a failure.

So my question is this "How do I write a class so that it will only execute if a package isn't installed?

Here is my install class:

 class carbonblack::install_carbonblack
{
  exec {'create directory':
   path => "/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/puppetlabs/bin:/root/bin",
   command => "mkdir /tmp/CarbonBlack", 
  }

  # Stage Files to /tmp and set permissions to install
  # Stage RPM
  file {'/tmp/CarbonBlack/CarbonBlackClientSetup-linux-v4.2.6.50413.rpm':
    owner => 'root',
    group => 'root',
    source => "puppet:///modules/carbonblack/CarbonBlackClientSetup-linux-v4.2.6.50413.rpm",
  }

  # Stage Script File
  file {'/tmp/CarbonBlack/CarbonBlackClientSetup-linux-v4.2.6.50413.sh':
    owner => 'root',
    group => 'root',
    mode => '0700',
    source => "puppet:///modules/carbonblack/CarbonBlackClientSetup-linux-v4.2.6.50413.sh",  
  }

  # Stage sensorsettings.ini
  file {'/tmp/CarbonBlack/sensorsettings.ini':
    owner => 'root',
    group => 'root',
    source => "puppet:///modules/carbonblack/sensorsettings.ini",
  }

  # Install Carbon Black by running CarbonBlackClientSetup-linux-v4.2.6.50413.sh
  exec {'install carbonblack':
   command => "/tmp/CarbonBlack/CarbonBlackClientSetup-linux-v4.2.6.50413.sh",
  }

  # After installed cleanup files
  exec {'cleanup':
    path => "/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/puppetlabs/bin:/root/bin",
    cwd => '/tmp',
    command => "rm -rf CarbonBlack",
  }
}

Here is init.pp

class carbonblack
(
  $service_enable  = $carbonblack::params::service_enable,
  $service_ensure  = $carbonblack::params::service_ensure,
  $package_ensure  = $carbonblack::params::package_ensure,
) inherits carbonblack::params
{
  service{'cbdaemon':
    enable  =>  $service_enable,  
    ensure  =>  $service_ensure,
  }
}

Here is my param class

class carbonblack::params
{
  $service_enable   = true
  $service_ensure   = 'running'
  $package_ensure   = 'present'
}
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2016-05-09 18:09:31 -0500

RyboCollier gravatar image

I did some research in the meantime and I found a useful section on the creates attribute. I've edited my puppet code to reflect the following:

# Install Carbon Black by running CarbonBlackClientSetup-linux-v4.2.6.50413.sh
  exec {'install carbonblack':
    command     => "/tmp/CarbonBlack_tmp_puppet/CarbonBlackClientSetup-linux-v4.2.6.50413.sh",
    creates     => "/tmp/CarbonBlack_tmp_puppet/cb_installed_true",
  }

  file {'/tmp/CarbonBlack_tmp_puppet/cb_installed_true':
    ensure => file,
  }

It basically works like this, wafter the CarbonBlackClientSetup-linux-v4.2.6.50413.sh script runs a file named cbinstalledtrue will be created. The script will only run if that file doesn't exist.

It works, but I'm curious if there are more elegant ways to do this. (I hate the idea of leaving temporary files around).

edit flag offensive delete link more

Comments

1

I was going to suggest the creates option, but you found it on your own. Have your script output a log file and use it like a lock file. Best option in my opinion.

UBPClaw gravatar imageUBPClaw ( 2016-05-09 23:50:16 -0500 )edit
1

answered 2016-05-09 22:07:45 -0500

mike.r gravatar image

never worked w carbon black, seems like a complicated install process

I would suggest that instead of doing tons of execs and file stages, tar everything into 1 tar.gz file and then do

file { '/tmp/carbon.tar.gz': 
   source      => "puppet:///carbon.tar.gz",
   checksum => md5,
}

Then, add a Exec default for path so you dont have to repeat it,

Exec {
   path => "/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/puppetlabs/bin:/root/bin",
}

and finally run your untar exec, and install exec, use "unless" statement to make the Execs idempotent, otherwise you run the risk of having different results w each puppet run

exec { 'untar':
  command => "tar -xvf -C /tmp/carbonblack",
  unless => "test -d /tmp/carbonblack",
}

exec { 'install':
   command =>  "/tmp/CarbonBlack/CarbonBlackClientSetup-linux-v4.2.6.50413.sh",
   unless => "rpm -qa | grep CarbonBlackClientSetup-linux-v4.2.6.50413.rpm",
}

that should do it. Rememner you want to avoid Execs as much as possible, Execs are evil and hard to make truly idempotent!

edit flag offensive delete link more

Comments

Thanks for your input Mike. I'll look into implementing some of your suggestions.

RyboCollier gravatar imageRyboCollier ( 2016-05-10 14:47:21 -0500 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2016-05-09 16:04:44 -0500

Seen: 213 times

Last updated: May 09 '16