From 609cd88eaf95b9aab09644ea339129ae33ebb4d5 Mon Sep 17 00:00:00 2001 From: Nikita Radchenko Date: Sun, 10 May 2020 15:10:24 +0300 Subject: [PATCH 1/4] Add service module --- Makefile | 2 +- README.md | 4 +- src/fileutils.lua | 10 +++++ src/service.lua | 96 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 src/service.lua diff --git a/Makefile b/Makefile index 9ad3287..03dcc76 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ .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 +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 WHITELIST=io,os,posix.,ubus FATPACKARGS=--whitelist $(WHITELIST) --truncate diff --git a/README.md b/README.md index 86e64e8..47eabc4 100644 --- a/README.md +++ b/README.md @@ -25,11 +25,12 @@ repository currently implements the following modules: - [lineinfile](https://docs.ansible.com/ansible/lineinfile_module.html) - opkg - [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 - 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 @@ -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 diff --git a/src/fileutils.lua b/src/fileutils.lua index 7ecb004..5503036 100644 --- a/src/fileutils.lua +++ b/src/fileutils.lua @@ -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 diff --git a/src/service.lua b/src/service.lua new file mode 100644 index 0000000..86f9c01 --- /dev/null +++ b/src/service.lua @@ -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) From 593194a3bb775abea5191413de515a79bdf64c09 Mon Sep 17 00:00:00 2001 From: Russell Leake Date: Sun, 26 Sep 2021 06:46:07 -0400 Subject: [PATCH 2/4] Add Dockerfile to simplify fatpack for environments without perl --- Dockerfile | 12 ++++++++++++ Makefile | 17 ++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a0a513f --- /dev/null +++ b/Dockerfile @@ -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" ] diff --git a/Makefile b/Makefile index 9ad3287..0248938 100644 --- a/Makefile +++ b/Makefile @@ -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/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/libary --rm --name="$(APP_NAME)" $(APP_NAME) + From 688ab0dc2afc5b6021e81a1d03055690227c3184 Mon Sep 17 00:00:00 2001 From: Russell Leake Date: Sun, 26 Sep 2021 07:15:20 -0400 Subject: [PATCH 3/4] Update Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0248938..36937c0 100644 --- a/Makefile +++ b/Makefile @@ -16,5 +16,5 @@ build: ## Build the container docker build -t $(APP_NAME) . run: ## Run container - docker run -i -t -v ${PWD}/library:/app/libary --rm --name="$(APP_NAME)" $(APP_NAME) + docker run -i -t -v ${PWD}/library:/app/library --rm --name="$(APP_NAME)" $(APP_NAME) From fbd05e62b78f0853ebdaa01c4a28fa692e4535d8 Mon Sep 17 00:00:00 2001 From: Russell Leake Date: Sat, 29 Jan 2022 13:57:53 -0500 Subject: [PATCH 4/4] Update README to correct URLs --- README.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 47eabc4..46bbd65 100644 --- a/README.md +++ b/README.md @@ -20,13 +20,13 @@ 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) -- [service](https://docs.ansible.com/ansible/service_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 @@ -234,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