📜 ⬆️ ⬇️

Synology DSM, Perl and Mojolicious

Once I remembered Synology DSM in Simultaneously assembling encrypted folders in Synology DSM , I decided to show how using Perl and Mojolicious web framework to make a simple application to fit your needs. I had to write on my knee the utility SynDevInfo , to display the necessary information on the device in the browser, since dear support does not know GNU / Linux, and users cannot log in via ssh.


Under the cat, the structure of the package, the application launch script and the script on Mojolicious :: Lite will be described.


Project structure


image

Application launch


Mojolicious supports a bunch of different ways to deploy an application. Using the principle: simple things should be done simply, using the approach described in the Built-in web server . With this method, the script runs in daemon mode and does not require an external web server.


#!/bin/sh export PERL5LIB="${SYNOPKG_PKGDEST}/3rdparty" case $1 in start) /usr/bin/perl ${SYNOPKG_PKGDEST}/SynDevInfo daemon -m production -l http://*:12345 & sleep 5; exit 0 ;; stop) PID=`ps -aef | grep "SynDevInfo" | grep -v grep | awk '{print $2}'` kill -9 $PID exit 0 ;; restart) exit 0 ;; status) exit 0 ;; log) exit 0 ;; esac 

application


 #!/usr/bin/env perl use strict; use utf8; use feature ':5.14'; use warnings FATAL => 'all'; use FindBin; BEGIN { unshift @INC, "$FindBin::Bin/lib" } use Mojolicious::Lite; use SynInfo::Info; my $CACHE_FILE = "/tmp/syninfo_cashe.json"; get '/' => sub { my $c = shift; $c->render(template => 'index', syno_def => $c->syn_info->syno_defines(), general => $c->syn_info->general(), volumes => $c->syn_info->volume(), networks=> $c->syn_info->networks(), uname => $c->syn_info->uname(), cpuinfo => $c->syn_info->cpuinfo() ); }; helper syn_info => sub { state $info = SynInfo::Info->load($CACHE_FILE) }; my $info = SynInfo::Info->new(); $info->save($CACHE_FILE); app->start; __DATA__ @@ index.html.ep % layout 'default'; % title 'Summary info'; <pre> PRODUCT: <%= $syno_def->{product} %> MODEL: <%= $general->{model} %> UNIQUE: <%= $syno_def->{unique} %> FIRMWARE: <%= $general->{firmware_ver} %>(<%= $general->{firmware_date} %>) CPU: <%= $general->{cpu_vendor} %> <%= $general->{cpu_family} %> <%= $general->{cpu_series} %> <%= $general->{cpu_clock_speed} %> Mhz, cores <%= $general->{cpu_cores} %> RAM: <%= $general->{ram_size} %> Mb <% foreach my $volume (@$volumes){ %> VOLUME: <%= $volume->{name} %> SIZE: <%= $volume->{total_size} %> Byte USED: <%= $volume->{used_size} %> Byte<% } %> <% foreach my $nif (@{$networks->{'nif'}}){ %> Interface: <%= $nif->{id} %> ADDRESS: <%= $nif->{addr} %> TYPE: <%= $nif->{type} %> <% } %> Name and information about current kernel: <% foreach my $key (sort keys %$uname){ chomp($uname->{$key}); %> <%= $key %>: <%= $uname->{$key}%> <% } %> cat /proc/cpuinfo <%= $cpuinfo %> </pre> @@ layouts/default.html.ep <!DOCTYPE html> <html> <head> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous"> <!-- Latest compiled and minified JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> <style type="text/css"> section{ background: #fff; border-radius: 7px 7px 7px 7px; -moz-border-radius: 7px 7px 7px 7px; -webkit-border-radius: 7px 7px 7px 7px; border: 0px solid #000000; margin-bottom: 10px; padding: 27px 27px 20px; } </style> <title><%= title %></title> </head> <body> <section> <%= content %> </section> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> </body> </html> = "sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin = "anonymous"> </ script> #!/usr/bin/env perl use strict; use utf8; use feature ':5.14'; use warnings FATAL => 'all'; use FindBin; BEGIN { unshift @INC, "$FindBin::Bin/lib" } use Mojolicious::Lite; use SynInfo::Info; my $CACHE_FILE = "/tmp/syninfo_cashe.json"; get '/' => sub { my $c = shift; $c->render(template => 'index', syno_def => $c->syn_info->syno_defines(), general => $c->syn_info->general(), volumes => $c->syn_info->volume(), networks=> $c->syn_info->networks(), uname => $c->syn_info->uname(), cpuinfo => $c->syn_info->cpuinfo() ); }; helper syn_info => sub { state $info = SynInfo::Info->load($CACHE_FILE) }; my $info = SynInfo::Info->new(); $info->save($CACHE_FILE); app->start; __DATA__ @@ index.html.ep % layout 'default'; % title 'Summary info'; <pre> PRODUCT: <%= $syno_def->{product} %> MODEL: <%= $general->{model} %> UNIQUE: <%= $syno_def->{unique} %> FIRMWARE: <%= $general->{firmware_ver} %>(<%= $general->{firmware_date} %>) CPU: <%= $general->{cpu_vendor} %> <%= $general->{cpu_family} %> <%= $general->{cpu_series} %> <%= $general->{cpu_clock_speed} %> Mhz, cores <%= $general->{cpu_cores} %> RAM: <%= $general->{ram_size} %> Mb <% foreach my $volume (@$volumes){ %> VOLUME: <%= $volume->{name} %> SIZE: <%= $volume->{total_size} %> Byte USED: <%= $volume->{used_size} %> Byte<% } %> <% foreach my $nif (@{$networks->{'nif'}}){ %> Interface: <%= $nif->{id} %> ADDRESS: <%= $nif->{addr} %> TYPE: <%= $nif->{type} %> <% } %> Name and information about current kernel: <% foreach my $key (sort keys %$uname){ chomp($uname->{$key}); %> <%= $key %>: <%= $uname->{$key}%> <% } %> cat /proc/cpuinfo <%= $cpuinfo %> </pre> @@ layouts/default.html.ep <!DOCTYPE html> <html> <head> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous"> <!-- Latest compiled and minified JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> <style type="text/css"> section{ background: #fff; border-radius: 7px 7px 7px 7px; -moz-border-radius: 7px 7px 7px 7px; -webkit-border-radius: 7px 7px 7px 7px; border: 0px solid #000000; margin-bottom: 10px; padding: 27px 27px 20px; } </style> <title><%= title %></title> </head> <body> <section> <%= content %> </section> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> </body> </html> 

In one file and the controller code and html templates. Before starting the I / O processing cycle, the model collects information and serializes it in json. At this point in time, the daemon does not process external requests, so a wait of 5 seconds is written in the startup script. In the helper, this model is deserialized back and displayed in the controller.


Running this result

')

Source: https://habr.com/ru/post/302746/


All Articles