Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zealws committed May 24, 2016
1 parent b4acb24 commit 60d1d33
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
57 changes: 54 additions & 3 deletions src/daemon/atcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ func NewAtcd(db *DbRunner, shaper *ShapingEngine, options *AtcdOptions) atc_thri
if options == nil {
options = &DefaultAtcdOptions
}
return &Atcd{
d := &Atcd{
db: db,
shaper: shaper,
options: *options,
}
return d
}

func (atcd *Atcd) GetAtcdInfo() (*atc_thrift.AtcdInfo, error) {
Expand Down Expand Up @@ -136,6 +137,7 @@ func (atcd *Atcd) CreateGroup(member string) (*atc_thrift.ShapingGroup, error) {
return nil, err
}
grp.ID = dbgrp.id
defer atcd.Cleanup()
return grp, nil
}

Expand Down Expand Up @@ -250,8 +252,7 @@ func (atcd *Atcd) LeaveGroup(id int64, to_remove, token string) error {
if err := atcd.shaper.LeaveGroup(id, tgt); err != nil {
return err
}
// FIXME: clean shaper's group too!
defer atcd.db.Cleanup()
defer atcd.Cleanup()
return atcd.db.DeleteMember(tgt)
}

Expand Down Expand Up @@ -318,6 +319,56 @@ func (atcd *Atcd) otp(group *DbGroup) *otp.TOTP {
}
}

func (atcd *Atcd) Cleanup() {
grps, err := atcd.db.GetOldGroups()
if err != nil {
Log.Printf("Couldn't clean old groups: %v", err)
} else {
n := atcd.deleteGroups(grps)
if n > 0 {
Log.Printf("Cleaned %d empty groups.", n)
}
}
grps, err = atcd.db.GetEmptyGroups()
if err != nil {
Log.Printf("Couldn't clean empty groups: %v", err)
} else {
n := atcd.deleteGroups(grps)
if n > 0 {
Log.Printf("Cleaned %d empty groups.", n)
}
}
}

func (atcd *Atcd) deleteGroups(grps <-chan *DbGroup) int {
n := 0
for grp := range grps {
if err := atcd.deleteGroup(grp); err != nil {
Log.Printf("Couldn't clean group %d: %v", grp.id, err)
} else {
n++
}
}
return n
}

func (atcd *Atcd) deleteGroup(grp *DbGroup) error {
members, err := atcd.db.GetMembersOf(grp.id)
if err != nil {
return fmt.Errorf("Couldn't get members: %v", grp.id, err)
} else {
for m := range members {
if err := atcd.shaper.LeaveGroup(grp.id, m); err != nil {
return fmt.Errorf("Couldn't clean member %v: %v", m, err)
}
}
}
if err := atcd.db.DeleteGroup(grp.id); err != nil {
return err
}
return atcd.shaper.DeleteGroup(grp.id)
}

func makeSecret() string {
// Can probably find a better source of random secrets than this...
return uuid.New()
Expand Down
2 changes: 1 addition & 1 deletion utils/test-cleanup.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

if [ "$(whoami)" != "root" ] ; then
sudo "$0"
Expand Down
2 changes: 1 addition & 1 deletion utils/test-setup.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

if [ "$(whoami)" != "root" ] ; then
sudo "$0"
Expand Down
20 changes: 20 additions & 0 deletions utils/test-status.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

if [ "$(whoami)" != "root" ] ; then
sudo "$0"
exit 0
fi

ip netns exec atc iptables -t mangle -nvL FORWARD

echo

ip netns exec atc tc qdisc list dev lan0
ip netns exec atc tc filter list dev lan0
ip netns exec atc tc class list dev lan0

echo

ip netns exec atc tc qdisc list dev wan0
ip netns exec atc tc filter list dev wan0
ip netns exec atc tc class list dev wan0

0 comments on commit 60d1d33

Please sign in to comment.