I’ve been trying to figure out how to do environment level version pinning using powershell. I feel there has to be a way to easily do this, but I’m not finding a way. Previously, I was using a linux host that was running knife. This was fine and dandy, and allowed the use of sed to do a stream replacement of the data that needed pinning. An example is shown below. It is based on variable replacement using Jenkins builds against a bash script.
# Update the environments version for this cookbook
echo "Updating cookbook version to ${PIPELINE_VERSION} for environment ${ENVIRONMENT}"
EDITOR="sed -i 's/.*${COOKBOOK}.*/ \"${COOKBOOK}\": \"= ${PIPELINE_VERSION}\",/g'" knife environment edit ${ENVIRONMENT}
Doing this is powershell, a language I’m not super familiar with is proving to be more difficult. I feel that something with "knife exec -E “api.post(‘organizations//environments//cookbook_versions’, :run_list => [‘cookbook@1.0.10’])” should work, and is probably optimal. Does anyone have a suggestion as to how this environment cookbook version pinning would best be accomplished?
I also found this knife-pin plugin, but I’m not a big Ruby expert. It appears to version pin a version to nodes, which isn’t what I want to do. I thought about adapting it, but I don’t know how.
I am getting closer to what I need with this code. Note that it is still a work in progress, as I notice that we never did an initial version pin if it isn’t there.:
Here’s my latest version of the pinCookbookVersionToEnvironment function… Note that now I have to rid of an error, but that shouldn’t be much of a problem. Once I figure out that, I will post a final solution, since someone may want to do what I needed to do.
Hey all that may have taken a look at this. I found through further testing that ConvertTo-Json likes to "nuke" your json object by not recursing through enough depth. I temporarily lost some data, so I found that this was the solution to that.
ConvertTo-Json -Compress -Depth 50
I also found that Unescaping Unicode was less than optimal. Since my group doesn't use non-exact matches, we likely won't run into problems with that. Thusly, I removed this:
Your mileage may vary, and I usually like to not have any imperfect bounds on my code, but I don't have time to make it "perfect". Nonetheless, this knowledge should save you some time if you are using it.
That’s kind of you and congrats for the hard work here, but FWIW my position is to stick to ruby code to play with chef objects, specially under windows where the codepage and carriage return difference with linux could become a real headache.
Knife exec is usually the perfect tool for this kind of operations.
For your particular use case (promoting a version into some environment), knife spork is the first coming to my mind, I think berkshelf have this kind of option too but I never used it so my memory can be faulty here.
Hey thanks! I've noticed that the whole ecosystem of Chef is kind of a pain to search on the internet. I mean, I get it, they went all in with the cooking terms, but using common English words is less than ideal.
From your article, this seems to be what I'd most like:
knife spork promote [ENVIRONMENT OR ENVIRONMENT GROUP NAME] COOKBOOK [--version, --remote]
I did take a look at knife exec, but it was less than ideal on Windows because other bash commands aren't there.
Well knife exec allow any ruby code, so usually you can do eveything in ruby (for sed in a replace way, I think of .gsub, etc.).
Now if you wish to get a ‘nice’ env under windows, chef-dk comes with most unix utilities, chef shell-init bash should give you a unix like shell to work in (even in a windows environment at start, but still take care of line endings, that’s the major pitfall)