hackpro-sdk
Version:
135 lines (126 loc) • 4.67 kB
JavaScript
;
/*
* Copyright 2018 balena.io
*
* 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.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const bluebird_1 = require("bluebird");
const _ = require("lodash");
const outdent_1 = require("outdent");
const resin_image_fs_1 = require("resin-image-fs");
const NETWORK_SETTINGS_KEYS = [
'wifiSsid',
'wifiKey',
'ip',
'netmask',
'gateway',
'routeMetric',
];
const nmWifiConfig = (index, options) => {
let config = outdent_1.outdent `
[connection]
id=balena-wifi-${pad(index)}
type=wifi
[wifi]
hidden=true
mode=infrastructure
ssid=${options.wifiSsid}
[ipv4]
`;
if (options.routeMetric) {
config += outdent_1.outdent `
route-metric=${options.routeMetric}
`;
}
if (options.ip && options.netmask && options.gateway) {
config += outdent_1.outdent `
method=manual
address1=${options.ip}/${options.netmask},${options.gateway}
`;
}
else {
config += outdent_1.outdent `
method=auto
`;
}
config += outdent_1.outdent `
[ipv6]
addr-gen-mode=stable-privacy
method=auto
`;
if (options.wifiKey) {
config += outdent_1.outdent `
[wifi-security]
auth-alg=open
key-mgmt=wpa-psk
psk=${options.wifiKey}
`;
}
console.log('network config file:\n', config);
return config;
};
const createNetworkConfigFiles = (networks) => {
return {
ethernet: _(networks)
.map('configuration')
.filter()
.value(),
wifi: _(networks)
.filter('wifiSsid')
.map((network, index) => {
return nmWifiConfig(index + 1, network);
})
.value(),
};
};
const pad = (num) => {
return _.padStart(`${num}`, 2, '0');
};
exports.execute = (operation, disk) => __awaiter(void 0, void 0, void 0, function* () {
if (!operation.data) {
throw new Error('config.json data missing from operation options');
}
if (!operation.partition) {
throw new Error('partition information missing from operation options');
}
let config = operation.data;
// FIXME: init with an empty list once the api no longer uses ('wifiSsid', 'wifiKey', 'ip', 'netmask', 'gateway')
const networks = [_.pick(config, ...NETWORK_SETTINGS_KEYS)];
if (config.network) {
networks.push(...config.network);
}
const networkConfigFiles = createNetworkConfigFiles(networks);
// FIXME: no need to remove wifiSsid, wifiKey, ip, netmask and gateway once api is updated
config = _.omit(config, 'network', ...NETWORK_SETTINGS_KEYS);
yield bluebird_1.using(resin_image_fs_1.interact(disk, operation.partition), (fs) => __awaiter(void 0, void 0, void 0, function* () {
yield fs.writeFileAsync('/config.json', JSON.stringify(config));
let index;
for (index = 0; index < networkConfigFiles.ethernet.length; index++) {
yield fs.writeFileAsync(`/system-connections/connection-${pad(index + 1)}`, networkConfigFiles.ethernet[index]);
}
for (index = 0; index < networkConfigFiles.wifi.length; index++) {
yield fs.writeFileAsync(`/system-connections/connection-${pad(index + 1)}`, networkConfigFiles.wifi[index]);
}
}));
});
//# sourceMappingURL=configure.js.map