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

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