2017-07-27 14:07:05 +03:00
// Generated by CoffeeScript 1.12.7
2017-03-08 23:30:24 +00:00
/ *
Copyright 2017 Resin . 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 .
* /
2017-07-26 22:48:27 +03:00
var BOOT _PARTITION , CONNECTIONS _FOLDER , CONNECTION _FILE , getConfiguration , getConfigurationSchema , inquirerOptions , prepareConnectionFile ;
2017-03-08 23:30:24 +00:00
2017-07-26 22:48:27 +03:00
BOOT _PARTITION = {
primary : 1
} ;
CONNECTIONS _FOLDER = '/system-connections' ;
getConfigurationSchema = function ( connnectionFileName ) {
if ( connnectionFileName == null ) {
connnectionFileName = 'resin-wifi' ;
}
return {
mapper : [
{
template : {
hostname : '{{hostname}}' ,
persistentLogging : '{{persistentLogging}}'
2017-03-08 23:30:24 +00:00
} ,
2017-07-26 22:48:27 +03:00
domain : [ [ 'config_json' , 'hostname' ] , [ 'config_json' , 'persistentLogging' ] ]
} , {
template : {
wifi : {
ssid : '{{networkSsid}}'
} ,
'wifi-security' : {
psk : '{{networkKey}}'
}
} ,
domain : [ [ 'system_connections' , connnectionFileName , 'wifi' ] , [ 'system_connections' , connnectionFileName , 'wifi-security' ] ]
}
] ,
files : {
system _connections : {
fileset : true ,
type : 'ini' ,
location : {
path : CONNECTIONS _FOLDER . slice ( 1 ) ,
partition : BOOT _PARTITION
2017-03-08 23:30:24 +00:00
}
} ,
2017-07-26 22:48:27 +03:00
config _json : {
type : 'json' ,
location : {
path : 'config.json' ,
partition : BOOT _PARTITION
2017-03-08 23:30:24 +00:00
}
}
2017-07-26 22:48:27 +03:00
}
} ;
} ;
inquirerOptions = function ( data ) {
return [
{
message : 'Network SSID' ,
type : 'input' ,
name : 'networkSsid' ,
"default" : data . networkSsid
} , {
message : 'Network Key' ,
type : 'input' ,
name : 'networkKey' ,
"default" : data . networkKey
} , {
message : 'Do you want to set advanced settings?' ,
type : 'confirm' ,
name : 'advancedSettings' ,
"default" : false
} , {
message : 'Device Hostname' ,
type : 'input' ,
name : 'hostname' ,
"default" : data . hostname ,
when : function ( answers ) {
return answers . advancedSettings ;
}
} , {
message : 'Do you want to enable persistent logging?' ,
type : 'confirm' ,
name : 'persistentLogging' ,
"default" : data . persistentLogging ,
when : function ( answers ) {
return answers . advancedSettings ;
2017-03-22 12:46:06 +03:00
}
2017-03-08 23:30:24 +00:00
}
2017-07-26 22:48:27 +03:00
] ;
} ;
getConfiguration = function ( data ) {
var _ , inquirer ;
_ = require ( 'lodash' ) ;
inquirer = require ( 'inquirer' ) ;
data = _ . assign ( data , {
persistentLogging : data . persistentLogging || false
} ) ;
return inquirer . prompt ( inquirerOptions ( data ) ) . then ( function ( answers ) {
return _ . merge ( data , answers ) ;
} ) ;
} ;
CONNECTION _FILE = '[connection]\nid=resin-wifi\ntype=wifi\n\n[wifi]\nhidden=true\nmode=infrastructure\nssid=My_Wifi_Ssid\n\n[wifi-security]\nauth-alg=open\nkey-mgmt=wpa-psk\npsk=super_secret_wifi_password\n\n[ipv4]\nmethod=auto\n\n[ipv6]\naddr-gen-mode=stable-privacy\nmethod=auto' ;
2017-07-27 14:07:05 +03:00
/ *
* if the ` resin-wifi ` file exists ( previously configured image or downloaded from the UI ) it ' s used and reconfigured
* if the ` resin-sample.ignore ` exists it ' s copied to ` resin-wifi `
* if the ` resin-sample ` exists it ' s reconfigured ( legacy mode , will be removed eventually )
* otherwise , the new file is created
* /
2017-07-26 22:48:27 +03:00
prepareConnectionFile = function ( target ) {
var _ , imagefs ;
_ = require ( 'lodash' ) ;
imagefs = require ( 'resin-image-fs' ) ;
return imagefs . listDirectory ( {
image : target ,
partition : BOOT _PARTITION ,
path : CONNECTIONS _FOLDER
} ) . then ( function ( files ) {
if ( _ . includes ( files , 'resin-wifi' ) ) {
return null ;
}
if ( _ . includes ( files , 'resin-sample.ignore' ) ) {
return imagefs . copy ( {
image : target ,
partition : BOOT _PARTITION ,
path : CONNECTIONS _FOLDER + "/resin-sample.ignore"
} , {
image : target ,
partition : BOOT _PARTITION ,
path : CONNECTIONS _FOLDER + "/resin-wifi"
} ) . thenReturn ( null ) ;
}
if ( _ . includes ( files , 'resin-sample' ) ) {
return 'resin-sample' ;
}
return imagefs . writeFile ( {
image : target ,
partition : BOOT _PARTITION ,
path : CONNECTIONS _FOLDER + "/resin-wifi"
} , CONNECTION _FILE ) . thenReturn ( null ) ;
} ) . then ( function ( connnectionFileName ) {
return getConfigurationSchema ( connnectionFileName ) ;
} ) ;
2017-03-22 12:46:06 +03:00
} ;
2017-03-08 23:30:24 +00:00
2017-03-22 12:46:06 +03:00
module . exports = {
signature : 'local configure <target>' ,
description : '(Re)configure a resinOS drive or image' ,
help : 'Use this command to configure or reconfigure a resinOS drive or image.\n\nExamples:\n\n $ resin local configure /dev/sdc\n $ resin local configure path/to/image.img' ,
root : true ,
action : function ( params , options , done ) {
2017-07-26 22:48:27 +03:00
var Promise , denymount , isMountedAsync , reconfix , umount , umountAsync ;
2017-03-22 12:46:06 +03:00
Promise = require ( 'bluebird' ) ;
2017-03-27 12:14:55 +03:00
umount = require ( 'umount' ) ;
umountAsync = Promise . promisify ( umount . umount ) ;
isMountedAsync = Promise . promisify ( umount . isMounted ) ;
2017-03-22 12:46:06 +03:00
reconfix = require ( 'reconfix' ) ;
denymount = Promise . promisify ( require ( 'denymount' ) ) ;
2017-07-26 22:48:27 +03:00
return prepareConnectionFile ( params . target ) . tap ( function ( ) {
return isMountedAsync ( params . target ) . then ( function ( isMounted ) {
if ( ! isMounted ) {
return ;
}
return umountAsync ( params . target ) ;
} ) ;
} ) . then ( function ( configurationSchema ) {
2017-03-22 12:46:06 +03:00
return denymount ( params . target , function ( cb ) {
2017-07-26 22:48:27 +03:00
return reconfix . readConfiguration ( configurationSchema , params . target ) . then ( getConfiguration ) . then ( function ( answers ) {
return reconfix . writeConfiguration ( configurationSchema , answers , params . target ) ;
2017-03-22 12:46:06 +03:00
} ) . asCallback ( cb ) ;
} ) ;
} ) . then ( function ( ) {
return console . log ( 'Done!' ) ;
} ) . asCallback ( done ) ;
}
} ;