← Back to team overview

cf-charmers team mailing list archive

[Merge] lp:~cf-charmers/charms/trusty/cf-cloud-controller/require-mysql into lp:~cf-charmers/charms/trusty/cf-cloud-controller/refactor-to-core

 

Whit Morriss has proposed merging lp:~cf-charmers/charms/trusty/cf-cloud-controller/require-mysql into lp:~cf-charmers/charms/trusty/cf-cloud-controller/refactor-to-core.

Requested reviews:
  Cory Johns (johnsca)

For more details, see:
https://code.launchpad.net/~cf-charmers/charms/trusty/cf-cloud-controller/require-mysql/+merge/219898

Add the ability to run the cloudcontroller against a mysql database
-- 
https://code.launchpad.net/~cf-charmers/charms/trusty/cf-cloud-controller/require-mysql/+merge/219898
Your team Cloud Foundry Charmers is subscribed to branch lp:~cf-charmers/charms/trusty/cf-cloud-controller/refactor-to-core.
=== modified file 'hooks/charmhelpers/contrib/cloudfoundry/contexts.py'
--- hooks/charmhelpers/contrib/cloudfoundry/contexts.py	2014-05-14 15:11:45 +0000
+++ hooks/charmhelpers/contrib/cloudfoundry/contexts.py	2014-05-16 19:52:39 +0000
@@ -25,6 +25,19 @@
     interface = 'nats'
     required_keys = ['nats_port', 'nats_address', 'nats_user', 'nats_password']
 
+    
+class MysqlDSNContext(RelationContext):
+    interface = 'db'
+    required_keys = ['user', 'password', 'host', 'database']
+    dsn_template = "mysql2://{user}:{password}@{host}:{port}/{database}"
+    def __call__(self):
+        ctx = RelationContext.__call__(self)
+        if ctx:
+            if 'port' not in ctx:
+                ctx['db']['port'] = '3306'
+            ctx['db']['dsn'] = self.dsn_template.format(**ctx['db'])
+        return ctx
+    
 
 class RouterContext(RelationContext):
     interface = 'router'
@@ -39,3 +52,5 @@
 class LoggregatorContext(RelationContext):
     interface = 'loggregator'
     required_keys = ['shared_secret', 'loggregator_address']
+
+

=== renamed file 'hooks/config.py' => 'hooks/common.py'
--- hooks/config.py	2014-04-03 20:03:40 +0000
+++ hooks/common.py	2014-05-16 19:52:39 +0000
@@ -1,4 +1,8 @@
 import os
+import subprocess
+from charmhelpers.core import host
+from charmhelpers.core import hookenv
+
 
 __all__ = ['CF_DIR', 'CC_PACKAGES', 'CC_DIR', 'CC_CONFIG_DIR',
            'CC_CONFIG_FILE', 'CC_DB_FILE', 'CC_JOB_FILE',
@@ -22,3 +26,14 @@
 NGINX_RUN_DIR = '/var/vcap/sys/run/nginx_ccng'
 NGINX_LOG_DIR = '/var/vcap/sys/log/nginx_ccng'
 FOG_CONNECTION = '/var/vcap/nfs/store'
+
+
+def db_migrate():
+    hookenv.log("Starting db:migrate...", hookenv.DEBUG)
+    with host.chdir(CC_DIR) as dir:
+        #TODO: make it idempotent by deleting existing db if exists
+        subprocess.check_call([
+            'sudo', '-u', 'vcap', '-g', 'vcap',
+            'CLOUD_CONTROLLER_NG_CONFIG={}'.format(CC_CONFIG_FILE),
+            'bundle', 'exec', 'rake', 'db:migrate'])
+        hookenv.log("Finished db:migrate in %s." % (dir))

=== added symlink 'hooks/db-relation-changed'
=== target is u'hooks.py'
=== modified file 'hooks/hooks.py'
--- hooks/hooks.py	2014-05-14 15:11:45 +0000
+++ hooks/hooks.py	2014-05-16 19:52:39 +0000
@@ -8,7 +8,7 @@
 from charmhelpers.core import services
 from charmhelpers.contrib.cloudfoundry import contexts
 
-import config as cf_config
+import common
 
 hooks = hookenv.Hooks()
 fileproperties = {'owner': 'vcap'}
@@ -19,10 +19,11 @@
         'templates': [
             {'source': 'cf-cloudcontroller.conf'},
             {'source': 'cloud_controller.yml',
-             'target': cf_config.CC_CONFIG_FILE,
+             'target': common.CC_CONFIG_FILE,
              'file_properties': fileproperties,
              'contexts': [contexts.NatsContext(),
-                          contexts.RouterContext()]}
+                          contexts.RouterContext(),
+                          contexts.MysqlDSNContext()]}
         ],
     },
     {
@@ -46,7 +47,13 @@
 def stop():
     services.stop_services()
 
+    
+@hooks.hook('db-relation-changed')
+def db_relation_changed():
+    services.reconfigure_services()
+    common.db_migrate()
 
+        
 @hooks.hook('nats-relation-changed')
 def nats_relation_changed():
     services.reconfigure_services()
@@ -64,3 +71,4 @@
         log("Relation {} with {}".format(
             hookenv.relation_id(), hookenv.remote_unit()))
     hooks.execute(sys.argv)
+

=== modified file 'hooks/install'
--- hooks/install	2014-05-14 15:11:45 +0000
+++ hooks/install	2014-05-16 19:52:39 +0000
@@ -5,45 +5,37 @@
 
 from charmhelpers.core import hookenv, host
 from charmhelpers.core.hookenv import log, DEBUG
-from charmhelpers.core import templating
 from charmhelpers.contrib.cloudfoundry.common import (
     prepare_cloudfoundry_environment
 )
 
-import config as cf_config
-
-
-templating.register([
-    {'source': 'cloud_controller.yml',
-     'target': cf_config.CC_CONFIG_FILE,
-     'file_properties': {'perms': 0664, 'owner': 'vcap'},
-     'contexts': [templating.StaticContext({
-         'router': {
-             'domain': 'fake',
-         },
-         'nats': {
-             'nats_user': 'fake',
-             'nats_password': 'fake',
-             'nats_address': 'fake',
-             'nats_port': '4222',
-         }
-     })]},
-])
-
-
-def cc_db_migrate():
-    # populate stub config with fake data for db:migrate
-    templating.render(cf_config.CC_CONFIG_FILE)
-    log("Starting db:migrate...", DEBUG)
-    with host.chdir(cf_config.CC_DIR) as dir:
-        #TODO: make it idempotent by deleting existing db if exists
-        subprocess.check_call([
-            'sudo', '-u', 'vcap', '-g', 'vcap',
-            'CLOUD_CONTROLLER_NG_CONFIG={}'.format(cf_config.CC_CONFIG_FILE),
-            'bundle', 'exec', 'rake', 'db:migrate'])
-        log("Finished db:migrate in %s." % (dir))
-
-
+import common
+
+
+def setup_templating():
+    from charmhelpers.core import templating
+    templating.register([
+        {'source': 'cloud_controller.yml',
+         'target': common.CC_CONFIG_FILE,
+        'file_properties': {'perms': 0664, 'owner': 'vcap'},
+        'contexts': [templating.StaticContext({
+            'router': {
+                'domain': 'fake',
+                },
+                'db':{
+                    'dsn': 'sqlite:///var/lib/cloudfoundry/cfcloudcontroller/db/cc.db'
+                    },
+                    'nats': {
+                        'nats_user': 'fake',
+                        'nats_password': 'fake',
+                        'nats_address': 'fake',
+                        'nats_port': '4222',
+                        }
+            })]},
+        ])
+    return templating
+
+    
 def disable_nginx_service():
     # reconfigure NGINX as upstart job and use specific config file
     host.service_stop('nginx')
@@ -57,26 +49,29 @@
 
 def install():
     # TODO build of directory service
-    prepare_cloudfoundry_environment(hookenv.config(), cf_config.CC_PACKAGES)
-    if not os.path.isfile(cf_config.CC_DB_FILE):
+    prepare_cloudfoundry_environment(hookenv.config(), common.CC_PACKAGES)
+    if not os.path.isfile(common.CC_DB_FILE):
         # TODO check permission of database file
-        host.write_file(cf_config.CC_DB_FILE, '', owner='vcap', group='vcap', perms=0664)
-    dirs = [cf_config.CF_DIR,
-            cf_config.CC_DIR,
-            cf_config.CC_RUN_DIR,
-            cf_config.NGINX_RUN_DIR,
-            cf_config.CC_LOG_DIR,
-            cf_config.NGINX_LOG_DIR,
-            cf_config.CC_CONFIG_DIR,
-            cf_config.FOG_CONNECTION,
+        host.write_file(common.CC_DB_FILE, '', owner='vcap', group='vcap', perms=0664)
+    dirs = [common.CF_DIR,
+            common.CC_DIR,
+            common.CC_RUN_DIR,
+            common.NGINX_RUN_DIR,
+            common.CC_LOG_DIR,
+            common.NGINX_LOG_DIR,
+            common.CC_CONFIG_DIR,
+            common.FOG_CONNECTION,
             '/var/vcap/data/cloud_controller_ng/tmp/uploads',
             '/var/vcap/data/cloud_controller_ng/tmp/staged_droplet_uploads']
     for item in dirs:
         host.mkdir(item, owner='vcap', group='vcap', perms=0775)
     host.chownr('/var/vcap', owner='vcap', group='vcap')
-    host.chownr(cf_config.CF_DIR, owner='vcap', group='vcap')
+    host.chownr(common.CF_DIR, owner='vcap', group='vcap')
     disable_nginx_service()
-    cc_db_migrate()
+    templating = setup_templating()
+    templating.render(common.CC_CONFIG_FILE)
+    common.db_migrate()    
+
 
 
 if __name__ == '__main__':

=== removed file 'hooks/utils.py'
--- hooks/utils.py	2014-03-26 17:43:00 +0000
+++ hooks/utils.py	1970-01-01 00:00:00 +0000
@@ -1,20 +0,0 @@
-from charmhelpers.fetch import (
-    apt_install,
-    filter_installed_packages
-)
-
-TEMPLATES_DIR = 'templates'
-
-try:
-    import jinja2
-except ImportError:
-    apt_install(filter_installed_packages(['python-jinja2']),
-                fatal=True)
-    import jinja2
-
-
-def render_template(template_name, context, template_dir=TEMPLATES_DIR):
-    templates = jinja2.Environment(
-        loader=jinja2.FileSystemLoader(template_dir))
-    template = templates.get_template(template_name)
-    return template.render(context)

=== modified file 'metadata.yaml'
--- metadata.yaml	2014-04-03 09:04:04 +0000
+++ metadata.yaml	2014-05-16 19:52:39 +0000
@@ -13,7 +13,11 @@
   provides-relation:
     interface: cf-cloud-controller
 requires:
-   nats:
-     interface: nats
-   router:
-     interface: router
+  nats:
+    interface: nats
+  router:
+    interface: router
+  db:
+    interface: mysql
+    optional: true
+   

=== modified file 'templates/cloud_controller.yml'
--- templates/cloud_controller.yml	2014-04-03 23:57:52 +0000
+++ templates/cloud_controller.yml	2014-05-16 19:52:39 +0000
@@ -54,7 +54,7 @@
 
 
 db: &db
-  database: sqlite:///var/lib/cloudfoundry/cfcloudcontroller/db/cc.db
+  database: {{db['dsn']}}
   max_connections: 25
   pool_timeout: 10
   log_level: debug2