chef rewind cookbook_file definition from a wrapper cookbook recipe - overloading

I am using an cookbook github.com opscode-cookbooks/openldap.
I wrote an wrapper cookbook "lab_openldap" that includes "openldap::server" recipe.
The server.rb recipe uses following clausule to upload the PEM file from cookbooks files/ssl/*.pem to server to the location node['openldap']['ssl_cert'].
if node['openldap']['tls_enabled'] && node['openldap']['manage_ssl']
cookbook_file node['openldap']['ssl_cert'] do
source "ssl/#{node['openldap']['server']}.pem"
mode 00644
owner "root"
group "root"
end
end
The PEM is tried to be read from "openldap" cookbook file/ssl/#{node['openldap']['server']}.pem" location.
I have my PEM file in wrapper "lab_openldap" cookbook file/ssl/#{node['openldap']['server']}.pem" location.
Is it possible to modify the "lab_openldap::server.rb" recipe to load PEM from a wrapper cookbook?
Notes:
I am aware of https://github.com/bryanwb/chef-rewind but it does not seem to manage this situation.
Update
The provided answer using r.resource is correct.
Actually the issue in the particular code is on "source" keyword that according to http://docs.opscode.com/resource_cookbook_file.html refers to the location of a file in the /files directory in a cookbook located in the chef-repo.
r = resources("cookbook_file[#{node['openldap']['ssl_cert']}]")
r.cookbook('lab_openldap')
cookbook_file node['openldap']['ssl_cert'] do
source "ssl/#{node['openldap']['server']}.pem"
mode 00644
owner "root"
group "root"
end

You can do this now in chef directly:
include_recipe "openldap::server"
edit_resource(:cookbook_file, node['openldap']['ssl_cert']) do
cookbook cookbook_name
end
Note that to avoid this situation from needing to be used, library cookbooks like openldap should be written as custom resources, rather than as recipes. They should then export properties allowing their templates to be overwritten, using the pattern in this answer:
https://stackoverflow.com/a/63570830/506908

Of course it is! You just need to set the cookbook attribute on the resource when you wrap it. By default, it's "the current cookbook", but you can change it:
r = resources("cookbook_file[#{node['openldap']['ssl_cert']}]")
r.cookbook('my_wrapper_cookbook')
If you look at Bryan's Chef Rewind, you'll see it does the same thing

Related

Can log4j and java util logging coexist

My application uses log4j but OkHttpClient uses java util logging. So apart from log4j.properties, I created a logging.properties file with the following contents:
handlers=java.util.logging.FileHandler
.level=FINE
okhttp3.internal.http2.level=FINE
java.util.logging.FileHandler.pattern = logs/%hjava%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
I then added this to jvm params used for starting the application -Djava.util.logging.config.file="file://${BASE_DIR}/logging.properties"
But I don't see any new folders being created as indicated by the Filehandler. Any one know why?
But I don't see any new folders being created as indicated by the Filehandler. Any one know why?
The FileHandler will not create any new folders. A directory must be created before the FileHandler will create a file.
The system property requires a path to file that is located on the filesystem It will not expand system properties or environment variables by using the dollar sign syntax.
You can use a relative path based off of the working directory or you have to use an absolute path to the logging.properties. The logging properties can not be packaged inside of an archive.
If you want to work around this limitation then you want to create a custom config class and use the java.util.logging.config.class property in conjunction with the java.util.logging.config.file property. You then write a class that reads the file://${BASE_DIR}/logging.properties and performs the needed transformation into a path to a file. Then update the configuration if you are using JDK9 or newer. On older versions you need to use readConfiguration and add code to work work around limitations of the LogManager

WinRT Create File at Customized Path

I am writing some code to create a file from a Windows 8 app in an standard way, the code looks like below:
using namespace Windows::Storage;
StorageFolder^ folder = KnownFolders::DocumentsLibrary;
String ^filename = ref new String(L"file.txt");
auto createFile = folder->CreateFileAsync(filename, CreationCollisionOption::ReplaceExisting);
concurrency::create_task(createFile).wait;
Now instead of using DocumentsLibrary, I want to write thid file to an customized file path, like:
C:\Users\<username>\AppData\Local\ExampleApp\ExampleFolder
How should I change the code to be able to do this? Thanks!
WinRT can only access a few folders. You have a few standard libraries like Pictures, Music, etc (Documents requires elevated rights) and you have the application data folders that you can find under \AppData\Local\Packages\yourpackage.
Inside of this package folder you have two main folders to store data: LocalState and RoamingState. As the names convey: the former is to store data locally while the latter will synchronize its contents whenever possible (according to the rules you define).
You can access these folders using the C++ equivalent of Windows.Storage.ApplicationData.Current.LocalFolder and Windows.Storage.ApplicationData.Current.RoamingFolder.
What you can do though is request explicit access through a FilePicker but this will prompt the user a window where he should target the directory himself.

Vagrant - Chef - templatefile check

how do I get it out to test whether there is a template file.
I want to load the webserver default.conf.erb only if there is no other appropriate config there.
I had thought so, but without success.
template "#{node[:nginx][:dir]}/sites-available/#{host_name}.loc.conf" do
variables attribs
if File.exists? ("/templates/default/#{host_name}.conf.erb")
source "#{host_name}.conf.erb"
else
source "default.conf.erb"
end
notifies :reload, "service[nginx]"
end
Such a concept (under the label of File Specificity) is already implemented in chef.
Just place your file in templates/host-#{host_name}/ instead of templates/default/.

How can I create a JSON webservice to store and retrieve data from a simple properties file?

How can I create a Java or Javascript JSON webservice to retrieve data from a simple properties file? My intention is to uses this as a global property storage for a Jenkins instance that runs many Unit tests. The master property file also needs to be capable of being manually edited and stored in source control.
I am just wondering what method people would recommend that would be the easiest for a junior level programmer like me. I need read capability at miniumum but, and if its not too hard, write capability also. Therefore, that means it is not required to be REST.
If something like this already exists in Java or Groovy, a link to that resource would be appreciated. I am a SoapUI expert but I am unsure if a mock service could do this sort of thing.
I found something like this in Ruby but I could not get it to work as I am not a Ruby programmer at all.
There are a multitude of Java REST frameworks, but I'm most familiar with Jersey so here's a Groovy script that gives a simple read capability to a properties file.
#Grapes([
#Grab(group='org.glassfish.jersey.containers', module='jersey-container-grizzly2-http', version='2.0'),
#Grab(group='org.glassfish.jersey.core', module='jersey-server', version='2.0'),
#Grab(group='org.glassfish.jersey.media', module='jersey-media-json-jackson', version='2.0')
])
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory
import org.glassfish.jersey.jackson.JacksonFeature
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.Produces
#Path("properties")
class PropertiesResource {
#GET
#Produces("application/json")
Properties get() {
new File("test.properties").withReader { Reader reader ->
Properties p = new Properties()
p.load(reader)
return p
}
}
}
def rc = new org.glassfish.jersey.server.ResourceConfig(PropertiesResource, JacksonFeature);
GrizzlyHttpServerFactory.createHttpServer('http://localhost:8080/'.toURI(), rc).start()
System.console().readLine("Press any key to exit...")
Unfortunately, since Jersey uses the 3.1 version of the asm library, there are conflicts with Groovy's 4.0 version of asm unless you run the script using the groovy-all embeddable jar (it won't work by just calling groovy on the command-line and passing the script). I also had to supply an Apache Ivy dependency. (Hopefully the Groovy team will resolve these in the next release--the asm one in particular has caused me grief in the past.) So you can call it like this (supply the full paths to the classpath jars):
java -cp ivy-2.2.0.jar:groovy-all-2.1.6.jar groovy.lang.GroovyShell restProperties.groovy
All you have to do is create a properties file named test.properties, then copy the above script into a file named restProperties.groovy, then run via the above command line. Then you can run the following in Unix to try it out.
curl http://localhost:8080/properties
And it will return a JSON map of your properties file.

Reading a file from Java Servlet

I wrote some code to read a file in my Java Servlet class. (I'm using Netbeans on Windows along with Tomcat server). However, my servlet cannot find the file!
After much digging, I found that I had to place the file I wanted to read in Tomcat/bin folder. That's very surprising. How can I get the path to my Webapps/ folder? Let's assume my website project is called "Web1".
Essentially what I'm doing is I'm trying to read my .xsl file for converting my DOM Document to be an HTML. At first I tried placing this .xsl file everywhere (at the same level as my index.jsp, in the same directory as my servlet class file, etc...but didnt work at all)
Also, when I finished transform(), my HTML file also goes into the Tomcat/bin folder~!
Can you use javax.servlet.ServletContext.getRealPath(String path)?
Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext..
The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).
Where are you consuming that XSL? If from your Java code place the file into src/java/resources so it will end up in the top of your classpath when the WAR is assembled /WEB-INF/classes/foo.xsl. Then you can use Class#getResource("foo.xsl") or even better if you are using DOM4J or equivalent there are ways of loading the file.
Now if it is you JavaScript that performs the transformation on the client that's a different story
Something like this might be more convenient for you:
java.net.URL url = ClassLoader.getSystemResource(file_name);
try {
InputStream is = url.openStream());
//Read the file and do stuff
} catch(IOException e) {
System.err.println("Error: could not load the file");
}
This will allow you to get an InputStream for a file within the classpath (in your case, something in the webapps folder). As for writing results, I'm not sure.