Github に公開鍵を登録して接続する手順

参考 qiita.com

git の設定関連

リモートリポジトリの設定は済ませておく。

git remote add <リモートリポジトリの名前> <リモートポジトリのssh接続先>

# 例
git remote add origin git@github.com:nokt2018/test.git

鍵ファイルの作成

このコマンドで秘密鍵・公開鍵のファイルを作る。

ssh-keygen -t rsa
# tオプションで暗号化形式を指定
# ここではrsaという形式を指定している。

このコマンドを打ち込むと
これから作成する鍵ファイル名を入力するよう言われる。

Enter file in which to save the key (/home/vagrant/.ssh/id_rsa): 

# 入力例
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa): /home/vagrant/.ssh/github

次にパスフレーズの設定をするように言われる。

Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 

パスフレーズを入力し終わると
秘密鍵、公開鍵が上記で指定した場所に作成される。

Your identification has been saved in /home/vagrant/.ssh/github. # 秘密鍵
Your public key has been saved in /home/vagrant/.ssh/github.pub. # 公開鍵

~/.ssh/cofig を作成または編集する

~/.ssh/cofig ファイルがなければ作る。
ここではコマンドを使用してファイルに追記する。

cat << EOF >> ~/.ssh/config
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/github
EOF

~/.ssh/config の記述について

Host github   # ssh github というコマンドを利用したときに使用される設定
  HostName github.com   # ssh の接続先、IPも可
  User git   # git という user 名で github.com にssh接続する
  IdentityFile ~/.ssh/github   # 秘密鍵ファイルの場所を指定する。
  Port 22   # ポートの設定もできる

githubに公開鍵を登録する。

下記のコマンドで表示される内容をコピーしてgithubに貼り付ける。

cat ~/.ssh/github.pub

この鍵ファイルは貼り付け時に一行になる。
改行されていたら操作を誤っている可能性が高いので注意する。

また行の末尾にはユーザ名@端末名が表示される。

接続の確認

ssh github

下記のようにgithubユーザ名が想定通り返ってくれば正しくSSH接続出来てる。

Hi nokt2018! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

あとは実際にgit pushして確かめる。

WSLでvagrant up した VM に vagrant ssh ではなく 通常のsshで接続する

大まかな手順

WSLでmetadataを扱えるようにする

/etc/wsl.confというファイルを無ければ作成し、
metadataを扱えるようにする記述を書き加える。

下記を実行する。

sudo bash -c 'cat << EOF >> /etc/wsl.conf
[automount]
options = "metadata"
EOF'

ファイルを作成したら一度ログアウトして入り直す。

参考 qiita.com

.ssh/config に vagrant ssh-config の情報を書き込み修正する

vagrant ssh-configを実行するとsshの情報が出てくる。

$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile F:/virtual_machines/vagrant_home/boxes/ubuntu18/0/virtualbox/vagrant_private_key
  IdentitiesOnly yes
  LogLevel FATAL

これを~/.ssh/configに追記すればよいのだが
IdentityFile のパスがWindowsのパスになっているので、
/mnt/c/ のような形に書き直す必要がある。

ホスト名も必要に応じて書き換える。

まずは vagrant ssh-config~/.ssh/config に追記

$ vagrant ssh-config >> ~/.ssh/config

次に ~/.ssh/config を修正。
最終的な~/.ssh/configは下記のようにした。

Host my_ubuntu18
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /mnt/f/virtual_machines/vagrant_home/boxes/ubuntu18/0/virtualbox/vagrant_private_key
  IdentitiesOnly yes
  LogLevel FATAL

.ssh/config と公開鍵のパーミッションを700にする

chmod 700 ~/.ssh/config
chmod 700 /mnt/f/virtual_machines/vagrant_home/boxes/ubuntu18/0/virtualbox/vagrant_private_key

ls コマンドなどでパーミッションが変更されているか確認する。 問題なければ ssh my_ubuntu18 のようにして接続確認する。

WSL 環境で vagrant up した時に 'Vagrant could not detect VirtualBox!' というエラー

VBoxManage という実行ファイルが見つからないというメッセージ

The provider 'virtualbox' that was requested to back the machine
'default' is reporting that it isn't usable on this system. The
reason is shown below:

Vagrant could not detect VirtualBox! Make sure VirtualBox is properly installed.
Vagrant uses the `VBoxManage` binary that ships with VirtualBox, and requires
this to be available on the PATH. If VirtualBox is installed, please find the
`VBoxManage` binary and add it to the PATH environmental variable.

 

前提条件

  • WindowsVirtualBoxはインストールされている

  • 下記の記述は~/.bashrcに書かれている

export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"

# VirtualBoxのパス
PATH=$PATH:"/mnt/c/Program Files/Oracle/VirtualBox"

下記のコマンドに対して次のように反応する

$ VBoxManage --version
プログラム 'VBoxManage' はまだインストールされていません。 次のように入力することでインストールできます:
sudo apt install virtualbox
# 拡張子をつけると反応する(パスは通っている)
$ VBoxManage.exe --version
6.0.12r133076

 

解決した方法

下記のように実行すると期待通りの動作をする。

 vagrant.exe up

Vagrantが拡張子を確認するようになる?

とりあえず毎回.exeとつけるのも面倒なのでエイリアスを設定する。
下記のように~/.bashrcに追記する

alias vagrant='vagrant.exe'

コマンドで実行

cat <<EOF >> ~/.bashrc
alias vagrant='vagrant.exe'
EOF

FirebaseUI を使用してユーザ作成時に Cloud Functions の auth.user().onCreate で user.displayName がnullになっていることの対策

やりたいこと

FirebaseUIを使用して新規ユーザ登録をして、
そのイベントをCloud Functionsで拾って
Cloud Firestore の "/users" コレクションに新規 user と同じ uid のドキュメントを 追加する。

ドキュメントの email, name フィールドは、
それぞれ FirebaseUI で入力した "email", "first & lastname" と同じにする。

困ったこと

Cloud Functions のコード
下記は失敗例

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";

admin.initializeApp();

functions.auth.user().onCreate(user => {
  const userUid = user.uid; 
  const userEmail = user.email; 
  const userName = user.displayName // 何故かnull

  const newUser = {
    email: userEmail,
    name: userName
  };

  return admin
    .firestore()
    .collection("users")
    .doc(userUid)
    .set(newUser);
});

user.uid と user.email はとれるが
user.displayName が null。どうやら仕様らしい。

https://github.com/firebase/firebase-functions/issues/95

最後のほうのコメントでfirebase-admin経由では取れる、とあったので、
試したところ取れた。

最終的なコード

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";

admin.initializeApp();

// 関数をasyncにする
exports.createUserDocument = functions.auth.user().onCreate(async user => {
  const userUid = user.uid;
  const userEmail = user.email;

  // admin.auth().getUser() 経由で取得, 取得するまでawait
  const authedUser = await admin.auth().getUser(userUid);
  const userName = authedUser.displayName;
  
  const newUser = {
    email: userEmail,
    name: userName
  };

  return admin
    .firestore()
    .collection("users")
    .doc(userUid)
    .set(newUser);
});

rubyインストール時にlibssl-devでエラーが出て、libssl-devをインストールしても同じエラーが出続けるとき

rbenvにて

ruby2.2.1をインストールしようとしたところ

「libssl-devがないので、下記コマンドを実行して」とエラーがでた。

 

apt-get install -y libssl-dev

 

通常なら上記コマンドを入れれば問題解決する。

 

 

しかし、

自分のrbenvはすでにruby2.5.0がインストール済み。

ruby2.5.0のイントール時に、

libssl-dev をインストールしたので、

じゃあ、どうしたらいいんだ?という状況。

 

 

ここを参照

github.com

 

 

結論から言えば、

下記コマンドで解決した。

apt-get install -y libssl1.0-dev

 

 

このコマンドを入力すると、

新しいバージョンのlibssl-devは削除されてしまうので注意。

(この環境でRuby2.5が動くかどうかは未確認)

 

 

古いバージョンの言語には、

関連するライブラリも古いバージョンを使わないといけない、

ということらしい。

 

pipenv + django でHerokuにデプロイした際にハマった(備忘録

pipenv を使用して django環境を作っていた。

 

HerokuにPushした際は問題ないと表示されていたが、

実際にURLに行ってみるとApplication errorと表示されていた。

 

 

Herokuエラーログ(一部)

django というモジュールが見つからんとのこと(そんなばかな)

2018-09-28T12:56:15.288358+00:00 app[web.1]: File "/app/pages_project/wsgi.py", line 12, in <module>
2018-09-28T12:56:15.288359+00:00 app[web.1]: from django.core.wsgi import get_wsgi_application
2018-09-28T12:56:15.288393+00:00 app[web.1]: ModuleNotFoundError: No module named 'django'

 

解決編は下の方。

 

 

結論とは関係ないが本読みながら進めていたので

下記のことをやった(いずれもpipenv shell 後)

  1. update Pipfile.lock
  2. make a new Procfile file
  3. install gunicorn as our web server
  4. make aone-line change to settings.py

 

 

1, Pipfileに下記を追記 (Python3.7を使っていることを明示)

[requires]
python_version = "3.7"

 

 実行(Pipfile.lockをつくるため)

$ pipenv lock

 

 

2. Procfileの作成

$ touch Procfile

 

 下記内容を作成したProcfileに記述

 gunicorn 使うの初めてだったのであまり理解できてないが

 簡単に調べた感じでは記述は問題なさそう

web: gunicorn pages_project.wsgi --log-file -

 

 

3. gunicornモジュールインストール

pipenv install gunicorn==19.8.1

 

 

4.settings.pyを変更

ALLOWED_HOSTS = ['*']

  ホストをワイルドカードにしているだけなので特に問題なし

 (セキュリティ云々は一旦おいておく)

 

 

 

その他 Herokuに対しての操作

$ heroku config:set DISABLE_COLLECTSTATIC=1

 

qiita.com

Rails で言うところのAssetをまとめる機能らしい。

 

Djangoドキュメント

https://django-staticfiles-doc-ja.readthedocs.io/en/latest/commands.html

 

Herokuの説明

devcenter.heroku.com

 

 

 

こちらも特に問題になるような操作ではない

$ heroku ps:scale web=1

 
heroku ps:scale worker=1 # ワーカープロセスを1つにします。

qiita.com

 

 

 

 

<解決編>

数時間悩んだが結局のところ非常に簡単な問題だった。

historyコマンドで確認したところ下記のような作業をしていた。

 

※やってはいけない例

pipenv shell

pip install django

(仮想環境に入ったあと、pipでdjangoをインストールする) 

 

これをするとPipfileにDjangoの情報が入らず、

そのせいでエラーが出てしまう。

 

 

なので、

アプリケーションのルートに行き

pipenv install django

 

を実行したところ、

PipfileとPipile.lockが自動的に書き換えられた。

 

その後、herokuにpushしたらエラーが解消。

 

 

-------------------------------------------------

つまり、Django導入時は必ず

pipenv install django

pipenv shell

 

 という順番を守りましょう、という結論。

-------------------------------------------------

 

  • 仮想環境に入ってからのpip install
  • pipenv install 

多分同じだろう、って思ってたが全然違いました。

 

 

おわり

EC2のUbuntu18.04にAWS Cloud9を入れるとターミナルがスクロールできない件の解消

やったこと

・EC2でアマゾン提供のAMIからUbuntu18.04インスタンスを作成

・上記インスタンスにCloud9を入れてWeb上で開発環境を作る

 

困ったこと

・Cloud9のターミナルのスクロールバーが表示されない

・つまりスクロールできない

 

原因

・Cloud9がtmux2.2までしか対応していない

・Ubuntu18.04はtmux2.6だった(うろ覚え)

 

ソース

https://forums.aws.amazon.com/thread.jspa?messageID=860422&tstart=0

This might have something to do with the version of the terminal multiplexer (tmux) that's installed. If so, it needs to be version 2.2 or earlier. You can confirm which version is installed by running the tmux -V command from the terminal. Also, note that the AWS Cloud9 IDE doesn't fully support the Z shell (zsh), in case you're trying to use that in your terminal.

Paul
AWS Cloud9 Documentation

 

 

 

<解決策>

Cloud9が使うtmuxを別途インストールしてリンクを置き換える。

 

cloud9ディレクトリの中にlocalディレクトリを作って

その中にtmux-2.2をイントール。

既存の .c9/bin/tmux を削除し、

削除したtmuxの名前でシンボリックリンクを貼り、

localディレクトリ内のtmuxを関連付ける(最後の行)

 

慎重にやるのであれば、

既存の.c9/bin/tmux は削除しないで

一旦リネームしておくといいと思う。

 

wget https://github.com/tmux/tmux/releases/download/2.2/tmux-2.2.tar.gz
tar xzf tmux-2.2.tar.gz
cd tmux-2.2
sudo apt-get install libevent-dev ncurses-dev -y
./configure --prefix="/home/ubuntu/.c9/local"
make
make install
rm /home/ubuntu/.c9/bin/tmux
ln -sf /home/ubuntu/.c9/local/bin/tmux /home/ubuntu/.c9/bin/tmux

 

ハマったポイント 

この作業をCloud9上のターミナルで実行するとエラーが起こるので、

PuTTYなどで接続して実行する。