rootfs-wip/src/backends.cr

62 lines
1.6 KiB
Crystal

require "./quadruplet.cr"
abstract class RootFS::Backend
class_getter name : String = ""
def name : String
@@name
end
# FIXME: Remove @application once `def execute` is moved in the current
# class.
def initialize(@directory : String, @application : ::RootFS::RootFS)
end
abstract def create!(architecture : String?, version : String?, variant : String?)
#abstract def add_package(name : String)
#abstract def remove_package(name : String)
class_property all = Array(::RootFS::Backend.class).new
macro register(name)
class ::RootFS::Backend::{{name.camelcase.id}} < ::RootFS::Backend
def self.name : String
{{name}}
end
{{yield}}
end
::RootFS::Backend.all << ::RootFS::Backend::{{name.camelcase.id}}
end
end
RootFS::Backend.register "debian" do
class_getter name : String = "debian"
def create!(architecture = nil, version = nil, variant = nil)
version = version || "buster"
architecture = architecture || "amd64"
@application.execute "debootstrap --arch '#{architecture}' '#{version}' '#{@directory}'"
end
end
RootFS::Backend.register "alpine" do
def create!(architecture = nil, version = nil, variant = nil)
version = version || "latest-stable"
if version == "latest"
version = "latest-stable"
elsif version == "latest-stable" || version == "edge"
else
version = "v" + version
end
arch = architecture || "x86_64"
@application.execute "apk --arch '#{arch}' -X http://dl-cdn.alpinelinux.org/alpine/#{version}/main/ -U --allow-untrusted --root '#{@directory}' --initdb add alpine-base"
end
end