Fixed setting hostname
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Wed, 7 Feb 2018 23:13:40 +0000 (15:13 -0800)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Wed, 7 Feb 2018 23:13:40 +0000 (15:13 -0800)
scripts/sethostname
scripts/ssh-bbctrl [new file with mode: 0755]
src/jade/templates/admin-view.jade
src/js/admin-view.js
src/pwr/config.h
src/pwr/main.c
src/py/bbctrl/Web.py

index 08b9edbe5492db4d479aebd02577d387afa1ada4..53cdf21c6414d1fa186146fa48724b6c4a15a6b6 100755 (executable)
@@ -1,17 +1,22 @@
 #!/bin/bash -e
 
-HOSTNAME="$1"
+HOSTNAME="$(echo "$1" | tr '[:upper:]' '[:lower:]')"
 
 if [ "$HOSTNAME" == "" ]; then
     echo "Usage: $0 <hostname>"
     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 (executable)
index 0000000..b7438ac
--- /dev/null
@@ -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"
index 13150aa63d605bf7eb5d524b6f60021eae34e70a..79871ccaa46a12880234d130c56f5f639f72fbe6 100644 (file)
@@ -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 <strong>{{hostname}}</strong>.
+        p Rebooting.  Please wait...
+        p Redirecting in {{redirectTimeout}} seconds.
+      div(slot="footer")
 
     message(:show.sync="passwordSet")
       h3(slot="header") Password Set
index b85c8fa9dd6ca8ab6ce7efc448601678d279c94f..83dd87494c397c3f889c9b00d79ff686077bfaef 100644 (file)
@@ -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));
       })
index 0ae901210a49f212054d87f115be21cd6bd138ae..19ccc3bf1a4689e3be9c5554949594b2d095e517 100644 (file)
@@ -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
 
index 77b1692843058231e8d94f2a80d94eb9d7716f6c..4c9c4d52a08933fe3ac7e4477a9c40318373957c 100644 (file)
@@ -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) {
index e81afae22342076ab5e30614aa32a1db06a4a55d..58c01e3190ac53ffcc5ba456a79874d5b0fdbdc6 100644 (file)
@@ -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),