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

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