Repository: ckan/ckan

Issue:
[Regression-2.9] Ckan Generate Config 
**CKAN version**

2.9

**Describe the bug**

In ckan 2.9, ckan generate config does not make a config file that can then be immediately edited. 

**Steps to reproduce**
In Ckan <= 2.8.5, 
```
  "$CKAN_HOME"/bin/paster make-config ckan "$CONFIG"

  "$CKAN_HOME"/bin/paster --plugin=ckan config-tool "$CONFIG" \
      "sqlalchemy.url = ${DATABASE_URL}" \
      "solr_url = ${SOLR_URL}" \
      "ckan.site_url = ${SITE_SCHEME}${SITE_HOST}" \
      "ckan.redis.url = ${REDIS_URL}" \
      ...
```
worked, regardless of the database url. 

In ckan 2.9:
```
    "$CKAN_HOME"/bin/ckan generate config "$CONFIG"

    "$CKAN_HOME"/bin/ckan config-tool "$CONFIG" \
      "ckan.storage_path = /var/lib/ckan" \
      "sqlalchemy.url = ${CKAN_DB_URL}"\
```
This fails, because the database is not reachable (and potentially the site url is not defined, IIRC)

**Expected behavior**

Either:
* Ckan generate config should provide the basic parameters required to run config-tool
* config-tool should be able to edit an incomplete ckan.ini file, by not attempting to contact the database, redis, or solr servers. 

```
ckan generate config $CKAN_INI
ckan config-tool $CKAN_INI \ 
    [options] 
```
should work. 

**Additional details**
If possible, please provide the full stack trace of the error raised,  or add screenshots to help explain your problem.

This is my current work-around, which is ugly:
```
  if [ ! -e "$CONFIG" ]; then
      "$CKAN_HOME"/bin/ckan generate config "$CONFIG"
      # site url can't be set with the config tool
      perl -pi -e "s|^(ckan.site_url =)$|\1 ${SITE_SCHEME}${SITE_HOST}|;" $CONFIG
      perl -pi -e "s|^#?(solr_url =).*$|\1 ${SOLR_URL}|;" $CONFIG
      perl -pi -e "s|^#?(ckan.redis.url =).*|\1 ${REDIS_URL}|;" $CONFIG
      # perl has issues with @db in the replacement
      sed -i -e "s|^sqlalchemy.url =.*$|sqlalchemy.url = ${CKAN_DB_URL}|;"  $CONFIG

  fi


  "$CKAN_HOME"/bin/ckan config-tool "$CONFIG" \
      "ckan.storage_path = /var/lib/ckan" \
      "sqlalchemy.url = ${CKAN_DB_URL}"\
      "ckan.site_url = ${SITE_SCHEME}${SITE_HOST}" \
      "solr_url = ${SOLR_URL}" \
```



Discussion:

Resolving patch:
diff --git a/ckan/cli/cli.py b/ckan/cli/cli.py
--- a/ckan/cli/cli.py
+++ b/ckan/cli/cli.py
@@ -36,6 +36,12 @@
 
 log = logging.getLogger(__name__)
 
+_no_config_commands = [
+    [u'config-tool'],
+    [u'generate', u'config'],
+    [u'generate', u'extension'],
+]
+
 
 class CkanCommand(object):
 
@@ -83,18 +89,20 @@ def _get_commands_from_entry_point(entry_point=u'ckan.click_command'):
 
 def _init_ckan_config(ctx, param, value):
     is_help = u'--help' in sys.argv
-    no_config = len(sys.argv) > 1 and sys.argv[1] in (
-        u'generate', u'config-tool')
+    no_config = False
+    if len(sys.argv) > 1:
+        for cmd in _no_config_commands:
+            if sys.argv[1:len(cmd) + 1] == cmd:
+                no_config = True
+                break
+    if no_config or is_help:
+        return
 
     try:
         ctx.obj = CkanCommand(value)
     except CkanConfigurationException as e:
-        # Some commands don't require the config loaded
-        if no_config or is_help:
-            return
-        else:
-            p.toolkit.error_shout(e)
-            raise click.Abort()
+        p.toolkit.error_shout(e)
+        raise click.Abort()
 
     if six.PY2:
         ctx.meta["flask_app"] = ctx.obj.app.apps["flask_app"]._wsgi_app


Test patch: