26
25

More than 5 years have passed since last update.

bqコマンドをインストールせずに使いたい&認証情報をリポジトリで管理したい

Posted at

会社のバッチ実行サーバーにはpythonは入ってるけどvirtualenvとかは入っていないので、Google BigQueryに投げるためのコマンドであるbqコマンドを使うにはどうしたらいいかと試したところ、良さそうな方法を見つけた。

まず、bqをインストールするには2つ方法がある。

  • pip install bigquery
  • Google Cloud SDKをインストールする

ちなみにPython 2.7限定。

どちらの方法でもbqコマンドは入るが、後者の場合はGoogle Could SDKのコマンドラッパーを経由して実行することになるので、認証情報の持ち方が変わったりする。

今回はサーバーに何も入れなくていい後者の方法でやったが、両方の方法を書いておく。

pip install bigqueryの場合

この場合は、bqを最初に実行すると

$ bq shell

Welcome to BigQuery! This script will walk you through the
process of initializing your .bigqueryrc configuration file.

First, we need to set up your credentials if they do not
already exist.


******************************************************************
** No OAuth2 credentials found, beginning authorization process **
******************************************************************

Go to the following link in your browser:

    https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fbigquery&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&client_id=****.apps.googleusercontent.com&access_type=offline

Enter verification code:

のようになって、出てきたURLをブラウザで開いて、OAuth認証して表示されるコードをコンソールに貼りつけると認証され、~/.bigquery.v2.token~/.bigqueryrcの2つのファイルができる。

サーバーにpip install bigqueryすることが出来る場合はこの.bigquery.v2.tokenというファイルをリポジトリに入れると、

$ bq --bigqueryrc=/path/to/bigqueryrc --credential_file=/path/to/bigquery.v2.token

のように実行できるはずである。

Google Could SDKの場合

https://developers.google.com/cloud/sdk/ にインストール方法が書いてある。

$ curl https://sdk.cloud.google.com | bash

という方法があるらしいが、試していない。今回はgoogle-cloud-sdk.tar.gzをダウンロードして展開する方法でやることにする。

展開すると、install.shというのがあるので、これを叩く。実はインストールスクリプトはどこにもファイルをインストールせず、~/.bash_profileにPATHを追加するだけで、install.shの質問に全部noと答えると、それすらもしない。

$ ./install.sh
Welcome to the Google Cloud SDK!

The Google Cloud SDK is currently in developer preview. To help improve the
quality of this product, we collect anonymized data on how the SDK is used.
You may choose to opt out of this collection now (by choosing 'N' at the below
prompt), or at any time in the future by running the following command:
    gcloud config --global-only set disable_usage_reporting true

Do you want to help improve the Google Cloud SDK (Y/n)?  n

This will install all the core command line tools necessary for working with
the Google Cloud Platform.

If you are developing an App Engine application, please select the
language your application is written in.  This will install the
required tools and runtimes for working in that language.  If
necessary, you can add and remove languages later through the gcloud
component manager.
  [1]  Java
  [2]  Python and PHP
  [3]  Go
  [4]  No App Engine (you can install App Engine tools later)
Please enter your numeric choice (4):  4

The following components will be installed:
    ------------------------------------------------------------------------------
    | BigQuery Command Line Tool                           |     2.0.18 | < 1 MB |
    | BigQuery Command Line Tool (Platform Specific)       |     2.0.18 | < 1 MB |
    | Cloud DNS Admin Command Line Interface               | 2014.04.30 | < 1 MB |
    | Cloud SDK Core Command Line Tools                    |          1 |        |
    | Cloud SDK Core Libraries (Platform Specific)         | 2014.05.06 | < 1 MB |
    | Cloud SQL Admin Command Line Interface               | 2014.05.06 | < 1 MB |
    | Cloud Storage Command Line Tool                      |       3.42 | 1.8 MB |
    | Cloud Storage Command Line Tool (Platform Specific)  |       3.42 | < 1 MB |
    | Compute Engine Command Line Interface (RC)           | 2014.05.22 | < 1 MB |
    | Compute Engine Command Line Tool                     |     1.15.0 | < 1 MB |
    | Compute Engine Command Line Tool (Platform Specific) |     1.15.0 | < 1 MB |
    ------------------------------------------------------------------------------

Performing in place update...

Installing: BigQuery Command Line Tool ... Done
Installing: BigQuery Command Line Tool (Platform Specific) ... Done
Installing: Cloud DNS Admin Command Line Interface ... Done
Installing: Cloud SDK Core Command Line Tools ... Done
Installing: Cloud SDK Core Libraries (Platform Specific) ... Done
Installing: Cloud SQL Admin Command Line Interface ... Done
Installing: Cloud Storage Command Line Tool ... Done
Installing: Cloud Storage Command Line Tool (Platform Specific) ... Done
Installing: Compute Engine Command Line Interface (RC) ... Done
Installing: Compute Engine Command Line Tool ... Done
Installing: Compute Engine Command Line Tool (Platform Specific) ... Done

Done!

The Google Cloud SDK installer will now prompt you to update an rc
file to bring the Google Cloud CLIs into your environment.

Enter path to an rc file to update, or leave blank to use
[/Users/atsushi/.bash_profile]:  /tmp/profile

Modify profile to update your $PATH? (Y/n)?  n

Source [/Users/atsushi/tmp/google-cloud-sdk/path.bash.inc]
in your profile to add the Google Cloud SDK command line tools to your $PATH.

Modify profile to enable bash completion? (Y/n)?  n

Source [/Users/atsushi/tmp/google-cloud-sdk/completion.bash.inc]
in your profile to enable bash completion for gcloud.

For more information on how to get started, please visit:
  https://developers.google.com/cloud/sdk/gettingstarted

これでbinディレクトリ以下のbqコマンドが使えるようになった。使おうとすると、今度は次のように言われるはずである。

$ ./bin/bq shell
You do not currently have an active account selected.
Please run:

  $ gcloud auth login

to obtain new credentials, or if you have already logged in with a
different account:

  $ gcloud config set account <account name>

to select an already authenticated account to use.

./bin/gcloud auth loginと打つとブラウザが開いて、OAuthの認証をするとbqが使えるようになっている。

認証情報は~/.config/gcloud/以下に保存されるが、実は変更することもできて

$ CLOUDSDK_CONFIG=/path/to/config ./bin/gcloud auth login

とかやると好きなディレクトリにconfigを出力できる。

このGoogle Cloud SDKのディレクトリとconfigを全部リポジトリに入れてしまい(もちろん.pycなどは.gitignoreする)、bqを実行するときは

$ CLOUDSDK_CONFIG=/path/to/config /path/to/bin/bq

のようにすれば良い。

まとめ

サーバーでpip install bigqueryできる場合はbq initしてできる~/.bigquery.v2.token~/.bigqueryrcをリポジトリに置けばよい。

これができないときは google-cloud-sdk.tar.gz をダウンロードして展開し、中に入ってCLOUDSDK_CONFIG=./config ./bin/gcloud auth loginとやって、このディレクトリを全部ひっくるめてリポジトリに置けばよい。

26
25
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
25