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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. configure_pg_sources,
  34. configure_analyst_opsvm
  35. )
  36. hooks = Hooks()
  37. CONFIGS = register_configs()
  38. @hooks.hook()
  39. def install():
  40. '''
  41. Install hook is run when the charm is first deployed on a node.
  42. '''
  43. status_set('maintenance', 'Executing pre-install')
  44. load_iptables()
  45. configure_sources(update=True)
  46. status_set('maintenance', 'Installing apt packages')
  47. pkgs = determine_packages()
  48. for pkg in pkgs:
  49. apt_install(pkg, options=['--force-yes'], fatal=True)
  50. load_iovisor()
  51. ensure_mtu()
  52. ensure_files()
  53. add_lcm_key()
  54. @hooks.hook('plumgrid-relation-changed')
  55. @restart_on_change(restart_map())
  56. def plumgrid_changed():
  57. '''
  58. This hook is run when relation between plumgrid-gateway and
  59. plumgrid-director is made.
  60. '''
  61. if director_cluster_ready():
  62. ensure_mtu()
  63. configure_analyst_opsvm()
  64. CONFIGS.write_all()
  65. @hooks.hook('config-changed')
  66. @restart_on_stop()
  67. @restart_on_change(restart_map())
  68. def config_changed():
  69. '''
  70. This hook is run when a config parameter is changed.
  71. It also runs on node reboot.
  72. '''
  73. charm_config = config()
  74. if charm_config.changed('lcm-ssh-key'):
  75. if add_lcm_key():
  76. log("PLUMgrid LCM Key added")
  77. if charm_config.changed('fabric-interfaces'):
  78. if not fabric_interface_changed():
  79. log("Fabric interface already set")
  80. if (charm_config.changed('install_sources') or
  81. charm_config.changed('plumgrid-build') or
  82. charm_config.changed('install_keys') or
  83. charm_config.changed('iovisor-build')):
  84. stop_pg()
  85. status_set('maintenance', 'Upgrading apt packages')
  86. if charm_config.changed('install_sources'):
  87. configure_pg_sources()
  88. configure_sources(update=True)
  89. pkgs = determine_packages()
  90. for pkg in pkgs:
  91. apt_install(pkg, options=['--force-yes'], fatal=True)
  92. remove_iovisor()
  93. load_iovisor()
  94. ensure_mtu()
  95. CONFIGS.write_all()
  96. @hooks.hook('upgrade-charm')
  97. @restart_on_change(restart_map())
  98. def upgrade_charm():
  99. ensure_mtu()
  100. CONFIGS.write_all()
  101. @hooks.hook('stop')
  102. def stop():
  103. '''
  104. This hook is run when the charm is destroyed.
  105. '''
  106. stop_pg()
  107. @hooks.hook('update-status')
  108. def update_status():
  109. if service_running('plumgrid'):
  110. status_set('active', 'Unit is ready')
  111. else:
  112. status_set('blocked', 'plumgrid service not running')
  113. def main():
  114. try:
  115. hooks.execute(sys.argv)
  116. except UnregisteredHookError as e:
  117. log('Unknown hook {} - skipping.'.format(e))
  118. if __name__ == '__main__':
  119. main()