Permalink
Browse files

Starting hosts from images for a faster startup

  • Loading branch information...
MohamedBassem committed Jul 30, 2015
1 parent 8ab4947 commit 241b74f7600a885fd1c9c66c94f28adfe2924613
Showing with 52 additions and 2 deletions.
  1. +51 −1 hosts.go
  2. +1 −1 jobs.go
View
@@ -51,7 +51,7 @@ func (ctx *context) ListHosts() []Host {
return hosts
}
func (ctx *context) NewHost() error {
func (ctx *context) NewHostFromConfig() error {
redisIP := strings.Split(ctx.config.RedisAddress, ":")[0]
redisPort := strings.Split(ctx.config.RedisAddress, ":")[1]
dropletName := "getaredis-server-" + generateRandomString(10)
@@ -126,6 +126,56 @@ write_files:
return err
}
func (ctx *context) NewHostFromImage() error {
redisIP := strings.Split(ctx.config.RedisAddress, ":")[0]
redisPort := strings.Split(ctx.config.RedisAddress, ":")[1]
dropletName := "getaredis-server-" + generateRandomString(10)
userData := `#cloud-config
runcmd:
- service docker restart
- htpasswd -b -c /etc/nginx/docker_auth/.htpasswd %v %v
- service nginx reload
write_files:
- path: /usr/local/bin/service_discovery
permissions: '0755'
content: |
#!/bin/bash
(
PUBLIC_IP=$(curl http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
PRIVATE_IP=$(curl http://169.254.169.254/metadata/v1/interfaces/private/0/ipv4/address)
NODE_NAME=%v
echo "AUTH %v";
while true; do
NUMBER_OF_CONTAINERS=$(($(docker ps | wc -l) - 1))
echo "SET server:$NODE_NAME '{\"PublicIP\":\"$PUBLIC_IP\",\"PrivateIP\":\"$PRIVATE_IP\",\"Name\":\"$NODE_NAME\",\"NumberOfContainers\":$NUMBER_OF_CONTAINERS}'";
echo "EXPIRE server:$NODE_NAME 10";
sleep 4;
done
) | telnet %v %v
`
userData = fmt.Sprintf(userData, ctx.config.Docker["user"], ctx.config.Docker["password"], dropletName, ctx.config.RedisPassword, redisIP, redisPort)
var sshKey *godo.DropletCreateSSHKey
if ctx.config.DropletSSHKeyID != -1 {
sshKey = &godo.DropletCreateSSHKey{ID: ctx.config.DropletSSHKeyID}
}
createRequest := &godo.DropletCreateRequest{
Name: dropletName,
Region: "nyc3",
Size: "512mb",
Image: godo.DropletCreateImage{
ID: 12949456,
},
PrivateNetworking: true,
UserData: userData,
SSHKeys: []godo.DropletCreateSSHKey{*sshKey},
}
_, _, err := ctx.digitalocean.Droplets.Create(createRequest)
return err
}
func (ctx *context) DeleteHost(ip string) error {
droplets, _, err := ctx.digitalocean.Droplets.List(nil)
if err != nil {
View
@@ -27,7 +27,7 @@ func MonitorHosts(ctx *context) (startedHosts bool, deletedHosts []string, err e
}
}
if zeros == 0 {
err = ctx.NewHost()
err = ctx.NewHostFromImage()
if err != nil {
return
}

0 comments on commit 241b74f

Please sign in to comment.