1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16package res 17 18import ( 19 "fmt" 20 "fotff/utils" 21 "strings" 22) 23 24type Resources struct { 25 DeviceSnList string `key:"device_sn_list"` 26 AddrList string `key:"build_server_addr_list" default:"127.0.0.1:22"` 27 User string `key:"build_server_user" default:"root"` 28 Passwd string `key:"build_server_password" default:"root"` 29 // BuildWorkSpace must be absolute 30 BuildWorkSpace string `key:"build_server_workspace" default:"/root/fotff/build_workspace"` 31 devicePool chan string 32 serverPool chan string 33} 34 35type BuildServerInfo struct { 36 Addr string 37 User string 38 Passwd string 39 WorkSpace string 40} 41 42var res Resources 43 44func init() { 45 utils.ParseFromConfigFile("resources", &res) 46 snList := strings.Split(res.DeviceSnList, ",") 47 addrList := strings.Split(res.AddrList, ",") 48 res.devicePool = make(chan string, len(snList)) 49 for _, sn := range snList { 50 res.devicePool <- sn 51 } 52 res.serverPool = make(chan string, len(addrList)) 53 for _, addr := range addrList { 54 res.serverPool <- addr 55 } 56} 57 58// Fake set 'n' fake packages and build servers. 59// Just for test only. 60func Fake(n int) { 61 var snList, addrList []string 62 for i := 0; i < n; i++ { 63 snList = append(snList, fmt.Sprintf("device%d", i)) 64 addrList = append(addrList, fmt.Sprintf("server%d", i)) 65 } 66 res.devicePool = make(chan string, len(snList)) 67 for _, sn := range snList { 68 res.devicePool <- sn 69 } 70 res.serverPool = make(chan string, len(addrList)) 71 for _, sn := range snList { 72 res.serverPool <- sn 73 } 74} 75 76func Num() int { 77 if cap(res.devicePool) < cap(res.serverPool) { 78 return cap(res.devicePool) 79 } 80 return cap(res.serverPool) 81} 82 83func DeviceList() []string { 84 return strings.Split(res.DeviceSnList, ",") 85} 86 87func GetDevice() string { 88 return <-res.devicePool 89} 90 91func ReleaseDevice(device string) { 92 res.devicePool <- device 93} 94 95func GetBuildServer() BuildServerInfo { 96 addr := <-res.serverPool 97 return BuildServerInfo{ 98 Addr: addr, 99 User: res.User, 100 Passwd: res.Passwd, 101 WorkSpace: res.BuildWorkSpace, 102 } 103} 104 105func ReleaseBuildServer(info BuildServerInfo) { 106 res.serverPool <- info.Addr 107} 108