Reputation: 2023
I am using pychef to run commands on the Chef server, largely with success. Pychef does not currently have cookbook support (uploading, downloading, etc), but it is exposed in the Chef Server REST API and pychef has functions that let you easily hit the chef api (putting together the auth credential headers and whatnot for you).
So I have an environment on the server called let's say my_environment
, and I'm trying to update a cookbook version in the environment file to say "some_cookbook": "= 1.2.3"
, so my entire environment file will look like:
{
"json_class": "Chef::Environment",
"chef_type": "environment",
"cookbook_versions": {
"some_cookbook": "= 1.2.3"
},
"description": "example environment file",
"name": "my_environment"
}
So, I create the chef api object like:
chef_api = chef.ChefAPI(BASE_CHEF_URL + "/" + my_organization, chef_key, CHEF_USER)
# BASE_CHEF_URL is something like https://my.chef.server/organizations
The ChefApi class has the request()
function on it, and I use it thus:
chef_api.request("PUT", "/environments/my_environment", data=json.dumps(environment_file_dict))
# environment_file_dict is the dictionary shown above.
However, I get an error message back saying: chef.exceptions.ChefServerError: The '_default' environment cannot be modified.
.
This is confusing, as given my endpoint I really don't expect this to attempt to change the _default
environment. I have tried looking through Chef and Knife code to find the source of the problem to no avail and the docs on the chef website also have not seemed to shed any light. What is also strange is I can do the same thing except with a "GET"
and pull down my environment file perfectly fine. Any ideas?
Upvotes: 1
Views: 1193
Reputation: 2023
Upon further investigation, I realize I was wrong about pychef and my question. This is a case of not reading the documentation enough. While it is true that pychef does not upload/download cookbooks, it does easily deal with chef Environments: https://pychef.readthedocs.org/en/latest/api.html#environments. What I did was I created a chef api in the same way as above:
chef_api = chef.ChefAPI(BASE_CHEF_URL + "/" + my_organization, chef_key, CHEF_USER)
Then I use the Environment object to update an existing environment:
import chef # This is the pychef module
env_obj = chef.Environment("my_environment", api=chef_api)
# env_obj now has attributes corresponding to the environment json that
# you can upload and download with knife or view on the chef server
env_obj.cookbook_versions["some_cookbook"] = "= 3.2.1"
env_obj.save() # This will save the object to the chef server
I, however, still have no explanation for the error "The '_default' environment cannot be modified".
Upvotes: 1