name: Simple Netcat with Client

location: localhost

services:

# the netcat server instance, running in listener mode (-l)
- type: org.apache.brooklyn.entity.software.base.VanillaSoftwareProcess
  id: netcat-server
  name: Simple Netcat Server
  launch.command: |
    echo hello | nc -l 4321 >> server-input &
    echo $! > $PID_FILE

  # a failure detector and a service restarter work together
  brooklyn.enrichers:
  - type: org.apache.brooklyn.policy.ha.ServiceFailureDetector
    brooklyn.config:
      # wait 15s after service fails before propagating failure
      serviceFailedStabilizationDelay: 15s

  brooklyn.policies:
  - type: org.apache.brooklyn.policy.ha.ServiceRestarter
    brooklyn.config:
      # repeated failures in a time window can cause the restarter to abort,
      # propagating the failure; a time window of 0 will mean it always restarts!
      failOnRecurringFailuresInThisDuration: 0
      
  brooklyn.initializers:
  # two sensors, recording the data sent to this netcat server:
  
  - type: org.apache.brooklyn.core.sensor.ssh.SshCommandSensor
    brooklyn.config:
      name: output.last
      command: tail -1 server-input
      
  - type: org.apache.brooklyn.core.sensor.ssh.SshCommandSensor
    brooklyn.config:
      name: output.all
      command: cat server-input

# a client to hit netcat
- type: org.apache.brooklyn.entity.software.base.VanillaSoftwareProcess
  name: Simple Pinger
  
  # set the hostname of the netcat instance as an env var for the scripts 
  env:
    TARGET_HOSTNAME: 
      $brooklyn:component("netcat-server").
        attributeWhenReady("host.name")
    
  # start/check/stop are no-op
  launch.command: ""
  checkRunning.command: ""
  stop.command: ""
  
  brooklyn.initializers:
  # but there is a sample effector which runs nc in client mode
  
  - type: org.apache.brooklyn.core.effector.ssh.SshCommandEffector
    brooklyn.config:
      name: sayHiNetcat
      description: Echo a small hello string to the netcat entity
      command: |
        echo $message | nc $TARGET_HOSTNAME 4321
      parameters:
        message:
          description: The string to pass to netcat
          defaultValue: hi netcat

# and add an enricher at the root so all sensors from netcat-server are visible on the root
brooklyn.enrichers:
- enricherType: org.apache.brooklyn.enricher.stock.Propagator
  brooklyn.config:
    enricher.producer: $brooklyn:component("netcat-server")
    enricher.propagating.propagatingAll: true
