wait for ssh server and docker daemon to be up

Docker-DCO-1.1-Signed-off-by: Aaron Feng <aaron.feng@gmail.com> (github: aaronfeng)
This commit is contained in:
Aaron Feng 2014-06-20 23:57:29 -04:00
parent 0285605f6a
commit d4a1ab2f7f
1 changed files with 53 additions and 13 deletions

View File

@ -2,6 +2,8 @@ package backends
import ( import (
"os" "os"
"net"
"net/http"
"os/signal" "os/signal"
"syscall" "syscall"
"path" "path"
@ -66,7 +68,11 @@ func (c *ec2Client) start(ctx *beam.Message) error {
} }
c.initDockerClientInstance(c.instance) c.initDockerClientInstance(c.instance)
c.waitForSsh()
c.startSshTunnel() c.startSshTunnel()
c.waitForDockerDaemon()
fmt.Printf("ec2 service up and running: region: %s zone: %s\n",
c.config.region.Name, c.config.zone)
ctx.Ret.Send(&beam.Message{Verb: beam.Ack, Ret: c.Server}) ctx.Ret.Send(&beam.Message{Verb: beam.Ack, Ret: c.Server})
return nil return nil
@ -140,8 +146,6 @@ func defaultConfigValues() (config *ec2Config) {
} }
func newConfig(args []string) (config *ec2Config, err error) { func newConfig(args []string) (config *ec2Config, err error) {
// TODO (aaron): fail fast on incorrect number of args
var optValPair []string var optValPair []string
var opt, val string var opt, val string
@ -211,7 +215,6 @@ func awsInit(config *ec2Config) (ec2Conn *ec2.EC2, err error) {
return ec2.New(auth, config.region), nil return ec2.New(auth, config.region), nil
} }
func (c *ec2Client) findInstance() (instance *ec2.Instance, err error) { func (c *ec2Client) findInstance() (instance *ec2.Instance, err error) {
filter := ec2.NewFilter() filter := ec2.NewFilter()
filter.Add("tag:Name", c.config.tag) filter.Add("tag:Name", c.config.tag)
@ -243,8 +246,6 @@ func (c *ec2Client) tagtInstance() error {
} }
func (c *ec2Client) startInstance() error { func (c *ec2Client) startInstance() error {
// TODO (aaron): make sure to wait for cloud-init to finish before
// executing docker commands
options := ec2.RunInstances{ options := ec2.RunInstances{
ImageId: c.config.ami, ImageId: c.config.ami,
InstanceType: c.config.instanceType, InstanceType: c.config.instanceType,
@ -300,6 +301,44 @@ func (c *ec2Client) initDockerClientInstance(instance *ec2.Instance) error {
return nil return nil
} }
func (c *ec2Client) waitForDockerDaemon() {
fmt.Println("waiting for docker daemon on remote machine to be available.")
for {
resp, _:= http.Get("http://localhost:" + c.config.sshLocalPort)
// wait for a response. any response to know docker daemon is up
if resp != nil {
break
}
fmt.Print(".")
time.Sleep(2 * time.Second)
}
fmt.Println()
}
func (c *ec2Client) waitForSsh() {
fmt.Println("waiting for ssh to be available. make sure ssh is open on port 22.")
conn := waitFor(c.instance.IPAddress, "22")
conn.Close()
}
func waitFor(ip, port string) (conn net.Conn) {
ipPort := fmt.Sprintf("%s:%s", ip, port)
var err error
for {
conn, err = net.DialTimeout("tcp", ipPort, time.Duration(3) * time.Second)
if err != nil {
fmt.Print(".")
time.Sleep(2 * time.Second)
} else {
fmt.Println()
break
}
}
return conn
}
func signalHandler(client *ec2Client) { func signalHandler(client *ec2Client) {
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt) signal.Notify(c, os.Interrupt)
@ -340,6 +379,7 @@ func Ec2() beam.Sender {
return err return err
})) }))
return backend return backend
} }