Skip to content

Commit

Permalink
Add package cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
j8r committed Sep 20, 2018
1 parent 7552e08 commit 0bbf44a
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 28 deletions.
12 changes: 8 additions & 4 deletions src/cli.cr
Expand Up @@ -6,6 +6,9 @@ require "./logger"
require "./service"
require "./system"

CONFIG_FILE = "./config.ini"
PREFIX = (::System::Owner.root? ? "/opt" : ENV["HOME"]) + "/dppm"

module CLI
extend self
include Clicr
Expand Down Expand Up @@ -128,10 +131,11 @@ module CLI
arguments: %w(package custom_vars...),
action: "Manager::Package::CLI.build",
},
# clean: {
# info: "Clean built packages not used by any applications",
# action: "::Manager::Cli.clean",
# },
clean: {
alias: 'c',
info: "Clean unused built packages by the applications",
action: "::Manager::Package::CLI.clean",
},
delete: {
alias: 'd',
info: "Delete a built package",
Expand Down
2 changes: 1 addition & 1 deletion src/manager/application.cr
Expand Up @@ -2,6 +2,6 @@ require "./application/*"

module Manager::Application
def self.logs(application, prefix = PREFIX, error = false)
File.read "#{prefix}/#{application}/#{error ? LOG_ERROR_PATH : LOG_OUTPUT_PATH}"
File.read "#{Path.new(prefix).app}/#{application}/#{error ? LOG_ERROR_PATH : LOG_OUTPUT_PATH}"
end
end
2 changes: 1 addition & 1 deletion src/manager/application/add.cr
Expand Up @@ -189,7 +189,7 @@ struct Manager::Application::Add

if ::System::Owner.root?
# Set the user and group owner
::System::Owner.add_user(@vars["uid"], @vars["user"], @pkg["description"]) if @add_user
::System::Owner.add_user(@vars["uid"], @vars["user"], @pkg["description"], @pkgdir + "/srv") if @add_user
::System::Owner.add_group(@vars["gid"], @vars["group"]) if @add_group
Utils.chown_r @pkgdir, @vars["uid"].to_i, @vars["gid"].to_i
end
Expand Down
1 change: 0 additions & 1 deletion src/manager/application/delete.cr
Expand Up @@ -41,7 +41,6 @@ struct Manager::Application::Delete
String.build do |str|
str << "\nname: " << @name
str << "\npackage: " << @package
str << "\nprefix: " << @prefix
str << "\npkgdir: " << @pkgdir
str << "\nuser: " << @user
str << "\ngroup: " << @group
Expand Down
2 changes: 1 addition & 1 deletion src/manager/list.cr
@@ -1,5 +1,5 @@
struct Manager::List
@path : Path
getter path : Path

def initialize(prefix)
@path = Path.new prefix
Expand Down
41 changes: 41 additions & 0 deletions src/manager/package/clean.cr
@@ -0,0 +1,41 @@
struct Manager::Package::Clean
getter prefix : String,
pkgdir : String,
packages = Set(String).new

def initialize(@prefix)
list = List.new @prefix
@pkgdir = list.path.pkg
Log.info "retrieving available packages", @pkgdir
list.pkg { |pkg| @packages << pkg }
Log.info "excluding used packages by applications", list.path.app
list.app do |app|
app_path = list.path.app + '/' + app
@packages.delete File.basename(File.real_path(app_path + "/app").rstrip("app"))
lib_path = app_path + "/lib/"
Dir.each_child lib_path do |lib_package|
@packages.delete File.basename(File.real_path lib_path + lib_package)
end
end
end

def simulate
String.build do |str|
str << "\npkgdir: " << @pkgdir
str << "\nunused packages: \n"
@packages.each do |pkg|
str << pkg << '\n'
end
end
end

def run
Log.info "deleting packages", @pkgdir
@packages.each do |pkg|
path = @pkgdir + '/' + pkg
FileUtils.rm_rf path
Log.info "package deleted", pkg
end
Log.info "packages cleaned", @pkgdir
end
end
13 changes: 12 additions & 1 deletion src/manager/package/cli.cr
@@ -1,9 +1,20 @@
module Manager::Package::CLI
extend self

def clean(no_confirm, config, mirror, pkgsrc, prefix)
Log.info "initializing", "clean"
task = Clean.new prefix
if task.packages.empty?
Log.info "No packages to clean", task.pkgdir
exit
end

Log.info "clean", task.simulate
task.run if no_confirm || ::CLI.confirm
end

def delete(no_confirm, config, mirror, pkgsrc, prefix, package, custom_vars)
Log.info "initializing", "delete"

task = Delete.new package, prefix

Log.info "delete", task.simulate
Expand Down
12 changes: 3 additions & 9 deletions src/manager/package/delete.cr
Expand Up @@ -3,27 +3,21 @@ struct Manager::Package::Delete
package : String,
pkgdir : String,
prefix : String,
version : String,
pkg : YAML::Any
version : String

def initialize(@package, @prefix)
@path = Path.new @prefix
@name, @version = @package.split '_'
@pkgdir = @path.pkg + '/' + @package
@pkgdir = Path.new(@prefix).pkg + '/' + package

# Checks
Manager.pkg_exists? @pkgdir
Log.info "getting package name", @pkgdir + "/pkg.yml"
@pkg = YAML.parse(File.read(@pkgdir + "/pkg.yml"))
@package = @pkg["package"].as_s
@name, @version = package.split '_'
end

def simulate
String.build do |str|
str << "\npackage: " << @package
str << "\nname: " << @name
str << "\nversion: " << @version
str << "\nprefix: " << @prefix
str << "\npkgdir: " << @pkgdir
end
end
Expand Down
6 changes: 0 additions & 6 deletions src/path.cr
@@ -1,9 +1,3 @@
# Global constant variables
CONFIG_FILE = "./config.ini"
PREFIX = (::System::Owner.root? ? "/opt" : ENV["HOME"]) + "/dppm"
LOG_OUTPUT_PATH = "log/output.log"
LOG_ERROR_PATH = "log/error.log"

struct Path
getter app : String
getter pkg : String
Expand Down
5 changes: 4 additions & 1 deletion src/service.cr
@@ -1,4 +1,7 @@
require "./service/**"
require "./service/*"

LOG_OUTPUT_PATH = "log/output.log"
LOG_ERROR_PATH = "log/error.log"

module Service
end
3 changes: 3 additions & 0 deletions src/service/openrc.cr
@@ -1,3 +1,6 @@
require "./system"
require "./openrc/*"

module Service::OpenRC
extend self
include Cli
Expand Down
3 changes: 3 additions & 0 deletions src/service/systemd.cr
@@ -1,3 +1,6 @@
require "./system"
require "./systemd/*"

module Service::Systemd
extend self
include Cli
Expand Down
6 changes: 3 additions & 3 deletions src/system/owner.cr
Expand Up @@ -72,7 +72,7 @@ module System::Owner
end

def available_id
id = 1000
id = 2000
uids = all_uids
gids = all_gids
(id..65535).each do |id|
Expand All @@ -81,8 +81,8 @@ module System::Owner
raise "the limit of 65535 for id numbers is reached, no ids available"
end

def add_user(id, name, description, shell = @@nologin)
File.open "/etc/passwd", "a", &.puts "#{name}:x:#{id}:#{id}:#{description}:/:#{@@nologin}"
def add_user(id, name, description, home_directory = "/", shell = @@nologin)
File.open "/etc/passwd", "a", &.puts "#{name}:x:#{id}:#{id}:#{description}:#{home_directory}:#{@@nologin}"
end

def add_group(id, name)
Expand Down

0 comments on commit 0bbf44a

Please sign in to comment.