Skip to content

Commit

Permalink
bug #399 Use dynamic env params for critical settings and restore Her…
Browse files Browse the repository at this point in the history
…oku deploys (dzuelke)

This PR was squashed before being merged into the master branch (closes #399).

Discussion
----------

Use dynamic env params for critical settings and restore Heroku deploys

Database, log destination, and secrets are now configurable via environment variables (via the quasi standard `DATABASE_URL`, and `SYMFONY_LOG`, and `SYMFONY_SECRET`). All three fall back to defaults from `parameters.yml(.dist)`.

For simplicity (one param versus many), a URL is now used again for SQLite, because that works fine since Doctrine DBAL 2.5.5.

Deploys to Heroku are now fixed again; @bocharsky-bw's PR #377 was not a fix for #371, because SQLite does not scale horizontally :p The original problem was @javiereguiluz's 56cfa66 change to Symfony 3.1, which used individual parameters for config. Again, no longer a problem since DBAL 2.5.5. It also uses `php://stderr` for env `prod` on Heroku only, which @javiereguiluz had also removed in 56cfa66 (for the future, I'd encourage separate commits for stuff like this, as it makes it much easier to untangle individual parts and reasons for changes).

This demo app ships with sample data in SQLite, but for other databases, the data needs to be seeded, and that can even happen in a prod env. @javiereguiluz' commit e2264b0 removed the fixtures bundle needed for that from the kernel init; it's now back.

This app now works just fine with the usual defaults both in `dev` and `prod` envs, but users who want to try running it on Docker or on a PaaS like Heroku can now trivially adjust relevant settings via environment variables.

/CC @stof @fabpot

Commits
-------

21fcb92 Use dynamic env params for critical settings and restore Heroku deploys
  • Loading branch information
javiereguiluz committed Nov 10, 2016
2 parents 1bf9dec + 21fcb92 commit 84de97a
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 18 deletions.
7 changes: 7 additions & 0 deletions app.json
Expand Up @@ -9,12 +9,19 @@
"repository": "https://github.com/symfony/symfony-demo",
"logo": "https://symfony.com/images/v5/pictos/demoapp.svg?v=4",
"success_url": "/",
"scripts": {
"postdeploy": "php bin/console doctrine:schema:create && php bin/console doctrine:fixtures:load -n"
},
"env": {
"SYMFONY_ENV": "prod",
"SYMFONY_LOG": "php://stderr",
"SYMFONY_SECRET": {
"description": "Extra entropy for %kernel.secret%; used for CSRF tokens, cookies and signed URLs.",
"generator": "secret"
}
},
"addons": [
"heroku-postgresql"
],
"image": "heroku/php"
}
2 changes: 1 addition & 1 deletion app/AppKernel.php
Expand Up @@ -21,6 +21,7 @@ public function registerBundles()
new WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle(),
new CodeExplorerBundle\CodeExplorerBundle(),
new AppBundle\AppBundle(),
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(), // used for initial population of non-SQLite databases in production envs
// uncomment the following line if your application sends emails
// new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
];
Expand All @@ -34,7 +35,6 @@ public function registerBundles()
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
}

return $bundles;
Expand Down
10 changes: 5 additions & 5 deletions app/config/config.yml
Expand Up @@ -31,7 +31,7 @@ framework:
# http://symfony.com/doc/current/book/http_cache.html#edge-side-includes
esi: { enabled: true }
translator: { fallback: "%locale%" }
secret: "%secret%"
secret: "%env(SYMFONY_SECRET)%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
Expand Down Expand Up @@ -61,10 +61,10 @@ twig:
# Doctrine Configuration (used to access databases and manipulate their information)
doctrine:
dbal:
# if you don't want to use SQLite, comment the two following lines
driver: "pdo_sqlite"
path: "%kernel.root_dir%/data/blog.sqlite"
# uncomment the following lines to use a database different than SQLite
# if you don't want to use SQLite, change the URL in parameters.yml or set the DATABASE_URL environment variable
url: "%env(DATABASE_URL)%"

# instead of using a URL, you may also uncomment the following lines to configure your database
# driver: pdo_mysql
# host: "%database_host%"
# port: "%database_port%"
Expand Down
2 changes: 1 addition & 1 deletion app/config/config_dev.yml
Expand Up @@ -15,7 +15,7 @@ monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
path: "%env(SYMFONY_LOG)%"
level: info
console:
type: console
Expand Down
2 changes: 1 addition & 1 deletion app/config/config_prod.yml
Expand Up @@ -19,7 +19,7 @@ monolog:
handler: nested
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
path: "%env(SYMFONY_LOG)%"
level: debug
console:
type: console
9 changes: 6 additions & 3 deletions app/config/parameters.yml.dist
Expand Up @@ -6,11 +6,11 @@ parameters:
# this demo application uses an embedded SQLite database to simplify setup.
# in a real Symfony application you probably will use a MySQL or PostgreSQL database
# the path must be relative or else it will not work on Windows
database_url: 'sqlite:///%kernel.root_dir%/data/blog.sqlite'
env(DATABASE_URL): 'sqlite:///%kernel.root_dir%/data/blog.sqlite'

# Uncomment this line to use a MySQL database instead of SQLite:
#
# database_url: 'mysql://root:pass@127.0.0.1:3306/symfony_demo'
# env(DATABASE_URL): 'mysql://root:pass@127.0.0.1:3306/symfony_demo'
#
# Furthermore, you must remove or comment out the "doctrine" section from config_dev.yml regarding SQLite!
#
Expand All @@ -35,4 +35,7 @@ parameters:
# used internally by Symfony in several places (CSRF tokens, URI signing,
# 'Remember Me' functionality, etc.)
# see: http://symfony.com/doc/current/reference/configuration/framework.html#secret
secret: 'secret_value_for_symfony_demo_application'
env(SYMFONY_SECRET): 'secret_value_for_symfony_demo_application'

# Destination for log files; can also be "php://stderr" etc
env(SYMFONY_LOG): %kernel.logs_dir%/%kernel.environment%.log
6 changes: 1 addition & 5 deletions composer.json
Expand Up @@ -64,11 +64,7 @@
"symfony-tests-dir": "tests",
"symfony-assets-install": "relative",
"incenteev-parameters": {
"file": "app/config/parameters.yml",
"env-map": {
"database_url": "DATABASE_URL",
"secret": "SYMFONY_SECRET"
}
"file": "app/config/parameters.yml"
}
}
}
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 84de97a

Please sign in to comment.