<commit_before>#!/bin/bash -e

# Copyright 2016 Google Inc. All rights reserved.
#
# 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.

USAGE='USAGE: One of...

To run locally:

    docker run -it -p "8081:8080" -v "${HOME}:/content" gcr.io/cloud-datalab/datalab

Or, to connect to a kernel gateway in a GCE VM;

    docker run -it -p "8081:8080" -v "${HOME}:/content" \
      -e "GATEWAY_VM=${PROJECT_ID}/${ZONE}/${INSTANCE}"
      gcr.io/cloud-datalab/datalab
'

ERR_MALFORMED_GATEWAY=1
ERR_LOGIN=2
ERR_PROJECT_NOT_FOUND=3
ERR_ZONE_NOT_FOUND=4
ERR_INSTANCE_NOT_FOUND=5
ERR_DEPLOY=6
ERR_TUNNEL_FAILED=7
ERR_GATEWAY_FAILED=7

# Run the 'setup-env' script to ensure that gcloud has been told to use
# a config directory under the mounted volume (so that the results of
# 'gcloud auth login' are persisted).
source /datalab/setup-env.sh

export PROJECT_ID=${PROJECT_ID:-`gcloud config list -q --format 'value(core.project)' 2> /dev/null`}
export ZONE=${ZONE:-`gcloud config list -q --format 'value(compute.zone)' 2> /dev/null`}

if [[ -n "${GATEWAY_VM}" ]]; then
  GATEWAY_PART_1=`echo "${GATEWAY_VM}" | cut -d '/' -f 1`
  GATEWAY_PART_2=`echo "${GATEWAY_VM}" | cut -d '/' -f 2`
  GATEWAY_PART_3=`echo "${GATEWAY_VM}" | cut -d '/' -f 3`

  if [[ -z "${GATEWAY_PART_3}" &&  -z "${GATEWAY_PART_3}" ]]; then
    export INSTANCE="${GATEWAY_PART_1}"
  elif [[ -z "${GATEWAY_PART_1}" || -z "${GATEWAY_PART_2}" || -z "${GATEWAY_PART_3}" ]]; then
    echo "Malformed gateway VM name"
    echo "${USAGE}"
    exit "${ERR_MALFORMED_GATEWAY}"
  else
    export PROJECT_ID="${GATEWAY_PART_1}"
    export ZONE="${GATEWAY_PART_2}"
    export INSTANCE="${GATEWAY_PART_3}"
  fi
fi

if [[ -n "${INSTANCE}" ]]; then
  USER_EMAIL=`gcloud info --format="value(config.account)"`
  if [[ -z "${USER_EMAIL}" ]]; then
    FAILED_LOGIN=""
    gcloud auth login || FAILED_LOGIN="true"
    if [[ -n "${FAILED_LOGIN}" ]]; then
      echo "Failed to log in to gcloud"
      exit "${ERR_LOGIN}"
    fi
  fi

  PROJECT_NOT_FOUND=""
  ZONE_NOT_FOUND=""
  INSTANCE_NOT_FOUND=""

  if [[ -z "${PROJECT_ID}" ]]; then
    read -p "Please enter the Google Cloud Platform project to use: " PROJECT_ID
  fi
  # Verify that the specified project exists...
  gcloud -q projects describe "${PROJECT_ID}" >/dev/null 2>&1 || PROJECT_NOT_FOUND="true"
  if [[ -n "${PROJECT_NOT_FOUND}" ]]; then<commit_msg>Fix an issue that when user is not logged in, run-with-gce.sh still thinks the user has logged in and proceeds.<commit_after>#!/bin/bash -e

# Copyright 2016 Google Inc. All rights reserved.
#
# 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.

USAGE='USAGE: One of...

To run locally:

    docker run -it -p "8081:8080" -v "${HOME}:/content" gcr.io/cloud-datalab/datalab

Or, to connect to a kernel gateway in a GCE VM;

    docker run -it -p "8081:8080" -v "${HOME}:/content" \
      -e "GATEWAY_VM=${PROJECT_ID}/${ZONE}/${INSTANCE}"
      gcr.io/cloud-datalab/datalab
'

ERR_MALFORMED_GATEWAY=1
ERR_LOGIN=2
ERR_PROJECT_NOT_FOUND=3
ERR_ZONE_NOT_FOUND=4
ERR_INSTANCE_NOT_FOUND=5
ERR_DEPLOY=6
ERR_TUNNEL_FAILED=7
ERR_GATEWAY_FAILED=7

# Run the 'setup-env' script to ensure that gcloud has been told to use
# a config directory under the mounted volume (so that the results of
# 'gcloud auth login' are persisted).
source /datalab/setup-env.sh

export PROJECT_ID=${PROJECT_ID:-`gcloud config list -q --format 'value(core.project)' 2> /dev/null`}
export ZONE=${ZONE:-`gcloud config list -q --format 'value(compute.zone)' 2> /dev/null`}

if [[ -n "${GATEWAY_VM}" ]]; then
  GATEWAY_PART_1=`echo "${GATEWAY_VM}" | cut -d '/' -f 1`
  GATEWAY_PART_2=`echo "${GATEWAY_VM}" | cut -d '/' -f 2`
  GATEWAY_PART_3=`echo "${GATEWAY_VM}" | cut -d '/' -f 3`

  if [[ -z "${GATEWAY_PART_3}" &&  -z "${GATEWAY_PART_2}" ]]; then
    export INSTANCE="${GATEWAY_PART_1}"
  elif [[ -z "${GATEWAY_PART_1}" || -z "${GATEWAY_PART_2}" || -z "${GATEWAY_PART_3}" ]]; then
    echo "Malformed gateway VM name"
    echo "${USAGE}"
    exit "${ERR_MALFORMED_GATEWAY}"
  else
    export PROJECT_ID="${GATEWAY_PART_1}"
    export ZONE="${GATEWAY_PART_2}"
    export INSTANCE="${GATEWAY_PART_3}"
  fi
fi

if [[ -n "${INSTANCE}" ]]; then
  USER_EMAIL=`gcloud auth list --format="value(account)"`
  if [[ -z "${USER_EMAIL}" ]]; then
    FAILED_LOGIN=""
    gcloud auth login || FAILED_LOGIN="true"
    if [[ -n "${FAILED_LOGIN}" ]]; then
      echo "Failed to log in to gcloud"
      exit "${ERR_LOGIN}"
    fi
  fi

  PROJECT_NOT_FOUND=""
  ZONE_NOT_FOUND=""
  INSTANCE_NOT_FOUND=""

  if [[ -z "${PROJECT_ID}" ]]; then
    read -p "Please enter the Google Cloud Platform project to use: " PROJECT_ID
  fi
  # Verify that the specified project exists...
  gcloud -q projects describe "${PROJECT_ID}" >/dev/null 2>&1 || PROJECT_NOT_FOUND="true"
  if [[ -n "${PROJECT_NOT_FOUND}" ]]; then
{'fraction_non_alphanumeric': 0.14473229706390328, 'fraction_numerical': 0.017616580310880828, 'mean_word_length': 3.9495726495726498, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '4615052', 'n_tokens_mistral': 2144, 'n_tokens_neox': 2118, 'n_words': 629}