LoginSignup
7
9

More than 5 years have passed since last update.

多分間違えてるColaboratoryの使い方(now入門)

Last updated at Posted at 2018-08-06

はじめに

nowのバージョンが2にあがりました。
こちらは旧バージョンで使い方です。

無料でサーバを作りたいと思ったのですが、ローカルに環境構築するのが面倒ですよね。
何をインストールしたのかわかんなくなったり、古いライブラリが悪さをしたり。。。
なので、クラウドで全部開発できる環境が欲しいと思っていました。

いろいろ調べたのですが、コードまでかけてデプロイもできる環境って結構少なかったりします。

※ 今までは cloud9を利用していたのですが、 AWSに買収されて、awsの内蔵エディタになりました。エディタ、ファイラーの使い勝手は変わらないのですが、ec2に乗せるため、環境構築やデプロイなどが意外と面倒です。

そこで、Colaboratory & now ならいつでもどこでも使えるじゃん?と思い立ちやってみました。

前回はこちら
多分間違えてるColaboratoryの使い方(Go入門)

Colaboratoryとは

Googleが作ったjupyterのクローン。googleアカウントさえあれば無料で利用できる。さらにGPUも無料!!

Google Colaboratoryにブラウザでアクセスするだけです。

jupyterとは

主に機械学習用に利用されている。
セルにPythonのコマンドを入力しながら実行しながらできるので試行錯誤がしやすい。
なれちゃうと、これなしでは生きていけなくなります。

nowとは

無料で利用できるPaaS(Platform as a Service)のひとつ。
他と比べて、登録&利用がメールアドレスのみ。
認証は、届いたメールをクリックするだけ。
非常に敷居が低いです。

現在、node.js, static html, Dockerの3種類利用できます。
さらに、独自ドメインも利用可能です。

この辺が分かりやすいです。
Now でクラウドの複雑さから解放されよう、今すぐに

環境設定

nodejsのバージョン確認

まず、nodeのバージョン確認を確認します。

入力セル
!node --version
出力セル
v8.11.3

v8系なら実用にほぼ問題ありません。他のバージョンを利用したい場合は、nvmをいれると便利かと思います。

ツールインストール

nowをインストールします。--unsafe-perm がないとインストールに失敗しました。

入力セル
!npm i -g --unsafe-perm now

出力は省略

サンプル作成

サンプル用の環境を作成します。フォルダを作成して移動します。

入力セル
# フォルダ作成
!mkdir sample
# 移動
%cd sample

ファイル作成

以下をそれぞれ実行してファイルを2つ作成します。

package.json
%%writefile package.json
{
    "name": "my-project",
    "version": "0.1.0",
    "dependencies": {
        "express": "4.13.4"
    },
    "scripts": {
        "start": "node index.js"
    }
}
index.js
%%writefile index.js
const app = require('express')();
app.get('/', (req, res) => {
    res.send('Welcome now in colab!');
});
app.listen();

version 2で追加。明示的にversion 1を利用するように指定します。

now.json
%%writefile now.json
{
  "version": 1
}

本来ならローカルで動作確認をしたいところですが、ポートを開けなさそうなので割愛。

デプロイ

よいよ、nowでデプロイします。

ログイン

メールアドレスを指定します。
実行すると指定したメールアドレスにメールが届くので、メール本文のリンクを開きます。

your@mailadress.comは自分のメアドにしてください。
事前登録は不要です。(もし、登録してあっても、手順は同じです。)

入力セル
!now login your@mailadress.com
出力セル
⠋ Sending you an email⠙ Sending you an email⠹ Sending you an email⠸ Sending you an email⠼ Sending you an email⠴ Sending you an email⠦ Sending you an email> We sent an email to kei0425@yahoo.co.jp. Please follow the steps provided
  inside it and make sure the security code matches Silly Stoat.
⠋ Waiting for your confirmation⠙ Waiting for your confirmation⠹ Waiting for your confirmation⠸ Waiting for your confirmation⠼ Waiting for your confirmation⠴ Waiting for your confirmation⠦ Waiting for your 

Waiting for your confirmationが何度も表示されるので、メーラーを開いて届いたメールのリンクを開きましょう。
すると、以下が出力されて、処理が終了します。

出力セル
✔ Fetched your personal details
> Ready! Authentication token and personal details saved in "~/.now"

デプロイ

now --publicで公開されます。

入力セル
!now --public
出力セル
> Deploying ~/sample under your@mailadress.com
> Using Node.js 8.11.3 (default)
> https://my-project-xxxxxxxxx.now.sh [in clipboard] (sfo1) [1s]
> Building…
> ▲ yarn
> yarn install v1.7.0
> warning package.json: No license field
> info No lockfile found.
> warning my-project@0.1.0: No license field
> [1/4] Resolving packages...
> [2/4] Fetching packages...
> [3/4] Linking dependencies...
> [4/4] Building fresh packages...
> success Saved lockfile.
> Done in 0.74s.
> ▲ Snapshotting deployment
> ▲ Saving deployment image (239.4K)
> Build completed
> Verifying instantiation in sfo1
> [0] 
> [0] my-project@0.1.0 start /home/nowuser/src
> [0] node index.js
> [0] 
> ✔ Scaled 1 instance in sfo1 [14s]
> Success! Deployment ready

出力3行目の「 https://my-project-xxxxxxxxx.now.sh 」がデプロイ先になります。
xxxxxは動的に変わるため注意しましょう。
アクセスして確認してみてください。

公開プロジェクトの確認

now lsで公開プロジェクトを確認できます。

入力セル
!now ls
出力セル
⠋ Fetching deployments in your@mailadress.com⠙> 1 total deployment found under your@mailadress.com [507ms]
> To list more deployments for an app run `now ls [app]`

  app           url                             inst #    type    state    age
  my-project    https://my-project-xxxxxxxxx.now.sh         1    NPM     READY    24s

削除

yを入力しないといけないのですが、入力できないため、echo yをパイプで渡しています。

入力セル
!echo y | now rm my-project
出力セル
⠼ Fetching deployment(s) "my-project" in your@mailadress.com> Found 1 deployment for removal in your@mailadress.com [640ms]
> The following 1 deployment will be permanently removed:
  hAerBFc4OMgEdHmow6W5BJeV      https://my-project-xxxxxxxxx.now.sh      3m ago
> Are you sure? [y/N] > Success! 1 deployment removed [2s]
- https://my-project-xxxxxxxxx.now.sh

おわりに

azureなどを利用すると、ブラウザでシェルも使えたりするそうです。
なので、無理にcolaboratoryを利用する必要もないのですが、一番のメリットは以下の3点かと思います。

  • 実行ログを残すことができる。
  • 実行ログにマークダウンでコメントを残せる。
  • そのファイルをなくさない。

何かを実行するたびに、qiitaに残せば上記メリットはなくなるのですが、qiitaに書き込むのもハードルが高いこともあるので、お手軽に試してみるのにはcolaboratoryを利用するのもありかと思います。

12時間で、VMがリセットされるのもある意味メリットになるかと

7
9
0

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
7
9