Rubyと筋肉とギターとわたし

筋トレが仕事です

【Ruby】スクレイピングに精度を求めるならNokogiriではなくSeleniumだ

どうもてぃです。

とある巨大ECサイト(通称熱帯雨林)をスクレイピングして遊んでいます。

今回は nokogiri で問題が発生したので記事にしました。

nokogiriのスクレイピング精度はあんまり良くないのを実体験で痛感しましたね。

environment

issue

ページネーション要素の取得です。

Nokogiriだと4〜6割の確率でページネーションを取得できない場合があります。

問題なのが、Seleniumだとページを読み込み終わるまで待つSelenium::WebDriver::Waitがあるのに対して、Nokogiriにはwaitが存在しません。

あと、NokogiriではJS等で動的に表示しているページは上手く取得できないことがあるとのこと。

だからSeleniumブラウジングしてスクレイピングする方が成功確率は高いと。

最初からSelenium使えばよかったですわ。PythonSeleniumだし。

solution

試しに、一番お手軽なスクレイピングサイト、巨大ECサイト熱帯雨林を使ってみます。

ここだと、ページネーションもありますので。

最近switchのスマブラが販売されて転売ヤーさんが乱立しているGCコンをスクレイピングしてみます。

require 'selenium-webdriver'
ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36"

# headlessモードのoptionをつける
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument("--user-agent=#{ua}")

driver = Selenium::WebDriver.for(:chrome, options: options)
driver.get('https://www.amazon.co.jp/gp/offer-listing/B07HC2F97Q/ref=dp_olp_new?ie=UTF8&condition=new')

# ページ読み込みのwaitを設定
wait = Selenium::WebDriver::Wait.new(timeout: 10)

# find_elementで単数取得
wait.until { driver.find_elements(:xpath, "//h3[@class='a-spacing-none olpSellerName']/span/a") }
seller_info = driver.find_elements(:xpath, "//h3[@class='a-spacing-none olpSellerName']/span/a")
wait.until { driver.find_elements(:xpath, "//ul[@class='a-pagination']/li/a").map { |el| el.attribute(:href) } }
pagination = driver.find_elements(:xpath, "//ul[@class='a-pagination']/li/a").map { |el| el.attribute(:href) }

# ページネーションの最後までクリックする
loop.with_index(1) do |_, i|
  puts "==================================#{i}回目==================================="
  puts driver.find_elements(:xpath, "//h3[@class='a-spacing-none olpSellerName']").map(&:text)

  break if driver.find_elements(:xpath, "//ul[@class='a-pagination']/li/a").last[-1] == '#'
  driver.find_elements(:xpath, "//ul[@class='a-pagination']/li/a").last.click
end

# 二回目のget urlも問題ない
driver.get('https://www.amazon.co.jp/sp?_encoding=UTF8&seller=A2W8UAZHGIMLV7')

driver.close

上記ソースを超簡単に説明。

# headlessモードのoptionをつける
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument("--user-agent=#{ua}")

Selenium::WebDriver::Chrome::Options.newSeleniumのオプションを設定するインスタンスを生成します。

AWSGCPで動かすことも想定にいれて、ヘッダレスでブラウザを起動しないようなオプションをつけています。

ついでにユーザーエージェントも設定してます。

# ページ読み込みのwaitを設定
wait = Selenium::WebDriver::Wait.new(timeout: 10)

スクレイピングをした際にページが全て表示されるまで待つ必要が有ります。

その設定を上記でやっています。使い方は

wait.until { driver.find_elements(:xpath, "//h3[@class='a-spacing-none olpSellerName']/span/a") }
seller_info = driver.find_elements(:xpath, "//h3[@class='a-spacing-none olpSellerName']/span/a")
wait.until { driver.find_elements(:xpath, "//ul[@class='a-pagination']/li/a").map { |el| el.attribute(:href) } }
pagination = driver.find_elements(:xpath, "//ul[@class='a-pagination']/li/a").map { |el| el.attribute(:href) }

のように、要素を取得する前にwait.utilを設定します。簡単です。

残りは要素の取得の処理がメインですね。

loopの部分でページネーションのリンクをクリックし続ける処理をやっています。そんな大したことはやってないです。

最後に

やはり、困った時はrubydocが役に立ちます。

Class: Selenium::WebDriver::Driver — Documentation for selenium-webdriver (0.0.28)

是非参考にしてみてください。

なにかあればコメント欄で〜。

ではでは。

【スクレイピング】HerokuでSelenium::WebDriver::Error::SessionNotCreatedError: session not created from tab crashed

あけましておめでとうございます。

どうもてぃです。

HerokuでSeleniumを使ったスクレイピングをした際にエラーが不規則に出たので共有します。

環境

参考

公式のリポジトリをまず参考にしました。issueが上がってましたね。

github.com

要するに、Heroku上のchromedriverのバージョンがサポートされているかどうか問題で動いたり動かなかったりするらしい。

最後の文にも書いてる通り

Yep, v59 confirmed. Not sure how folks are using this pre-59 :)

https://developers.google.com/web/updates/2017/04/headless-chrome

If this issue persists after v59 and associated Chromedriver are released, I'll re-open it.

chromedriverがリリースされた後に問題が上がればまたissueをopenにするぜ

だって。

対策

このスクレイピングに関しては絶対にうまく行くという保証ができないので、エラーが出た際に落ちないようにするしか無いです。

もしくはAWSGCPを使うか、ですね。

今回は一時的にテスト環境としてHerokuを使っているので、本番環境ができるまではHerokuで我慢し、エラーをキャッチする方法で対処します。

require 'selenium-webdriver'

# selenium headlessモードでchromeを立ち上げる
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options.add_argument('--disable-gpu')

# userAgentの設定
webdriver = Webdriver::UserAgent.driver(browser: :chrome, agent: :random, orientation: :landscape)
user_agent = webdriver.execute_script('return navigator.userAgent')
options.add_argument("--user-agent=#{user_agent}")

begin
  driver = Selenium::WebDriver.for(:chrome, options: options)
rescue => e
  # Selenium::WebDriver::Error::SessionNotCreatedError: session not created from tab crashed
  puts e
  next
end

driver.get(url) # urlにはスクレイピングしたいurlをセットする

終わりに

一旦はこれで解決です。

日本語での解決方法がなかなかみつからなかったので記事にしました。

他に解決方法がありましたら教えていただけると幸いです。

またGitHubの方で進展が有りましたら記事にすると思います。

では、今年もよろしくお願いしますー。

【Ruby】スクレイピングをするときはUserAgentを設定しましょう

どうもてぃです。

現在業務でスクレイピングをしたデータをシステム上でよしなにやって、楽しく過ごしています。

今回とある、巨大なECサイト(俗称:熱帯雨林)をスクレイピングした際にエラーに見舞われたので、解決策を残しておこうと思います。

実行環境

つかったもの

Rubyスクレイピングといえばnokogiriですよね。

もちろん、今回もこの子を使いました。

何が起こったか

以下がソース。

一般的な使い方ですね。

require 'open-uri'
require 'nokogiri'

sufixes = [1, 2, 3, 4]
sufixes.each do |sufix|
  url = Constants::BASE_URL + suffix
  charset = nil

  begin
    html = open(url) do |f|
      charset = f.charset
      f.read
    end
  rescue => e
    puts e
    next
  end

  doc = Nokogiri::HTML.parse(html, nil, charset)
 ・
 ・
 ・
end

上記の書き方だと、スクレイピング先が多くなればなるほど、一定確率で 503エラーが発生します。

これは、スクレイピングするURLをopenするときに、ユーザーエージェントが指定していないため発生するそう。

なので、擬似的にユーザーエージェントを指定してあげる。

解決方法

開発者コンソールを開いて、Network -> ctrl + rで更新 -> bookmarkのファイルを選択 -> RequestHeaderのユーザーエージェントをコピーして適当に使う。

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/xxxxxx (KHTML, like Gecko) Chrome/xxxxxx Safari/xxxxx

こんな感じのやつ。

これでなくても、ユーザーエージェントを偽装してやればいいので、正直言えばなんでもいい。

require 'open-uri'
require 'nokogiri'

opt = {}
opt['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/xxxxxx (KHTML, like Gecko) Chrome/xxxxxx Safari/xxxxx'

sufixes = [1, 2, 3, 4]
sufixes.each do |sufix|
  url = Constants::BASE_URL + suffix
  charset = nil

  begin
    # open時にぶち込む
    html = open(url, opt) do |f|
      charset = f.charset
      f.read
    end
  rescue => e
    puts e
    next
  end

  doc = Nokogiri::HTML.parse(html, nil, charset)
 ・
 ・
 ・
end

こうすると、503エラーでスクレイピング失敗することがなくなります。

nokogiriでこんなエラー出たの初めてだったので結構困りました。

解決できてよかった。

【GAE + CloudSQL + Rails】bundle exec rails appengine:exec -- bundle exec rails db:migrate時にCloudSQLで接続エラーになる

 f:id:rdwbocungelt5:20181112160913p:plain

どうもてぃです。

今回始めてGAEを使います。

gemのwheneverがheroku上で使えなかったので、もういっそのことステージング環境を作ってしまい、あとからそのスナップショットで本番環境を作ろうと企んでいたところ、そもそも初っ端からつまづきました。

環境

まずは参考記事

いろんな記事やリファレンスを参考にしました。

[cloudsql] Error 403: The client is not authorized to make this request., notAuthorized · Issue #10 · GoogleCloudPlatform/kubernetes-engine-samples · GitHub

Google App Engine から接続する  |  Cloud SQL ドキュメント  |  Google Cloud

Google Compute Engine(GCE)からCloud SQL接続でハマった - Qiita

RailsアプリをGAEにデプロイしCloudSQL, CloudStorageと連携させる - Qiita

GoogleAppEngine+CloudSQL(MySQL)+Rails5環境を作成する - Qiita

GAEにデプロイしたRailsからGoogle Cloud SQL に疎通できない時に確認すること - 俺、サービス売って家買うんだ

約一週間ほど溶かしました。。。

まだ完璧に解決したわけではないですが。

試したこと

Cloud SQLの有効化

こちらにアクセスしてCloud SQLを有効化します。

console.cloud.google.com

ダッシュボードにきたら「APIとサービスの有効化」をクリック。

f:id:rdwbocungelt5:20181112154547p:plain

検索フォームが出てくるので、「Cloud SQL」と入力。

f:id:rdwbocungelt5:20181112155028p:plain

出てきたCloud SQLをクリックし、遷移先でAPIを有効化させたらおk(画像貼るのめんどくさくなった)。

Unixソケットでのプロキシ接続

以下の手順で接続しました。

環境が64bitのLinuxなので

https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64

wgetする。

$ cd ~; wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy

$ chmod +x cloud_sql_proxy

$ sudo mkdir /cloudsql; sudo chmod 777 /cloudsql

$ ./cloud_sql_proxy -dir=/cloudsql -instances=<INSTANCE_CONNECTION_NAME>

INSTANCE_CONNECTION_NAMEgcloud sql instances describe [INSTANCE_NAME]で出てくるconnectionNameを使います(INSTANCE_NAMECloudSQLの名前)。

もしくはブラウザからGCPにアクセスして、プロジェクトのCloudSQLで該当のSQLインスタンスを見ればわかると思います。

プロキシで接続すると、

2018/11/12 15:40:17 Rlimits for file descriptors set to {&{8500 1048576}}
2018/11/12 15:40:23 Listening on /cloudsql/<INSTANCE_CONNECTION_NAME>/.s.PGSQL.5432 for <INSTANCE_CONNECTION_NAME>
2018/11/12 15:40:23 Ready for new connections
。

と出てきます。

問題なく接続できてるようです。認証もできてるはず。

gcloudでsqlに接続してみる

CloudSQLにgcloudで接続してみます。

$ gcloud sql connect <INSTANCE_NAME> --user=<USER_NAME>

Whitelisting your IP for incoming connection for 5 minutes...done.                                                                                                                            
Connecting to database with SQL user [USER_NAME].Password for user <USER_NAME>: 
psql (9.5.14, server 9.6.6)
WARNING: psql major version 9.5, server major version 9.6.
         Some psql features might not work.
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES128-GCM-SHA256, bits: 128, compression: off)
Type "help" for help.

USER_NAME名=>

うん、立ち上がります。

大丈夫そう。

ローカルからCloudSQLへマイグレーション

おそらく準備はある程度できているので(app.yaml環境変数も作った)、あとはappengine経由でマイグレーションするだけ。

デプロイも終わってる。

$ bundle exec rails appengine:exec -- bundle exec rails db:migrate
・
・
・
・
・
・
・
・
・
・
・
・

---------- CONNECT CLOUDSQL ----------
ERROR: Failed to start cloud_sql_proxy
2018/11/12 04:58:03 errors parsing config:
    googleapi: Error 403: The client is not authorized to make this request., notAuthorized
ERROR
ERROR: build step 0 "gcr.io/google-appengine/exec-wrapper:latest" failed: exit status 1
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ERROR: (gcloud.builds.submit) build 33763c5f-5791-490b-bade-8e3dda956688 completed with status "FAILURE"
rails aborted!
AppEngine::Util::Gcloud::GcloudFailed: GCloud failed with result code 1
/home/user/project/vendor/bundle/ruby/2.5.0/gems/appengine-0.4.6/lib/appengine/util/gcloud.rb:175:in `execute'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/appengine-0.4.6/lib/appengine/exec.rb:302:in `start'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/appengine-0.4.6/lib/appengine/tasks.rb:255:in `start_and_report_errors'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/appengine-0.4.6/lib/appengine/tasks.rb:155:in `block in setup_exec_task'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/railties-5.2.1/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/railties-5.2.1/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/railties-5.2.1/lib/rails/command.rb:48:in `invoke'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/railties-5.2.1/lib/rails/commands.rb:18:in `<main>'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `block in require'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:253:in `load_dependency'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => appengine:exec
(See full trace by running task with --trace)

なんで?

問題はこいつかな。

googleapi: Error 403: The client is not authorized to make this request., notAuthorized

IAM周りを見直し

とりあえずサービスアカウントが作成されているか確認しました。

<プロジェクトID>@appspot.gserviceaccount.comがあるかどうかブラウザで確認。存在してましたネ。

ちなみにgcloud projects get-iam-policy <PROJECT_ID>で該当のプロジェクトのIAMが取得できます。

もうひとつ足りなかった

CloudSQL用のIAMで権限が足りないようでした。

以下の記事をちゃんと見なおしたら発覚。

qiita.com

<プロジェクト番号>@cloudbuild.gserviceaccount.comに対してのroleが正しく割り当てられてませんでした。

なので、roleをEditor(編集者)へ変更。

もう一度マイグレーションしてみる

$ bundle exec rails appengine:exec -- bundle exec rails db:migrate
・
・
・
・
・
・
・
・
・
・
・
・

---------- CONNECT CLOUDSQL ----------
cloud_sql_proxy is running.

---------- EXECUTE COMMAND ----------
bundle exec rails db:migrate
rails aborted!
PG::ConnectionBad: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
/app/vendor/bundle/ruby/2.5.0/gems/pg-1.1.3/lib/pg.rb:56:in `initialize'
/app/vendor/bundle/ruby/2.5.0/gems/pg-1.1.3/lib/pg.rb:56:in `new'
/app/vendor/bundle/ruby/2.5.0/gems/pg-1.1.3/lib/pg.rb:56:in `connect'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:684:in `connect'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:215:in `initialize'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:40:in `new'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:40:in `postgresql_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:809:in `new_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `checkout_new_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:832:in `try_to_checkout_new_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:793:in `acquire_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:521:in `checkout'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:380:in `connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:1008:in `retrieve_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/connection_handling.rb:90:in `connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/tasks/database_tasks.rb:172:in `migrate'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/railties/databases.rake:60:in `block (2 levels) in <main>'
/app/vendor/bundle/ruby/2.5.0/gems/railties-5.2.1/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/app/vendor/bundle/ruby/2.5.0/gems/railties-5.2.1/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/app/vendor/bundle/ruby/2.5.0/gems/railties-5.2.1/lib/rails/command.rb:48:in `invoke'
/app/vendor/bundle/ruby/2.5.0/gems/railties-5.2.1/lib/rails/commands.rb:18:in `<main>'
/app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
/app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
/app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `block in require'
/app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:253:in `load_dependency'
/app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
ERROR
ERROR: build step 0 "gcr.io/google-appengine/exec-wrapper:latest" failed: exit status 1
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ERROR: (gcloud.builds.submit) build c15c595c-6d32-4aa0-9411-397d95b3f12e completed with status "FAILURE"
rails aborted!
AppEngine::Util::Gcloud::GcloudFailed: GCloud failed with result code 1
/home/user/project/vendor/bundle/ruby/2.5.0/gems/appengine-0.4.6/lib/appengine/util/gcloud.rb:175:in `execute'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/appengine-0.4.6/lib/appengine/exec.rb:302:in `start'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/appengine-0.4.6/lib/appengine/tasks.rb:255:in `start_and_report_errors'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/appengine-0.4.6/lib/appengine/tasks.rb:155:in `block in setup_exec_task'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/railties-5.2.1/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/railties-5.2.1/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/railties-5.2.1/lib/rails/command.rb:48:in `invoke'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/railties-5.2.1/lib/rails/commands.rb:18:in `<main>'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `block in require'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:253:in `load_dependency'
/home/user/project/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => appengine:exec
(See full trace by running task with --trace)

一旦 googleapi: error 403地獄から抜け出しました。

あとはPG::ConnectionBadの解決のみ。

後日書きます。

追記

すぐに解決しました。

config/database.ymlproductionhostを設定してなかったせいでした。

app.yaml構成はこんな感じです。

runtime: ruby
env: flex
entrypoint: bundle exec rackup --port $PORT
skip_files:
  - ^vendor
automatic_scaling:
  min_num_instances: 1
  max_num_instances: 5
  cool_down_period_sec: 120
  cpu_utilization:
    target_utilization: 0.6
beta_settings:
  cloud_sql_instances:  <INSTANCE_CONNECTION_NAME>
threadsafe: true
includes:
  - secret.yaml

secret.yamlに必要な環境変数をセットしてます。

database.ymlのpasswordやhost、socketもここに書いてます。

database.ymlがこんな感じ。

・
・
・
・
・
・
・
・
・
production:
  <<: *default
  host: <%= ENV['POSTGRES_HOST'] %>
  database: <%= ENV['POSTGRES_DATABASE'] %>
  username: <%= ENV['POSTGRES_USER'] %>
  password: <%= ENV['POSTGRES_PASSWORD'] %>
  socket: <%= ENV['CLOUDSQL_STAGING_SOCKET'] %>

環境変数名が違いますが、 sockethost は同じで /cloudsql/<INSTANCE_CONNECTION_NAME> をセットしてます。

hostを設定し、デプロイ後もう一度試したら念願のマイグレーションが通りました。

ホント長かった。

もうこの手順は忘れません。

【Rails】Heroku + Rails + Sendgridでメール送信ができない

f:id:rdwbocungelt5:20181022120235p:plain

どうもてぃです。

今回のことで3日ほど時間を溶かしました。

同じようなことではまってる人が見当たらなかったので、記事にします。

環境

以下僕がやった設定を書きます。

公式ドキュメントの設定

Heroku公式のドキュメント通りにやっていきました。

ドキュメント通りです。まじで。

devcenter.heroku.com

今回はstaging環境を作成し、Herokuへあげました。

設定はproductionをまるまるコピーしてconfig/environments/staging.rbを作成し、上記ドキュメント通りにやった感じです。

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.smtp_settings = {
    user_name: ENV['SENDGRID_USERNAME'],
    password: ENV['SENDGRID_PASSWORD'],
    domain: 'yourdomain.com',
    address: 'smtp.sendgrid.net',
    port: 587,
    authentication: :plain,
    enable_starttls_auto: true
  }

↑をぶち込んだだけ。

Heroku環境変数の設定

Herokuの環境変数heroku configで確認できます。

=== Config Vars
DATABASE_URL:                                      <database url>
HEROKU_POSTGRESQL_NAVY_URL:   <navy postgresql url>
LANG:                                                        en_US.UTF-8
RACK_ENV:                                               staging
RAILS_ENV:                                               staging
RAILS_LOG_TO_STDOUT:                       enabled
RAILS_SERVE_STATIC_FILES:                enabled
SECRET_KEY_BASE:                              <your secret key>
SENDGRID_PASSWORD:                        <your sendgrid password>
SENDGRID_USERNAME:                        <your sendgrid username>

もしくは、 ダッシュボード Settings配下の Config Varsの「Reveal Config Vars」で確認することが出来ます。

f:id:rdwbocungelt5:20181023130534p:plain

RACK_ENVRAILS_ENVstagingへ、SENDGRID_PASSWORDSENDGRID_USERNAMEheroku addons:add sendgrid:starterをやった際に自動的に設定されます。 ます ここで足りないのは、 SENDGRID_API_KEY

Sendgridの画面から生成して設定をします。

$ heroku addons:open sendgrid

でブラウザとともに、Sendgridのダッシュボードが開きます。

開いたら、 Settings -> API Keysを開き、 Create API Keyをクリック。

f:id:rdwbocungelt5:20181023133406p:plain

適当なAPI Key Nameを設定し、アクセスはFull Accessへ(プロジェクトに応じて変更してください)。

Create & Viewをクリックすると、以下のような画面でkeyが表示されます。

f:id:rdwbocungelt5:20181023141316p:plain

黒く塗りつぶしているところをクリックするとコピーされるので、これをターミナル上からHerokuの環境変数に設定します。

$ heroku config:set SENDGRID_API_KEY='さっきコピーしたKey'

以上、必要な設定は終わり。

これで、適当なところでActionMailerを動かせばいい。

メールが届かない

書いてる通りに設定を行って、いろんな記事を渡り歩いたんですが、メールが送信できない。

というよりも、メール送信はできているけど、Sendgrid経由でメールが送信できていないようでした。

ローカルから実際にSendgrid経由でメールを送ってみたコードが以下(公式に書いてるのと同じ)。

github.com

gemの dotenv.envを設定し、実際に動かしたのですが、Sendgrid経由でメール送信できている。

SendgridのダッシュボードのActivityにも表示されている。API Keyに問題があるわけではなさそう。

次にheroku run rails cで実際にActionMailerでメールを送信してみましたが、コンソール上にはメール送信が完了したとのログしか残らない。

エラーも表示されない。。。 どうしたものか。

設定を見なおした

trelloを見なおしていたら、config/environments/staging.rbがなかった時に別でメール設定を行っていたことが発覚しました。

それが、 config/initializers/mail.rb、こいつ。

if Rails.env.production?
  ActionMailer::Base.delivery_method = :smtp
  ActionMailer::Base.smtp_settings = {
    address: 'smtp.gmail.com',
    domain: 'gmail.com',
    port: 587,
    user_name: ENV['MAIL_ADDRESS'],
    password: ENV['MAIL_PASSWORD'],
    authentication: 'plain',
    enable_starttls_auto: true,
  }
elsif Rails.env.development?
  ActionMailer::Base.delivery_method = :letter_opener
else
  ActionMailer::Base.delivery_method = :test
end

config/environments/staging.rbにメールの設定を書いているのですが、実際にメールが送られるときにはinitializers配下が実行されるようで、何も設定していなかった僕はstaging環境ではすべてtest環境が動くようになっていたのです。

こりゃあ、全部Sendgrid経由でメールが送れないわけですわ。

ということで、config/initializers/mail.rbへHeroku用にstagingの設定を書き込みました。

if Rails.env.production?
  ActionMailer::Base.delivery_method = :smtp
  ActionMailer::Base.smtp_settings = {
    address: 'smtp.gmail.com',
    domain: 'gmail.com',
    port: 587,
    user_name: ENV['MAIL_ADDRESS'],
    password: ENV['MAIL_PASSWORD'],
    authentication: 'plain',
    enable_starttls_auto: true,
  }
elsif Rails.env.development?
  ActionMailer::Base.delivery_method = :letter_opener
elsif Rails.env.staging?
  ActionMailer::Base.delivery_method = :smtp
  ActionMailer::Base.default_url_options = { host: 'yourhost.com' }
  ActionMailer::Base.perform_deliveries = true
  ActionMailer::Base.smtp_settings = {
    user_name: ENV['SENDGRID_USERNAME'],
    password: ENV['SENDGRID_PASSWORD'],
    domain: 'herokuapp.com',
    address: 'smtp.sendgrid.net',
    port: 587,
    authentication: :plain,
    enable_starttls_auto: true
  }
else
  ActionMailer::Base.delivery_method = :test
end

解決

ちょうどGitHubが落ちてしまって、この設定のプルリクがマージできなかったのですが、今朝障害が直ったようでして、僕の方の障害も一緒に直りました。

さすがGitHub様様です。

elementaryOS(Ubuntu 16.04.5 LTS)でUSキーボードを使うと幸せになれる

どうもてぃです。

プログラマーたるもの、やはりUSキーボードでしょう。

経緯としては、やっぱりmacのようなトラックパッドを実現するのは、他社のPCでは厳しいというのと、せっかくならUSにしてみようと言う好奇心から買ってみました。

以下、現PCへの苦情まとめ。


  • タイピングをしていると、トラックパッドに手のひらが当たって入力できない
  • ctrl + t を押して新規タブを作成すると高確率でフォームにカーソルが行かない
  • 移動が思ったように行かない
  • キー配列がゴミ(一番右になんかおる)
  • やっぱり押し心地が悪い

etc...


f:id:rdwbocungelt5:20181015095849j:plain

とまあ、上げればキリが無いですが、毎日使うものなのでストレスはそれなりにたまります。

お金に余裕が出てきたので、とりあえずUSキーボード入門として安いやつ買ってみました。

ThinkPadのキーボード

いいですよね。

サブ機でちょっと前にThinkPad X280を使ってたのですが、赤◯首とキーの押し心地が凄い良い。

という思い出があったので、ThinkPadのキーボードを買いました。

これ。


飽食なのでアフィリリンク貼っときます。

bluetoothにしなかったのは、レビューにも合ったのですが、遅延のせいで制御があまりうまく行かなかったりするそうなので今回は見送りました。

援助していただいているスーパーエンジニアの方がHHKBを有線で使っていたのもあってそれに倣いました。

コードが邪魔なので、これも買いました。


ちょっと長くて、向きをミスりましたがいまのところ満足。不便なら買い換えます。

設定

ibuz-mozcfcitxで設定しました。

始めは以下の記事を参考に設定。

qiita.com

ただ、なぜか上の記事の「Mozcのキー設定変更」以下をやってみても、ctrl + spaceだと文字切り替えがうまく行かず。。。

独自のやり方で設定しました。

上の記事の「キーボードレイアウト変更」までやった状態です。

まず、左上のアプリケーションから「アプリの検索」で「Fcitx 設定」を開く。

f:id:rdwbocungelt5:20181015103728p:plain

次にタブで「全体の設定」へ行き、入力メソッドのオンオフを設定する。

僕の場合、スペース右のAltで変更できるようにしました。

macっぽく。

f:id:rdwbocungelt5:20181015103830p:plain

これだけで一旦設定は終了です。

ただ、これだと、US -> Mozc -> USと行くと、右上の表記はUSなのに、実際に打つ文字はJISになっているという現象が起きます。

なので、Mozcを解除した際に、USへ切り替えるように設定します(これ以外にも、キーボードレイアウトの状態を修正しても直らなかった)。

f:id:rdwbocungelt5:20181015104600p:plain

上タブの「入力メソッド」へ行き、左下の+を押下。

f:id:rdwbocungelt5:20181015104720p:plain

セレクトボックスの一番下にUSがあるので選択してOK。

f:id:rdwbocungelt5:20181015104803p:plain

この状態になったら、必要なくなったキーボード - 日本語を選択して、-で削除する。

f:id:rdwbocungelt5:20181015104913p:plain

最後にキーボード - 英語(US)を選択して、で優先順位を上げる。

f:id:rdwbocungelt5:20181015105023p:plain

これで完了。

超快適

f:id:rdwbocungelt5:20181015105530j:plain

あとは外付けキーボードを接続した際に、内蔵キーボードを無効化する設定をしたら終了なのですが、時間がかかりそうなのでまた今度やる。

めっちゃ快適ですー^^

壊れたらHHKB買います。


【Linux】Ubuntuにheroku CLIがインストールできない

f:id:rdwbocungelt5:20181009115813j:plain

段々と寒くなってきましたね。

どうもてぃです。

macだと簡単なheroku cliの導入に手こずりました。

環境

  • Ubuntu 16.04.5 LTS
  • elementary OS 0.4.1 Loki

現象

テスト環境でわりかしデプロイが簡単で多用されるheroku

デプロイコストが少なかったので使おうと思ったのですが、ubuntuでこんなにハマるとは思ってもみなかったですね。

公式ページを参考にやってみました。

devcenter.heroku.com

$ wget -qO- https://cli-assets.heroku.com/install-ubuntu.sh | sh
This script requires superuser access to install apt packages.
You will be prompted for your password by sudo.
[sudo] user のパスワード:                
+ dpkg -s apt-transport-https
+ echo deb https://cli-assets.heroku.com/apt ./
+ dpkg -s heroku-toolbelt
+ true
+ curl https://cli-assets.heroku.com/apt/release.key
+ apt-key add -
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1737  100  1737    0     0   9819      0 --:--:-- --:--:-- --:--:--  9869
OK
+ apt-get update
無視:1 http://dl.google.com/linux/chrome/deb stable InRelease
取得:2 https://dl.yarnpkg.com/debian stable InRelease [13.3 kB]                                                                            
取得:3 https://dl.winehq.org/wine-builds/ubuntu xenial InRelease [4,701 B]                                                                                                                    
取得:4 https://download.docker.com/linux/ubuntu zesty InRelease [49.8 kB]                                                                                                                     
取得:5 http://packages.elementary.io/appcenter xenial InRelease [6,128 B]                                                                                                               
取得:6 https://dl.yarnpkg.com/debian stable/main amd64 Packages [7,905 B]                                                                                                                     
取得:7 https://dl.yarnpkg.com/debian stable/main i386 Packages [7,905 B]                                                                                                                      
取得:8 https://dl.yarnpkg.com/debian stable/main all Packages [7,905 B]                                                                                                                       
取得:9 https://dl.winehq.org/wine-builds/ubuntu xenial/main amd64 Packages [43.3 kB]                                                                                                          
取得:10 https://cli-assets.heroku.com/apt ./ InRelease [2,533 B]                                                                                                                      
取得:11 http://dl.google.com/linux/chrome/deb stable Release [1,189 B]                                                                                                                        
取得:12 https://download.docker.com/linux/ubuntu zesty/stable amd64 Packages [2,477 B]                                                                                                 
取得:13 http://dl.google.com/linux/chrome/deb stable Release.gpg [819 B]                                                                                                                      
取得:14 https://cli-assets.heroku.com/apt ./ Packages [620 B]                                                                                                                       
取得:15 http://packages.elementary.io/appcenter xenial/main amd64 Packages [78.3 kB]                                                                     
取得:16 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]                                                            
取得:17 http://dl.google.com/linux/chrome/deb stable/main amd64 Packages [1,381 B]                                               
取得:18 http://ppa.launchpad.net/elementary-os/stable/ubuntu xenial InRelease [17.5 kB]                                               
取得:19 http://packages.elementary.io/appcenter xenial/main i386 Packages [614 B]                       
取得:20 http://packages.elementary.io/appcenter xenial/main amd64 DEP-11 Metadata [83.4 kB]          
取得:21 http://packages.elementary.io/appcenter xenial/main DEP-11 64x64 Icons [289 kB]                                                 
取得:22 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]                                                 
無視:23 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial InRelease                  
取得:24 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]                
取得:25 http://archive.ubuntu.com/ubuntu xenial-security InRelease [107 kB]                    
取得:26 http://ppa.launchpad.net/elementary-os/os-patches/ubuntu xenial InRelease [23.8 kB]
取得:27 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1,201 kB]
取得:28 http://archive.ubuntu.com/ubuntu xenial/main Translation-ja [290 kB]                                                
取得:29 http://ppa.launchpad.net/philip.scott/elementary-tweaks/ubuntu xenial InRelease [18.1 kB]
取得:30 http://archive.ubuntu.com/ubuntu xenial/main Translation-en [568 kB]                        
取得:31 http://archive.ubuntu.com/ubuntu xenial/main amd64 DEP-11 Metadata [733 kB]                      
取得:32 http://archive.ubuntu.com/ubuntu xenial/main DEP-11 64x64 Icons [409 kB]                                                                                
取得:33 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [8,344 B]                                                                          
取得:34 http://archive.ubuntu.com/ubuntu xenial/restricted Translation-en [2,908 B]                                        
取得:35 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 DEP-11 Metadata [186 B] 
取得:36 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [7,532 kB]                                              
取得:37 http://ppa.launchpad.net/ubuntuhandbook1/audacity/ubuntu xenial InRelease [17.5 kB]                                     
取得:38 http://archive.ubuntu.com/ubuntu xenial/universe Translation-ja [1,034 kB]                                            
取得:39 http://archive.ubuntu.com/ubuntu xenial/universe Translation-en [4,354 kB]                                                                        
取得:40 http://archive.ubuntu.com/ubuntu xenial/universe amd64 DEP-11 Metadata [3,410 kB]                                                                 
取得:41 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial InRelease [17.5 kB]               
取得:42 http://archive.ubuntu.com/ubuntu xenial/universe DEP-11 64x64 Icons [7,448 kB]                 
取得:43 http://ppa.launchpad.net/elementary-os/stable/ubuntu xenial/main Sources [21.4 kB]                                                                                                    
取得:44 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [144 kB]                                                                                                            
取得:45 http://archive.ubuntu.com/ubuntu xenial/multiverse Translation-ja [8,704 B]                                                                                                           
取得:46 http://archive.ubuntu.com/ubuntu xenial/multiverse Translation-en [106 kB]                                                                                                            
取得:47 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 DEP-11 Metadata [63.8 kB]                                                                                                    
取得:48 http://archive.ubuntu.com/ubuntu xenial/multiverse DEP-11 64x64 Icons [230 kB]                                                                                                        
取得:49 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [856 kB]                                                                                                          
取得:50 http://archive.ubuntu.com/ubuntu xenial-updates/main Translation-en [349 kB]                                                                                                          
取得:51 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 DEP-11 Metadata [320 kB]                                                                                                   
取得:52 https://packagecloud.io/slacktechnologies/slack/debian jessie InRelease [23.5 kB]                                                                                                     
取得:53 http://archive.ubuntu.com/ubuntu xenial-updates/main DEP-11 64x64 Icons [237 kB]                                                                                                      
取得:54 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [7,556 B]                                                                                                   
取得:55 http://archive.ubuntu.com/ubuntu xenial-updates/restricted Translation-en [2,272 B]                                                                                                   
取得:56 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 DEP-11 Metadata [157 B]                                                                                              
取得:57 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [690 kB]                                                                                                      
取得:58 http://archive.ubuntu.com/ubuntu xenial-updates/universe Translation-en [279 kB]                                                                                                      
取得:59 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 DEP-11 Metadata [247 kB]                                                                                               
取得:60 http://ppa.launchpad.net/elementary-os/stable/ubuntu xenial/main amd64 Packages [40.0 kB]                                                                                             
取得:61 http://archive.ubuntu.com/ubuntu xenial-updates/universe DEP-11 64x64 Icons [335 kB]                                                                                                  
取得:62 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [16.4 kB]                                                                                                   
取得:63 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse Translation-en [8,344 B]                                                                                                   
取得:64 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 DEP-11 Metadata [5,968 B]                                                                                            
取得:65 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse DEP-11 64x64 Icons [14.3 kB]                                                                                               
取得:66 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [7,304 B]                                                                                                       
取得:67 http://archive.ubuntu.com/ubuntu xenial-backports/main Translation-en [4,456 B]                                                                                                       
取得:68 https://packagecloud.io/slacktechnologies/slack/debian jessie/main amd64 Packages [9,293 B]                                                                                           
取得:69 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 DEP-11 Metadata [3,328 B]                                                                                                
取得:70 http://archive.ubuntu.com/ubuntu xenial-backports/main DEP-11 64x64 Icons [29 B]                                                                                                      
取得:71 http://archive.ubuntu.com/ubuntu xenial-backports/restricted amd64 DEP-11 Metadata [194 B]                                                                                            
取得:72 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [7,804 B]                                                                                                   
取得:73 http://archive.ubuntu.com/ubuntu xenial-backports/universe Translation-en [4,184 B]                                                                                                   
取得:74 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 DEP-11 Metadata [5,104 B]                                                                                            
無視:75 https://packagecloud.io/slacktechnologies/slack/debian jessie/main i386 Packages                                                                                                      
取得:76 http://archive.ubuntu.com/ubuntu xenial-backports/universe DEP-11 64x64 Icons [1,789 B]                                                                                               
取得:77 http://archive.ubuntu.com/ubuntu xenial-backports/multiverse amd64 DEP-11 Metadata [216 B]                                                                                            
取得:78 http://archive.ubuntu.com/ubuntu xenial-backports/multiverse DEP-11 64x64 Icons [29 B]                                                                                                
取得:79 http://archive.ubuntu.com/ubuntu xenial-security/main amd64 Packages [563 kB]                                                                                                         
取得:75 https://packagecloud.io/slacktechnologies/slack/debian jessie/main i386 Packages [4,991 B]                                                                                            
取得:80 http://archive.ubuntu.com/ubuntu xenial-security/main Translation-en [237 kB]                                                                                                         
取得:81 http://archive.ubuntu.com/ubuntu xenial-security/main amd64 DEP-11 Metadata [67.7 kB]                                                                                                 
取得:82 http://archive.ubuntu.com/ubuntu xenial-security/main DEP-11 64x64 Icons [68.0 kB]                                                                                                    
取得:83 http://archive.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [7,204 B]                                                                                                  
取得:84 http://archive.ubuntu.com/ubuntu xenial-security/restricted Translation-en [2,152 B]                                                                                                  
取得:85 http://archive.ubuntu.com/ubuntu xenial-security/restricted amd64 DEP-11 Metadata [200 B]                                                                                             
取得:86 http://ppa.launchpad.net/elementary-os/stable/ubuntu xenial/main i386 Packages [39.9 kB]                                                                                              
取得:87 http://archive.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [387 kB]                                                                                                     
取得:88 http://archive.ubuntu.com/ubuntu xenial-security/universe Translation-en [147 kB]                                                                                                     
取得:89 http://archive.ubuntu.com/ubuntu xenial-security/universe amd64 DEP-11 Metadata [108 kB]                                                                                              
取得:90 http://archive.ubuntu.com/ubuntu xenial-security/universe DEP-11 64x64 Icons [145 kB]                                                                                                 
取得:91 http://archive.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [3,460 B]                                                                                                  
取得:92 http://archive.ubuntu.com/ubuntu xenial-security/multiverse Translation-en [1,744 B]                                                                                                  
取得:93 http://archive.ubuntu.com/ubuntu xenial-security/multiverse amd64 DEP-11 Metadata [212 B]                                                                                             
取得:94 http://archive.ubuntu.com/ubuntu xenial-security/multiverse DEP-11 64x64 Icons [29 B]                                                                                                 
取得:95 http://ppa.launchpad.net/elementary-os/stable/ubuntu xenial/main Translation-en [13.0 kB]                                                                                             
無視:96 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial Release                                                                                                                      
取得:97 http://ppa.launchpad.net/elementary-os/os-patches/ubuntu xenial/main Sources [24.0 kB]                                                                                                
取得:98 http://ppa.launchpad.net/elementary-os/os-patches/ubuntu xenial/main amd64 Packages [57.1 kB]                                                                                         
取得:99 http://ppa.launchpad.net/elementary-os/os-patches/ubuntu xenial/main i386 Packages [56.6 kB]                                                                                          
取得:100 http://ppa.launchpad.net/elementary-os/os-patches/ubuntu xenial/main Translation-en [28.0 kB]                                                                                        
取得:101 http://ppa.launchpad.net/philip.scott/elementary-tweaks/ubuntu xenial/main amd64 Packages [816 B]                                                                                    
取得:102 http://ppa.launchpad.net/philip.scott/elementary-tweaks/ubuntu xenial/main i386 Packages [820 B]                                                                                     
取得:103 http://ppa.launchpad.net/philip.scott/elementary-tweaks/ubuntu xenial/main Translation-en [304 B]                                                                                    
取得:104 http://ppa.launchpad.net/ubuntuhandbook1/audacity/ubuntu xenial/main amd64 Packages [988 B]                                                                                          
取得:105 http://ppa.launchpad.net/ubuntuhandbook1/audacity/ubuntu xenial/main i386 Packages [992 B]                                                                                           
取得:106 http://ppa.launchpad.net/ubuntuhandbook1/audacity/ubuntu xenial/main Translation-en [492 B]                                                                                          
取得:107 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial/main amd64 Packages [1,556 B]                                                                                                
取得:108 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial/main i386 Packages [1,556 B]                                                                                                 
取得:109 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial/main Translation-en [928 B]                                                                                                  
無視:110 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main amd64 Packages                                                                                                         
無視:111 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main i386 Packages                                                                                                          
無視:112 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main all Packages                                                                                                           
無視:113 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-ja_JP                                                                                                      
無視:114 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-ja                                                                                                         
無視:115 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-en                                                                                                         
無視:116 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main amd64 DEP-11 Metadata                                                                                                  
無視:117 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main DEP-11 64x64 Icons                                                                                                     
無視:110 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main amd64 Packages                                                                                                         
無視:111 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main i386 Packages                                                                                                          
無視:112 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main all Packages                                                                                                           
無視:113 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-ja_JP                                                                                                      
無視:114 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-ja                                                                                                         
無視:115 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-en                                                                                                         
無視:116 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main amd64 DEP-11 Metadata                                                                                                  
無視:117 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main DEP-11 64x64 Icons                                                                                                     
無視:110 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main amd64 Packages                                                                                                         
無視:111 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main i386 Packages                                                                                                          
無視:112 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main all Packages                                                                                                           
無視:113 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-ja_JP                                                                                                      
無視:114 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-ja
無視:115 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-en
無視:116 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main amd64 DEP-11 Metadata
無視:117 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main DEP-11 64x64 Icons
無視:110 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main amd64 Packages
無視:111 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main i386 Packages
無視:112 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main all Packages
無視:113 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-ja_JP
無視:114 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-ja
無視:115 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-en
無視:116 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main amd64 DEP-11 Metadata
無視:117 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main DEP-11 64x64 Icons
無視:110 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main amd64 Packages
無視:111 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main i386 Packages
無視:112 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main all Packages
無視:113 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-ja_JP
無視:114 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-ja
無視:115 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-en
無視:116 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main amd64 DEP-11 Metadata
無視:117 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main DEP-11 64x64 Icons
エラー:110 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main amd64 Packages
  404  Not Found
無視:111 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main i386 Packages
無視:112 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main all Packages
無視:113 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-ja_JP
無視:114 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-ja
無視:115 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main Translation-en
無視:116 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main amd64 DEP-11 Metadata
無視:117 http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial/main DEP-11 64x64 Icons
34.3 MB を 45秒 で取得しました (750 kB/s)
パッケージリストを読み込んでいます... 完了
W: リポジトリ http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu xenial Release には Release ファイルがありません。
N: このようなリポジトリから取得したデータは認証できないので、データの使用は潜在的に危険です。
N: リポジトリの作成とユーザ設定の詳細は、apt-secure(8) man ページを参照してください。
E: http://ppa.launchpad.net/hunter-kaller/ppa/ubuntu/dists/xenial/main/binary-amd64/Packages の取得に失敗しました  404  Not Found
E: いくつかのインデックスファイルのダウンロードに失敗しました。これらは無視されるか、古いものが代わりに使われます。

$ heroku --version
heroku: コマンドが見つかりません

heroku: command not found

つらい。

だめだった対処法

まず、herokuがあるかどうか確認。

$ sudo updatedb

$ sudo locate heroku
/var/lib/apt/lists/cli-assets.heroku.com_apt_._InRelease
/var/lib/apt/lists/cli-assets.heroku.com_apt_._Packages

wgetで取ったはずなのにない。

次にaptのパッケージupdateミスのせいかと思い、いろいろ試した。

以下の記事を参考にした。

qiita.com

これでもapt updateがうまく行かず、ダメだった。

最終的な対処法

npmをつかってheroku cliを入れました。

npm install -g heroku-cliはもう使われてないそうなので、npm install -g herokuを入れる。

$ sudo npm install -g heroku
/usr/local/bin/heroku -> /usr/local/lib/node_modules/heroku/bin/run
+ heroku@7.16.6
added 375 packages from 264 contributors in 11.284s

$ heroku --version
heroku/7.16.6 linux-x64 node-v10.2.1

やっと解決。

終わりに

デプロイコストがないとはいいましたが、heroku環境作るのに一日も費やしてしまいました。

反省してます。