Skip to content
This repository has been archived by the owner on Aug 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #115 from ibuildthecloud/fixes
Browse files Browse the repository at this point in the history
Fix circular links for serve side flow
  • Loading branch information
ibuildthecloud committed Oct 26, 2015
2 parents 3abc64d + 2aa19d8 commit 0c06c1c
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 14 deletions.
28 changes: 28 additions & 0 deletions executor/handlers/common.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package handlers

import (
"errors"
"fmt"

"github.com/Sirupsen/logrus"
"github.com/rancher/go-machine-service/events"
"github.com/rancher/go-machine-service/locks"
"github.com/rancher/go-rancher/client"
)

var (
ErrTimeout = errors.New("Timeout waiting service")
)

func emptyReply(event *events.Event, apiClient *client.RancherClient) error {
reply := newReply(event)
return publishReply(reply, apiClient)
Expand Down Expand Up @@ -35,3 +44,22 @@ func reply(event *events.Event, apiClient *client.RancherClient, data map[string
reply.Data = data
return publishReply(reply, apiClient)
}

func WithLock(f func(event *events.Event, apiClient *client.RancherClient) error) func(event *events.Event, apiClient *client.RancherClient) error {
return func(event *events.Event, apiClient *client.RancherClient) error {
lockKey := fmt.Sprintf("%s:%s", event.ResourceType, event.ResourceId)
lock := locks.Lock(lockKey)
if lock == nil {
logrus.Infof("Busying processing %s", lockKey)
return nil
}
defer lock.Unlock()

err := f(event, apiClient)
if err == ErrTimeout {
logrus.Infof("Timeout processing %s", lockKey)
return nil
}
return nil
}
}
5 changes: 5 additions & 0 deletions executor/handlers/create_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func createEnvironment(logger *logrus.Entry, event *events.Event, apiClient *cli
return err
}

// This is to make sure circular links work
if err := project.Create(); err != nil {
return err
}

uuid := context.Uuid()
if uuid == "" {
return emptyReply(event, apiClient)
Expand Down
5 changes: 4 additions & 1 deletion executor/handlers/upgrade_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"errors"
"fmt"
"time"

"github.com/Sirupsen/logrus"
"github.com/docker/libcompose/utils"
Expand Down Expand Up @@ -128,11 +129,13 @@ func wait(apiClient *client.RancherClient, service *client.Service) error {
if service.Transitioning != "yes" {
break
}
time.Sleep(500 * time.Millisecond)
}

switch service.Transitioning {
case "yes":
return fmt.Errorf("Timeout waiting for %s to finish", service.Name)
logrus.Infof("Timeout waiting for %s to finish", service.Name)
return ErrTimeout
case "no":
return nil
default:
Expand Down
8 changes: 4 additions & 4 deletions executor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ func Main() {
logger.Info("Starting rancher-compose-executor")

eventHandlers := map[string]events.EventHandler{
"environment.create": handlers.CreateEnvironment,
"environment.upgrade": handlers.UpgradeEnvironment,
"environment.finishupgrade": handlers.FinishUpgradeEnvironment,
"environment.rollback": handlers.RollbackEnvironment,
"environment.create": handlers.WithLock(handlers.CreateEnvironment),
"environment.upgrade": handlers.WithLock(handlers.UpgradeEnvironment),
"environment.finishupgrade": handlers.WithLock(handlers.FinishUpgradeEnvironment),
"environment.rollback": handlers.WithLock(handlers.RollbackEnvironment),
"ping": func(event *events.Event, apiClient *client.RancherClient) error {
return nil
},
Expand Down
3 changes: 3 additions & 0 deletions rancher/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ func (f *NormalFactory) upgrade(r *RancherService, existingService *rancherClien
}

if service {
// Scale must be changed through "scale" not "up", so always copy scale existing scale
config.Scale = existingService.Scale

logrus.Infof("Updating %s", r.Name())
schemaType := rancherClient.SERVICE_TYPE
switch FindServiceType(r) {
Expand Down
78 changes: 69 additions & 9 deletions tests/integration/cattletest/core/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ def test_stack_create_upgrade_finish(client):
env.upgrade(dockerCompose=template,
rancherCompose=rancher_compose,
externalId='foo2')

env = client.wait_success(env)
env = client.wait_success(env, timeout=120)
for s in env.services():
s = client.wait_success(s)
if s.name == 'one':
Expand Down Expand Up @@ -136,18 +135,19 @@ def test_stack_create_and_upgrade(client):
'''

# TODO: externalId should not be in upgrade
env.upgrade(dockerCompose=template,
rancherCompose=rancher_compose,
externalId='foo2')

env = client.wait_success(env)
env = env.upgrade(dockerCompose=template,
rancherCompose=rancher_compose,
externalId='foo2')
env = client.wait_success(env, timeout=120)
assert env.state == 'upgraded'
for s in env.services():
s = client.wait_success(s)
if s.name == 'one':
assert s.state == 'upgraded'

env.rollback()
env = client.wait_success(env)
env = env.rollback()
env = client.wait_success(env, timeout=120)
assert env.state == 'active'
for s in env.services():
s = client.wait_success(s)
assert s.state == 'active'
Expand All @@ -156,5 +156,65 @@ def test_stack_create_and_upgrade(client):
assert env.previousExternalId is None


def test_stack_change_scale_upgrade(client):
name = 'project-' + random_str()
template = '''
one:
image: nginx
'''
rancher_compose = '''
one:
scale: 2
'''
env = client.create_environment(name=name, dockerCompose=template,
rancherCompose=rancher_compose)
env = client.wait_success(env)
env = client.wait_success(env.activateservices())
assert env.state == 'active'
s = find_one(env.services)
assert s.launchConfig.imageUuid == 'docker:nginx'
assert s.scale == 2

template = '''
one:
image: nginx:2
'''
# Something else about the service needs to change too, like metadata
# scale is ignore in the diff
rancher_compose = '''
one:
metadata:
foo: bar
scale: 4
'''
env.upgrade(dockerCompose=template,
rancherCompose=rancher_compose)
env = client.wait_success(env, timeout=120)
assert env.state == 'upgraded'
s = find_one(env.services)
assert s.launchConfig.imageUuid == 'docker:nginx:2'
assert s.scale == 2


def test_stack_create_circles(client):
name = 'project-' + random_str()
template = '''
one:
image: nginx
links:
- two
two:
image: nginx
links:
- one
'''
env = client.create_environment(name=name, dockerCompose=template)
env = client.wait_success(env)

for s in env.services():
find_one(s.consumedservices)


def _base():
return path.dirname(__file__)

0 comments on commit 0c06c1c

Please sign in to comment.