Is there a way to log commands from Rails console in Heroku? - ruby-on-rails

Hi I am using Heroku and Ruby on Rails. In heroku I can do
heroku run rails c -a app
Which gives me console access, and I can do administrative things like deleting records
I am working in a company setting and I would like to have a log of all commands entered in the console. So if something is wrong, we can audit the logs.
Is there a way to record what is typed in the rails console?

Rails allows you to store the console history but this won't be saved in Heroku, at least not permanently.
In Heroku, when you run heroku run rails console Heroku will create a one-off dyno which will only live as long as the console session. Rails will store the history of commands on the filesystem but once the console is exited the dyno will be cleaned up and the command history file will be deleted along with it.
Rails console is a Railtie and it has some lifecycle hooks, so you could come up with some custom way to persist commands, but there is no built-in way to do it.

Yes, you can now using Heroku's shield private spaces, https://elements.heroku.com/addons/heroku-private-spaces.

Related

Identify Heroku user account in running Rails console

Is it possible to identify the heroku username of the current user connected to an app through a running console?
So say I connect to a rails console on my heroku app using
heroku run rails c --app my-app
I'd like to be able to run a command and have it print out:
heroku.user#example.com
Where heroku.user#example.com is the email / username of the user who connected to the console.
I tried a couple basic linux commands like
`whoami`
but you get a generic username like u12345
The goal is to instrument certain fields in our app with audit trails so that we can track when and who accessed sensitive information, as well as how they accessed it (CLI, through the Web UI, etc.)
I just asked heroku this same question via a support ticket, and they do not currently support this functionality. That said, you can use heroku logs to see who and when a rails console was opened.
2019-04-12T15:48:05.727693+00:00 app[api]: Starting process with command `rails console` by user me#mycompany.org

Ruby on Rails / Heroku where are my users emails stored

I am new to Ruby on Rails and Heroku. I have installed the gem 'Devise' in my RoR app and I use Heroku to deploy my app. The sign up process works fine but where are my users emails stored (the users that sign up for my website)? I want to be able to see/extract a list of all the emails.
You can view your users and any other database records by running:
heroku run rails console
from a terminal window. And from there you can run any rails console command as you can on your local machine like User.all
One thing I have found useful is to use either the config variables or the Postgres dashboard to get the URL information for the database. I then use that information to construct the JDBC URL for my IDE, RubyMine, to connect to Heroku. As a result, I can view and manipulate the data in all my tables on Heroku from my IDE. You can do something similar for the SQL tool of your choice.

Syncing Heroku and localhost databases

Hey so I am following the One Month Rails guide to learning Ruby and I have hit a wall on one of the lessons. I have just finished uploading an image with Paperclip, and as I finished my work on my localhost and checked it on Heroku, something went wrong. The pin/image appears to have been pushed to my Heroku account, the only problem is that the username and password that works for my localhost:3000 won't work for my Heroku account. The same password should work for both, but for some reason something is wrong. I wish I could give you the action that is going on in my terminal, but the ruby rails is the only thing that has a continuous status flow. The problem may have been when I switched my password after not using my account on localhost for a few weeks, but i thought that once i "git pushed" that to heroku master, it would've synced. I have tried heroku run rake db:setup which didn't seem to do too much as well as wrestled by way through "Importing a Heroku Postgres Database with PG Backups", but I had some trouble working through that. Any ideas? Thanks for the help.
Your 'database.yml' should not be sent to Heroku, they take care of that, creating a new database.yml config file with the proper DB access details.
Try logging into your Heroku instance and deleting the file.
Edit: nevermind, assumed you were not able to connect to the DB, not to login into the website.
So if I'm understanding you correctly, you've deployed your application to Heroku and the login (to your application) that was working locally doesn't work on Heroku.
Deploying your application doesn't deploy data. Assuming you've run heroku run rake db:migrate then your database schemas will at least match.
At this point, you've got a couple of options.
Use a seeds.rb file which you can load with heroku run db:seed to setup some 'seed' data so that you can login.
Push your local database to Heroku - either via heroku db:push or using heroku pg:transfer provided by https://github.com/ddollar/heroku-pg-transfer
Use heroku run console to create your user account via the command line
User.create(email: 'someemail.com', password: 'somepassword', password_confirmation: 'somepassword')
I'd be inclined to go with the later option.
How did the user get in their in the first place? Perhaps going back to that step in the tutorial - just remember, if you are using rails console locally to use heroku run console on Heroku.

Rails/Heroku : db:push runs correctly but does not create the DB

I've been having problems yesterday with Heroku shared Postgres DB. It looks like my db is not correctly 'pushed' to my heroku DB.
Configuration
Using 'pg' gem for both development and production environments.
Using Heroku's 'shared-database' add-on.
Running Rails 3.1.1.
What I'm trying to do
Push my source code to Heroku and then synchronize my postgres DB with Heroku's one. No problem for the source itself, but I can't get my DB up and running.
What I've tried so far
1: Source code push : git push heroku master. OK. This works.
2: Then I try to run : heroku db:push. Seems to be running normally, no error, the schema is sent to Heroku, the different tables are correctly detected and seem to be sent as well :
3: Let's check it out, is everything allright ? heroku info :
Doh ! The database is still empty (and therefore I get DB-related errors when accessing dynamic pages). Can't understand what's happening, I've spent time on this issue yesterday evening but only been mpessing with Heroku for a little while and never encountered this issue. Any clues ?
I'm not convinced that the Database size figure is a live figure. Here's a clean deployment of an app to Heroku and the output of heroku info at each stage. The first run is post application creation, the second after a push of the code, the third after a heroku db:push.
The database was uploaded before the third output and the application is running however the DB:size is not reporting the figure.
Is your application actually erroring because the database isn't present?
The problem is in the commit process.
You should run manually after each commit from your console :
heroku run rake db:migrate
Enjoy
daniel

How to tell when Rails app is activated by migration?

I'm trying to create a migration for my app, and in this app I'm using a gem that tries to startup a different service upon app startup. Apparently, creating a migration...
rails generate migration AddSomeStuffToTable stuff:string
...activates the app, and this gem which tries to connect to startup the service. It appears that starting up the app via generating a migration makes the service startup unable to connect, so it just keeps sleeping and trying again, never actually running the migration.
In this gem, I've already dealt with this for rake, so this is what I've got so far:
MyService.start unless defined? Rake or defined? IRB
This handles the rake problem (like rake db:migrate, rake db:populate), but how can I handle the creation of migration, which (as far as I know) is not a Rake task?
You could try using environment variables for disabling the service:
MyService.start unless ENV['NO_SERVICE']
And run your command like this:
NO_SERVICE=1 rails generate migration AddSomeStuffToTable stuff:string
However, I doubt this scales well, especially if multiple developers are in the app. A better approach might be to do the reverse of this, to only start the service if a particular env variable is present. However, going this direction, you'd need to make sure your app servers set this variable, for example:
Apache: SetEnv START_SERVICE 1
nginx: env START_SERVICE=1
thin: START_SERVICE=1 thin start

Resources