Why do modules use classes in puppet (instead of defines) - puppet

I've been creating a couple of classes (in different modules) for puppet. Both, separately, require maven. So both classes have something like the following:
class { "maven::maven":
version => "3.0.5"
}
(using the https://forge.puppetlabs.com/maestrodev/maven module from puppet forge)
But, if I have one node that has both of my classes, puppet complains because class 'maven::maven' is declared twice. I feel like each of my classes should be free to declare all of the things it needs. If a node has more than one class both of which require maven, then I don't see the problem.
So, my question is: was the author of that maven module wrong to use a class, should he have used a define instead? (because you can use/call/whatever a define multiple times). It appears that if he had used a define I would be able to have the block of code as many times as I like, so if he was right to use a class, why?
Thanks.

I think the rationale behind this is best explained in John Arundel's Puppet 3 Beginner's Guide:
So if you're wondering which to use, consider:
Will you need to have multiple instances of this on the same node
(for example, a website)? If so, use a definition.
Could this
cause conflicts with other instances of the same thing on this node
(for example, a web server)? If so, use a class.

If you're passing in parameters to a class, there is a possibility of a conflict, the problem being that it is not clear which sets of parameters will get used.
If your first module required maven with 3.0.5 but your second module required maven with 3.0.6, puppet would not know which one to use.
The puppet module, in making it a class and not a resource/defined type, does not handle the resolution as well, because it was probably intended for a single install.
Puppet currently only supports re-using class declarations without parameters, ie. include maven::maven.
Finally, declaring dependencies on other modules in your own module is a tricky thing, that I do not know how to fully resolve yet.

Related

Languages with a NodeJS/CommonJS style module system

I really like the way NodeJS (and it's browser-side counterparts) handle modules:
var $ = require('jquery');
var config = require('./config.json');
module.exports = function(){};
module.exports = {...}
I am actually rather disappointed by the ES2015 'import' spec which is very similar to the majority of languages.
Out of curiosity, I decided to look for other languages which implement or even support a similar export/import style, but to no avail.
Perhaps I'm missing something, or more likely, my Google Foo isn't up to scratch, but it would be really interesting to see which other languages work in a similar way.
Has anyone come across similar systems?
Or maybe someone can even provide reasons that it isn't used all that often.
It is nearly impossible to properly compare these features. One can only compare their implementation in specific languages. I collected my experience mostly with the language Java and nodejs.
I observed these differences:
You can use require for more than just making other modules available to your module. For example, you can use it to parse a JSON file.
You can use require everywhere in your code, while import is only available at the top of a file.
require actually executes the required module (if it was not yet executed), while import has a more declarative nature. This might not be true for all languages, but it is a tendency.
require can load private dependencies from sub directories, while import often uses one global namespace for all the code. Again, this is also not true in general, but merely a tendency.
Responsibilities
As you can see, the require method has multiple responsibilities: declaring module dependencies and reading data. This is better separated with the import approach, since import is supposed to only handle module dependencies. I guess, what you like about being able to use the require method for reading JSON is, that it provides a really easy interface to the programmer. I agree that it is nice to have this kind of easy JSON reading interface, however there is no need to mix it with the module dependency mechanism. There can just be another method, for example readJson(). This would separate the concerns, so the require method would only be needed for declaring module dependencies.
Location in the Code
Now, that we only use require for module dependencies, it is a bad practice to use it anywhere else than at the top of your module. It just makes it hard to see the module dependencies when you use it everywhere in your code. This is why you can use the import statement only on top of your code.
I don't see the point where import creates a global variable. It merely creates a consistent identifier for each dependency, which is limited to the current file. As I said above, I recommend doing the same with the require method by using it only at the top of the file. It really helps to increase the readability of the code.
How it works
Executing code when loading a module can also be a problem, especially in big programs. You might run into a loop where one module transitively requires itself. This can be really hard to resolve. To my knowledge, nodejs handles this situation like so: When A requires B and B requires A and you start by requiring A, then:
the module system remembers that it currently loads A
it executes the code in A
it remembers that is currently loads B
it executes the code in B
it tries to load A, but A is already loading
A is not yet finished loading
it returns the half loaded A to B
B does not expect A to be half loaded
This might be a problem. Now, one can argue that cyclic dependencies should really be avoided and I agree with this. However, cyclic dependencies should only be avoided between separate components of a program. Classes in a component often have cyclic dependencies. Now, the module system can be used for both abstraction layers: Classes and Components. This might be an issue.
Next, the require approach often leads to singleton modules, which cannot be used multiple times in the same program, because they store global state. However, this is not really the fault of the system but the programmers fault how uses the system in the wrong way. Still, my observation is that the require approach misleads especially new programmers to do this.
Dependency Management
The dependency management that underlays the different approaches is indeed an interesting point. For example Java still misses a proper module system in the current version. Again, it is announced for the next version, but who knows whether this will ever become true. Currently, you can only get modules using OSGi, which is far from easy to use.
The dependency management underlaying nodejs is very powerful. However, it is also not perfect. For example non-private dependencies, which are dependencies that are exposed via the modules API, are always a problem. However, this is a common problem for dependency management so it is not limited to nodejs.
Conclusion
I guess both are not that bad, since each is used successfully. However, in my opinion, import has some objective advantages over require, like the separation of responsibilities. It follows that import can be restricted to the top of the code, which means there is only one place to search for module dependencies. Also, import might be a better fit for compiled languages, since these do not need to execute code to load code.

How to stop two orchard modules conflicting

I have two Orchard Modules.
Both have implementations of IAppSettings , which is defined in an external dll, and referenced in the modules via nuget package (So I cannot use IDependency ).
I wire these up using an Autofac Module class in each module.
Unfortunately this leads to "last registration wins" and both modules will use the last registered implementation, even though the "expected" result would be that each uses their own.
To be clear, each module is developed by a separate team, who don't co-ordinate with each other, but do use the same guidelines for module creation. The example above is just one instance of this occurring, but it is fair to assume there would be more.
How might I go about ensuring that each team can register their own dependencies for their modules, without constantly having to check with the authors of other modules?
There is one Autofac container per tenant, not per (Orchard) modules. You see the implications of this.
However this couldn't be much differently since interaction between modules would be seriously hindered if dependencies would be scoped to extensions.
Also one of the points of DI is that you can override the implementation: this is also desired here, since if you implement a dependency in Module A, then also in Module B (where Module B depends on Module A) then Module B can override the default implementation. This is a good thing.
Instead of wanting to require specific implementations for your interfaces what kind of defeats DI you could implement the strategy pattern for example. But if you tell more details I could help more.

Can I easily modify classes in Dart libraries?

I am using the pathfinding library, but I am wondering in general not just for this single situation.
It provides a class Grid which uses a class Node to hold basic informations. The documentations of Node says:
[...] and custom attributes may be added, depending on the algorithms' needs.
Is there a way to define attributes for Node (like steppedOnXTimes)? More specifically I want the to add the attributes to the original Node class or overwrite it, just extending it would not suffice since the Grid would still use the original Node class.
There is no way to do this. The pathfinding library was ported from a JavaScript version and I guess the documentation was not updated to reflect this.
For your situation, the best you can do is probably to extend grid.dart and provide a custom constructor and implementation of _buildNodes that uses your own Node class that extends the provided Node class.
There is no general solution that I am aware of.

Puppet Execution Flow

I have this node.pp and I am wondering how puppet is going to execute it.
node 'agent.puppet.demo' {
include ssh
include postfix
include mysql
include apache
}
On the agent node, when I run this:
$ puppetd -t -d
The puppet is not executing it sequentially meaning, it does not execute ssh first, then postfix, ...
Does anyone know why this is? Is it because it is called 'declarative language' where the order of execution does not really matter?
If this is the case, then I can just in a certain way, declare what I want and puppet will figure out how to execute it?
Disclaimer: I am one of the developers of Puppet.
It will execute it in a consistent but unpredictable order, with the exception of any explicit or implicit dependencies in the code. Explicit dependencies are things that you specify with the subscribe or require metaparameters. Implicit dependencies come from the autorequire feature, which does things like automatically apply file resources in a sensible order.
The reason for this isn't so much that the language is declarative, but rather the language is declarative because order doesn't matter for most things in the underlying problem space.
For example, there really isn't much connection between managing ssh and managing postfix for most people - you could do the work in either order, or even at the same time, and everything would work out the same.
That frees us up to improve things in a whole lot of ways that "everything is in linear order" doesn't. We are working, for example, to batch up package installs while still respecting the explicit dependencies outside packages.
So, the order of execution and dependencies follows the underlying problem, and we have preserved that property to be able to do more awesome things.
The goal is exactly what you say at the end: that you declare what you want, and we take care of all the details of getting it there. In time we hope to be much smarter about logical dependencies, so you have to say even less to get that, too.
Disclaimer: I am still pretty new to puppet :)
The key is to think of everything in terms of dependencies. For class dependencies, I like to use the Class['a'] -> Class['b'] syntax. Say you have a tomcat class that requires a jdk class which downloads/installs the sun jdk from oracle. In your tomcat class, you can specify this with
Class['jdk'] -> Class['tomcat']
Alternatively you can declare a class with a require meta parameter rather than using include.

Groovy script runner architecture

Initial info: I have a groovy app (let's call it Runner) which is capable of running anything implementing certain interface (let's call it Runnable). And I have a pool of Runnables (groovy scripts) which should be visible to this app at the init stage and which app will call (through the interface and passing an object as a param).
Task: What I need is a way to load and call all the Runnables from the Runner.
Requirements: It's tricky, as scripts may not follow certain package structure and can be placed on the same machine as Runner but virtually in any place. They can also be named differently (open discussion for mandatory java alike naming: class name == file name) and can be skipped for now (though if there's gonna be advice on that it's cool!).
NOTES: I imagine it possible through having a config file in which scripts are configured (absolute path is provided) and to load them using this stuff and either cast Object to Runnable interface and trigger what I need or to invokeMethod(...). But have no idea if it can be done easier (there should be a way, cause it looks all too clumsy). I also cant think of a way to handle file naming issue and multiple classes in one file issue.
P.S.: Such long description might cause misunderstanding so please comment on vague parts.
I think you need to know all classes implementing an interface. Find Java classes implementing an interface may be of interest to you.
The option to have a config file in which script's absolute paths are written is good and proved to be a working solution. You'll have to deal with class loading of whatever is not visible in the app class loader. In particular you'll have to deal with annotation based POJO serialization problems. Singleton of Runnable loader is a good practice.

Resources