[firebase]コマンド1つでfirebaseコマンドが使用可能な環境を構築する

カテゴリ: firebase

firebaseでは、コマンドライン(cli)からfirebaseの環境を操作するための、firebaseコマンドがあります。今回は、このfirebaseコマンドが使えるVMをコマンド1つで構築できるVagrantfileを作成しました。

Vagrantfileの中身

以下の内容をVagrantfileの名前で保存します

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  # config.vm.synced_folder "../data", "/vagrant_data"

  config.vm.provision "shell", inline: <<-SHELL
     # install node.js
     curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash -
     sudo yum -y install nodejs
     sudo yum -y install npm

     sudo npm -g install firebase-tools
  SHELL
end

環境の構築

環境構築は、前述のVagrantfileが存在するディレクトリでvagrant upコマンドを実行するだけで完了です。

$ vagrant up

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
...
    default: + firebase-tools@3.18.4
    default: added 533 packages in 63.554s

vagrant upが終わった後に、vagrant sshコマンドでvm内にログインしたらすぐにfirebaseコマンドを使用可能な状態になっています。

$ vagrant ssh

[vagrant@localhost ~]$ firebase --version
3.18.4

[vagrant@localhost ~]$ firebase

  Usage: firebase [options] [command]

  Options:

    -V, --version                              output the version number
    -P, --project <alias_or_project_id>        the Firebase project to use for this command
    -j, --json                                 output JSON instead of text, also triggers non-interactive mode
    --token <token>                            supply an auth token for this command
    --non-interactive                          error out of the command instead of waiting for prompts
    --interactive                              force interactive shell treatment even when not detected
    --debug                                    print verbose debug output and keep a debug log file
    -h, --help                                 output usage information

  Commands:
    ...

firebaseユーザ認証を行う

環境を作ったら始めに行うのはログイン認証です。
firebase loginコマンドを実行すると、以下のように認証ページのURLが表示されるので、ホストOSのブラウザ上で認証を済ませます。

$ firebase login

(node:4153) ExperimentalWarning: The fs.promises API is experimental
? Allow Firebase to collect anonymous CLI usage and error reporting information? No

Visit this URL on any device to log in:
https://accounts.google.com/o/oauth2/auth?client_id=...

Waiting for authentication...

認証が終わると、ブラウザでは以下のようなURLに遷移しますが、エラーになるはずです。
これは、認証処理を行なっているPC(ゲストOS)上でfirebase loginコマンドが実行されていることを期待されているのに対して、実際はホストOSで認証を行なってしまったからです。

http://localhost:9005/?state=xxx&code=xxx&authuser=x&session_state=xxx&prompt=xxx'

一方で、ゲストOS上のコマンドラインはWaiting for authentication...で止まっています。

認証情報をfirebase loginコマンドへ返すには、別のターミナルからゲストOSへsshログインして下記のコマンドを実行すれば良いです。URLはブラウザに表示されていたものをッコピー&ペーストします

curl 'http://localhost:9005/?state=xxx&code=xxx&authuser=x&session_state=xxx&prompt=xxx'

curlコマンドを実行すると、firebase loginコマンドでの認証処理が正常に終了します。


Amazonでおトクに買い物する方法
AmazonチャージでポイントGET


Amazonは買いもの前にAmazonギフト券をチャージしてポイントをゲットしないと損!

こちらもおススメ

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です