2 * Copyright (C) 2017-2018 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
26 "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1"
27 common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
28 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1"
31 // IPROJECT interface implementation for native/path mapping projects
37 folder *xsapiv1.FolderConfig
40 // NewProjectPathMap Create a new instance of PathMap
41 func NewProjectPathMap(ctx *Context, svr *XdsServer) *PathMap {
45 folder: &xsapiv1.FolderConfig{},
51 func (p *PathMap) Add(cfg xaapiv1.ProjectConfig) (*xaapiv1.ProjectConfig, error) {
54 errMsg := "ClientPath sanity check error (%d): %v"
56 // Sanity check to verify that we have RW permission and path-mapping is correct
58 if !common.Exists(dir) {
59 // try to create if not existing
60 if err := os.MkdirAll(dir, 0755); err != nil {
61 return nil, fmt.Errorf("Cannot create ClientPath directory: %s", dir)
64 if !common.Exists(dir) {
65 return nil, fmt.Errorf("ClientPath directory is not accessible: %s", dir)
67 if file, err = ioutil.TempFile(dir, ".xds_pathmap_check"); err != nil {
68 return nil, fmt.Errorf(errMsg, 1, err)
70 // Write a specific message that will be check by server during folder add
71 msg := "Pathmap checked message written by xds-agent ID: " + p.Config.AgentUID + "\n"
72 if n, err := file.WriteString(msg); n != len(msg) || err != nil {
73 return nil, fmt.Errorf(errMsg, 2, err)
77 os.Remove(file.Name())
82 // Convert to Xds folder
83 fld := p.server.ProjectToFolder(cfg)
84 fld.DataPathMap.CheckFile = file.Name()
85 fld.DataPathMap.CheckContent = msg
87 // Send request to create folder on XDS server side
88 err = p.server.FolderAdd(fld, p.folder)
93 // 2nd part of sanity checker
94 // check specific message added by XDS Server during folder add processing
95 content, err := ioutil.ReadFile(file.Name())
97 return nil, fmt.Errorf(errMsg, 3, err)
99 if !strings.Contains(string(content),
100 "Pathmap checked message written by xds-server ID") {
101 return nil, fmt.Errorf(errMsg, 4, "file content differ")
104 return p.GetProject(), nil
108 func (p *PathMap) Delete() error {
109 return p.server.FolderDelete(p.folder.ID)
112 // GetProject Get public part of project config
113 func (p *PathMap) GetProject() *xaapiv1.ProjectConfig {
114 prj := p.server.FolderToProject(*p.folder)
115 prj.ServerID = p.server.ID
119 // Setup Setup local project config
120 func (p *PathMap) Setup(prj xaapiv1.ProjectConfig) (*xaapiv1.ProjectConfig, error) {
121 p.folder = p.server.ProjectToFolder(prj)
123 if err := p.events.Emit(xaapiv1.EVTProjectChange, np, ""); err != nil {
129 // Update Update some field of a project
130 func (p *PathMap) Update(prj xaapiv1.ProjectConfig) (*xaapiv1.ProjectConfig, error) {
131 if p.folder.ID != prj.ID {
132 return nil, fmt.Errorf("Invalid id")
135 err := p.server.FolderUpdate(p.server.ProjectToFolder(prj), p.folder)
140 return p.GetProject(), nil
143 // GetServer Get the XdsServer that holds this project
144 func (p *PathMap) GetServer() *XdsServer {
148 // Sync Force project files synchronization
149 func (p *PathMap) Sync() error {
153 // IsInSync Check if project files are in-sync
154 func (p *PathMap) IsInSync() (bool, error) {