From: Joseph Coffland Date: Wed, 7 Feb 2018 23:13:40 +0000 (-0800) Subject: Fixed setting hostname X-Git-Url: https://git.buildbotics.com/?a=commitdiff_plain;h=5b097e9b9e51bec81c0dd3a46cdf7c8dc73bf26e;p=bbctrl-firmware Fixed setting hostname --- diff --git a/scripts/sethostname b/scripts/sethostname index 08b9edb..53cdf21 100755 --- a/scripts/sethostname +++ b/scripts/sethostname @@ -1,17 +1,22 @@ #!/bin/bash -e -HOSTNAME="$1" +HOSTNAME="$(echo "$1" | tr '[:upper:]' '[:lower:]')" if [ "$HOSTNAME" == "" ]; then echo "Usage: $0 " exit 1 fi -if [ $(echo "$HOSTNAME" | tr '[:upper:]' '[:lower:]') == "localhost" ]; then +if [ "$HOSTNAME" == "localhost" ]; then echo "Cannot set hostname to 'localhost'" exit 1 fi +if [ "$HOSTNAME" =~ ^.*\.local$ ]; then + echo "Hostname cannot end with '.local'" + exit 1 +fi + if [[ ! "$HOSTNAME" =~ ^[a-zA-Z0-9-]{1,63}$ ]]; then echo "Invalid hostname '$HOSTNAME'" exit 1 @@ -19,5 +24,3 @@ fi sed -i "s/^127.0.1.1\([[:space:]]*\).*$/127.0.1.1\1$HOSTNAME/" /etc/hosts echo "$HOSTNAME" > /etc/hostname -/etc/init.d/hostname.sh -sync diff --git a/scripts/ssh-bbctrl b/scripts/ssh-bbctrl new file mode 100755 index 0000000..b7438ac --- /dev/null +++ b/scripts/ssh-bbctrl @@ -0,0 +1,16 @@ +#!/bin/bash + +USER=bbmc +HOST=bbctrl.local + +if [ $# -eq 1 ]; then + if [[ "$1" = *@ ]]; then + LOGIN="$1" + else + LOGIN=$USER@"$1" + fi +else + LOGIN=$USER@$HOST +fi + +ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$LOGIN" diff --git a/src/jade/templates/admin-view.jade b/src/jade/templates/admin-view.jade index 13150aa..79871cc 100644 --- a/src/jade/templates/admin-view.jade +++ b/src/jade/templates/admin-view.jade @@ -65,13 +65,15 @@ script#admin-view-template(type="text/x-template") message(:show.sync="firmwareUpgrading") h3(slot="header") Firmware upgrading - p(slot="body") Please wait. . . + p(slot="body") Please wait... message(:show.sync="hostnameSet") h3(slot="header") Hostname Set - p(slot="body") - | Hostname was successfuly set to {{hostname}}. - | Poweroff and restart the controller for this change to take effect. + div(slot="body") + p Hostname was successfuly set to {{hostname}}. + p Rebooting. Please wait... + p Redirecting in {{redirectTimeout}} seconds. + div(slot="footer") message(:show.sync="passwordSet") h3(slot="header") Password Set diff --git a/src/js/admin-view.js b/src/js/admin-view.js index b85c8fa..83dd874 100644 --- a/src/js/admin-view.js +++ b/src/js/admin-view.js @@ -18,6 +18,7 @@ module.exports = { hostnameSet: false, usernameSet: false, passwordSet: false, + redirectTimeout: 0, latest: '', hostname: '', username: '', @@ -39,6 +40,7 @@ module.exports = { api.get('hostname').done(function (hostname) { this.hostname = hostname; }.bind(this)); + api.get('remote/username').done(function (username) { this.username = username; }.bind(this)); @@ -46,9 +48,30 @@ module.exports = { methods: { + redirect: function (hostname) { + if (0 < this.redirectTimeout) { + this.redirectTimeout -= 1; + setTimeout(function () {this.redirect(hostname)}.bind(this), 1000); + + } else { + location.hostname = hostname; + this.hostnameSet = false; + } + }, + + set_hostname: function () { api.put('hostname', {hostname: this.hostname}).done(function () { + this.redirectTimeout = 45; this.hostnameSet = true; + + api.put('reboot').always(function () { + var hostname = this.hostname; + if (String(location.hostname).endsWith('.local')) + hostname += '.local'; + this.redirect(hostname); + }.bind(this)); + }.bind(this)).fail(function (error) { alert('Set hostname failed: ' + JSON.stringify(error)); }) diff --git a/src/pwr/config.h b/src/pwr/config.h index 0ae9012..19ccc3b 100644 --- a/src/pwr/config.h +++ b/src/pwr/config.h @@ -55,7 +55,7 @@ enum { }; -// ADC +// ADC channels enum { CS1_ADC, // Motor current CS2_ADC, // Vdd current @@ -96,6 +96,7 @@ enum { #define VOLTAGE_REF_R2 1000 #define CURRENT_REF_MUL 1970 + // Addresses 0x60 to 0x67 #define I2C_ADDR 0x60 #define I2C_MASK 0b00000111 diff --git a/src/pwr/main.c b/src/pwr/main.c index 77b1692..4c9c4d5 100644 --- a/src/pwr/main.c +++ b/src/pwr/main.c @@ -70,7 +70,7 @@ ISR(TWI_SLAVE_vect) { if (status & I2C_DATA_INT_BM) { if (status & I2C_READ_BM) { - // send response + // Send response if (byte < 2) { switch (byte++) { case 0: TWSD = reg; break; @@ -85,7 +85,7 @@ ISR(TWI_SLAVE_vect) { } else if (status & I2C_ADDRESS_STOP_INT_BM) { if (status & I2C_ADDRESS_MATCH_BM) { - // read address + // Read address uint8_t addr = (TWSD >> 1) & I2C_MASK; if (addr < NUM_REGS) { diff --git a/src/py/bbctrl/Web.py b/src/py/bbctrl/Web.py index e81afae..58c01e3 100644 --- a/src/py/bbctrl/Web.py +++ b/src/py/bbctrl/Web.py @@ -22,6 +22,10 @@ def call_get_output(cmd): return s +class RebootHandler(bbctrl.APIHandler): + def put_ok(self): subprocess.Popen('reboot') + + class HostnameHandler(bbctrl.APIHandler): def get(self): p = subprocess.Popen(['hostname'], stdout = subprocess.PIPE) @@ -32,7 +36,7 @@ class HostnameHandler(bbctrl.APIHandler): def put(self): if 'hostname' in self.json: if subprocess.call(['/usr/local/bin/sethostname', - self.json['hostname']]) == 0: + self.json['hostname'].strip()]) == 0: self.write_json('ok') return @@ -276,6 +280,7 @@ class Web(tornado.web.Application): handlers = [ (r'/websocket', WSConnection), + (r'/api/reboot', RebootHandler), (r'/api/hostname', HostnameHandler), (r'/api/remote/username', UsernameHandler), (r'/api/remote/password', PasswordHandler),