Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2005-2014 Red Hat, Inc. Red Hat licenses this file to you under the Apache License, version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package io.fabric8.commands.hadoop;
 
 import java.util.List;
 import java.util.Map;
 
 import  org.apache.felix.gogo.commands.Command;
 import  org.apache.felix.gogo.commands.Option;
 import  org.apache.karaf.shell.console.AbstractAction;
 
 @Command(scope = "hadoop", name = "create", description = "Create an Hadoop cluster")
 public class CreateAction extends AbstractAction {
 
     @Option(name = "--name")
     private String name = "default";
 
     @Option(name = "--name-node")
     private String nameNode = "namenode";
 
     @Option(name = "--secondary-name-node")
     private String secondaryNameNode;
 
     @Option(name = "--data-nodes")
     private List<StringdataNodes = Collections.singletonList("datanode");
 
     @Option(name = "--job-tracker")
     private String jobTracker;
 
     @Option(name = "--task-trackers")
     private List<StringtaskTrackers = Collections.emptyList();
 
     @Option(name = "--create-children")
     private boolean createChildren = false;
 
     @Option(name = "--force")
     private boolean force = false;
 
     private final FabricService fabricService;
 
     CreateAction(FabricService fabricService) {
         this. = fabricService;
     }
 
     @Override
     protected Object doExecute() throws Exception {
         Container[] containers = .getContainers();
         if ( == null || .isEmpty()) {
             throw new IllegalArgumentException("The name node and at least one data node must be specified");
         }
         if (!.isEmpty() &&  == null) {
             throw new IllegalArgumentException("Can not specify task trackers if no job tracker is specified");
         }
         if (.isEmpty() &&  != null) {
             throw new IllegalArgumentException("At least one task tracker node must be specified");
         }
         if (!) {
             if (findContainer(containers) == null) {
                 throw new IllegalStateException("Container " +  + " does not exists");
             }
             if ( != null && findContainer(containers) == null) {
                 throw new IllegalStateException("Container " +  + " does not exists");
             }
             for (String n : ) {
                 if (findContainer(containersn) == null) {
                     throw new IllegalStateException("Container " + n + " does not exists");
                 }
             }
             if ( != null && findContainer(containers) == null) {
                 throw new IllegalStateException("Container " +  + " does not exists");
             }
             for (String n : ) {
                 if (findContainer(containersn) == null) {
                     throw new IllegalStateException("Container " + n + " does not exists");
                }
            }
        }
        for (String p : Arrays.asList("hadoop-" + ,
                                      "hadoop-" +  + "-namenode",
                                      "hadoop-" +  + "-secondary-namenode",
                                      "hadoop-" +  + "-datanode",
                                      "hadoop-" +  + "-job-tracker",
                                      "hadoop-" +  + "-task-tracker",
                                      "insight-hdfs-" + )) {
            Profile profile = null;
            try {
                profile = .getDefaultVersion().getProfile(p);
            } catch (Throwable t) {
                // Ignore
            }
            if (profile != null) {
                if () {
                    profile.delete();
                } else {
                    throw new IllegalStateException("Profile " + profile.getId() + " already exists. Use --force to recreate the profiles.");
                }
            }
        }
        Version version = .getDefaultVersion();
        Profile hadoop = version.getProfile("hadoop");
        Map<StringMap<StringString>> configs;
        Profile cluster = version.createProfile("hadoop-" + );
        cluster.setParents(new Profile[] { hadoop });
        configs = new HashMap<StringMap<StringString>>();
        configs.put("io.fabric8.hadoop"new HashMap<StringString>());
        configs.get("io.fabric8.hadoop").put("fs.default.name""hdfs://${zk:" +  + "/ip}:9000");
        configs.get("io.fabric8.hadoop").put("dfs.http.address""hdfs://${zk:" +  + "/ip}:9002");
        cluster.setConfigurations(configs);
        Profile nameNodeProfile = version.createProfile("hadoop-" +  + "-namenode");
        nameNodeProfile.setParents(new Profile[]{cluster});
        configs = new HashMap<StringMap<StringString>>();
        configs.put("io.fabric8.hadoop"new HashMap<StringString>());
        configs.get("io.fabric8.hadoop").put("nameNode""true");
        nameNodeProfile.setConfigurations(configs);
        Profile secondaryNameNodeProfile = version.createProfile("hadoop-" +  + "-secondary-namenode");
        secondaryNameNodeProfile.setParents(new Profile[]{cluster});
        configs = new HashMap<StringMap<StringString>>();
        configs.put("io.fabric8.hadoop"new HashMap<StringString>());
        configs.get("io.fabric8.hadoop").put("secondaryNameNode""true");
        secondaryNameNodeProfile.setConfigurations(configs);
        Profile dataNodeProfile = version.createProfile("hadoop-" +  + "-datanode");
        dataNodeProfile.setParents(new Profile[]{cluster});
        configs = new HashMap<StringMap<StringString>>();
        configs.put("io.fabric8.hadoop"new HashMap<StringString>());
        configs.get("io.fabric8.hadoop").put("dataNode""true");
        dataNodeProfile.setConfigurations(configs);
        Profile jobTrackerProfile = version.createProfile("hadoop-" +  + "-job-tracker");
        jobTrackerProfile.setParents(new Profile[]{cluster});
        configs = new HashMap<StringMap<StringString>>();
        configs.put("io.fabric8.hadoop"new HashMap<StringString>());
        configs.get("io.fabric8.hadoop").put("jobTracker""true");
        jobTrackerProfile.setConfigurations(configs);
        Profile taskTrackerProfile = version.createProfile("hadoop-" +  + "-task-tracker");
        taskTrackerProfile.setParents(new Profile[]{cluster});
        configs = new HashMap<StringMap<StringString>>();
        configs.put("io.fabric8.hadoop"new HashMap<StringString>());
        configs.get("io.fabric8.hadoop").put("taskTracker""true");
        taskTrackerProfile.setConfigurations(configs);
        Profile insightProfile = version.createProfile("insight-hdfs-" + );
        insightProfile.setParents(new Profile[]{ version.getProfile("insight-hdfs")});
        configs = new HashMap<StringMap<StringString>>();
        configs.put("io.fabric8.insight.elasticsearch-default"new HashMap<StringString>());
        configs.get("io.fabric8.insight.elasticsearch-default").put("gateway.hdfs.uri""hdfs://${zk:" +  + "/ip}:9000");
        insightProfile.setConfigurations(configs);
        // Name node
        Container nameNodeContainer = findContainer(containers);
        if (nameNodeContainer == null && ) {
            nameNodeContainer = createChild();
        }
        addProfile(nameNodeContainernameNodeProfile);
        // Secondary name node
        if ( != null) {
            Container secondaryNameNodeContainer = findContainer(containers);
            if (secondaryNameNodeContainer == null && ) {
                secondaryNameNodeContainer = createChild();
            }
            addProfile(secondaryNameNodeContainersecondaryNameNodeProfile);
        }
        // Data nodes
        for (String n : ) {
            Container cont = findContainer(containersn);
            if (cont == null) {
                cont = createChild(n);
            }
            addProfile(contdataNodeProfile);
        }
        // Job tracker
        if ( != null) {
            Container jobTrackerContainer = findContainer(containers);
            if (jobTrackerContainer == null && ) {
                jobTrackerContainer = createChild();
            }
            addProfile(jobTrackerContainerjobTrackerProfile);
        }
        // Task trackers
        for (String n : ) {
            Container cont = findContainer(containersn);
            if (cont == null) {
                cont = createChild(n);
            }
            addProfile(conttaskTrackerProfile);
        }
        return null;
    }
    private Container createChild(String namethrows URISyntaxException {
        CreateContainerOptions options = CreateChildContainerOptions.builder()
                .name(name)
                .parent(.getCurrentContainer().getId()).build();
        CreateContainerMetadata[] metadatas = .createContainers(options);
        Container container = metadatas[0].getContainer();
        return container;
    }
    private void addProfile(Container containerProfile profile) {
        List<Profileprofiles = new ArrayList<Profile>();
        for (Profile p : container.getProfiles()) {
            if (!isAncestor(pprofile)) {
                profiles.add(p);
            }
        }
        profiles.add(profile);
        container.setProfiles(profiles.toArray(new Profile[profiles.size()]));
    }
    private boolean isAncestor(Profile parentProfile child) {
        if (child.getId().equals(parent.getId())) {
            return true;
        }
        for (Profile p : child.getParents()) {
            if (isAncestor(parentp)) {
                return true;
            }
        }
        return false;
    }
    private Container findContainer(Container[] containersString name) {
        for (Container cont : containers) {
            if (name.equals(cont.getId())) {
                return cont;
            }
        }
        return null;
    }
New to GrepCode? Check out our FAQ X