Rails & Heroku: Running a script through rails runner using local files - ruby-on-rails

So, I have an app deployed on Heroku, and I'm trying to populate the database on the app through a script I wrote. I have the database in a text file, and the script runs through the file and populates the database. I don't want to push the database file to the heroku server, since it's a very large file.
Is there any way to do this on Heroku? It works fine locally, but I can't get it to work on the Heroku server.
I've tried
heroku run rails runner PATH/TO/SCRIPT LOCAL/PATH/TO/DATABASE --app my_app
to no avail.

To run a local script on Heroku:
irbify.rb script.rb | heroku run rails console --app=my_app
irbify.rb is a silly tiny script I wrote to convert a script to a single eval statement.
Regarding passing data: you can serialize it in some form and put it inside script.
Hope it helps someone.
UPDATE: this does not work well anything beyond trivial datasets.

You can also upload your script to a gist and then do:
binding.eval(open("your gist raw url").read)
I had to use global variables ($dollar_prefixed) since the context would not get filled with the variables (using Pry), otherwise it went well.

Related

Rails server not working when copying back from staging deploy

I ssh copied a rails app from a staging server because the development repository has been lost. My goal is to create a new development code base using the deployed code as a source. So far I have removed a hidden .bundle folder and replaces several aliases with folders and files. I then ran bundle install. For the database I did a sql dump from staging and used it to build a development database. I think I'm ready to run rails server. But when I try to run rails server in the base directory. It gives me the rails command line help as if I was running rails s in a directory with no app.
I'm not even sure if it is possible to reverse a deploy this way. I've looked at the rails guide on the app initialization process and all the files seem to be in place.
Remember that for Rails to start up, you need bin/rails, bin/bundle, config/boot, etc.
If you restore those files, it should work again.

Rails with Heroku: How to download a CSV created by a rake task

I deployed "Harrys Prelauncher" on Heroku and try to do the teardown (currently just testing). See here: https://github.com/harrystech/prelaunchr#teardown
After running the rake task ...
heroku run rake prelaunchr:create_winner_csvs
... a csv file is created in "/lib/assets", but I dont know how to access the file (it works locally in development).
How can I download or access the file?
Heroku uses "ephemeral" filesystem that is not guaranteed to preserve changes made at runtime. Simply put, if it's not pushed to git (I assume you're using git with heroku), it's not guaranteed to exist in all the instances of your app. It may exist in one of them, but you may have no simple way of accessing that specific filesystem. And you shouldn't, really.
It's done like that so that multiple instances of the same app can be fired up seamlessly. Of course, that requires some discipline: storage of any meaningful state outside: in the database, on external disk, anywhere. The benefit of this is horizontal scalability: should you be short on resources, you can fire up another web dyno that would (normally) behave exactly the same way. New dynos are started from bundles that are packed on git push and thus do not contain any changes you may have made in another instance.
A workaround may be running heroku run bash, so that you end up in an interactive shell linked to another instance of your bundle.
Then you can make that file (by running your rake task) and access its contents in any way you deem reasonable. Text files can be echoed into the console with cat and copy-pasted anywhere else. That's a dirty way.
A much cleaner way would be rigging the app to send the file in question via email. and it's one of the few reasonable ways if that rake task is invoked by the Rails app itself.
I ran into this problem recently while developing the Prelaunchr campaign for a client. Assuming you have a local version of your app, you can "pull" your Heroku database down to your local machine, set that as your development database in database.yml, and run the rake task from your local app, which should now have the same database as your heroku version. Here is the command to pull the db (subbing out name_for_database & heroku_app_name with your own):
heroku pg:pull HEROKU_POSTGRESQL_COPPER_URL name_for_database --app heroku_app_name
Make sure to restart your local server to see the new database info populated.

Add Seeds file after Dokku build

I am using dokku-alot to deploy my Rails 4 app to my staging server and everything is working just swell.
One requirement I have with my current project is in regards to seed file data. I've had to keep my seeds.rb file out of version control because of sensitive information. However, I can't figure out how to add the seeds.rb file into the container after a build.
I've tried ssh root#myhost ap_name which gets me into the VM but even if I scp the files into there, the container doesn't see them. How can I drop a few files where my rails code is in the docker image?
Depending on how much information is in your seeds.rb file, you could use environmental variables. This the solution I ended up using.
You basically set the variable: config:set my-app SECRET=whateversupersecretinfo. Then in your code, you can extract that app variable by using ENV['SECRET']. (This works pretty much the same in Heroku) Not sure if that would solve your use case, but leaving this answer here for posterity.
subnote: In Node.js you can extract these variables like process.env.SECRET

Access or send a local file to Heroku

I've got a local .csv file with updated user data that I want to run a simple one-time script on in production (on Heroku, Rails 4 app). It's open source, so I can't just include the file in the repo without exposing the data. I'd like to be able to do it from the CLI but can't seem to figure it out.
There's probably a better solution, but I've been trying unsuccessfully to set the contents of the file (which isn't huge) to an environment variable on Heroku. My Bash skills are weak...
$ heroku config:set MY_CSV=<./my_local_csv_path.csv
didn't work -- it will not set anything. (running $ heroku config shows the env variable blank -- MY_CSV:).
Is there a better way to make this .csv file accessible to a script on Heroku? I suspect it's a similar problem to just accessing the local filesystem from the Heroku console.
I just did something similar to what you are doing where the user uploads a CSV for processing in my app. I am also hosting on heroku. Check out...
http://railscasts.com/episodes/396-importing-csv-and-excel
Basically let your model doing your processing for you that way you don't have to store any files on heroku.
I was able to do this with the heroku cli -e flag. It has to be a relatively small file. (all arguments combined are limited to 32kb)
It goes something like this:
# gzip and base64 encode the file
# (without the encoding, the gzip characters break this argument)
FILE_CONTENTS=$(cat /path/to/file.csv | gzip -fc | base64)
# Pass the variable from above to the dyno above.
heroku run -a APP_NAME -e "FILE_CONTENTS=\"$FILE_CONTENTS\"" rails c
Then in your console, you can get the csv by running:
csv_contents = ActiveSupport::Gzip.decompress(
Base64.decode64(
ENV['FILE_CONTENTS']
)
)

reading a file in rails app deployed on heroku

I have a rails app deployed in heroku. In the app I have a simple txt file. I have a ruby script that loops on all records in this file, makes a URL out of it and then does other stuff with it.
I can not do this locally and then transfer the data to my heroku app.
From heroku console is it possible to run a traditional simple ruby script that would loop over a simple txt file in my heroku app?
yes you can read a text file, as long as it is pushed to Heroku with your code, the syntax is the same as reading files on your localhost
s = File.read("#{Rails.root}/app/assets/some_text_file.txt")
Heroku does not allow writing files

Resources