Skip to content

Commit

Permalink
Merge pull request #14 from nqminds/feat/integrate-on-off-boarding-sc…
Browse files Browse the repository at this point in the history
…ripts

Feat/integrate on off boarding scripts
  • Loading branch information
ionut-cmd authored Dec 21, 2023
2 parents 48e5980 + c264b2e commit 1b82d55
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 46 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/create-demo-app-debs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ jobs:
- name: Rename x86_64 binary for packaging
run: mv server_x86_64 brski-demo-app-deb/opt/demo-server/server_x86_64

- name: Set execute permission on x86_64 Server Binary
run: sudo chmod +x brski-demo-app-deb/opt/demo-server/server_x86_64

- name: Upload x86_64 server binary as an artifact
uses: actions/upload-artifact@v2
with:
Expand Down Expand Up @@ -105,6 +102,9 @@ jobs:
name: server-x86_64-binary
path: debian-brski/brski-demo-app-deb/opt/demo-server/

- name: Set execute permission on x86_64 Server Binary
run: sudo chmod +x debian-brski/brski-demo-app-deb/opt/demo-server/server_x86_64

- name: Build x86_64 Debian Package
run: dpkg-deb --build debian-brski/brski-demo-app-deb brski-demo-app-deb-x86_64.deb

Expand Down
5 changes: 0 additions & 5 deletions connect-to-brski/offboard.sh

This file was deleted.

15 changes: 0 additions & 15 deletions connect-to-brski/onboard.sh

This file was deleted.

13 changes: 12 additions & 1 deletion debian-brski/brski-demo-app-deb/DEBIAN/postinst
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#!/bin/bash

# Create necessary directories
mkdir -p /opt/demo-server/html
mkdir -p /opt/demo-server/bash-scripts

# Set permissions
chown -R root:root /opt/demo-server
chmod -R 755 /opt/demo-server


# Ask the user for a port number
echo "Please enter the port number for the demo-server service:"
read PORT
Expand All @@ -12,9 +21,11 @@ fi

# Write the port number to the configuration file
echo "Port selected: $PORT"
mkdir -p /opt/demo-server/html # Create the directory if it doesn't exist
echo "port=$PORT" > /opt/demo-server/html/server.conf

chown root:root /opt/demo-server/html/server.conf
chmod 644 /opt/demo-server/html/server.conf

# Copy the systemd service file
cp /opt/demo-server/demo-server.service /etc/systemd/system/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/bin/bash
# offboard.sh
echo "Offboarding IoT device..."

# TODO: Add offboard logic
echo "Starting offboarding process."

# Disconnect from brski-open if connected
echo "Disconnecting from brski-open (if connected)..."
nmcli device disconnect wlan0
echo "Disconnected from brski-open."

echo "Offboarding process completed."
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
#!/bin/bash
# onboard.sh
echo "Onboarding IoT device..."

# TODO: add onboarding logic
echo "Starting onboarding process."

# Disconnect from brski-secure if connected
echo "Disconnecting from brski-secure (if connected)..."
sudo nmcli device disconnect wlan0
echo "Disconnected from brski-secure."

# Connect to brski-open
echo "Connecting to brski-open..."
sudo nmcli device wifi connect 'brski-open' ifname wlan0
echo "Connected to brski-open."

sleep 2

echo "Running brski preq command..."
sudo brski -c /etc/brski/config.ini -qqqq preq

echo "Waiting for the pledge voucher"
sleep 2

if [ $? -eq 0 ]; then
echo "brski preq command successful."
sudo nmcli device disconnect wlan0
echo "Disconnected from brski-open."

sleep 2

echo "Connecting to brski-secure..."
sudo nmcli device wifi connect 'brski-secure' password '1234554321' ifname wlan0
echo "Connected to brski-secure."
else
echo "Error: brski preq command failed."
fi

echo "Onboarding process completed."
12 changes: 12 additions & 0 deletions debian-brski/brski-demo-app-deb/opt/demo-server/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef CONFIG_H
#define CONFIG_H


#define CONFIG_FILE_PATH "/opt/demo-server/html/server.conf"
#define HTML_BASE_PATH "/opt/demo-server/html"
#define ONBOARDING_LOG_FILE_PATH "/opt/demo-server/html/onboarding_log_file.txt"
#define OFFBOARDING_LOG_FILE_PATH "/opt/demo-server/html/offboarding_log_file.txt"
#define ONBOARDING_SCRIPT_PATH "/opt/demo-server/bash-scripts/onboard.sh"
#define OFFBOARDING_SCRIPT_PATH "/opt/demo-server/bash-scripts/offboard.sh"

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ button:hover {
margin-top: 10px;
border-radius: 5px;
text-align: left;
white-space: pre-line;
max-height: 300px;
overflow-x: auto;
max-width: 400px;
}

.server-logs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,38 @@ function getServerPort(callback) {
});
}

function sendRequest(endpoint, logElementId, statusElementId, statusText, clearLogElementId, clearStatusElementId) {
function getLogFile(logFile, logElementId) {
fetch(logFile)
.then(response => response.text())
.then(text => {
var logElement = document.getElementById(logElementId);
logElement.textContent = text;
})
.catch(error => {
console.error('Error fetching log file:', error);
});
}

function sendRequest(endpoint, logElementId, statusElementId, statusText, clearLogElementId, clearStatusElementId, logFile) {
getServerPort(function(port) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:" + port + endpoint, true);
var url = window.location.protocol + "//" + window.location.hostname + ":" + port + endpoint;
xhr.open("POST", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
var logElement = document.getElementById(logElementId);
var statusElement = document.getElementById(statusElementId);
var clearLogElement = document.getElementById(clearLogElementId);
var clearStatusElement = document.getElementById(clearStatusElementId);

if (xhr.status === 200) {
logElement.textContent = xhr.responseText; // add logs here
statusElement.textContent = statusText;
statusElement.classList.add('status-green');

// clear the opposite status and log
clearLogElement.textContent = '';
clearStatusElement.textContent = '';
clearStatusElement.classList.remove('status-green');
setInterval(() => getLogFile(logFile, logElementId), 1000); // Poll every 1 seconds
} else {
statusElement.textContent = 'Status: Error';
statusElement.textContent = 'Status: Error - ' + xhr.responseText;
statusElement.classList.remove('status-green');
}
}
Expand All @@ -45,9 +55,9 @@ function sendRequest(endpoint, logElementId, statusElementId, statusText, clearL
}

document.getElementById('onboard').onclick = function() {
sendRequest('/onboard', 'onboardLog', 'onboardStatus', 'Onboarded', 'offboardLog', 'offboardStatus');
sendRequest('/onboard', 'onboardLog', 'onboardStatus', 'Onboarded', 'offboardLog', 'offboardStatus', '../onboarding_log_file.txt');
};

document.getElementById('offboard').onclick = function() {
sendRequest('/offboard', 'offboardLog', 'offboardStatus', 'Offboarded', 'onboardLog', 'onboardStatus');
sendRequest('/offboard', 'offboardLog', 'offboardStatus', 'Offboarded', 'onboardLog', 'onboardStatus', '../offboarding_log_file.txt');
};
92 changes: 85 additions & 7 deletions debian-brski/brski-demo-app-deb/opt/demo-server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include "config.h"

#define DEFAULT_PORT 8082

int write_port_to_conf_file(int port) {
FILE *file = fopen("/opt/demo-server/html/server.conf", "w");
FILE *file = fopen(CONFIG_FILE_PATH, "w");
if (file == NULL) {
perror("Failed to open config file");
return -1;
Expand Down Expand Up @@ -56,13 +58,48 @@ static int serve_file(const char *filename, struct MHD_Response **response) {
return MHD_NO;
}

fread(buffer, 1, size, file);
size_t read_bytes = fread(buffer, 1, size, file);
if (read_bytes != size) {
fprintf(stderr, "Error reading file: %s\n", filename);
free(buffer);
fclose(file);
return MHD_NO;
}

*response = MHD_create_response_from_buffer(size, buffer, MHD_RESPMEM_MUST_FREE);
fclose(file);

return MHD_YES;
}

static int execute_script_async(const char *script, const char *output_file) {
pid_t pid = fork();
if (pid == -1) {
// Handle error in fork
return -1;
} else if (pid > 0) {
// Parent process
fprintf(stdout, "Started script (PID: %d): %s\n", pid, script);
return 0;
} else {
// Child process
int fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {
fprintf(stderr, "Failed to open output file: %s\n", output_file);
exit(EXIT_FAILURE);
}

dup2(fd, STDOUT_FILENO); // Redirect stdout to output file
dup2(fd, STDERR_FILENO); // Redirect stderr to output file
close(fd);

execl("/bin/bash", "bash", "-c", script, (char *) NULL);
fprintf(stderr, "Failed to execute script: %s\n", script);
exit(EXIT_FAILURE);
}
}


static enum MHD_Result answer_to_connection(void *cls, struct MHD_Connection *connection,
const char *url, const char *method,
const char *version, const char *upload_data,
Expand All @@ -80,7 +117,7 @@ static enum MHD_Result answer_to_connection(void *cls, struct MHD_Connection *co
}

char filepath[1024];
snprintf(filepath, sizeof(filepath), "/opt/demo-server/html%s", url);
snprintf(filepath, sizeof(filepath), "%s%s", HTML_BASE_PATH, url);

if (serve_file(filepath, &response) == MHD_YES) {
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
Expand Down Expand Up @@ -108,11 +145,52 @@ static enum MHD_Result answer_to_connection(void *cls, struct MHD_Connection *co
MHD_RESPMEM_PERSISTENT);

if (strcmp(url, "/onboard") == 0) {
fprintf(stdout, "Received onboard request\n");
execute_script("/opt/demo-server/bash-scripts/onboard.sh");
const char *output_file = ONBOARDING_LOG_FILE_PATH;
// Execute the onboarding script
int script_result = execute_script_async(ONBOARDING_SCRIPT_PATH, output_file);

sleep(5);

FILE *file = fopen(output_file, "r");
if (file == NULL) {
perror("Error opening log file");

return MHD_NO;
}

fseek(file, 0, SEEK_END);
long size = ftell(file);
rewind(file);

char *buffer = malloc(size + 1);
if (buffer == NULL) {
perror("Memory allocation failed");
fclose(file);
return MHD_NO;
}

fread(buffer, 1, size, file);
buffer[size] = '\0'; // Null-terminate the buffer
fclose(file);

struct MHD_Response *response = MHD_create_response_from_buffer(size, buffer, MHD_RESPMEM_MUST_FREE);
int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);

return ret;
} else if (strcmp(url, "/offboard") == 0) {
fprintf(stdout, "Received offboard request\n");
execute_script("/opt/demo-server/bash-scripts/offboard.sh");
fprintf(stdout, "Received offboard request\n");
const char *output_file = OFFBOARDING_LOG_FILE_PATH;

execute_script_async(OFFBOARDING_SCRIPT_PATH, output_file);
const char *ok_page = "Offboarding initiated";
response = MHD_create_response_from_buffer(strlen(ok_page),
(void *)ok_page,
MHD_RESPMEM_PERSISTENT);
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
return ret;

} else {
const char *not_found_page = "Not Found";
response = MHD_create_response_from_buffer(strlen(not_found_page),
Expand Down

0 comments on commit 1b82d55

Please sign in to comment.