Sharing code between puppet providers

So you’ve written that custom puppet type for something and start working on another puppet type in the same module. What if you needed to share some code between this types? Is there a way of code-reuse that works with the plugin sync mechanism?

Yes, there is.

Puppet even has two possible ways of sharing code between types.

Option #1: a shared base provider

A provider in puppet is basically a class associated with a certain (puppet) type and there can be a lot providers for a single type (just look at the package provider!). It seems quiet natural, that it’s possible to define a parent class for those providers. So natural, that even the official puppet documentation writes about it.

Option #2: Shared libraries

The second option is a shared library, shipped in a certain namespace in the lib directory of the module, whereas the idea is mostly sketched in the feature ticket #14149. Basically one defines a class in the special Puppetx namespace, using the author- and module-name in the class name, in order to avoid conflicts with other modules.

This example would be saved to

lib/<author>/<modulename>

in your module’s folder and be included in your provider with something along the following:

Compatibility with Puppet 4:

In puppet 4 the name of the namespace has changed slightly. It’s now called ‚PuppetX‘ instead of ‚Puppetx‘ and is stored in a file ‚puppet_x.rb‘, which means that the require and the module name itself need to be changed:

For backward compatibility with puppet 3 you could instead add something like this, according to my co-worker mxey, who knows way more about ruby then I do:

Apart from this you’d need to change the require to be conditional on the puppet-version and refer to the module by the aliased version (which is left as an exercise for the reader ;))

PHP and big numbers

One would expect, that one of the most used script languages of the world would be ableto do proper comparisons of numbers, even big numbers, right?Well, PHP is not such a language, at least not on 32bit systems.Given a script like this:

<?

$t1 = „1244431010010381771“;

$t2 = „1244431010010381772“;

if ($t1 == $t2) {

    print „equaln“;

}

?>

A current PHP version will output:

schoenfeld@homer ~ % php5 test.php
equal

It will do the right thing on 64bit systems (not claiming that the numbers are equal).Interesting enough: An equal-type-equality check (see my article from a few years ago) will not tell that the two numbers are equal.

Things that make you a good programmer

If you ever wondered if you are a good programmer (not), you might think about the following points:

1. Repeat yourself. How else would you keep yourself busy, if your customer has new requirements?

2. Re-Using code is for people who take the other side of the street if a big dog walks along. No risk, no fun. How else would you find out that the common idiom you use is really the way the job has to be done?

3. If you have a coding convention (e.g. how code has to be indented): Just ignore it. Its a good thing to have editors go crazy, when trying to automatically detect the indenting of a source file. By always confusing the editor you keep up the fun of the people, who try to change your code. It would be too boring for them, to simply edit the file, without the quiz which quoting applies to your code. Extra points for those who additionaly (to mixing tabs, spaces, 4 and 8 space, expandtab and no expandtab) write a vim modeline into their file that – by guarantee – does not match the indenting of the file.

4. If you can complicate things: Do it. Numeric indices in arrays can be exchanged by arbitrary strings, this makes your code more interesting. Especially if you have to move elements in that array. And complicated codes makes people, who don’t know it, think that you are a good programmer. One step nearer to your goal, isn’t it?

5. When working with different classes invent a system that auto-loads the classes you need. Don’t document it, that would be a risk for your job. Its not neccessary anyway, because good programmers like the riddle and finding out what gets called, where and why, is a simple but entertaining riddle. After all documenting is a bad idea as well. Especially if you make your system open source, because with a good documentation, it might be to easy for competitors to use your code to make money.

6. If you find ways to do extra function calls: Do it. It gives you the chance to refactor your code, if the customer notices that it is too slow. Great opportunity, hu?

(But, seriously: Don’t hear on me. Its just a cynical way to express my feelings.)

Cool PHP-Code.

Did you know that in PHP you can write something like that:

$test = „foobar“;
$test = sTr_RePlace(„bar“, „baz“, $test);
$x = sPrinTf(„%s is strange.“, $test);

pRint $x . „n“;
eCho „foo“;

What makes me frighten is that this actually used, e.g. by this code snippet which exists (in a similar form) in an unnamed PHP-project:

echo sPrintF(_(„Bla bla bla: %s“), $bla))

And yes, they do use echo to output the result of sprintf.

Update: So i got this great comment. The commentator wants to point out that the senseless use of „echo sprintf“ is because of gettext. He says „That’s simply the way you use gettext.“ But this is simply not true. The difference between printf and sprintf is that the first one outputs the string, while the second one returns it. That means that in the above example printf could be used (instead of sprintf) without a useless echo call in front of it. The reason for using sprintf (and eventually the reason because you find it in a lot of applications using gettext) is that you can use it to fill a variable with the translated string or to use the string in-place. A common use-case for this is to handle a translated string to a template engine, for example.

PHP and the great „===“ operator

Lets suppose you’ve got an array with numeric indices:

[0] => ‚bla‘
[1] => ‚blub‘

Now you want to do something if the element ‚bla‘ is found in that array.
Well, you know that PHP has a function array_search, which returns the key(s) of the found values. Lets say, you write something like this:

if(array_search($array, ‚bla‘)) { do_something }

Would you expect that do_something would actually do something?
If yes: You are wrong.
If no: Great, you’ve understand some parts of PHP – insanitygoodness..

Actually, if ‚bla‘ wouldn’t have the index 0 it would work, because 1, 2, 3, 4, etc. is TRUE. But unfortunately PHP has some sort of implicit casting, which makes 0 behave like a FALSE, depending on the context. So following this, the if() works for all elements except 0.

You might be tempted to write

if(array_search… != FALSE)

But this wouldn’t help you, because 0 would still evaluate to FALSE, leading to if(FALSE != FALSE) which is (hopefully obviously) never true.

A PHP beginner (or even an intermediate, if he never stumbled across this case) might ask:
Whats the solution for this dilemma?

Luckily the PHP documentation is great. It tells you about this. And additionally PHP has got this great operator (===), which causes people to ask „WTF?“ when they hear about it for the first time. Additionaly to comparing the values to each other, they also check the type of the variable. This leads to the wanted result because 0 is an integer, while FALSE is Boolean. So the solution for our problem looks like this:

if (array_search($array, ‚bar‘) !== FALSE) {

Isn’t this great?

To the rescue, git is here!

Consider the following scenario:

You work on a project for a customer that is handled in a Subversion Repository.
The work you get comes in form of some tickets. Tickets may affect different parts
of the project, but they could also affect the same parts of the project. Testing of your work
is done by a different person. For some reasons its wanted that the fix for each ticket
is committed in only one commit in the subversion repository and only after it has been
tested. Commits for two tickets changing the same files should not be mixed.

Now: How do you avoid a mess when working with several patches that possibly affect the same files?

The answer is: git with git-svn. Currently my workflow looks the following:

  1. Create a branch for each ticket
  2. Make my changes for the ticket in this branch
  3. Create a patch from the changes in this branch and supply the tester with it
  4. Wait for feedback and eventually repeat steps 2-4
  5. When testing is finished, run git rebase -i master in the branch, now squash each commit into one commit, build a proper commit message from the template git provides.
  6. Switch to master branch and merge the changes from the branch.
  7. Rebase master against the latest svn (git svn rebase) and dcommit

That workflow works good for me. It gives me the possibility to do micro-commits as much as I want and still only commit a well-defined, well-tested commit into the projects SVN.
Just some minor drawbacks I haven’t yet solved (you know, git /can/ do everything, but that is actually a problem in itself):

  • Its a bit annoying that I need to use git rebase -i and change n-1 lines for a number of n commits, so that they are squashed. It would be handy to say: Squash all commits, that happened in this branch, into one.
  • Creating the diff for the tester requires me to do git log, search for the last commit before my first commit in that branch and use it with git diff to create a patch. I had a quick look at git-rev-parse and felt that is overwhelming complex to do find out how to do it better. To many possibilites. git is complex.

For now I cannot tell how good merging works, as soon as conflicts arise. But I guess git can do well, although there is the possibility that it could get complex again.

Nevertheless, its probably not a credit to git, but instead to DVCSes in common, but anyway. I like it.