|
| 1 | +OpenShift Documentation Project <dev@lists.openshift.redhat.com> |
| 2 | +DOC_BRANCH_VERSION |
| 3 | +:data-uri: |
| 4 | +:icons: |
| 5 | + |
| 6 | +[[cartridge_events]] |
| 7 | +== Cartridge Events |
| 8 | + |
| 9 | +OpenShift provides a publish and subscribe system that enables a cartridge to act when a developer adds or removes another cartridge in an application. |
| 10 | + |
| 11 | +The _Publishes_ and _Subscribes_ elements in the [filename]#$cartridge_name/metadata/manifest.yml# file detail support for cartridge events. |
| 12 | + |
| 13 | +[[cartridge_event_publishing]] |
| 14 | +=== Cartridge Event Publishing |
| 15 | + |
| 16 | +When OpenShift adds a cartridge to an application, it uses entries in the _Publishes_ section of the [filename]#$cartridge_name/metadata/manifest.yml# file to construct events sent to other cartridges in the application. Define publish events in the [filename]#manifest.yml# file using the following format: |
| 17 | + |
| 18 | +---- |
| 19 | +Publishes: |
| 20 | + <event_name>: |
| 21 | + Type: "<event type>" |
| 22 | +---- |
| 23 | + |
| 24 | +.PHP Cartridge Publishes Entry |
| 25 | +---- |
| 26 | +Publishes: |
| 27 | + get-php-ini: |
| 28 | + Type: "FILESYSTEM:php-ini" |
| 29 | + publish-http-url: |
| 30 | + Type: "NET_TCP:httpd-proxy-info" |
| 31 | + publish-gear-endpoint: |
| 32 | + Type: "NET_TCP:gear-endpoint-info" |
| 33 | +---- |
| 34 | + |
| 35 | +For each _Publishes_ entry, OpenShift runs a script named [filename]#$cartridge_name/hooks/$event_name#. |
| 36 | + |
| 37 | +OpenShift joins lines of output that the [filename]#hooks/$event_name# script writes to [literal]#stdout# with single spaces, then inputs the result to subscriber scripts in other cartridges that match the [variable]#Type# of the publish event. The input to matching subscriber scripts is prefaced with [filename]#hooks/<event_name> <gear_name> <namespace> <gear_uuid>#. |
| 38 | + |
| 39 | +[[cartridge_event_subscriptions]] |
| 40 | +=== Cartridge Event Subscriptions |
| 41 | + |
| 42 | +When OpenShift adds a cartridge to an application, it uses entries in the _Subscribes_ section of the [filename]#$cartridge_name/metadata/manifest.yml# file in other cartridges to determine what actions to take for those other cartridges. Define subscribe events in the [filename]#manifest.yml# file using the following format: |
| 43 | + |
| 44 | +---- |
| 45 | +Subscribes: |
| 46 | + <event_name>: |
| 47 | + Type: "<event type>" |
| 48 | +---- |
| 49 | + |
| 50 | +.PHP Cartridge Subscribes Entry |
| 51 | +---- |
| 52 | +Subscribes: |
| 53 | + set-env: |
| 54 | + Type: "ENV:*" |
| 55 | + Required: false |
| 56 | + set-mysql-connection-info: |
| 57 | + Type: "NET_TCP:db:mysql" |
| 58 | + Required: false |
| 59 | + set-postgres-connection-info: |
| 60 | + Type: "NET_TCP:db:postgres" |
| 61 | + Required: false |
| 62 | + set-doc-url: |
| 63 | + Type: "STRING:urlpath" |
| 64 | + Required: false |
| 65 | +---- |
| 66 | + |
| 67 | +When OpenShift processes a cartridge publish script, it inputs the result to subscriber scripts in other cartridges that match the [variable]#Type# of the publish event. The input to matching subscriber scripts is prefaced with [filename]#$cartridge_name/hooks/<event_name> <gear_name> <namespace> <gear_uuid>#. |
| 68 | + |
| 69 | +For each matching _Subscribes_ entry, OpenShift runs a script named [filename]#$cartridge_name/hooks/$event_name#. OpenShift must send and process entries marked with +Required: true+. |
| 70 | + |
| 71 | +The publisher script determines the format of the information input to the subscriber script. Ensure that subscriber script can parse the input correctly. |
| 72 | + |
| 73 | +[[cartridge_event_example]] |
| 74 | +=== Cartridge Event Example |
| 75 | +In this example, an application developer adds a MySQL database cartridge to a PHP application. The publish and subscribe relationship between the cartridges enables the PHP cartridge to set environment variables on its gear so it can connect to the new MySQL cartridge, which is on a different gear. |
| 76 | + |
| 77 | +*MySQL Cartridge as Publisher* |
| 78 | + |
| 79 | +The MySQL cartridge lists a [variable]#publish-mysql-connection-info# event in the _Publishes_ section of its [filename]#mysql/metadata/manifest.yml# file: |
| 80 | + |
| 81 | +---- |
| 82 | +Publishes: |
| 83 | + publish-mysql-connection-info: |
| 84 | + Type: "NET_TCP:db:mysql" |
| 85 | +---- |
| 86 | + |
| 87 | +The MySQL cartridge implements a script in [filename]#mysql/hooks/publish-mysql-connection-info#. |
| 88 | + |
| 89 | +*PHP Cartridge as Subscriber* |
| 90 | + |
| 91 | +The PHP cartridge lists a [variable]#set-mysql-connection-info# event in the _Subscribes_ section of its [filename]#php/metadata/manifest.yml# file: |
| 92 | + |
| 93 | +---- |
| 94 | +Subscribes: |
| 95 | + set-mysql-connection-info: |
| 96 | + Type: "NET_TCP:db:mysql" |
| 97 | +---- |
| 98 | + |
| 99 | + |
| 100 | +The PHP cartridge implements a script in [filename]#php/hooks/set-mysql-connection-info#. |
| 101 | + |
| 102 | +*Cartridge Event Communication Process* |
| 103 | + |
| 104 | +OpenShift matches the event [variable]#Type# in the PHP cartridge's _Subscribes_ list to the event [variable]#Type# in the MySQL cartridge's _Publishes_ list. In this example, the event [variable]#Type# is "NET_TCP:db:mysql". |
| 105 | + |
| 106 | +The MySQL cartridge's [filename]#publish-mysql-connection-info# script outputs the username, host, port, URL, and password required to connect to the MySQL instance: |
| 107 | + |
| 108 | +---- |
| 109 | +OPENSHIFT_MYSQL_DB_USERNAME=username; |
| 110 | +OPENSHIFT_MYSQL_DB_PASSWORD=password; |
| 111 | +OPENSHIFT_MYSQL_DB_HOST=hostname; |
| 112 | +OPENSHIFT_MYSQL_DB_PORT=port; |
| 113 | +OPENSHIFT_MYSQL_DB_URL=url; |
| 114 | +---- |
| 115 | + |
| 116 | +OpenShift sends the output of the MySQL cartridge's [filename]#publish-mysql-connection-info# to the PHP cartridge's [filename]#set-mysql-connection-info# script using the following format: |
| 117 | + |
| 118 | +---- |
| 119 | +hooks/publish-mysql-connection-info gear_name namespace gear_uuid 'OPENSHIFT_MYSQL_DB_USERNAME=username;OPENSHIFT_MYSQL_DB_PASSWORD=password;OPENSHIFT_MYSQL_DB_HOST=hostname;OPENSHIFT_MYSQL_DB_PORT=port;OPENSHIFT_MYSQL_DB_URL=url;' |
| 120 | +---- |
| 121 | + |
| 122 | +Note that the publisher script determines the format of the information input to the subscriber script. When writing subscriber scripts, ensure that they parse the input correctly. |
| 123 | + |
0 commit comments