Skip to content

Commit

Permalink
Merge pull request #269 from subutai-io/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dilshat authored Jan 18, 2018
2 parents 469e42f + 98c672c commit 331ec07
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
29 changes: 13 additions & 16 deletions cli/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
)

var (
lock lockfile.Lockfile
owners = []string{"subutai", "jenkins", "docker", ""}
)

Expand All @@ -47,7 +46,7 @@ type metainfo struct {
Version string `json:"version"`
File string `json:"filename"`
Signs map[string]string `json:"signature"`
Hash struct {
Hash struct {
Md5 string
Sha256 string
} `json:"hash"`
Expand Down Expand Up @@ -224,29 +223,24 @@ func idToName(id string, kurjun *http.Client, token string) string {
}

// lockSubutai creates lock file for period of import for certain template to prevent conflicts during write operation
func lockSubutai(file string) bool {
func lockSubutai(file string) (lockfile.Lockfile, error) {
lock, err := lockfile.New("/var/run/lock/subutai." + file)
if log.Check(log.DebugLevel, "Init lock "+file, err) {
return false
return lock, err
}

err = lock.TryLock()
if log.Check(log.DebugLevel, "Locking file "+file, err) {
if p, err := lock.GetOwner(); err == nil {
cmd, err := ioutil.ReadFile(fmt.Sprintf("/proc/%v/cmdline", p.Pid))
if err != nil || !(strings.Contains(string(cmd), "subutai") && strings.Contains(string(cmd), "import")) {
if p, err2 := lock.GetOwner(); err2 == nil {
cmd, err2 := ioutil.ReadFile(fmt.Sprintf("/proc/%v/cmdline", p.Pid))
if err2 != nil || !(strings.Contains(string(cmd), "subutai") && strings.Contains(string(cmd), "import")) {
log.Check(log.DebugLevel, "Removing broken lockfile /var/run/lock/subutai."+file, os.Remove("/var/run/lock/subutai."+file))
}
}
return false
return lock, err
}

return true
}

// unlockSubutai removes lock file
func unlockSubutai() {
lock.Unlock()
return lock, nil
}

// LxcImport function deploys a Subutai template on a Resource Host. The import algorithm works with both the global template repository and a local directory
Expand Down Expand Up @@ -289,10 +283,13 @@ func LxcImport(name, version, token string, torrent bool, auxDepList ...string)
}

log.Info("Importing " + name)
for !lockSubutai(t.name + ".import") {

var lock lockfile.Lockfile
var err error
for lock, err = lockSubutai(t.name + ".import"); err != nil; lock, err = lockSubutai(t.name + ".import") {
time.Sleep(time.Second * 1)
}
defer unlockSubutai()
defer lock.Unlock()

if container.IsContainer(t.name) {
log.Info(t.name + " instance exist")
Expand Down
12 changes: 11 additions & 1 deletion cli/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/subutai-io/agent/lib/gpg"
ovs "github.com/subutai-io/agent/lib/net"
"github.com/subutai-io/agent/log"
"github.com/nightlyone/lockfile"
)

// MapPort exposes internal container ports to sockExt RH interface. It supports udp, tcp, http(s) protocols and other reverse proxy features
Expand Down Expand Up @@ -51,6 +52,15 @@ func MapPort(protocol, sockInt, sockExt, policy, domain, cert string, list, remo
sockInt != "10.10.10.1:"+strings.Split(sockExt, ":")[1]:
log.Error("Reserved system ports")
case len(sockInt) != 0:

var mapping = protocol + domain + sockInt + sockExt
var lock lockfile.Lockfile
var err error
for lock, err = lockSubutai(mapping + ".map"); err != nil; lock, err = lockSubutai(mapping + ".map") {
time.Sleep(time.Second * 1)
}
defer lock.Unlock()

// check sockExt port and create nginx config
if portIsNew(protocol, sockInt, domain, &sockExt) {
newConfig(protocol, sockExt, domain, cert, sslbcknd)
Expand Down Expand Up @@ -162,7 +172,7 @@ func portIsNew(protocol, sockInt, domain string, sockExt *string) bool {
if !bolt.PortInMap(protocol, (*sockExt), "", "") && socket[1] != "80" {
log.Error("Port is busy")
} else if bolt.PortInMap(protocol, (*sockExt), domain, sockInt) {
log.Error("Map is already exists")
log.Error("Mapping already exists")
}
return !bolt.PortInMap(protocol, (*sockExt), domain, "")
}
Expand Down
5 changes: 3 additions & 2 deletions cli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import (
// if a newer version is found, installs it. Please note, system security policies requires that such commands should be performed by the superuser manually,
// otherwise an application's attempt to update itself will be blocked.
func Update(name string, check bool) {
if !lockSubutai(name + ".update") {
lock, err := lockSubutai(name + ".update")
if err != nil {
log.Error("Another update process is already running")
}
defer unlockSubutai()
defer lock.Unlock()

if name == "rh" {
updateRH(name, check)
Expand Down

0 comments on commit 331ec07

Please sign in to comment.