Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dockerfile to simplify fatpack for environments without perl #9

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM perl:5.20

RUN apt-get -y update
RUN apt-get -y upgrade
RUN cpanm Data::Compare

COPY src/ /app/src
COPY Makefile /app
RUN mkdir /app/library
WORKDIR /app

CMD [ "make" ]
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
.PHONY: all

all: ./library/copy.lua ./library/file.lua ./library/lineinfile.lua ./library/opkg.lua ./library/ping.lua ./library/slurp.lua ./library/stat.lua ./library/ubus.lua ./library/uci.lua
.PHONY: all

APP_NAME=fatpacker
WHITELIST=io,os,posix.,ubus
FATPACKARGS=--whitelist $(WHITELIST) --truncate

# LOCAL DEFAULT TASKS
all: ./library/copy.lua ./library/file.lua ./library/lineinfile.lua ./library/opkg.lua ./library/ping.lua ./library/service.lua ./library/slurp.lua ./library/stat.lua ./library/ubus.lua ./library/uci.lua

./library/%.lua : ./src/%.lua ./src/*.lua
./src/fatpack.pl --input $^ --output $(dir $@) $(FATPACKARGS)

# DOCKER TASKS
# Build the container
build: ## Build the container
docker build -t $(APP_NAME) .

run: ## Run container
docker run -i -t -v ${PWD}/library:/app/library --rm --name="$(APP_NAME)" $(APP_NAME)

19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ this project was born.

As the OpenWrt community seems to have a strange affection for lua, this
repository currently implements the following modules:
- [copy](https://docs.ansible.com/ansible/copy_module.html)
- [file](https://docs.ansible.com/ansible/file_module.html)
- [lineinfile](https://docs.ansible.com/ansible/lineinfile_module.html)
- [copy](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html)
- [file](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html)
- [lineinfile](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html)
- opkg
- [ping](https://docs.ansible.com/ansible/ping_module.html)
- [stat](https://docs.ansible.com/ansible/stat_module.html)
- [ping](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ping_module.html)
- [service](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_module.html)
- [stat](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/stat_module.html)
- ubus
- uci

Copy, file, lineinfile, stat, ping and opkg are mostly straightforward ports of the
Copy, file, lineinfile, stat, service, ping and opkg are mostly straightforward ports of the
official python modules included in the Ansible v2.1.1.0 release. However, there
were some simplifications made:
- selinux file attributes are not supported
Expand Down Expand Up @@ -98,6 +99,7 @@ For the following modules, please refer to the upstream documentation
- [lineinfile](https://docs.ansible.com/ansible/lineinfile_module.html)
- [opkg](https://docs.ansible.com/ansible/opkg_module.html)
- [ping](https://docs.ansible.com/ansible/ping_module.html)
- [service](https://docs.ansible.com/ansible/service_module.html)
- [stat](https://docs.ansible.com/ansible/stat_module.html)

## ubus module
Expand Down Expand Up @@ -232,6 +234,11 @@ An more complex example showing the usage of forcelist:
- uci commit
```

# Invoke
```
ansible-playbook playbook.yml
```

# Contributing

Give me all your pullrequests :) If you find a bug in one of the provided modules
Expand Down
10 changes: 10 additions & 0 deletions src/fileutils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ function FileUtil.islnk(path)
end
end

function FileUtil.isfile(path)
local pstat = stat.lstat(path)

if pstat then
return 0 ~= stat.S_ISREG(pstat['st_mode'])
else
return false
end
end

function FileUtil.stat(path)
return stat.stat(path)
end
Expand Down
96 changes: 96 additions & 0 deletions src/service.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/lua

local Ansible = require("ansible")
local File = require("fileutils")

function service_script(service)
return "/etc/init.d/" .. service
end

function service_exists(service)
return File.isfile(service_script(service))
end

function service_enabled(module, service)
local rc, out, err = service_command(module, service, "enabled")

return 0 == rc, out, err
end

function service_command(module, service, action)
return module:run_command(service_script(service) .. " " .. action)
end

function service_running(module, service)
if 0 == module:run_command("pgrep -P 1 " .. service) then
return true
end

return false
end

function main(arg)
local module = Ansible.new({
name = { required = true },
state = { choices = { "", "started", "stopped", "restarted", "reloaded" } },
enabled = { type = 'bool' },
})

module:parse(arg[1])

local p = module:get_params()

local service = p["name"]
local state = p["state"]
local enable = p["enabled"]

local changed = false
local msg = {}
local actions = {}

if (state == nil or state == "") and (enable == nil) then
module:fail_json({ msg = "at least one of 'state' and 'enabled' are required" })
end

if not service_exists(service) then
module:fail_json({ msg = string.format("service '%s' does not exist", service) })
end

if enable then
if not service_enabled(module, service) then
actions[#actions + 1] = "enable"
end
elseif enable == false then
if service_enabled(module, service) then
actions[#actions + 1] = "disable"
end
end

if state ~= nil then
local action = string.gsub(state, "p?ed$", "")

if not (("start" == action and service_running(module, service)) or ("stop" == action and not service_running(module, service))) then
actions[#actions + 1] = action
end
end

if #actions > 0 then
if not module:check_mode() then
for i = 1, #actions do
local rc, out, err = service_command(module, service, actions[i])

if rc ~= 0 then
module:fail_json({ msg = string.format("service '%s' has failed to %s", service, actions[i]),
service = { rc = rc, out = out, err = err } })
end

msg[#msg + 1] = string.format("executed '%s' command", actions[i])
end
end
changed = true
end

module:exit_json({ changed = changed, msg = table.concat(msg, "; ") })
end

main(arg)