X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=lib%2Fagent%2Fproject-pathmap.go;h=875384e09ae5a76bb8df9a3366f2fd924e2adf3a;hb=a2cc38902ff7528870822110c4f04329a3918564;hp=6c49d6a29b758491a2678cefdbeebb3e81f811ca;hpb=4695555e178bcabe54c5bf82117c9c4cef5440b5;p=src%2Fxds%2Fxds-agent.git diff --git a/lib/agent/project-pathmap.go b/lib/agent/project-pathmap.go index 6c49d6a..875384e 100644 --- a/lib/agent/project-pathmap.go +++ b/lib/agent/project-pathmap.go @@ -1,12 +1,40 @@ +/* + * Copyright (C) 2017-2018 "IoT.bzh" + * Author Sebastien Douheret + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package agent +import ( + "fmt" + "io/ioutil" + "os" + "strings" + + "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent/lib/xaapiv1" + common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib" + "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1" +) + // IPROJECT interface implementation for native/path mapping projects // PathMap . type PathMap struct { *Context server *XdsServer - folder *XdsFolderConfig + folder *xsapiv1.FolderConfig } // NewProjectPathMap Create a new instance of PathMap @@ -14,22 +42,65 @@ func NewProjectPathMap(ctx *Context, svr *XdsServer) *PathMap { p := PathMap{ Context: ctx, server: svr, - folder: &XdsFolderConfig{}, + folder: &xsapiv1.FolderConfig{}, } return &p } // Add a new project -func (p *PathMap) Add(cfg ProjectConfig) (*ProjectConfig, error) { +func (p *PathMap) Add(cfg xaapiv1.ProjectConfig) (*xaapiv1.ProjectConfig, error) { var err error + var file *os.File + errMsg := "ClientPath sanity check error (%d): %v" - // SEB TODO: check local/server directory access + // Sanity check to verify that we have RW permission and path-mapping is correct + dir := cfg.ClientPath + if !common.Exists(dir) { + // try to create if not existing + if err := os.MkdirAll(dir, 0755); err != nil { + return nil, fmt.Errorf("Cannot create ClientPath directory: %s", dir) + } + } + if !common.Exists(dir) { + return nil, fmt.Errorf("ClientPath directory is not accessible: %s", dir) + } + if file, err = ioutil.TempFile(dir, ".xds_pathmap_check"); err != nil { + return nil, fmt.Errorf(errMsg, 1, err) + } + // Write a specific message that will be check by server during folder add + msg := "Pathmap checked message written by xds-agent ID: " + p.Config.AgentUID + "\n" + if n, err := file.WriteString(msg); n != len(msg) || err != nil { + return nil, fmt.Errorf(errMsg, 2, err) + } + defer func() { + if file != nil { + os.Remove(file.Name()) + file.Close() + } + }() + + // Convert to Xds folder + fld := p.server.ProjectToFolder(cfg) + fld.DataPathMap.CheckFile = file.Name() + fld.DataPathMap.CheckContent = msg - err = p.server.FolderAdd(p.server.ProjectToFolder(cfg), p.folder) + // Send request to create folder on XDS server side + err = p.server.FolderAdd(fld, p.folder) if err != nil { return nil, err } + // 2nd part of sanity checker + // check specific message added by XDS Server during folder add processing + content, err := ioutil.ReadFile(file.Name()) + if err != nil { + return nil, fmt.Errorf(errMsg, 3, err) + } + if !strings.Contains(string(content), + "Pathmap checked message written by xds-server ID") { + return nil, fmt.Errorf(errMsg, 4, "file content differ") + } + return p.GetProject(), nil } @@ -39,22 +110,36 @@ func (p *PathMap) Delete() error { } // GetProject Get public part of project config -func (p *PathMap) GetProject() *ProjectConfig { +func (p *PathMap) GetProject() *xaapiv1.ProjectConfig { prj := p.server.FolderToProject(*p.folder) prj.ServerID = p.server.ID return &prj } -// UpdateProject Set project config -func (p *PathMap) UpdateProject(prj ProjectConfig) (*ProjectConfig, error) { +// Setup Setup local project config +func (p *PathMap) Setup(prj xaapiv1.ProjectConfig) (*xaapiv1.ProjectConfig, error) { p.folder = p.server.ProjectToFolder(prj) np := p.GetProject() - if err := p.events.Emit(EVTProjectChange, np); err != nil { + if err := p.events.Emit(xaapiv1.EVTProjectChange, np, ""); err != nil { return np, err } return np, nil } +// Update Update some field of a project +func (p *PathMap) Update(prj xaapiv1.ProjectConfig) (*xaapiv1.ProjectConfig, error) { + if p.folder.ID != prj.ID { + return nil, fmt.Errorf("Invalid id") + } + + err := p.server.FolderUpdate(p.server.ProjectToFolder(prj), p.folder) + if err != nil { + return nil, err + } + + return p.GetProject(), nil +} + // GetServer Get the XdsServer that holds this project func (p *PathMap) GetServer() *XdsServer { return p.server