Storage has now it's own package.

Remove global `sessions` map and use configured storage.
Add a `types` package so both `pwd` and `storage` can access without
circular dependencies.
Now the session is prepared when requested and not on load.
This commit is contained in:
Jonathan Leibiusky @xetorthio
2017-06-14 18:35:21 -03:00
parent 2eff799c58
commit e9911abf94
22 changed files with 814 additions and 499 deletions

12
pwd/types/client.go Normal file
View File

@@ -0,0 +1,12 @@
package types
type Client struct {
Id string
ViewPort ViewPort
Session *Session
}
type ViewPort struct {
Rows uint
Cols uint
}

63
pwd/types/instance.go Normal file
View File

@@ -0,0 +1,63 @@
package types
import (
"context"
"net"
"sync"
"github.com/play-with-docker/play-with-docker/docker"
)
type UInt16Slice []uint16
func (p UInt16Slice) Len() int { return len(p) }
func (p UInt16Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p UInt16Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type Instance struct {
Image string `json:"image"`
Name string `json:"name"`
Hostname string `json:"hostname"`
IP string `json:"ip"`
IsManager *bool `json:"is_manager"`
Mem string `json:"mem"`
Cpu string `json:"cpu"`
Alias string `json:"alias"`
ServerCert []byte `json:"server_cert"`
ServerKey []byte `json:"server_key"`
CACert []byte `json:"ca_cert"`
Cert []byte `json:"cert"`
Key []byte `json:"key"`
IsDockerHost bool `json:"is_docker_host"`
Docker docker.DockerApi `json:"-"`
Session *Session `json:"-"`
Terminal net.Conn `json:"-"`
ctx context.Context `json:"-"`
tempPorts []uint16 `json:"-"`
Ports UInt16Slice
rw sync.Mutex
}
func (i *Instance) SetUsedPort(port uint16) {
i.rw.Lock()
defer i.rw.Unlock()
for _, p := range i.tempPorts {
if p == port {
return
}
}
i.tempPorts = append(i.tempPorts, port)
}
func (i *Instance) GetUsedPorts() []uint16 {
i.rw.Lock()
defer i.rw.Unlock()
return i.tempPorts
}
func (i *Instance) CleanUsedPorts() {
i.rw.Lock()
defer i.rw.Unlock()
i.tempPorts = []uint16{}
}

55
pwd/types/session.go Normal file
View File

@@ -0,0 +1,55 @@
package types
import (
"sync"
"time"
)
type Session struct {
Id string `json:"id"`
Instances map[string]*Instance `json:"instances"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
PwdIpAddress string `json:"pwd_ip_address"`
Ready bool `json:"ready"`
Stack string `json:"stack"`
StackName string `json:"stack_name"`
ImageName string `json:"image_name"`
Clients []*Client `json:"-"`
closingTimer *time.Timer `json:"-"`
scheduled bool `json:"-"`
ticker *time.Ticker `json:"-"`
rw sync.Mutex `json:"-"`
prepared bool `json:"-"`
}
func (s *Session) Lock() {
s.rw.Lock()
}
func (s *Session) Unlock() {
s.rw.Unlock()
}
func (s *Session) StopTicker() {
if s.ticker != nil {
s.ticker.Stop()
}
}
func (s *Session) SetTicker(t *time.Ticker) {
s.ticker = t
}
func (s *Session) SetClosingTimer(t *time.Timer) {
s.closingTimer = t
}
func (s *Session) ClosingTimer() *time.Timer {
return s.closingTimer
}
func (s *Session) IsPrepared() bool {
return s.prepared
}
func (s *Session) SetPrepared() {
s.prepared = true
}