...

Source file src/github.com/datawire/ambassador/v2/cmd/entrypoint/demomode.go

Documentation: github.com/datawire/ambassador/v2/cmd/entrypoint

     1  package entrypoint
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"time"
     7  
     8  	"github.com/datawire/ambassador/v2/pkg/acp"
     9  	"github.com/datawire/dlib/dgroup"
    10  	"github.com/datawire/dlib/dlog"
    11  )
    12  
    13  // bootDemoMode: start demo mode running. This is more obnoxious than one might
    14  // think, since we have to start our two demo services _and_ we have to prime
    15  // the acp.AmbassadorWatcher so that health checks work.
    16  func bootDemoMode(ctx context.Context, group *dgroup.Group, ambwatch *acp.AmbassadorWatcher) {
    17  	group.Go("demo_auth", func(ctx context.Context) error {
    18  		cmd := subcommand(ctx, "/usr/bin/python3", "/ambassador/demo-services/auth.py")
    19  		return cmd.Run()
    20  	})
    21  
    22  	group.Go("demo_qotm", func(ctx context.Context) error {
    23  		cmd := subcommand(ctx, "/usr/bin/python3", "/ambassador/demo-services/qotm.py")
    24  		return cmd.Run()
    25  	})
    26  
    27  	// XXX More disgusting hackery. Here's the thing: in demo mode, we don't actually
    28  	// push any snapshots through to diagd, because diagd just reads its config from
    29  	// the filesystem. Since we don't push any snapshots, the acp.AmbassadorWatcher
    30  	// never decides that Ambassador is ready.
    31  	//
    32  	// To fake that out, we're going to wait for Envoy to be alive, then just inform
    33  	// the acp.AmbassadorWatcher that _clearly_ we've pushed and processed a snapshot.
    34  	// Ewwww.
    35  	go func() {
    36  		for {
    37  			resp, err := http.Get("http://localhost:8001/ready")
    38  
    39  			if err == nil {
    40  				if resp.StatusCode == 200 {
    41  					// Done! Tell the acp.AmbassadorWatcher that we've sent and processed
    42  					// a snapshot, so that it'll actually take Ambassador to ready status.
    43  					ambwatch.NoteSnapshotSent()
    44  					time.Sleep(5 * time.Millisecond)
    45  					ambwatch.NoteSnapshotProcessed()
    46  					break
    47  				}
    48  			}
    49  
    50  			time.Sleep(5 * time.Second)
    51  		}
    52  	}()
    53  
    54  	go func() {
    55  		// Wait for Ambassador to claim that it's ready.
    56  		for {
    57  			resp, err := http.Get("http://localhost:8877/ambassador/v0/check_ready")
    58  
    59  			if err == nil {
    60  				if resp.StatusCode == 200 {
    61  					// Done! Spit out the magic string that the demo test needs to see.
    62  					dlog.Infof(ctx, "AMBASSADOR DEMO RUNNING")
    63  					break
    64  				}
    65  			}
    66  
    67  			time.Sleep(5 * time.Second)
    68  		}
    69  	}()
    70  }
    71  

View as plain text