Monday, August 17, 2015

CHEF Server with jclouds

CHEF solo is a standalone simple application which can be used to manage single instance.But if there is a need to manage multiple instances and want better control over organisations Cookbook's and Data Bags CHEF server will be a good choice .

Environment

CHEF server
Unlicensed version of CHEF server can be downloaded from (link) their site and supported up to 25 nodes.And there also a service provided by CHEF which provide a  access to one of their server by registering to manage.chef.io which is used in this article.

jclouds
When it comes to controlling CHEF server jclouds will be good choice and it also provide number of adapters to communication with other cloud services also.To authenticate to the CHEF server private key is needed

Activity

In this example java client is connected to the CHEF server and  list avilable data bags.

    import com.google.common.base.Charsets;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.Set;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.jclouds.ContextBuilder;
    import org.jclouds.enterprisechef.EnterpriseChefApi;

    public class Main {

        public static void main(String[] args) {

            String client = "ubuntu";
            String organization = "ubuntu_sl";
            String pemFile = System.getProperty("user.home") + "/.chef/" + client + ".pem";
            String credential = null;
            EnterpriseChefApi chefApi;
            try {
                credential = new String(Files.readAllBytes(Paths.get(pemFile)), Charsets.UTF_8);
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            chefApi = ContextBuilder.newBuilder("enterprisechef")
                    .endpoint("https://api.opscode.com/organizations/" + organization)
                    .credentials(client, credential).buildApi(EnterpriseChefApi.class);
            Set<String> databags = chefApi.listDatabags();
            for (String databag : databags) {
                System.out.println(" ******************** " + databag);
            }
            try {
                chefApi.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

No comments:

Post a Comment