ilanap | 061ca93 | 2019-08-04 10:16:33 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016-2017 European Support Limited |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | 'use strict' |
| 17 | |
| 18 | const fs = require('fs'); |
| 19 | |
| 20 | var pathToRoot = process.env.TESTS_BASE; |
| 21 | if (!pathToRoot.endsWith("/")) { |
| 22 | pathToRoot += "/"; |
| 23 | } |
| 24 | var envConfig = require(pathToRoot + 'config.json'); |
| 25 | var protocol = (process.env.PROTOCOL !== undefined) ? process.env.PROTOCOL : 'https'; |
| 26 | |
| 27 | try { |
| 28 | envConfig = require(pathToRoot + 'environments/dockerConfig.json'); |
| 29 | } catch (e) { |
| 30 | } |
| 31 | |
| 32 | function run() { |
| 33 | var inputArgs = process.argv.slice(2); |
| 34 | let changeConfig = false; |
| 35 | if (process.env.K8S_CONF_PATH !== undefined) { |
| 36 | console.log('updating with kubernetes services'); |
| 37 | let k8sConfig = require(pathToRoot + process.env.K8S_CONF_PATH); |
| 38 | mapK8sPod2Docker(k8sConfig, inputArgs[0], inputArgs[1]); |
| 39 | changeConfig = true; |
| 40 | } else { |
| 41 | console.log('not updating at all'); |
| 42 | } |
| 43 | if (changeConfig) { |
| 44 | let data = JSON.stringify(envConfig, null, 2); |
| 45 | console.log('writing config file: ' + pathToRoot+'environments/dockerConfig.json'); |
| 46 | console.log(data); |
| 47 | fs.writeFileSync(pathToRoot+'environments/dockerConfig.json', data); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | function mapK8sPod2Docker(k8sConfig, id, k8sid) { |
| 52 | let item = k8sConfig.items.find(item => { |
| 53 | if (item.spec !== undefined && item.spec.ports !== undefined) { |
| 54 | let spec = item.spec.ports.find(port => { |
| 55 | if (port.name === k8sid) { |
| 56 | return true; |
| 57 | } |
| 58 | }); |
| 59 | return (spec !== undefined); |
| 60 | } else { |
| 61 | return false; |
| 62 | } |
| 63 | }); |
| 64 | |
| 65 | item.spec.ports.forEach(port => { |
| 66 | if (port.name === k8sid) { |
| 67 | envConfig[id].port = port.nodePort; |
| 68 | let rancherData = JSON.parse(item.metadata.annotations["field.cattle.io/publicEndpoints"]); |
| 69 | let address = rancherData.find(address => { |
| 70 | return address.port === port.nodePort; |
| 71 | }); |
| 72 | envConfig[id].port = address.port; |
| 73 | envConfig[id].server = address.addresses[0]; |
| 74 | envConfig[id].protocol = protocol; |
| 75 | envConfig[id].user = process.env.SDC_USER_ID; |
| 76 | } |
| 77 | }); |
| 78 | |
| 79 | } |
| 80 | |
| 81 | run(); |