1# -*- coding: utf-8 -*- # 2# Copyright 2015 Google LLC. All Rights Reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15"""Utility functions for gcloud bigtable emulator.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import unicode_literals 20 21import os 22from googlecloudsdk.command_lib.emulators import util 23from googlecloudsdk.core import execution_utils 24from googlecloudsdk.core import log 25from googlecloudsdk.core.util import platforms 26 27BIGTABLE = 'bigtable' 28BIGTABLE_TITLE = 'Google Cloud Bigtable emulator' 29BIGTABLE_EXECUTABLE = 'cbtemulator' 30 31 32def GetDataDir(): 33 return util.GetDataDir(BIGTABLE) 34 35 36def BuildStartArgs(args): 37 """Builds the command for starting the bigtable emulator. 38 39 Args: 40 args: (list of str) The arguments for the bigtable emulator, excluding the 41 program binary. 42 43 Returns: 44 A list of command arguments. 45 """ 46 bigtable_dir = util.GetEmulatorRoot(BIGTABLE) 47 bigtable_executable = os.path.join(bigtable_dir, BIGTABLE_EXECUTABLE) 48 if platforms.OperatingSystem.Current() is platforms.OperatingSystem.WINDOWS: 49 bigtable_executable += '.exe' 50 return execution_utils.ArgsForExecutableTool(bigtable_executable, *args) 51 52 53def GetEnv(args): 54 """Returns an environment variable mapping from an argparse.Namespace.""" 55 return { 56 'BIGTABLE_EMULATOR_HOST': 57 '%s:%s' % (args.host_port.host, args.host_port.port) 58 } 59 60 61def Start(args): 62 bigtable_args = BuildStartArgs(util.BuildArgsList(args)) 63 log.status.Print('Executing: {0}'.format(' '.join(bigtable_args))) 64 with util.Exec(bigtable_args) as bigtable_process: 65 util.WriteEnvYaml(GetEnv(args), GetDataDir()) 66 util.PrefixOutput(bigtable_process, BIGTABLE) 67