How to use API to upload a cookbook to chef server

Now i was embed chef to our framework, my code is more likely chef-dk, i used API to interact with chef-server, and now i can access the chef-sever, get nodes information from chef-server, start a push job and so on, but how to upload a cookbook to chef-server ? and i don’t want to use knife command because this is a web framework i can only use API. i didn’t find any post cookbook API in the doc, so what can i do ?

Cookbooks uploads aren’t a single API, it’s a collection of multiple API calls and different subsystems. Generally this is best handled by tools like Berkshelf and the Policyfile uploader so there hasn’t been much reason to expose a high-level API for it.

You are right, but as i have said, our framework need to be convenient to user, and we want user to deploy their solution only by web UI.

So we have to let user to upload their solution to our server, then we generate a cookbook dynamically, and after that we have to use scp tool to copy it to chef-dk, then use ssh tool to upload the cookbook to chef server, finally we use push job api to trigger one or multiple clients by our framework backend, that’s the workflow what i think can solve this problem, but it indeed.a little hard to do.

By the way, our framework is written by java, so it seems i can’t use Policyfile uploader directly.

That sounds awfully complex to me, and several of the steps seem unrelated to your problem.

Why not:

  • user uploads solution to the server
  • you create the cookbook dynamically
    (unrelated: move the cookbook over by scp to another machine)
  • your server invokes “knife cookbook upload”. You can use the knife tool as a form of API.
  • invoke the trigger for the push job.

Kevin Keane
Whom the IT Pros Call
The NetTech
Our values: Privacy, Liberty, Justice

@coderanger, @kkeane,

Thanks for reply, here is my deployment component.

Yes, i know it is complex.

Why not:

  • user uploads solution to the server --- we want user only to Interact with web UI, because make it easy and stupid is our goals, we event don't want user to know we use chef, because our user maybe some workers, they don't know how to work with linux.
  • you create the cookbook dynamically ---- so i have to create cookbook dynamically in the backend of my web server and use scp to move the cookbook to chef-server.
    (unrelated: move the cookbook over by scp to another machine)
  • your server invokes "knife cookbook upload". You can use the knife tool as a form of API. --- yes, now we use ssh to send knife command as a form of api.
  • invoke the trigger for the push job. ---yes, you are right, now we use API to trigger a push job

But now , i have a problem:
As i'm using push job model, i can't get the result of which steps is the job doing. I mean when i triggered a client to execute a recipe, i want to know which step is the client executing now. How to solve it?

Just a random thought. You can create/update node attributes at the end of every resource/action. And keep polling(reading) the attributes (via api) to track the progress.

Not related to your question.
Which service/tool have you used to produce the image?

No, it is not that complex, and one easy step is to install chef-dk on the Web server, give it its own user key so you know when it’s the application that did something.

Use knife as backend tool from your Web app, you won’t have to scp nor to remotely call knife.

Bonus: you may even generate a key for each webapp user and use this user/key pair when calling knife for more traceability of who did what on the chef server. Using env variable in knife.rb is well covered in docs, and I’ll be happy to give guidance if needed (with more insight of how the webapp is done, language used, Auth method, etc.)

The Chef code is here if you want to read it and see what the API calls are:

Software Ideas Modeler

Hi @yxyyysdy, did you manage by any chance to upload a cookbook via Chef Server API?
If so, do you have any example of the JSON body you send through?

Many thanks in advance.