Wednesday, 28 October 2015

Qt: Updating styles based on dynamic properties

#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QStyle>
#include <QDebug>

class App : public QObject
{
    Q_OBJECT
public:
    enum Alert
    {
        None,
        Warning,
        Critical
    };
    Q_ENUM(Alert);

    App(int argc, char** argv)
        : app(argc, argv)
    {}

    int exec()
    {
        window.setCentralWidget(&main);

        label.setText("hello world");
        label.setProperty("alert", QVariant::fromValue(None));

        button.setText("alert");

        QObject::connect(&button, &QPushButton::clicked, [this]()
            {
                label.setProperty("alert", QVariant::fromValue(Critical));
                label.setProperty("foo",   false);

                label.style()->unpolish(&label);
                label.style()->polish(&label);
                label.update();
            });

        layout.addWidget(&label);
        layout.addWidget(&button);
        main.setLayout(&layout);

        app.setStyleSheet(R"(
            QLabel[alert="None"]                { color: green; } 
            QLabel[alert="Warning"]             { color: blue;  }
            QLabel[alert="Critical"][foo=true]  { color: red;     }
            QLabel[alert="Critical"][foo=false] { color: magenta; }
        )");

        window.show();
        return app.exec();
    }

    QApplication app;
    QMainWindow  window;
    QWidget      main;
    QLabel       label;
    QPushButton  button;
    QVBoxLayout  layout;
};

int main(int argc, char** argv)
{
    return App(argc, argv).exec();

}


Tuesday, 6 October 2015

Gnome - change default application for text files

xdg-mime query default text/plain
xdg-mime default sublime_text.desktop text/plain

xdg-mime query filetype application/x-shellscript
xdg-mime query default application/x-shellscript

Wednesday, 19 August 2015

Send email from script

Install ssmtp:

    $ sudo apt-get install ssmtp

Edit the ssmtp config file:

    $ sudo vim /etc/ssmtp/ssmtp.conf

Enter this in the file:

root=username@gmail.com
mailhub=smtp.gmail.com:465
rewriteDomain=gmail.com
AuthUser=username
AuthPass=password (create a app-specific password in google accounts)
FromLineOverride=YES
UseTLS=YES

Enter the email address of the person who will receive your email:

    $ ssmtp recepient_name@gmail.com

Now enter this:

To: recipient_name@gmail.com
From: username@gmail.com
Subject: Sent from a terminal!

Your content goes here. Lorem ipsum dolor sit amet, consectetur adipisicing.
(Notice the blank space between the subject and the body.)

To send the email: Ctrl + D

You can also save the text mentioned in Point 5 into a text file and send it using:

    $ ssmtp recipient_name@gmail.com < filename.txt

http://askubuntu.com/questions/12917/how-to-send-mail-from-the-command-line

Friday, 7 August 2015

Supervisord & Rundeck

supervisord

install

$ sudo easy_install supervisor

generate SHA-1 hash of password to be used for TCP access

$ echo -n password | sha1sum | awk '{print $1}'

enable TCP access

sudo vim /etc/supervisor/supervisord.conf

[inet_http_server]
port = 127.0.0.1:9001
username = user
password = {SHA}1235678

configure supervisorctl command line access (note password cannot be SHA hash, has to be plaintext)

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock
serverurl=http://localhost:9001
username = user
password = abcdef

add a program

sudo vim /etc/supervisor/conf.d/app_example.conf

[program:app_example]
command=app_example
directory=/path/to/app
autostart:false
autorestart:false
startsecs:5
startretries:3
user:prod
redirect_stderr:true
stdout_logfile:/path/to/app/logfile.log
stdout_logfile_maxbytes:1000GB

supervisorctl - enter interactive mode

supervisorctl -s http://localhost:9001 -u user -p password

start/stop

    supervisorctl -s http://localhost:9001 -u user -p password start group:app

reread/restart/update

supervisorctl -s http://localhost:9001 -u user -p password update (or reread, restart)

reread just reads config changes, but doesn’t restart any affected processes
restart restarts the names process without loading any config changes
update rereads and restarts apps with changed configuration

rundeck

download and install from http://rundeck.org/downloads.html

add user to rundeck group

$ sudo usermod -a -G rundeck steve

add write permissions to rundeck group

$ sudo chmod g+w -R /var/rundeck

Friday, 24 July 2015

Postgres

Postgres

install server

sudo apt-get install postgresql postgresql-contrib

install client

sudo apt-get install postgresql-client libpqxx-dev pgadmin3

configure server

As user postgres, run psql, connecting to database postgres

sudo -u postgres psql postgres

In the psql shell, set the postgres user’s password, and then press ctrl-d to exit the psql shell

\password postgres

create a new user, with an associated database

as user postgres, run createuser to create a new user dev

sudo -u postgres createuser -D -A -P dev

the options are:

  • -D: no database creation rights
  • -A: no add user rights
  • -P: ask for password

as user postgres, run createdb to create a new database devbd owned by user dev

sudo -u postgres createdb -O dev devdb

set the new user’s password

sudo -u postgres psql

\password dev
\password prod

install server instrumentation

as user postgres, create the adminpack extension

sudo -u postgres psql
CREATE EXTENSION adminpack;

change server authentication method

default authentication is peer. peer authentication obtains the client’s operating system user name from the kernel and uses it as the allowed database user name. This only works for local connections.

change to md5, which is password-based authentication, with the password sent over the connection as a md5 hash.

sudo vi /etc/postgresql/9.3/main/pg_hba.conf

change the line

# Database administrative login by Unix domain socket
local   all             postgres                                peer

to

# Database administrative login by Unix domain socket
local   all             postgres                                md5    

get postgres to reload the config

sudo /etc/init.d/postgresql reload

Wednesday, 3 June 2015

GNU make variables

$@ - target's filename
$< - name of the first prerequisite
$? - list of all prerequisites newer than target
$^ - list of all unique prerequisites
$+ - list of all prerequisites, including duplicates (useful for linking)
$| - list of all order-only prerequisites
$* - stem; the part of the filename that matched the ‘%’ in the target pattern

$(@D) - directory part of the target (if no '/' appears, it is '.')
$(@F) - filename patr of the target (equivalent to $(notdir $@))

$(<D) - directory part of the first prerequisite
$(<F) - filename part of the first prerequisite

$(?D) - directory parts of the list of all prerequisites newer than target
$(?F) - filename parts of the list of all prerequisites newer than target

$(^D) - directory parts of the list of all unique prerequisites
$(^F) - filename parts of the list of all unique prerequisites

$(+D) - directory parts of the list of all prerequisites, including duplicates
$(+F) - filename parts of the list of all prerequisites, including duplicates

$(*D) - directory part of the stem
$(*F) - filename part of the stem


Tuesday, 2 June 2015

CheckInstall

CheckInstall keeps track of all the files created when installing from source ($ make install), builds a standard binary package and installs it using the system package management software (apt / yum etc), allowing you to later uninstall it

 tar -zxvf source-app.tar.gz;
 cd source/ ;
 ./configure;
 make;
 sudo checkinstall make install;

https://wiki.debian.org/CheckInstall