Juju Charm - PLUMgrid Gateway
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pg_gw_hooks.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/python
  2. # Copyright (c) 2015, PLUMgrid Inc, http://plumgrid.com
  3. # The hooks of this charm have been symlinked to functions
  4. # in this file.
  5. import sys
  6. from charmhelpers.core.host import service_running
  7. from charmhelpers.core.hookenv import (
  8. Hooks,
  9. UnregisteredHookError,
  10. log,
  11. config,
  12. status_set
  13. )
  14. from charmhelpers.fetch import (
  15. apt_install,
  16. configure_sources,
  17. )
  18. from pg_gw_utils import (
  19. register_configs,
  20. ensure_files,
  21. restart_map,
  22. stop_pg,
  23. determine_packages,
  24. load_iovisor,
  25. remove_iovisor,
  26. ensure_mtu,
  27. add_lcm_key,
  28. fabric_interface_changed,
  29. load_iptables,
  30. restart_on_change,
  31. restart_on_stop,
  32. director_cluster_ready
  33. )
  34. hooks = Hooks()
  35. CONFIGS = register_configs()
  36. @hooks.hook()
  37. def install():
  38. '''
  39. Install hook is run when the charm is first deployed on a node.
  40. '''
  41. status_set('maintenance', 'Executing pre-install')
  42. load_iptables()
  43. configure_sources(update=True)
  44. status_set('maintenance', 'Installing apt packages')
  45. pkgs = determine_packages()
  46. for pkg in pkgs:
  47. apt_install(pkg, options=['--force-yes'], fatal=True)
  48. load_iovisor()
  49. ensure_mtu()
  50. ensure_files()
  51. add_lcm_key()
  52. @hooks.hook('plumgrid-relation-changed')
  53. @restart_on_change(restart_map())
  54. def plumgrid_changed():
  55. '''
  56. This hook is run when relation between plumgrid-gateway and
  57. plumgrid-director is made.
  58. '''
  59. if director_cluster_ready():
  60. ensure_mtu()
  61. CONFIGS.write_all()
  62. @hooks.hook('config-changed')
  63. @restart_on_stop()
  64. @restart_on_change(restart_map())
  65. def config_changed():
  66. '''
  67. This hook is run when a config parameter is changed.
  68. It also runs on node reboot.
  69. '''
  70. charm_config = config()
  71. if charm_config.changed('lcm-ssh-key'):
  72. if add_lcm_key():
  73. log("PLUMgrid LCM Key added")
  74. if charm_config.changed('fabric-interfaces'):
  75. if not fabric_interface_changed():
  76. log("Fabric interface already set")
  77. if (charm_config.changed('install_sources') or
  78. charm_config.changed('plumgrid-build') or
  79. charm_config.changed('install_keys') or
  80. charm_config.changed('iovisor-build')):
  81. stop_pg()
  82. status_set('maintenance', 'Upgrading apt packages')
  83. configure_sources(update=True)
  84. pkgs = determine_packages()
  85. for pkg in pkgs:
  86. apt_install(pkg, options=['--force-yes'], fatal=True)
  87. remove_iovisor()
  88. load_iovisor()
  89. ensure_mtu()
  90. CONFIGS.write_all()
  91. @hooks.hook('upgrade-charm')
  92. @restart_on_change(restart_map())
  93. def upgrade_charm():
  94. ensure_mtu()
  95. CONFIGS.write_all()
  96. @hooks.hook('stop')
  97. def stop():
  98. '''
  99. This hook is run when the charm is destroyed.
  100. '''
  101. stop_pg()
  102. @hooks.hook('update-status')
  103. def update_status():
  104. if service_running('plumgrid'):
  105. status_set('active', 'Unit is ready')
  106. else:
  107. status_set('blocked', 'plumgrid service not running')
  108. def main():
  109. try:
  110. hooks.execute(sys.argv)
  111. except UnregisteredHookError as e:
  112. log('Unknown hook {} - skipping.'.format(e))
  113. if __name__ == '__main__':
  114. main()