<commit_before>/**
 * (c) Copyright 2014 WibiData, Inc.
 *
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * Licensed 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 org.kiji.maven.plugins;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.ExecutionException;

import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;

/**
 * An in-process way to start and stop a Bento cluster running in a Docker container. This class
 * wraps commands sent to the bento script in the Bento cluster installation.
 */
public final class BentoCluster {
  /**
   * Where in the bento-cluster environment are the generated site files stored.
   */
  private static final String[] SITE_FILE_RELATIVE_PATHS = new String[] {
      "hbase/hbase-site.xml",
      "hadoop/core-site.xml",
      "hadoop/mapred-site.xml",
      "hadoop/yarn-site.xml",
  };

  /** The singleton instance. */
  private static BentoCluster instance = null;

  /**
   * Initializes the singleton.
   *
   * @param bentoName name of the Bento cluster container.
   * @param venvRoot to the python virtualenv to install bento-cluster to.
   * @param dockerAddress of the docker daemon to use to manage bento instances.
   * @param mavenLog the maven log.
   */
  public static void setInstance(
      final String bentoName,
      final File venvRoot,
      final String dockerAddress,
      final Log mavenLog
  ) {
    Preconditions.checkState(
        venvRoot.mkdirs() || venvRoot.exists(),
        "Failed to create venv root: %s",
        venvRoot.getAbsolutePath()
    );
    instance = new BentoCluster(bentoName, venvRoot, dockerAddress, mavenLog);
  }

  /**
   * Returns true if the singleton has been initialized.
   *
   * @return true if the singleton has been initialized.
   */
  public static boolean isInstanceSet() {
    return instance != null;
  }

  /**
   * Gets the singleton instance.
   *
   * @return the singleton instance.
   */
  public static BentoCluster getInstance() {
    return instance;
  }

  /**
   * Returns the address to connect to docker with.
   *
   * If the OS is linux, returns a connection string for the docker instance on the default local
   * unix socket. If the OS is mac, returns a connection string for the boot2docker instance stored
   * in the environment variable DOCKER_HOST.
   *
   * @return the address to connect to docker with.
   */
  public static String getDockerAddress() {
    final String osNameProperty = System.getProperty("os.name");
    if (osNameProperty.contains("Linux")) {
      return "unix://";
    } else if (osNameProperty.contains("OS X")) {
      Preconditions.checkState(
          !System.getenv().containsKey("DOCKER_HOST"),
          "Please set environment variable DOCKER_HOST to contain the address to boot2docker"
      );
      return System.getenv("DOCKER_HOST");
    } else {
      throw new IllegalStateException(
          String.format("Unsupported operating system: %s", osNameProperty)<commit_msg>BMP-7. Tests fail on OS X.<commit_after>/**
 * (c) Copyright 2014 WibiData, Inc.
 *
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * Licensed 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 org.kiji.maven.plugins;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.ExecutionException;

import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;

/**
 * An in-process way to start and stop a Bento cluster running in a Docker container. This class
 * wraps commands sent to the bento script in the Bento cluster installation.
 */
public final class BentoCluster {
  /**
   * Where in the bento-cluster environment are the generated site files stored.
   */
  private static final String[] SITE_FILE_RELATIVE_PATHS = new String[] {
      "hbase/hbase-site.xml",
      "hadoop/core-site.xml",
      "hadoop/mapred-site.xml",
      "hadoop/yarn-site.xml",
  };

  /** The singleton instance. */
  private static BentoCluster instance = null;

  /**
   * Initializes the singleton.
   *
   * @param bentoName name of the Bento cluster container.
   * @param venvRoot to the python virtualenv to install bento-cluster to.
   * @param dockerAddress of the docker daemon to use to manage bento instances.
   * @param mavenLog the maven log.
   */
  public static void setInstance(
      final String bentoName,
      final File venvRoot,
      final String dockerAddress,
      final Log mavenLog
  ) {
    Preconditions.checkState(
        venvRoot.mkdirs() || venvRoot.exists(),
        "Failed to create venv root: %s",
        venvRoot.getAbsolutePath()
    );
    instance = new BentoCluster(bentoName, venvRoot, dockerAddress, mavenLog);
  }

  /**
   * Returns true if the singleton has been initialized.
   *
   * @return true if the singleton has been initialized.
   */
  public static boolean isInstanceSet() {
    return instance != null;
  }

  /**
   * Gets the singleton instance.
   *
   * @return the singleton instance.
   */
  public static BentoCluster getInstance() {
    return instance;
  }

  /**
   * Returns the address to connect to docker with.
   *
   * If the OS is linux, returns a connection string for the docker instance on the default local
   * unix socket. If the OS is mac, returns a connection string for the boot2docker instance stored
   * in the environment variable DOCKER_HOST.
   *
   * @return the address to connect to docker with.
   */
  public static String getDockerAddress() {
    final String osNameProperty = System.getProperty("os.name");
    if (osNameProperty.contains("Linux")) {
      return "unix://";
    } else if (osNameProperty.contains("OS X")) {
      final String address = System.getenv().get("DOCKER_HOST");
      Preconditions.checkState(address != null,
          "Please set environment variable DOCKER_HOST to contain the address to boot2docker");
      return address;
    } else {
      throw new IllegalStateException(
          String.format("Unsupported operating system: %s", osNameProperty)
{'fraction_non_alphanumeric': 0.0889438729001172, 'fraction_numerical': 0.0027347310847766638, 'mean_word_length': 3.3073471676948962, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3524806', 'n_tokens_mistral': 2166, 'n_tokens_neox': 2100, 'n_words': 780}