I'm writing a Puppet module to enforce a configuration and restart the service if needed. I'd like to do something like that:
class app_cfg {
$app_files = ['file1', 'file2', 'file3']
$app_files.each |String $file| {
exec {"sed -i 's/bar/foo/g' $file":
path => ['/bin'],
onlyif => 'grep "bar" $file 2>/dev/null',
cwd => '/opt/app/config/',
}
} ~>
service { 'app':
ensure => running,
enable => true,
}
}
I could add "notify" inside the exec but I don't want to restart my service for each file, and it looks like the notify chaining arrow doesn't work after an each loop. Do you guys have any idea how to make it work?
Thanks a lot :)
I found a workaround by inserting a for loop inside the exec resource instead of using each:
class app_cfg {
service { 'app':
ensure => running,
enable => true,
}
exec {"for file in file1 file2 file3; do sed -i 's/bar/foo/g' \$file; done":
path => ['/bin'],
onlyif => 'grep "bar" $file 2>/dev/null',
cwd => '/opt/app/config/',
provider => shell,
notify => Service['app']
}
}
I'm still pretty curious about the first solution! Did you ever manage to do something like that?
Thanks!
I could add "notify" inside the exec but I don't want to restart my
service for each file, and it looks like the notify chaining arrow
doesn't work after an each loop.
It is important to understand that the code in a Puppet manifest describes only how to build a catalog that in turn describes the desired configuration of your server. The catalog lists the classes and resources declared, their parameters and properties, and the relationships among them. The logic within a manifest does not map to actions performed when applying the resulting catalog; it is reflected only in which classes and resources are catalogued, and what their parameters, properties, and relationships are.
Thus, when you use one of the chaining arrows in a manifest, you are instructing the catalog builder to record a relationship between the two resources in the catalog, not an explicit instruction to apply or refresh a resource. No declared resource will be applied at more than once or refreshed more times than it is applied. It will be perfectly fine, therefore, to put an appropriate chain expression inside your loop, but what you cannot do is put the Service declaration itself inside the loop, because that would produce multiple declarations of it. So you might do any of these:
Declare the service (once), and use a resource reference to it in your chain expression inside the loop:
class app_cfg {
service { 'app':
ensure => running,
enable => true,
}
['file1', 'file2', 'file3'].each |String $file| {
exec {"sed -i 's/bar/foo/g' $file":
path => ['/bin'],
onlyif => 'grep "bar" $file 2>/dev/null',
cwd => '/opt/app/config/',
} ~> Service['App']
}
}
As above, but use the appropriate resource metaparameter instead of a chain expression:
class app_cfg {
service { 'app':
ensure => running,
enable => true,
}
['file1', 'file2', 'file3'].each |String $file| {
exec {"sed -i 's/bar/foo/g' $file":
path => ['/bin'],
onlyif => 'grep "bar" $file 2>/dev/null',
cwd => '/opt/app/config/',
notify => Service['app'],
}
}
}
Or declare the relationships with the help of a collector:
class app_cfg {
['file1', 'file2', 'file3'].each |String $file| {
exec {"sed -i 's/bar/foo/g' $file":
path => ['/bin'],
onlyif => 'grep "bar" $file 2>/dev/null',
cwd => '/opt/app/config/',
tag => 'app_notifier',
}
}
Exec<|tag == 'app_notifier|>
~> service { 'app':
ensure => running,
enable => true,
}
}
Related
In a particular project, I have a lot of Puppet exec resources with pipes. This seems to work just fine.
exec { 'foobar':
command => 'foo | bar',
}
However, there are occasions where foo fails. The default behavior is to report the exit code only for the last command in the pipeline. I can fix this manually.
exec { 'foobar':
command => 'set -o pipefail; foo | bar',
provider => shell,
}
But, I want to make sure this happens in all these cases automatically. I'd like to avoid manual find/replace and auditing all uses of exec.
Am I missing some useful attribute?
Is there a wrapper I can use?
Am I, unfortunately, looking at a custom resource?
Am I missing some useful attribute?
No, Exec has no attribute that would automatically prepend additional code to the command.
Is there a wrapper I can use?
I'm not confident I understand what you mean by a "wrapper", but do see below.
Am I, unfortunately, looking at a custom resource?
If you're asking whether you need to implement a native custom type, then surely not. You can undoubtedly address this problem with a (DSL-level) defined type, though you will need to adjust all your Exec declarations to be declarations of the defined type instead. This may be what you mean by a "wrapper" -- I'm fairly sure there's no existing one for your particular purpose, but it shouldn't be hard to create one:
define mymodule::exec (
command => $title,
creates => 'NOT SET',
cwd => 'NOT SET',
# ... all other regular parameters ...
) {
$real_provider = $provider ? { 'NOT SET' => 'shell', default => $provider }
if $real_provider == 'shell' {
$real_command = "set -o pipefail; $command"
} else {
warning('Non-shell command declared via mymodule::exec')
$real_command = $command
}
exec { $title:
command => $real_command,
provider => $real_provider,
creates => $creates ? { 'NOT SET' => undef, default => $creates },
cwd => $cwd ? { 'NOT SET' => undef, default => $cwd },
# ... all remaining regular parameters ...
}
}
I'm trying to use a Mojolicious one-liner to display a message to users about a service outage. I'd like the message to display regardless of the route. Here's what I have which is slightly modified from what's in the documentation.
perl -Mojo -E 'a("/" => {text => "The service is down for maintenance."})->start' daemon
That works for / but not anything else. I added an asterisk to turn it into a wildcard route.
perl -Mojo -E 'a("/*" => {text => "The service is down for maintenance."})->start' daemon
That matches all routes except for /. Is there a way to match all routes in a single definition?
Yes, you can. Try these examples:
perl -Mojo -E 'app->routes->get( "/" => { text => "start" }); app->routes->get( "/*any" => { text => "any" }); app->start' get /
perl -Mojo -E 'app->routes->get( "/" => { text => "start" }); app->routes->get( "/*any" => { text => "any" }); app->start' get /some_route
Here you define catch all route *any after the specific one /
Documentation
How about:
perl -Mojo -E 'a("/*any" => {text => "The service is down for maintenance."})->start' daemon
I think it works for all urls but '/'.
If you create a named placeholder, with a default value of anything, I believe it does what you want:
perl -Mojo -E 'a("/*x" => { text => "The service is down for maintenance.", x => ''})->start' daemon
May not be the prettiest code you'll ever see, but it's only a few more characters :-)
i'm experimenting with elasticsearch within mojolicious.
I'm reasonably new at both.
I wanted to create a helper to store the ES connection and I was hoping to pass the helper configuration relating to ES (for example the node info, trace_on file etc).
If I write the following very simple helper, it works;
has elasticsearch => sub {
return Search::Elasticsearch->new( nodes => '192.168.56.21:9200', trace_to => ['File','/tmp/elasticsearch.log'] );
};
and then in startup
$self->helper(es => sub { $self->app->elasticsearch() });
however if I try to extend that to take config - like the following -
it fails. I get an error "cannot find index on package" when the application calls $self->es->index
has elasticsearch => sub {
my $config = shift;
my $params->{nodes} = '192.168.56.21:' . $config->{port};
$params->{trace_to} = $config->{trace_to} if $config->{trace_to};
my $es = Search::Elasticsearch->new( $params );
return $es;
};
and in startup
$self->helper(es => sub { $self->app->elasticsearch($self->config->{es}) });
I assume I'm simply misunderstanding helpers or config or both - can someone enlighten me?
Just fyi, in a separate controller file I use the helper as follows;
$self->es->index(
index => $self->_create_index_name($index),
type => 'crawl_data',
id => $esid,
body => {
content => encode_json $data,
}
);
that works fine if I create the helper using the simple (1st) form above.
I hope this is sufficient info? please let me know if anything else is required?
First of all, has and helper are not the same. has is a lazily built instance attribute. The only argument to an attribute constructor is the instance. For an app, it would look like:
package MyApp;
has elasticsearch => sub {
my $app = shift;
Search::ElasticSearch->new($app->config->{es});
};
sub startup {
my $app = shift;
...
}
This instance is then persistent for the life of the application after first use. I'm not sure if S::ES has any reconnect-on-drop logic, so you might need to think about it a permanent object is really what you want.
In contrast a helper is just a method, available to the app, all controllers and all templates (in the latter case, as a function). The first argument to a helper is a controller instance, whether the current one or a new one, depending on context. Therefore you need to build your helper like:
has (elasticsearch => sub {
my ($c, $config) = #_;
$config ||= $c->app->config->{es};
Search::ElasticSearch->new($config);
});
This mechanism will build the instance on demand and can accept pass-in arguments, perhaps for optional configuration override as I have shown in that example.
I hope this answers your questions.
I am trying to install postgresql, set up a database and a user, but I am stuck at the setting up a database and use part. What I have so far is:
# Install postgres
class postgres::install {
package { [
'postgresql',
'postgresql-contrib',
]:
ensure => "installed",
}
}
But now how do I use this to create a user, database and grant all permissions on that database to that user?
You can do this a couple of ways, but the easiest way to do it repeatedly is with a define that calls a couple of execs:
Exec {
path => [
'/usr/local/sbin',
'/usr/local/bin',
'/usr/bin',
'/usr/sbin',
'/bin',
'/sbin',
]
}
define postgres::db_setup (
$dbname,
){
exec { "configure_db_${dbname}":
command => "pg command here using ${dbname}; touch success.txt"
creates => "db_${dbname}_success.txt"
}
}
define postgres::user_setup (
$dbuser,
$dbpassword,
$dbname,
){
exec { "configure_user_${dbuser}_on_${dbname}":
command => "pg command here using ${dbuser} on ${dbname} identified by ${dbpassword}; touch usersuccess.txt"
creates => "user_${dbuser}_success.txt"
}
Then when you call the defines:
postgres::db_setup { 'this_new_db':
dbname => 'mynewdbname'
}
postgres::db_user { 'this_new_db_user_on_dbname':
dbuser => 'mynewdbuser',
dbpassword => 'blahblah',
dbname => 'mynewdbname',
require => Postgres::Db_setup['this_new_db'],
}
This is a very dirty way to accomplish what you want by using dummy files to register when an exec has been completed. In my MySQL environment I use execs on scripts created with erb templates to allow more refined error checking.
I am trying to understand Puppet parameterized classes. I have a parameterized class defined this way:
class defaults(
$no_samba = 'FALSE'
)
{
if ($no_samba =~ /TRUE/) {
notify { "will not install samba": } ;
} else {
# install samba here
}
# More server install tasks here...
}
Furthermore, I define a basenode as follows:
node basenode
{
class {'defaults':
no_samba => 'FALSE',
}
}
and then I instantiate a server:
node myserver1 inherits basenode {
Class['defaults'] { no_samba => 'TRUE' }
}
However, this does not work. The myserver1 node does not show the notify message indicating that samba will not be installed.
Here's my non-typo answer - I think you're running into http://projects.puppetlabs.com/issues/7890
Here's a code sample where I tweaked your code to get the effect you're looking for, based on the rewritten example in the ticket:
class defaults(
$no_samba = 'FALSE'
)
{
notify {"no_samba_hack" :
message => "$no_samba";
}
if ($no_samba =~ /TRUE/) {
notify { "will not install samba": }
} else {
# install samba here
}
# More server install tasks here...
}
class basenode($no_samba="FALSE") {
class {defaults: no_samba => $no_samba}
}
node yourserver {
class { 'basenode' : no_samba => 'TRUE'}
}
When I run that with 'puppet apply sample.pp' with puppet 2.7.11 on Ubuntu 12.04, I get the following output:
notice: will not install samba
notice: /Stage[main]/Defaults/Notify[will not install samba]/message: defined 'message' as 'will not install samba'
notice: TRUE
notice: /Stage[main]/Defaults/Notify[no_samba_hack]/message: defined 'message' as 'TRUE'
notice: Finished catalog run in 0.05 seconds
Was samba installed on myserver1, and/or did any of the other server install tasks get triggered ? If only the notify message wasn't printed, then it may really be a problem with notify type versus the notice function.
Notify should look like "notify{"i have curly brackets and a trailing colon":}
Notice is called like a function: notice("i use parenthesis")
Try changing 'notify' to 'notice' and see if it works. You may also want to check the puppet syntax with 'puppet parser validate default.pp' (assuming your default class is in default.pp)
I believe it has to do with scope. It looks like you're creating the 'default' class in the base node and then setting a resource default for the 'default' class after the fact in something that inherits that basenode.
http://docs.puppetlabs.com/guides/language_guide.html
"Defaults are not global — they only affect the current scope and scopes below the current one."
Here is simple example:
class apache-setup {
class { 'apache':
mpm_module => 'prefork',
}
}
include apache-setup
Or:
class { '::mysql::server':
config_file => '/etc/my.cnf',
root_password => 'root', # Sets MySQL root password.
override_options => {
'mysqld' => {
'max_connections' => '512',
'max_allowed_packet' => '256M',
'log' => 'ON',
'log_slow_queries' => 'ON',
'general_log' => 'ON',
'wait_timeout' => '28800',
}
}
}