ACF mvc.lua example

From Alpine Linux
Revision as of 03:10, 7 March 2016 by RuthHughes (talk | contribs) (minor updates)

Set the hostname with mvc.lua

In this example we will create a simple hostname-setting command-line application using mvc.lua. Once the controller/model are built, you can use the same code to set the hostname via the web with a web-based application controller.


For this example, we will assume you have root access on the linux box you are running on (preferably an Alpine Linux box!)

Get the mvc.lua module

Get the mvc.lua module from the git repository.

wget http://git.alpinelinux.org/cgit/acf-core/plain/lua/mvc.lua

Create a model and controller

Create a file hostname-controller.lua, defining the functions that an "end user" could run. We will only create one action, edithostname, which will be used to read and update the hostname. Since the action makes changes to the system, it naturally takes the form of a 'form':

hostname-controller.lua

-- Controller for editing hostname
local mymodule = {} 

mymodule.edithostname = function (self)
        return self.handle_form(self, self.model.get_hostname, self.model.set_hostname, self.clientdata, "Edit", "Edit Hostname", "Hostname Updated")
end                                   

return mymodule

Create a file hostname-model.lua, defining the model functions to get and set the hostname. We return a cfe table for each function including the form with the one entry for hostname:

hostname-model.lua

-- Model functions for retrieving / setting the hostname
local mymodule = {}

-- Create a cfe defining the form for editing the hostname and containing the current value
mymodule.get_hostname = function(self, clientdata)
        local retval = cfe({ type="group", value={}, label="Hostname" })

        -- Warning - io.popen has security risks, never pass user data to io.popen
        local f = io.popen ("/bin/hostname")
        local n = f:read("*a") or "none"
        f:close()
        n=string.gsub(n, "\n$", "")

        retval.value.hostname = cfe({ value=n, label="Hostname" })

        return retval
end

-- Set the hostname from the value contained in the cfe created by get_hostname
mymodule.set_hostname = function(self, hostnameform, action)
        local success = true

        -- Check to make sure the name is valid
        if (hostnameform.value.hostname.value == "") then
                success = false
                hostnameform.value.hostname.errtxt = "Hostname must not be blank"
        elseif (#hostnameform.value.hostname.value > 16) then
                success = false
                hostnameform.value.hostname.errtxt = "Hostname must be 16 characters or less"
        elseif (string.find(hostnameform.value.hostname.value, "[^%w%_%-]")) then
                success = false
                hostnameform.value.hostname.errtxt = "Hostname can contain alphanumerics only"
        end

        -- If it is valid, set the hostname
        if ( success ) then
                local f = io.open("/etc/hostname", "w")
                if f then
                        f:write(hostnameform.value.hostname.value .. "\n")
                        f:close()
                end
                -- Warning - io.popen has security risks, never pass user data to io.popen
                f = io.popen ("/bin/hostname -F /etc/hostname")
                f:close()
        else
                hostnameform.errtxt = "Failed to set hostname"
        end

        return hostnameform
end

return mymodule

Optionally test the model code (without mvc.lua)

If you want, you can create a test.lua script to validate the model code works on its own:

test.lua

require("mvc") -- Needed for cfe function definition
m=require("hostname-model")

local form = m.get_hostname()
form.value.hostname.value = arg[1] or ""
form = m.set_hostname(nil, form)

if form.errtxt then
        print("FAILED: "..form.value.hostname.errtxt or form.errtxt)
end
form = m.get_hostname()
print(form.value.hostname.value)

You can then test this with:

#lua test.lua "Alpine"
 Alpine
#lua test.lua "Invalid Name"
 FAILED: Hostname can contain alphanumerics only
 Alpine

Add the package to the ACF framework

To make the model and controller work within the ACF mvc.lua framework, we must do several things.

1. Install the ACF core package:

apk add acf-core

Optionally, you can add the entire web-based ACF framework:

setup-acf

2. Modify the ACF configuration file to look in /etc/acf/app/ for additional packages. Edit the /etc/acf/acf.conf file to add the /etc/acf/app/ directory to the appdir comma-separated list:

appdir=/etc/acf/app/,/usr/share/acf/app/

3. Move the model and controller to the new package directory. We will call the package "test":

mkdir -p /etc/acf/app/test
mv hostname-*.lua /etc/acf/app/test

4. Test the new package using the acf-cli application:

# acf-cli /test/hostname/edithostname
result = {}
result["label"] = "Edit Hostname"
result["option"] = "Edit"
result["type"] = "form"
result["value"] = {}
result["value"]["hostname"] = {}
result["value"]["hostname"]["label"] = "Hostname"
result["value"]["hostname"]["type"] = "text"
result["value"]["hostname"]["value"] = "Alpine"
# acf-cli /test/hostname/edithostname hostname=test submit=true
result = {}
result["descr"] = "Hostname Updated"
result["label"] = "Edit Hostname"
result["option"] = "Edit"
result["type"] = "form"
result["value"] = {}
result["value"]["hostname"] = {}
result["value"]["hostname"]["label"] = "Hostname"
result["value"]["hostname"]["type"] = "text"
result["value"]["hostname"]["value"] = "test"

Note that the first case read the existing hostname and the second case updated it. The output of the acf-cli application is a serialized version of the cfe form, which is good for testing but not too useful in real life.

Enable the package in the ACF web interface

1. Add the web-based ACF framework:

setup-acf

2. Configure all users to have access to the new hostname action. Edit "/etc/acf/app/test/hostname.roles" file and add GUEST permission for the edithostname action:

echo "GUEST=hostname/edithostname" > /etc/acf/app/test/hostname.roles

The new action should now be visible by browsing to https://IP-of-host/cgi-bin/acf/test/hostname/edithostname. Obviously you might want to reconsider providing GUEST access to this action, because this allows unauthenticated users to modify your hostname.

3. Add the new hostname action to the ACF menu. Edit "/etc/acf/app/test/hostname.menu" file and add a menu item:

echo "Test Hostname Edit edithostname" > /etc/acf/app/test/hostname.menu

You will need to log off from the ACF interface (or delete the session cookie) before the new menu item will be visible.

Make an MVC based application

You have two options for creating an MVC based application of your own.

1. Use the dispatch function. This is the method used by the web interface and the acf-cli applicat ... \n

private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

Offers private label cosmetics with custom design. We produce lip balms, serum, creams and more. Easy to buy with eshop. Private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

Offers private label cosmetics with custom design. We produce lip balms, serum, creams and more. Easy to buy with eshop. Private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

Offers private label cosmetics with custom design. We produce lip balms, serum, creams and more. Easy to buy with eshop. Private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

Offers private label cosmetics with custom design. We produce lip balms, serum, creams and more. Easy to buy with eshop. Private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

Offers private label cosmetics with custom design. We produce lip balms, serum, creams and more. Easy to buy with eshop. Private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]