Compare commits
No commits in common. "aa6611249b6fa77683da711139f87acdca9e8387" and "78363194bffa4c218ee4f875abbbf554a38a212b" have entirely different histories.
aa6611249b
...
78363194bf
15 changed files with 99 additions and 254 deletions
|
|
@ -28,7 +28,7 @@ func TestListSubnets_Empty(t *testing.T) {
|
|||
|
||||
func TestListSubnets_WithData(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "created")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||
kv.AddInDB(db, "subnet/sn-2/state", "creating")
|
||||
kv.AddInDB(db, "subnet/sn-2/vpc", "vpc-1")
|
||||
|
|
@ -55,7 +55,7 @@ func TestListSubnets_InvalidMethod(t *testing.T) {
|
|||
|
||||
func TestPostSubnet_Created(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
req := SubnetCreateRequest{
|
||||
Name: "sn-new",
|
||||
VPC: "vpc-1",
|
||||
|
|
@ -91,7 +91,7 @@ func TestPostSubnet_MissingFields(t *testing.T) {
|
|||
|
||||
func TestPostSubnet_IfaceTypeOptional(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
req := SubnetCreateRequest{
|
||||
Name: "sn-opt",
|
||||
VPC: "vpc-1",
|
||||
|
|
@ -126,8 +126,8 @@ func TestPostSubnet_VPCNotFound(t *testing.T) {
|
|||
|
||||
func TestPostSubnet_Duplicate(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-exist/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
kv.AddInDB(db, "subnet/sn-exist/state", "created")
|
||||
req := SubnetCreateRequest{
|
||||
Name: "sn-exist",
|
||||
VPC: "vpc-1",
|
||||
|
|
@ -163,7 +163,7 @@ func TestPostSubnet_VPCDeleting(t *testing.T) {
|
|||
|
||||
func TestPostSubnet_BridgeMode_Success(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
req := SubnetCreateRequest{
|
||||
Name: "sn-br",
|
||||
VPC: "vpc-1",
|
||||
|
|
@ -190,7 +190,7 @@ func TestPostSubnet_BridgeMode_Success(t *testing.T) {
|
|||
|
||||
func TestPostSubnet_UnknownMode(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
req := SubnetCreateRequest{
|
||||
Name: "sn-1",
|
||||
VPC: "vpc-1",
|
||||
|
|
@ -219,7 +219,7 @@ func TestPostSubnet_InvalidBody(t *testing.T) {
|
|||
|
||||
func TestGetSubnet_Found(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "created")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||
kv.AddInDB(db, "subnet/sn-1/cidr", "10.0.0.0/24")
|
||||
kv.AddInDB(db, "subnet/sn-1/interface_ip", "10.0.0.1")
|
||||
|
|
@ -231,7 +231,7 @@ func TestGetSubnet_Found(t *testing.T) {
|
|||
}
|
||||
var result Subnet
|
||||
json.NewDecoder(w.Body).Decode(&result)
|
||||
if result.Name != "sn-1" || result.State != "running" {
|
||||
if result.Name != "sn-1" || result.State != "created" {
|
||||
t.Errorf("résultat inattendu : %+v", result)
|
||||
}
|
||||
if result.VPC != "vpc-1" {
|
||||
|
|
@ -261,7 +261,7 @@ func TestGetSubnet_EmptyName(t *testing.T) {
|
|||
|
||||
func TestDeleteSubnet_Success(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "subnet/sn-del/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-del/state", "created")
|
||||
req := httptest.NewRequest(http.MethodDelete, "/subnets/sn-del", nil)
|
||||
w := httptest.NewRecorder()
|
||||
s.SubnetByNameHandler(w, req)
|
||||
|
|
@ -287,7 +287,7 @@ func TestDeleteSubnet_NotFound(t *testing.T) {
|
|||
|
||||
func TestSubnetByName_InvalidMethod(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "created")
|
||||
req := httptest.NewRequest(http.MethodPut, "/subnets/sn-1", nil)
|
||||
w := httptest.NewRecorder()
|
||||
s.SubnetByNameHandler(w, req)
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ import (
|
|||
|
||||
func TestVmFromDB_SingleDisk(t *testing.T) {
|
||||
entries := map[string]string{
|
||||
"vm/vm-1/state": "running",
|
||||
"vm/vm-1/state": "started",
|
||||
"vm/vm-1/subnet": "sn-1",
|
||||
"vm/vm-1/ip": "10.0.0.5",
|
||||
"vm/vm-1/metadata_port": "1234",
|
||||
|
|
@ -37,7 +37,7 @@ func TestVmFromDB_SingleDisk(t *testing.T) {
|
|||
|
||||
func TestVmFromDB_MultiDisk(t *testing.T) {
|
||||
entries := map[string]string{
|
||||
"vm/vm-2/state": "running",
|
||||
"vm/vm-2/state": "started",
|
||||
"vm/vm-2/subnet": "sn-1",
|
||||
"vm/vm-2/ip": "10.0.0.6",
|
||||
"vm/vm-2/metadata_port": "1235",
|
||||
|
|
@ -62,7 +62,7 @@ func TestVmFromDB_MultiDisk(t *testing.T) {
|
|||
func TestVmFromDB_SlotGap(t *testing.T) {
|
||||
// sdb absent — sda et sdc seulement
|
||||
entries := map[string]string{
|
||||
"vm/vm-3/state": "running",
|
||||
"vm/vm-3/state": "started",
|
||||
"vm/vm-3/subnet": "sn-1",
|
||||
"vm/vm-3/ip": "10.0.0.7",
|
||||
"vm/vm-3/metadata_port": "1236",
|
||||
|
|
@ -94,7 +94,7 @@ func TestVmFromDB_SlotGap(t *testing.T) {
|
|||
|
||||
func TestStartVM_MultiDisk(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "created")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||
|
||||
body, _ := json.Marshal(VMCreateRequest{
|
||||
|
|
@ -128,7 +128,7 @@ func TestStartVM_MultiDisk(t *testing.T) {
|
|||
|
||||
func TestStartVM_StorageReturnedInResponse(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "created")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||
|
||||
body, _ := json.Marshal(VMCreateRequest{
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ func TestListVpcs_Empty(t *testing.T) {
|
|||
|
||||
func TestListVpcs_WithData(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/v1/state", "running")
|
||||
kv.AddInDB(db, "vpc/v1/state", "created")
|
||||
kv.AddInDB(db, "vpc/v2/state", "creating")
|
||||
w := httptest.NewRecorder()
|
||||
s.VpcsHandler(w, httptest.NewRequest(http.MethodGet, "/vpcs", nil))
|
||||
|
|
@ -104,7 +104,7 @@ func TestPostVpc_InvalidCIDR(t *testing.T) {
|
|||
|
||||
func TestPostVpc_Duplicate(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-exist/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-exist/state", "created")
|
||||
body, _ := json.Marshal(VPCCreateRequest{Name: "vpc-exist", CIDR: "10.0.0.0/16"})
|
||||
w := httptest.NewRecorder()
|
||||
s.VpcsHandler(w, httptest.NewRequest(http.MethodPost, "/vpcs", bytes.NewReader(body)))
|
||||
|
|
@ -126,7 +126,7 @@ func TestPostVpc_InvalidBody(t *testing.T) {
|
|||
|
||||
func TestGetVpc_Found(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
req := httptest.NewRequest(http.MethodGet, "/vpcs/vpc-1", nil)
|
||||
w := httptest.NewRecorder()
|
||||
s.VpcByNameHandler(w, req)
|
||||
|
|
@ -135,7 +135,7 @@ func TestGetVpc_Found(t *testing.T) {
|
|||
}
|
||||
var result VPC
|
||||
json.NewDecoder(w.Body).Decode(&result)
|
||||
if result.Name != "vpc-1" || result.State != "running" {
|
||||
if result.Name != "vpc-1" || result.State != "created" {
|
||||
t.Errorf("résultat inattendu : %+v", result)
|
||||
}
|
||||
}
|
||||
|
|
@ -162,7 +162,7 @@ func TestGetVpc_EmptyName(t *testing.T) {
|
|||
|
||||
func TestDeleteVpc_Success(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-del/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-del/state", "created")
|
||||
req := httptest.NewRequest(http.MethodDelete, "/vpcs/vpc-del", nil)
|
||||
w := httptest.NewRecorder()
|
||||
s.VpcByNameHandler(w, req)
|
||||
|
|
@ -186,37 +186,10 @@ func TestDeleteVpc_NotFound(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestDeleteVpc_ConflictWhileCreating(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-wip/state", "creating")
|
||||
req := httptest.NewRequest(http.MethodDelete, "/vpcs/vpc-wip", nil)
|
||||
w := httptest.NewRecorder()
|
||||
s.VpcByNameHandler(w, req)
|
||||
if w.Code != http.StatusConflict {
|
||||
t.Errorf("attendu 409, obtenu %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteVpc_AllowedFromError(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-ko/state", "error")
|
||||
req := httptest.NewRequest(http.MethodDelete, "/vpcs/vpc-ko", nil)
|
||||
w := httptest.NewRecorder()
|
||||
s.VpcByNameHandler(w, req)
|
||||
if w.Code != http.StatusAccepted {
|
||||
t.Fatalf("attendu 202, obtenu %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
var result VPC
|
||||
json.NewDecoder(w.Body).Decode(&result)
|
||||
if result.State != "deleting" {
|
||||
t.Errorf("state attendu deleting, obtenu %q", result.State)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteVpc_BlockedByActiveSubnet(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-busy/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-busy/state", "created")
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "created")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-busy")
|
||||
req := httptest.NewRequest(http.MethodDelete, "/vpcs/vpc-busy", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
|
@ -228,7 +201,7 @@ func TestDeleteVpc_BlockedByActiveSubnet(t *testing.T) {
|
|||
|
||||
func TestVpcByName_InvalidMethod(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
req := httptest.NewRequest(http.MethodPut, "/vpcs/vpc-1", nil)
|
||||
w := httptest.NewRecorder()
|
||||
s.VpcByNameHandler(w, req)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import (
|
|||
"time"
|
||||
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"git.g3e.fr/syonad/two/internal/subnet"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
|
|
@ -35,18 +34,18 @@ func (c CreateSubnetCommand) Prepare(db *badger.DB, cfg *configuration.Config) e
|
|||
if _, err := kv.GetFromDB(db, "subnet/"+c.Name+"/state"); err == nil {
|
||||
return fmt.Errorf("subnet %q already exists", c.Name)
|
||||
}
|
||||
vpcState, err := state.Get(db, "vpc/"+c.VPC)
|
||||
vpcState, err := kv.GetFromDB(db, "vpc/"+c.VPC+"/state")
|
||||
if err != nil {
|
||||
return fmt.Errorf("vpc %q not found", c.VPC)
|
||||
}
|
||||
if vpcState != state.Creating && vpcState != state.Running {
|
||||
if vpcState == "deleting" || vpcState == "deleted" {
|
||||
return fmt.Errorf("vpc %q is %s", c.VPC, vpcState)
|
||||
}
|
||||
localIface, ok := cfg.Interfaces[c.IfaceType]
|
||||
if !ok {
|
||||
localIface = cfg.DefaultInterface
|
||||
}
|
||||
state.Set(db, c.Key(), state.Creating)
|
||||
kv.AddInDB(db, "subnet/"+c.Name+"/state", "creating")
|
||||
kv.AddInDB(db, "subnet/"+c.Name+"/vpc", c.VPC)
|
||||
kv.AddInDB(db, "subnet/"+c.Name+"/mode", c.Mode)
|
||||
kv.AddInDB(db, "subnet/"+c.Name+"/local_iface", localIface)
|
||||
|
|
@ -62,19 +61,16 @@ func (c CreateSubnetCommand) Prepare(db *badger.DB, cfg *configuration.Config) e
|
|||
func (c CreateSubnetCommand) Execute(db *badger.DB, cfg *configuration.Config) error {
|
||||
timeout := time.After(time.Duration(cfg.Dispatcher.TimeoutSeconds) * time.Second)
|
||||
for {
|
||||
vpcState, err := state.Get(db, "vpc/"+c.VPC)
|
||||
state, err := kv.GetFromDB(db, "vpc/"+c.VPC+"/state")
|
||||
if err != nil {
|
||||
return fmt.Errorf("vpc %q not found while waiting", c.VPC)
|
||||
}
|
||||
if vpcState == state.Running {
|
||||
if state == "created" {
|
||||
break
|
||||
}
|
||||
if vpcState != state.Creating {
|
||||
return fmt.Errorf("vpc %q is %s, cannot create subnet %q", c.VPC, vpcState, c.Name)
|
||||
}
|
||||
select {
|
||||
case <-timeout:
|
||||
return fmt.Errorf("timed out waiting for vpc %q to be running", c.VPC)
|
||||
return fmt.Errorf("timed out waiting for vpc %q to be created", c.VPC)
|
||||
case <-time.After(time.Duration(cfg.Dispatcher.PollSeconds) * time.Second):
|
||||
}
|
||||
}
|
||||
|
|
@ -88,25 +84,21 @@ type DeleteSubnetCommand struct {
|
|||
func (c DeleteSubnetCommand) Key() string { return "subnet/" + c.Name }
|
||||
|
||||
func (c DeleteSubnetCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
|
||||
current, err := state.Get(db, c.Key())
|
||||
if err != nil {
|
||||
if _, err := kv.GetFromDB(db, "subnet/"+c.Name+"/state"); err != nil {
|
||||
return fmt.Errorf("subnet %q not found", c.Name)
|
||||
}
|
||||
if !state.CanDelete(current) {
|
||||
return fmt.Errorf("subnet %q cannot be deleted while %s", c.Name, current)
|
||||
}
|
||||
return state.Set(db, c.Key(), state.Deleting)
|
||||
return kv.AddInDB(db, "subnet/"+c.Name+"/state", "deleting")
|
||||
}
|
||||
|
||||
func (c DeleteSubnetCommand) Execute(db *badger.DB, _ *configuration.Config) error {
|
||||
if err := subnet.DeleteSubnet(db, c.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
current, err := state.Get(db, c.Key())
|
||||
state, err := kv.GetFromDB(db, "subnet/"+c.Name+"/state")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if current == state.Deleted {
|
||||
if state == "deleted" {
|
||||
kv.DeleteInDB(db, "subnet/"+c.Name)
|
||||
}
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ func testCfg() *configuration.Config {
|
|||
|
||||
func TestCreateSubnetCommand_Prepare_Success(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
cmd := CreateSubnetCommand{
|
||||
Name: "sn-1", VPC: "vpc-1", VxlanID: 100,
|
||||
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
|
||||
|
|
@ -37,7 +37,7 @@ func TestCreateSubnetCommand_Prepare_Success(t *testing.T) {
|
|||
|
||||
func TestCreateSubnetCommand_Prepare_UsesIfaceTypeMapping(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
cmd := CreateSubnetCommand{
|
||||
Name: "sn-1", VPC: "vpc-1", VxlanID: 100,
|
||||
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
|
||||
|
|
@ -51,7 +51,7 @@ func TestCreateSubnetCommand_Prepare_UsesIfaceTypeMapping(t *testing.T) {
|
|||
|
||||
func TestCreateSubnetCommand_Prepare_UsesDefaultIfaceWhenTypeUnknown(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
cmd := CreateSubnetCommand{
|
||||
Name: "sn-1", VPC: "vpc-1", VxlanID: 100,
|
||||
IfaceType: "inconnu", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
|
||||
|
|
@ -65,8 +65,8 @@ func TestCreateSubnetCommand_Prepare_UsesDefaultIfaceWhenTypeUnknown(t *testing.
|
|||
|
||||
func TestCreateSubnetCommand_Prepare_Duplicate(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-exist/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
kv.AddInDB(db, "subnet/sn-exist/state", "created")
|
||||
cmd := CreateSubnetCommand{
|
||||
Name: "sn-exist", VPC: "vpc-1", VxlanID: 100,
|
||||
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
|
||||
|
|
@ -113,7 +113,7 @@ func TestCreateSubnetCommand_Prepare_VPCDeleted(t *testing.T) {
|
|||
|
||||
func TestCreateSubnetCommand_Prepare_DefaultsToVxlanMode(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
cmd := CreateSubnetCommand{
|
||||
Name: "sn-1", VPC: "vpc-1", VxlanID: 100,
|
||||
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
|
||||
|
|
@ -130,7 +130,7 @@ func TestCreateSubnetCommand_Prepare_DefaultsToVxlanMode(t *testing.T) {
|
|||
|
||||
func TestCreateSubnetCommand_Prepare_BridgeMode_Success(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
cmd := CreateSubnetCommand{
|
||||
Name: "sn-1", VPC: "vpc-1", Mode: "bridge",
|
||||
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
|
||||
|
|
@ -150,7 +150,7 @@ func TestCreateSubnetCommand_Prepare_BridgeMode_Success(t *testing.T) {
|
|||
|
||||
func TestCreateSubnetCommand_Prepare_BridgeMode_NoVxlanID(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
cmd := CreateSubnetCommand{
|
||||
Name: "sn-1", VPC: "vpc-1", Mode: "bridge",
|
||||
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
|
||||
|
|
@ -163,7 +163,7 @@ func TestCreateSubnetCommand_Prepare_BridgeMode_NoVxlanID(t *testing.T) {
|
|||
|
||||
func TestCreateSubnetCommand_Prepare_UnknownMode(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
cmd := CreateSubnetCommand{
|
||||
Name: "sn-1", VPC: "vpc-1", Mode: "vlan",
|
||||
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
|
||||
|
|
@ -175,7 +175,7 @@ func TestCreateSubnetCommand_Prepare_UnknownMode(t *testing.T) {
|
|||
|
||||
func TestCreateSubnetCommand_Prepare_DefaultRouteStored(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
cmd := CreateSubnetCommand{
|
||||
Name: "sn-1", VPC: "vpc-1", VxlanID: 100,
|
||||
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
|
||||
|
|
@ -195,7 +195,7 @@ func TestCreateSubnetCommand_Prepare_DefaultRouteStored(t *testing.T) {
|
|||
|
||||
func TestCreateSubnetCommand_Prepare_DefaultRouteFalseByDefault(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "created")
|
||||
cmd := CreateSubnetCommand{
|
||||
Name: "sn-1", VPC: "vpc-1", VxlanID: 100,
|
||||
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
|
||||
|
|
@ -211,7 +211,7 @@ func TestCreateSubnetCommand_Prepare_DefaultRouteFalseByDefault(t *testing.T) {
|
|||
|
||||
func TestDeleteSubnetCommand_Prepare_Success(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "subnet/sn-del/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-del/state", "created")
|
||||
cmd := DeleteSubnetCommand{Name: "sn-del"}
|
||||
if err := cmd.Prepare(db, nil); err != nil {
|
||||
t.Fatalf("Prepare a échoué : %v", err)
|
||||
|
|
@ -222,32 +222,6 @@ func TestDeleteSubnetCommand_Prepare_Success(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestDeleteSubnetCommand_Prepare_RefusedWhileCreating(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "subnet/sn-wip/state", "creating")
|
||||
cmd := DeleteSubnetCommand{Name: "sn-wip"}
|
||||
if err := cmd.Prepare(db, nil); err == nil {
|
||||
t.Error("Prepare devrait refuser la suppression d'un subnet en creating")
|
||||
}
|
||||
s, _ := kv.GetFromDB(db, "subnet/sn-wip/state")
|
||||
if s != "creating" {
|
||||
t.Errorf("l'état ne devrait pas changer, obtenu %q", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteSubnetCommand_Prepare_AllowedFromError(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "subnet/sn-ko/state", "error")
|
||||
cmd := DeleteSubnetCommand{Name: "sn-ko"}
|
||||
if err := cmd.Prepare(db, nil); err != nil {
|
||||
t.Fatalf("Prepare devrait accepter un subnet en error : %v", err)
|
||||
}
|
||||
s, _ := kv.GetFromDB(db, "subnet/sn-ko/state")
|
||||
if s != "deleting" {
|
||||
t.Errorf("state attendu deleting, obtenu %q", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteSubnetCommand_Prepare_NotFound(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
cmd := DeleteSubnetCommand{Name: "sn-inexistant"}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ import (
|
|||
"time"
|
||||
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"git.g3e.fr/syonad/two/internal/vm"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
|
|
@ -37,18 +36,18 @@ func (c StartVMCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
|
|||
if _, err := kv.GetFromDB(db, "vm/"+c.Name+"/state"); err == nil {
|
||||
return fmt.Errorf("vm %q already exists", c.Name)
|
||||
}
|
||||
subnetState, err := state.Get(db, "subnet/"+c.Subnet)
|
||||
subnetState, err := kv.GetFromDB(db, "subnet/"+c.Subnet+"/state")
|
||||
if err != nil {
|
||||
return fmt.Errorf("subnet %q not found", c.Subnet)
|
||||
}
|
||||
if subnetState != state.Creating && subnetState != state.Running {
|
||||
if subnetState == "deleting" || subnetState == "deleted" {
|
||||
return fmt.Errorf("subnet %q is %s", c.Subnet, subnetState)
|
||||
}
|
||||
port, err := allocateMetadataPort(db)
|
||||
if err != nil {
|
||||
return fmt.Errorf("allocate metadata port: %w", err)
|
||||
}
|
||||
state.Set(db, c.Key(), state.Creating)
|
||||
kv.AddInDB(db, "vm/"+c.Name+"/state", "starting")
|
||||
kv.AddInDB(db, "vm/"+c.Name+"/subnet", c.Subnet)
|
||||
kv.AddInDB(db, "vm/"+c.Name+"/ip", c.IP)
|
||||
kv.AddInDB(db, "vm/"+c.Name+"/metadata_port", strconv.Itoa(port))
|
||||
|
|
@ -94,19 +93,16 @@ func allocateMetadataPort(db *badger.DB) (int, error) {
|
|||
func (c StartVMCommand) Execute(db *badger.DB, cfg *configuration.Config) error {
|
||||
timeout := time.After(time.Duration(cfg.Dispatcher.TimeoutSeconds) * time.Second)
|
||||
for {
|
||||
subnetState, err := state.Get(db, "subnet/"+c.Subnet)
|
||||
state, err := kv.GetFromDB(db, "subnet/"+c.Subnet+"/state")
|
||||
if err != nil {
|
||||
return fmt.Errorf("subnet %q not found while waiting", c.Subnet)
|
||||
}
|
||||
if subnetState == state.Running {
|
||||
if state == "created" {
|
||||
break
|
||||
}
|
||||
if subnetState != state.Creating {
|
||||
return fmt.Errorf("subnet %q is %s, cannot start vm %q", c.Subnet, subnetState, c.Name)
|
||||
}
|
||||
select {
|
||||
case <-timeout:
|
||||
return fmt.Errorf("timed out waiting for subnet %q to be running", c.Subnet)
|
||||
return fmt.Errorf("timed out waiting for subnet %q to be created", c.Subnet)
|
||||
case <-time.After(time.Duration(cfg.Dispatcher.PollSeconds) * time.Second):
|
||||
}
|
||||
}
|
||||
|
|
@ -120,25 +116,21 @@ type StopVMCommand struct {
|
|||
func (c StopVMCommand) Key() string { return "vm/" + c.Name }
|
||||
|
||||
func (c StopVMCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
|
||||
current, err := state.Get(db, c.Key())
|
||||
if err != nil {
|
||||
if _, err := kv.GetFromDB(db, "vm/"+c.Name+"/state"); err != nil {
|
||||
return fmt.Errorf("vm %q not found", c.Name)
|
||||
}
|
||||
if !state.CanDelete(current) {
|
||||
return fmt.Errorf("vm %q cannot be stopped while %s", c.Name, current)
|
||||
}
|
||||
return state.Set(db, c.Key(), state.Deleting)
|
||||
return kv.AddInDB(db, "vm/"+c.Name+"/state", "stopping")
|
||||
}
|
||||
|
||||
func (c StopVMCommand) Execute(db *badger.DB, cfg *configuration.Config) error {
|
||||
if err := vm.StopVM(db, c.Name, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
current, err := state.Get(db, c.Key())
|
||||
state, err := kv.GetFromDB(db, "vm/"+c.Name+"/state")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if current == state.Deleted {
|
||||
if state == "stopped" {
|
||||
kv.DeleteInDB(db, "vm/"+c.Name)
|
||||
}
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import (
|
|||
|
||||
func TestStartVMCommand_Prepare_SingleDisk(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "created")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||
|
||||
cmd := StartVMCommand{
|
||||
|
|
@ -34,7 +34,7 @@ func TestStartVMCommand_Prepare_SingleDisk(t *testing.T) {
|
|||
|
||||
func TestStartVMCommand_Prepare_MultiDisk(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "created")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||
|
||||
cmd := StartVMCommand{
|
||||
|
|
@ -66,7 +66,7 @@ func TestStartVMCommand_Prepare_MultiDisk(t *testing.T) {
|
|||
|
||||
func TestStartVMCommand_Prepare_SlotGap(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "created")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||
|
||||
// sdb absent au boot — slot réservé pour hotplug
|
||||
|
|
@ -96,7 +96,7 @@ func TestStartVMCommand_Prepare_SlotGap(t *testing.T) {
|
|||
|
||||
func TestStartVMCommand_Prepare_NoVolumePath(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "created")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||
|
||||
cmd := StartVMCommand{
|
||||
|
|
@ -114,54 +114,9 @@ func TestStartVMCommand_Prepare_NoVolumePath(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// --- StopVMCommand.Prepare ---
|
||||
|
||||
func TestStopVMCommand_Prepare_Success(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vm/vm-run/state", "running")
|
||||
cmd := StopVMCommand{Name: "vm-run"}
|
||||
if err := cmd.Prepare(db, nil); err != nil {
|
||||
t.Fatalf("Prepare a échoué : %v", err)
|
||||
}
|
||||
s, _ := kv.GetFromDB(db, "vm/vm-run/state")
|
||||
if s != "deleting" {
|
||||
t.Errorf("state attendu deleting, obtenu %q", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStopVMCommand_Prepare_RefusedWhileCreating(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vm/vm-wip/state", "creating")
|
||||
cmd := StopVMCommand{Name: "vm-wip"}
|
||||
if err := cmd.Prepare(db, nil); err == nil {
|
||||
t.Error("Prepare devrait refuser l'arrêt d'une VM en creating")
|
||||
}
|
||||
s, _ := kv.GetFromDB(db, "vm/vm-wip/state")
|
||||
if s != "creating" {
|
||||
t.Errorf("l'état ne devrait pas changer, obtenu %q", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStopVMCommand_Prepare_AllowedFromError(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vm/vm-ko/state", "error")
|
||||
cmd := StopVMCommand{Name: "vm-ko"}
|
||||
if err := cmd.Prepare(db, nil); err != nil {
|
||||
t.Fatalf("Prepare devrait accepter une VM en error : %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStopVMCommand_Prepare_NotFound(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
cmd := StopVMCommand{Name: "vm-inexistante"}
|
||||
if err := cmd.Prepare(db, nil); err == nil {
|
||||
t.Error("Prepare devrait échouer si la VM n'existe pas")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartVMCommand_Prepare_Duplicate(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vm/vm-exist/state", "running")
|
||||
kv.AddInDB(db, "vm/vm-exist/state", "started")
|
||||
|
||||
cmd := StartVMCommand{
|
||||
Name: "vm-exist",
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import (
|
|||
"time"
|
||||
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"git.g3e.fr/syonad/two/internal/vpc"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
|
|
@ -30,7 +29,7 @@ func (c CreateVPCCommand) Prepare(db *badger.DB, _ *configuration.Config) error
|
|||
if err := kv.AddInDB(db, "vpc/"+c.Name+"/cidr", c.CIDR); err != nil {
|
||||
return err
|
||||
}
|
||||
return state.Set(db, c.Key(), state.Creating)
|
||||
return kv.AddInDB(db, "vpc/"+c.Name+"/state", "creating")
|
||||
}
|
||||
|
||||
func (c CreateVPCCommand) Execute(db *badger.DB, _ *configuration.Config) error {
|
||||
|
|
@ -44,13 +43,9 @@ type DeleteVPCCommand struct {
|
|||
func (c DeleteVPCCommand) Key() string { return "vpc/" + c.Name }
|
||||
|
||||
func (c DeleteVPCCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
|
||||
current, err := state.Get(db, c.Key())
|
||||
if err != nil {
|
||||
if _, err := kv.GetFromDB(db, "vpc/"+c.Name+"/state"); err != nil {
|
||||
return fmt.Errorf("vpc %q not found", c.Name)
|
||||
}
|
||||
if !state.CanDelete(current) {
|
||||
return fmt.Errorf("vpc %q cannot be deleted while %s", c.Name, current)
|
||||
}
|
||||
entries, err := kv.ListByPrefix(db, "subnet/")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to list subnets: %w", err)
|
||||
|
|
@ -60,12 +55,12 @@ func (c DeleteVPCCommand) Prepare(db *badger.DB, _ *configuration.Config) error
|
|||
continue
|
||||
}
|
||||
subnetName := strings.Split(key, "/")[1]
|
||||
s, err := state.Get(db, "subnet/"+subnetName)
|
||||
if err != nil || (s != state.Deleting && s != state.Deleted) {
|
||||
state, err := kv.GetFromDB(db, "subnet/"+subnetName+"/state")
|
||||
if err != nil || (state != "deleting" && state != "deleted") {
|
||||
return fmt.Errorf("subnet %q must be deleted before deleting vpc %q", subnetName, c.Name)
|
||||
}
|
||||
}
|
||||
return state.Set(db, c.Key(), state.Deleting)
|
||||
return kv.AddInDB(db, "vpc/"+c.Name+"/state", "deleting")
|
||||
}
|
||||
|
||||
func (c DeleteVPCCommand) Execute(db *badger.DB, cfg *configuration.Config) error {
|
||||
|
|
@ -79,8 +74,8 @@ func (c DeleteVPCCommand) Execute(db *badger.DB, cfg *configuration.Config) erro
|
|||
for key, value := range entries {
|
||||
if strings.HasSuffix(key, "/vpc") && value == c.Name {
|
||||
subnetName := strings.Split(key, "/")[1]
|
||||
s, _ := state.Get(db, "subnet/"+subnetName)
|
||||
if s == state.Deleting {
|
||||
state, _ := kv.GetFromDB(db, "subnet/"+subnetName+"/state")
|
||||
if state == "deleting" {
|
||||
pending = true
|
||||
break
|
||||
}
|
||||
|
|
@ -98,11 +93,11 @@ func (c DeleteVPCCommand) Execute(db *badger.DB, cfg *configuration.Config) erro
|
|||
if err := vpc.DeleteVPC(db, c.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
current, err := state.Get(db, c.Key())
|
||||
state, err := kv.GetFromDB(db, "vpc/"+c.Name+"/state")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if current == state.Deleted {
|
||||
if state == "deleted" {
|
||||
kv.DeleteInDB(db, "vpc/"+c.Name)
|
||||
}
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ func TestCreateVPCCommand_Prepare_NewVPC(t *testing.T) {
|
|||
|
||||
func TestCreateVPCCommand_Prepare_Duplicate(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-exist/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-exist/state", "created")
|
||||
cmd := CreateVPCCommand{Name: "vpc-exist", CIDR: "10.0.0.0/16"}
|
||||
if err := cmd.Prepare(db, nil); err == nil {
|
||||
t.Error("Prepare devrait échouer sur un VPC déjà existant")
|
||||
|
|
@ -51,7 +51,7 @@ func TestCreateVPCCommand_Prepare_InvalidCIDR(t *testing.T) {
|
|||
|
||||
func TestDeleteVPCCommand_Prepare_Success(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-del/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-del/state", "created")
|
||||
cmd := DeleteVPCCommand{Name: "vpc-del"}
|
||||
if err := cmd.Prepare(db, nil); err != nil {
|
||||
t.Fatalf("Prepare a échoué : %v", err)
|
||||
|
|
@ -70,45 +70,10 @@ func TestDeleteVPCCommand_Prepare_NotFound(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestDeleteVPCCommand_Prepare_RefusedWhileCreating(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-wip/state", "creating")
|
||||
cmd := DeleteVPCCommand{Name: "vpc-wip"}
|
||||
if err := cmd.Prepare(db, nil); err == nil {
|
||||
t.Error("Prepare devrait refuser la suppression d'un VPC en creating")
|
||||
}
|
||||
s, _ := kv.GetFromDB(db, "vpc/vpc-wip/state")
|
||||
if s != "creating" {
|
||||
t.Errorf("l'état ne devrait pas changer, obtenu %q", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteVPCCommand_Prepare_RefusedWhileDeleting(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-gone/state", "deleting")
|
||||
cmd := DeleteVPCCommand{Name: "vpc-gone"}
|
||||
if err := cmd.Prepare(db, nil); err == nil {
|
||||
t.Error("Prepare devrait refuser un VPC déjà en deleting")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteVPCCommand_Prepare_AllowedFromError(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-ko/state", "error")
|
||||
cmd := DeleteVPCCommand{Name: "vpc-ko"}
|
||||
if err := cmd.Prepare(db, nil); err != nil {
|
||||
t.Fatalf("Prepare devrait accepter un VPC en error : %v", err)
|
||||
}
|
||||
s, _ := kv.GetFromDB(db, "vpc/vpc-ko/state")
|
||||
if s != "deleting" {
|
||||
t.Errorf("state attendu deleting, obtenu %q", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteVPCCommand_Prepare_BlockedByActiveSubnet(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-busy/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-busy/state", "created")
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "created")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-busy")
|
||||
cmd := DeleteVPCCommand{Name: "vpc-busy"}
|
||||
if err := cmd.Prepare(db, nil); err == nil {
|
||||
|
|
@ -118,7 +83,7 @@ func TestDeleteVPCCommand_Prepare_BlockedByActiveSubnet(t *testing.T) {
|
|||
|
||||
func TestDeleteVPCCommand_Prepare_AllowedWhenSubnetDeleted(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-ok/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-ok/state", "created")
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "deleted")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-ok")
|
||||
cmd := DeleteVPCCommand{Name: "vpc-ok"}
|
||||
|
|
@ -129,7 +94,7 @@ func TestDeleteVPCCommand_Prepare_AllowedWhenSubnetDeleted(t *testing.T) {
|
|||
|
||||
func TestDeleteVPCCommand_Prepare_AllowedWhenSubnetDeleting(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-ok/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-ok/state", "created")
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "deleting")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-ok")
|
||||
cmd := DeleteVPCCommand{Name: "vpc-ok"}
|
||||
|
|
|
|||
|
|
@ -7,18 +7,18 @@ import (
|
|||
"git.g3e.fr/syonad/two/internal/ebtables"
|
||||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
"git.g3e.fr/syonad/two/pkg/systemd"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
func CreateSubnet(db *badger.DB, subnetName string) error {
|
||||
current, err := state.Get(db, "subnet/"+subnetName)
|
||||
state, err := kv.GetFromDB(db, "subnet/"+subnetName+"/state")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if current != state.Creating {
|
||||
if state != "creating" {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -31,7 +31,7 @@ func CreateSubnet(db *badger.DB, subnetName string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
return state.Set(db, "subnet/"+subnetName, state.Running)
|
||||
return kv.AddInDB(db, "subnet/"+subnetName+"/state", "created")
|
||||
}
|
||||
|
||||
func createSubnet(db *badger.DB, subnetName string, d subnetData) error {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import (
|
|||
"git.g3e.fr/syonad/two/internal/ebtables"
|
||||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
"git.g3e.fr/syonad/two/pkg/systemd"
|
||||
|
||||
|
|
@ -15,11 +14,11 @@ import (
|
|||
)
|
||||
|
||||
func DeleteSubnet(db *badger.DB, subnetName string) error {
|
||||
current, err := state.Get(db, "subnet/"+subnetName)
|
||||
state, err := kv.GetFromDB(db, "subnet/"+subnetName+"/state")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if current != state.Deleting {
|
||||
if state != "deleting" {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -45,7 +44,7 @@ func DeleteSubnet(db *badger.DB, subnetName string) error {
|
|||
return fmt.Errorf("unknown subnet mode %q", d.mode)
|
||||
}
|
||||
|
||||
return state.Set(db, "subnet/"+subnetName, state.Deleted)
|
||||
return kv.AddInDB(db, "subnet/"+subnetName+"/state", "deleted")
|
||||
}
|
||||
|
||||
func stopDHCP(db *badger.DB, subnetName string, d subnetData) error {
|
||||
|
|
|
|||
|
|
@ -12,17 +12,17 @@ import (
|
|||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/internal/qemu"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
func StartVM(db *badger.DB, name string, cfg *configuration.Config) error {
|
||||
current, err := state.Get(db, "vm/"+name)
|
||||
state, err := kv.GetFromDB(db, "vm/"+name+"/state")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if current != state.Creating {
|
||||
if state != "starting" {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -84,7 +84,7 @@ func StartVM(db *badger.DB, name string, cfg *configuration.Config) error {
|
|||
return fmt.Errorf("start qemu: %w", err)
|
||||
}
|
||||
|
||||
return state.Set(db, "vm/"+name, state.Running)
|
||||
return kv.AddInDB(db, "vm/"+name+"/state", "started")
|
||||
}
|
||||
|
||||
func copyFile(src, dst string) error {
|
||||
|
|
|
|||
|
|
@ -12,17 +12,17 @@ import (
|
|||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/internal/qmp"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
func StopVM(db *badger.DB, name string, cfg *configuration.Config) error {
|
||||
current, err := state.Get(db, "vm/"+name)
|
||||
state, err := kv.GetFromDB(db, "vm/"+name+"/state")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if current != state.Deleting {
|
||||
if state != "stopping" {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -65,7 +65,7 @@ func StopVM(db *badger.DB, name string, cfg *configuration.Config) error {
|
|||
os.Remove(varsPath)
|
||||
}
|
||||
|
||||
return state.Set(db, "vm/"+name, state.Deleted)
|
||||
return kv.AddInDB(db, "vm/"+name+"/state", "stopped")
|
||||
}
|
||||
|
||||
func waitQMPDead(socketPath string, timeout, poll time.Duration) {
|
||||
|
|
|
|||
|
|
@ -5,15 +5,15 @@ import (
|
|||
|
||||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
func CreateVPC(db *badger.DB, name string) error {
|
||||
if current, err := state.Get(db, "vpc/"+name); err != nil {
|
||||
if state, err := kv.GetFromDB(db, "vpc/"+name+"/state"); err != nil {
|
||||
return err
|
||||
} else if current == state.Creating {
|
||||
} else if state == "creating" {
|
||||
vpcID := strings.SplitN(name, "-", 2)[1]
|
||||
|
||||
if err := netns.Create(name); err != nil {
|
||||
|
|
@ -48,7 +48,7 @@ func CreateVPC(db *badger.DB, name string) error {
|
|||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return state.Set(db, "vpc/"+name, state.Running)
|
||||
kv.AddInDB(db, "vpc/"+name+"/state", "created")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,15 +5,15 @@ import (
|
|||
|
||||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
func DeleteVPC(db *badger.DB, name string) error {
|
||||
if current, err := state.Get(db, "vpc/"+name); err != nil {
|
||||
if state, err := kv.GetFromDB(db, "vpc/"+name+"/state"); err != nil {
|
||||
return err
|
||||
} else if current == state.Deleting {
|
||||
} else if state == "deleting" {
|
||||
vpcID := strings.SplitN(name, "-", 2)[1]
|
||||
|
||||
if err := netif.DeleteLink("vp-" + vpcID + "-e"); err != nil {
|
||||
|
|
@ -23,7 +23,7 @@ func DeleteVPC(db *badger.DB, name string) error {
|
|||
if err := netns.Delete(name); err != nil {
|
||||
return err
|
||||
}
|
||||
return state.Set(db, "vpc/"+name, state.Deleted)
|
||||
kv.AddInDB(db, "vpc/"+name+"/state", "deleted")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue