Running flask cli commands on Heroku - heroku

In Flask we can define CLI commands by passing it to app.cli.command
It's all fine and dandy, and I can run my initdb command on my local machine with python3 -m flask -a appname initdb
I've uploaded the app successfully to Heroku, and it's live and working except for the pages that require database interaction. My initdb command itself should already work with Heroku, with one problem, I cannot run initdb on Heroku.
Running this heroku run python3 -m flask -a appname initdb produces the output:
Usage: python -m flask [OPTIONS] COMMAND [ARGS]...
Error: No such command "initdb".
While using heroku run python3 -m flask -a appname.py initdb produces the following: (the same command without heroku run also initializes the db on my machine:
▸ Couldn't find that app.
Which is weird since running heroku ls allows me to see appname.py on the current directory.

If you encouter this error:
Couldn't find that app.
It is because you passed an invalid app name as your -a, --app option for heroku run. Run heroku apps to find what are valid names and try again.

Related

How to schedule PostgreSql procedure using Heroku scheduler?

I am trying to schedule PostgreSql procedure using Heroku scheduler. I tried heroku pg:psql -c "call procedure_name();" --app app-name but gets the error from scheduler "bash: heroku: command not found"
That error is telling you important information. The Heroku CLI is not present in your environment, so bash complains that heroku isn't a command that it recognizes.
If you want to run heroku commands from your application you'll need to include the cli buildpack when you build your application to make sure it's properly bundled. Once you've included the buildpack and triggered a new build, this should work.

How do I use phoenix console (iex) with heroku?

I'm trying to change some data on heroku that I normally do with iex -s , but the following command does nothing.
heroku run iex
The command that worked was
heroku run iex -S mix
When dealing with phoenix project just use:
heroku run iex -S mix phoenix.server
To launch the server in your iex console
or simply
heroku run iex -S mix
to compile the files without starting the server.
Note that if you deploy your app to Heroku using the native release mechanism that comes with Mix since Elixir 1.8, you may simply ssh into the running dyno and remote into the running BEAM node:
# In your local terminal
heroku ps:exec
# In the dyno
_build/prod/rel/your_app/bin/your_app remote
This works out of the box.

How do I run heroku-toolbelt commands without the `--app` parameter

I used to run heroku create and deploying with git push heroku master but I've since moved onto a Github based workflow where code is first pushed to my Github repo, and deployed via a web hook.
I now have to run --app appname or -a appname after every command, and it gets annoying.
e.g.
heroku run rails db:migrate -a appname # instead of
heroku run rails db:migrate
heroku run rails console -a appname # instead of
heroku run rails console
heroku run logs -t -a appname # instead of
heroku run logs -t
How do I force heroku-toolbelt to link this repo to the heroku app so that I can run heroku without the --app parameter
Add a remote named heroku with the url https://git.heroku.com/appname.git
Run this:
$ git remote add heroku https://git.heroku.com/appname.git

heroku run bash is not working

Why is heroku run bash not working?
$ heroku login
Enter your Heroku credentials.
Email: xx#yy.zz
Password (typing will be hidden):
Logged in as xx#yy.zz
$ heroku run bash
▸ No app specified.
▸ Run this command from an app folder or specify which app to use with --app APP
$ heroku run --app bash
▸ Usage: heroku run COMMAND
▸
▸ Example: heroku run bash
$ heroku run "ls /usr/bin/"
▸ No app specified.
▸ Run this command from an app folder or specify which app to use with --app APP
$ heroku run --app "ls /usr/bin/"
▸ Usage: heroku run COMMAND
▸
▸ Example: heroku run bash
$ heroku version
heroku-toolbelt/3.42.22 (universal.x86_64-darwin15) ruby/2.0.0
heroku-cli/4.27.11-7569c5d (amd64-darwin) go1.5.2
=== Installed Plugins
heroku-apps#1.1.0
heroku-cli-addons#0.1.1
heroku-fork#4.0.0
heroku-git#2.4.4
heroku-local#4.1.5
heroku-run#2.9.2
heroku-spaces#2.0.9
heroku-status#1.2.4
As the error message indicates, you need to specify which heroku app you want to run commands on. (If you're in a git repository with a remote pointing to heroku, it will use that one by default; that's what it means by "from an app folder".)
$ heroku apps
will give you a list.
$ heroku run --app INSERT_APP_NAME_HERE bash
will run bash on that app.
This error is because of not specifying the app you want to use. Try this
$heroku app
example
$heroku run bash --app examp
need to manually specify the name of the image with the run
command like:
heroku run bash --type worker -a app_name

Access to heroku toolbelt commands in a scheduled heroku dyno?

I want to call heroku postgres backup/restore commands within a scheduled heroku task, but the heroku toolbelt isn't available from bash prompt, so I can't call heroku commands:
$ heroku run bash --app myapp
Running `bash` attached to terminal... up, run.4805
~ $ heroku --version
bash: heroku: command not found
How can I get heroku commands available in my scheduled bash script?
I don't know anything about Ruby or Ruby Gems.
None of the other solutions worked for me since Heroku locks the filesystem after the buildpacks finish running.
There's a third-party buildpack that installs the CLI for you. First you set your auth key as an ENV variable on your app:
heroku config:set HEROKU_API_KEY=`heroku auth:token` -a myapp
Then add the buildpack:
heroku buildpacks:add https://github.com/heroku/heroku-buildpack-cli -a myapp
Redeploy your app and it should be able to access your apps via the CLI. If you have https://devcenter.heroku.com/articles/dyno-metadata enabled, you can even access your current app's name with $HEROKU_APP_NAME.
Put this at the top of your scheduled bash script:
# install heroku toolbelt
# inspired by https://toolbelt.heroku.com/install.sh
curl -s https://s3.amazonaws.com/assets.heroku.com/heroku-client/heroku-client.tgz | tar xz
mv heroku-client/* .
rmdir heroku-client
PATH="bin:$PATH"
then, for example, you can call:
heroku pg:reset HEROKU_POSTGRESQL_YELLOW_URL --app myapp-staging --confirm myapp-staging
heroku pgbackups:restore HEROKU_POSTGRESQL_YELLOW_URL `heroku pgbackups:url --app myapp-production` --app myapp-staging --confirm myapp-staging
and poof! Staging database is updated from production database.
As Heather Piwowar said, you can indeed download and untar the heroku tollbelt yourself, but no need to move files around after this. Here is a shorter version:
curl -s https://s3.amazonaws.com/assets.heroku.com/heroku-client/heroku-client.tgz | tar xz
PATH="/app/heroku-client/bin:$PATH"
You can now use the heroku command as you wish.
For authentification, you may want to set the HEROKU_API_KEY env var (using heroku config:set HEROKU_API_KEY=1234567890 -a your-app-name).
Also, note that the first use of the heroku command will run longer than expected because it will try to install the latest version, dependencies and core plugins.
You can use Heroku Scheduler and configure the following command (as an example) to create a database backup:
curl -s https://s3.amazonaws.com/assets.heroku.com/heroku-client/heroku-client.tgz \
| tar xz && ./heroku-client/bin/heroku pg:backups:capture -a you-app-name-here
For this to work, you need to add a Config Variable named HEROKU_API_KEY and set its value to the "API Key" value from your Accounts page.

Resources