How do i connect my postgres db in heroku? - ruby

I am learning rack and I built a test todo app. My test app uses postgres db. I am trying to deploy my app to heroku free account.
Currenlty I am trying to connect to postgres using this
DB = PG.connect :hostaddr => "localhost", :port => 5432, :dbname => 'testdb', :user => "postgres", :password => "postgres"
I checked my logs because my app was not workign when I visited the url and found out
2017-06-01T13:22:35.540907+00:00 app[web.1]: Puma starting in single mode...
2017-06-01T13:22:35.540929+00:00 app[web.1]: * Version 3.8.2 (ruby 2.3.4-p301), codename: Sassy Salamander
2017-06-01T13:22:35.540930+00:00 app[web.1]: * Min threads: 5, max threads: 5
2017-06-01T13:22:35.540975+00:00 app[web.1]: * Environment: production
2017-06-01T13:22:35.583653+00:00 app[web.1]: ! Unable to load application: PG::ConnectionBad: could not connect to server: Connection refused
2017-06-01T13:22:35.583655+00:00 app[web.1]: Is the server running on host "127.0.0.1" and accepting
2017-06-01T13:22:35.583656+00:00 app[web.1]: TCP/IP connections on port 5432?
2017-06-01T13:22:35.583775+00:00 app[web.1]: bundler: failed to load command: puma (/app/vendor/bundle/ruby/2.3.0/bin/puma)
2017-06-01T13:22:35.583838+00:00 app[web.1]: PG::ConnectionBad: could not connect to server: Connection refused
2017-06-01T13:22:35.583839+00:00 app[web.1]: Is the server running on host "127.0.0.1" and accepting
2017-06-01T13:22:35.583840+00:00 app[web.1]: TCP/IP connections on port 5432?
Edit 1
I found the postgres db creds in the heroku and used that and got this err
2017-06-01T14:27:11.889525+00:00 app[web.1]: Puma starting in single mode...
2017-06-01T14:27:11.889599+00:00 app[web.1]: * Version 3.8.2 (ruby 2.3.4-p301), codename: Sassy Salamander
2017-06-01T14:27:11.889633+00:00 app[web.1]: * Min threads: 5, max threads: 5
2017-06-01T14:27:11.889671+00:00 app[web.1]: * Environment: production
2017-06-01T14:27:11.934347+00:00 app[web.1]: ! Unable to load application: PG::ConnectionBad: could not translate host name "ec2-23-23-234-118.compute-1.amazonaws.com" to address: Name or service not known
2017-06-01T14:27:11.934473+00:00 app[web.1]: bundler: failed to load command: puma (/app/vendor/bundle/ruby/2.3.0/bin/puma)
2017-06-01T14:27:11.934522+00:00 app[web.1]: PG::ConnectionBad: could not translate host name "ec2-23-23-234-118.compute-1.amazonaws.com" to address: Name or service not known

You can get your database config on Heroku from the DATABASE_URL environment variable:
ENV['DATABASE_URL']
That is a connection URL which looks like this:
postgres://$user:$pass#$db_host.com:5432/$db_name
You can pass that string directly to your PG client initializer:
DB = PG.connect ENV['DATABASE_URL']
DB is now your connected db client.

Using this solved it DB = PG.connect ENV["HEROKU_POSTGRESQL_SILVER_URL"]

Related

Run a ruby script in a docker container which connects to mongodb localhost port

I am a newbie with docker and trying my hands onto it. I am facing a minor problem, your help in any way would be appreciated!
I have a ruby script in which I am connecting to my localhost port which is assigned to my mongodb database. I am using MongoClient to connect to the database from the script:
clientDB = Mongo::Client.new(["localhost:37017"], :database => 'Database', :user => 'user', :password => 'password')
or
clientDB = Mongo::Client.new(["127.0.0.1:37017"], :database => 'Database', :user => 'user', :password => 'password')
If I run the script using ruby monthly_count_script.rb command, it works perfectly but as I am exploring Docker, I want to run that script into a container. So to run a container I am using following command
docker run -it --rm --name my-running-script -v "$PWD":/usr/src/app -v "$PWD"/../gems:/usr/local/bundle -w /usr/src/app --net=host ruby ruby monthly_count_script.rb
Unfortunately I am keep getting following error and I don't know why it is not able to connect to the localhost 37017 port even though my database is working properly and I am able to connect to the database using Robomongo or mongodb console.
This is the error log:
D, [2018-06-04T14:15:11.527381 #1] DEBUG -- : MONGODB | Topology type 'single' initializing.
D, [2018-06-04T14:15:11.527639 #1] DEBUG -- : MONGODB | Server 127.0.0.1:37017 initializing.
D, [2018-06-04T14:15:11.529252 #1] DEBUG -- : MONGODB | Connection refused - connect(2) for 127.0.0.1:37017
D, [2018-06-04T14:15:11.530774 #1] DEBUG -- : MONGODB | Topology type 'single' initializing.
D, [2018-06-04T14:15:11.531058 #1] DEBUG -- : MONGODB | Server 127.0.0.1:37017 initializing.
D, [2018-06-04T14:15:11.532518 #1] DEBUG -- : MONGODB | Connection refused - connect(2) for 127.0.0.1:37017
D, [2018-06-04T14:15:12.032037 #1] DEBUG -- : MONGODB | Connection refused - connect(2) for 127.0.0.1:37017
D, [2018-06-04T14:15:12.533348 #1] DEBUG -- : MONGODB | Connection refused - connect(2) for 127.0.0.1:37017
D, [2018-06-04T14:15:13.036087 #1] DEBUG -- : MONGODB | Connection refused - connect(2) for 127.0.0.1:37017
Looking forward to your help/guidance.
FYI I was following this documentation for running a ruby script in docker:
https://docs.docker.com/samples/library/ruby/
Local ports are not available from inside Docker containers, you only can connect to ports opened in the container or from other containers with --link.
To connect to host's ports you should use the IP for the docker network's gateway.
To get the gateway IP of your container run:
docker inspect --format='{{range .NetworkSettings.Networks}}{{.Gateway}}{{end}}'
Usually it will return 172.17.0.1 (I repeat, this answer is only valid for default network configurations, assuming that the container is connected to a bridge network)
Then you can update the mongo client config:
clientDB = Mongo::Client.new(["172.17.0.1:37017"], :database => 'Database', :user => 'user', :password => 'password')
You can access mongodb on your computer from docker with host.docker.internal. This (or a similar, can't remember) SO-thread helped me.

"PG::ConnectionBad - could not connect to server: Connection refused" Sinatra server on Heroku

My Mad Libs program (code, Heroku app; earlier version on repl.it) runs fine in my Ubuntu VirtualBox, and for a few minutes after pushing the code to Heroku, it was actually working on Heroku, too. Then after maybe 15-30 minutes either I did something or something happened on the server, and ever since the app page says "Internal Server Error" and the relevant part of server logs read:
2018-04-14T02:03:45.717579+00:00 heroku[web.1]: Starting process with command `bundle exec ruby app.rb -p 7330`
2018-04-14T02:03:48.443468+00:00 app[web.1]: [2018-04-14 02:03:48] INFO ruby 2.4.0 (2016-12-24) [x86_64-linux]
2018-04-14T02:03:48.443866+00:00 app[web.1]: == Sinatra (v2.0.1) has taken the stage on 7330 for production with backup from WEBrick
2018-04-14T02:03:48.443437+00:00 app[web.1]: [2018-04-14 02:03:48] INFO WEBrick 1.3.1
2018-04-14T02:03:48.444166+00:00 app[web.1]: [2018-04-14 02:03:48] INFO WEBrick::HTTPServer#start: pid=4 port=7330
2018-04-14T02:03:48.855874+00:00 heroku[web.1]: State changed from starting to up
2018-04-14T02:04:04.372745+00:00 heroku[router]: at=info method=GET path="/" host=youmadlibs.herokuapp.com request_id=118d681f-3402-44aa-9d04-75d930461195 fwd="74.135.49.116" dyno=web.1 connect=0ms service=20ms status=500 bytes=854 protocol=https
2018-04-14T02:04:04.368607+00:00 app[web.1]: 2018-04-14 02:04:04 - PG::ConnectionBad - could not connect to server: Connection refused
2018-04-14T02:04:04.368644+00:00 app[web.1]: Is the server running on host "localhost" (127.0.0.1) and accepting
2018-04-14T02:04:04.368648+00:00 app[web.1]: TCP/IP connections on port 5432?
2018-04-14T02:04:04.368651+00:00 app[web.1]: :
2018-04-14T02:04:04.368653+00:00 app[web.1]: /app/vendor/bundle/ruby/2.4.0/gems/pg-1.0.0/lib/pg.rb:56:in `initialize'
2018-04-14T02:04:04.368654+00:00 app[web.1]: /app/vendor/bundle/ruby/2.4.0/gems/pg-1.0.0/lib/pg.rb:56:in `new'
2018-04-14T02:04:04.368656+00:00 app[web.1]: /app/vendor/bundle/ruby/2.4.0/gems/pg-1.0.0/lib/pg.rb:56:in `connect'
2018-04-14T02:04:04.368659+00:00 app[web.1]: /app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:684:in `connect'
etc.
The relevant lines seem to be:
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?
I've tried a bunch of stuff, like restarting the server, reducing the dynos to 0 and then re-upping to 1, and running $ heroku run psql -h HEROKU_POSTGRESQL_SILVER_URL -U deploy youmadlibs . I read the Heroku documentation for connecting Ruby to databases, and for database.yml, it has host: localhost. I tried that (despite the fact that it had worked before with the setting I have below, which I took from a tutorial). I also tried a different set of database.yml settings from this tutorial. No dice! I read many answers here and elsewhere and can't figure it out. How can I get the database-server connection reestablished?
database.yml:
development:
adapter: sqlite3
database: db/madlibs.sqlite
host: localhost
pool: 5
timeout: 5000
production:
url: <%= ENV['DATABASE_URL'] %>
adapter: postgresql
database: mydb
host: <%= ENV['DATABASE_HOST'] %>
database: <%= ENV['DATABASE_NAME'] %>
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
pool: 5
timeout: 5000
.env:
DATABASE_URL=<...my database name...>
config/environment.rb:
configure :development do
set :database, {adapter: "sqlite3", database: "db/madlibs.sqlite3"}
set :show_exceptions, true
end
configure :production do
db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
end
Gemfile:
source 'https://rubygems.org'
ruby '2.4.0'
gem 'sinatra', '2.0.1'
gem 'pg', '1.0.0'
gem 'activerecord', '5.2.0'
gem 'sinatra-activerecord', '2.0.13'
gem 'sinatra-flash', '0.3.0' # Probably not nec.
gem 'sinatra-redirect-with-flash', '0.2.1' # Probably not nec.
gem 'rake', '10.4.2'
gem 'rack', '2.0.3'
gem 'sass', '3.4.24'
gem 'json', '2.1.0'
group :development do
gem 'sqlite3', '1.3.13'
gem 'tux', '0.3.0' # Maybe unnec.
end
group :production do
end
The top of app.rb:
require 'sinatra'
require 'sinatra/activerecord'
require './config/environment'
enable :sessions
class Madlib < ActiveRecord::Base
end
You don't need the other lines if you have the db url, it can be a 1 liner, and also let postgres timeout it's default, no need to set it. You probably want a larger pool size in prod. Go to the postgres database you should have added to your app in Heroku. Click on the View Credentials button. Copy the data from there to your local .env file and also add .env to your .gitignore file because you don't want to commit your private credentials to your public repo.
Then on heroku
database.yml:
development:
adapter: sqlite3
database: db/madlibs.sqlite
host: localhost
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: unicode
host: <%= ENV['DB_HOST'] %>
username: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
port: <%= ENV['DB_PORT'] %>
database: <%= ENV['DB_NAME'] %>
pool: 25
Alternatively, you could just use 1 environment variable which concatenates all of this into a URI which would look something like
#postgresql://username:password#db-shared-us-east-1-or-something.blah:1234/db
#If you use this format you can use just a single environment variable somthinglike
production:
adapter: postgresql
encoding: unicode
url: <%= ENV['DATABASE_URI'] %>
pool: 25
In either case you will need to set whatever environment variables you'll use in your database.yml file set in your Heroku dashboard at: https://dashboard.heroku.com/apps/yourappname/settings and click on Reveal Config Vars. This is where you will set the needed vars as this is where your app will load them from. Set either the vars in my first example, or a single one as in my 2nd example. Either way should work.
Make sure you have the pg gem in your production group in Gemfile. And make sure you've set your environment variables on your heroku server.

rake aborted! PG::ConnectionBad: fe_sendauth: no password supplied sinatra

I can't seem to get this to work. I try to run bundle exec rake db:migrate after running bundle exec rake db:create and it gives me this error:
rake aborted!
PG::ConnectionBad: fe_sendauth: no password supplied
/home/david/DBC/survey-gorilla-challenge/Rakefile:106:in block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
I've tried looking around, but it all seems like a solution for rails, whereas I'm using this with Sinatra and activerecord. I'm not sure if that makes any difference. I even tried changing my pg_hba.conf, which looks like this currently:
IPv4 local connections:
host all all 127.0.0.1/32 md5
IPv6 local connections:
host all all ::1/128 md5
I'm not sure how to get this working. Any help is appreciated.
I solved it. I changed my pg_hba.conf to
TYPE DATABASE USER ADDRESS METHOD
"local" is for Unix domain socket connections only
host all all 127.0.0.1/32 trust
IPv4 local connections:
host all PC 127.0.0.1/32 trust
IPv6 local connections:
host all all ::1/128 trust
and then restarted postgres with sudo nano /etc/postgresql/9.3/main/pg_hba.conf. This gets the migration going.

heroku using gulp - no error in logs - still

I am trying to setup heroku deploy using gulp...
2015-05-31T12:39:43.160121+00:00 app[web.1]: [12:39:43] Webserver
started at http://localhost:24018
2015-05-31T12:39:43.208954+00:00
app[web.1]: [12:39:43] Finished 'serve' after 544 ms
2015-05-31T12:40:39.255239+00:00 heroku[web.1]: Error R10 (Boot
timeout) -> Web process failed to bind to $PORT within 60 seconds of
launch
2015-05-31T12:40:39.255427+00:00 heroku[web.1]: Stopping
process with SIGKILL
as per the logs webserver does get started successfully at given port.. below is how I am getting
port: process.env.PORT || 8080,
so is there any other port heroku expects application start or is there a way one can know which port heroku is checking??
note : the app does starts successfully if I do forman start web, so what can be the issue here?? though forman starts the application at a port 5000. I tried to hardcore port to 5000 but still got the same error as above...
the problem was i am using gulp-webserver which defaults to localhost, just need to change that to host: '0.0.0.0'
gulp.task('heroku', ['wiredep','inject'], function () {
return gulp.src(config.base)
.pipe(plugins.webserver({
host: '0.0.0.0',
port: process.env.PORT,
livereload: false,
open: false
}));
});

ruby - check ssh connection to a remote through another machine

I am using aws sdk in order to create machines, and as soon as the machines are created, i want to check ssh connection to those machines.
the problem is - from my machines, i don't have access to EC2 machines, but there is another machine on my network, which i have access to through ssh, and this machine do have access to the EC2 machines.
Now, is there a simple way through ruby to check ssh to EC2 machine ?
I've tried the following:
proxy = Net::SSH::Proxy::HTTP.new('<proxy ip>', 22)
Net::SSH.start('<EC2 machine's IP>', '<USER>', :proxy => proxy) do |ssh|
end
Errno::ECONNRESET: Connection reset by peer
from /home/galt/.rvm/gems/ruby-1.9.3-p484/gems/net-ssh-2.7.0/lib/net/ssh/proxy/http.rb:76:in `gets'
from /home/galt/.rvm/gems/ruby-1.9.3-p484/gems/net-ssh-2.7.0/lib/net/ssh/proxy/http.rb:76:in `parse_response'
from /home/galt/.rvm/gems/ruby-1.9.3-p484/gems/net-ssh-2.7.0/lib/net/ssh/proxy/http.rb:62:in `open'
from /home/galt/.rvm/gems/ruby-1.9.3-p484/gems/net-ssh-2.7.0/lib/net/ssh/transport/session.rb:67:in `block in initialize'
from /home/galt/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/1.9.1/timeout.rb:55:in `timeout'
from /home/galt/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/1.9.1/timeout.rb:100:in `timeout'
from /home/galt/.rvm/gems/ruby-1.9.3-p484/gems/net-ssh-2.7.0/lib/net/ssh/transport/session.rb:67:in `initialize'
from /home/galt/.rvm/gems/ruby-1.9.3-p484/gems/net-ssh-2.7.0/lib/net/ssh.rb:200:in `new'
from /home/galt/.rvm/gems/ruby-1.9.3-p484/gems/net-ssh-2.7.0/lib/net/ssh.rb:200:in `start'
from (irb):12
from /home/galt/.rvm/rubies/ruby-1.9.3-p484/bin/irb:12:in `<main>'
thanks !
The Net::SSH::Proxy::HTTP class is meant to point to a HTTP proxy for your ssh connections. So, you will need to set up a HTTP proxy on that machine to be able to proxy your connections through it.
If you can't do that, I suggest you use SSH reverse tunnels. We will achieve this by opening the port 8080 on the local machine, forward the traffic sent there to Server using the SSH tunnel and make Server forward it to the EC2 instance on port 22. The net-ssh ruby gem already provide commands to help us with that, but we will need to run 2 sessions in parallel, using threads:
proxy_thread = Thread.new do
Net::SSH.start('<proxy ip>', '<user>') do |ssh|
ssh.forward.remote(8080, "<AWS ip>", 22)
ssh.loop { true }
end
end
proxy_thread.start
Net::SSH.start('localhost', '<localhost user>', port: 8080) do |ssh|
#done
end
proxy_thread.kill

Resources