Skip to content

Commit

Permalink
Fixes #17632: Translate packer configuration to lua
Browse files Browse the repository at this point in the history
  • Loading branch information
peckpeck committed Jun 4, 2020
1 parent 4508626 commit f366988
Show file tree
Hide file tree
Showing 28 changed files with 2,092 additions and 227 deletions.
8 changes: 6 additions & 2 deletions packer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ DESTDIR = $(CURDIR)/target
mkdir -p $(DESTDIR)/build
cp common/*.dhall $(DESTDIR)/build
cp systems/$@.dhall $(DESTDIR)/build/system.dhall
ifeq ($(CLOUD_TOKEN),)
# don't know ho to exclude data in dhall, so it's always in and we remot it when we don't want it
# sed -i -e 's/, vagrant_cloud//' $(DESTDIR)/build/template.dhall
endif
dhall-to-json --file $(DESTDIR)/build/template.dhall > $(DESTDIR)/result.json
packer validate $(DESTDIR)/result.json
packer build -on-error=abort target/result.json
packer validate -var cloud_token=$(CLOUD_TOKEN) $(DESTDIR)/result.json
# packer build -on-error=abort target/result.json

clean:
rm -rf target output-virtualbox-iso
Expand Down
6 changes: 5 additions & 1 deletion packer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ Small project to generate Packer files to maintain our vagrant cloud boxes.

## Dependencies

* `dhall-to-json` binary, from [https://dhall-lang.org/](https://dhall-lang.org/)
* `dhall-to-json` binary, from https://github.com/dhall-lang/dhall-haskell/releases (take only dhall-to-json binary)
* packer: https://packer.io/downloads.html
* virtualbox

## Build

Expand All @@ -16,4 +17,7 @@ make debian10
# Same with an agent
make debian10_agent
# To publish debian-10 on vagrant cloud
make debian10 CLOUD_TOKEN=xxx
```
Binary file added packer/common/.system.lua.swp
Binary file not shown.
81 changes: 0 additions & 81 deletions packer/common/functions.dhall

This file was deleted.

24 changes: 24 additions & 0 deletions packer/common/json.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--[[
Licensed according to the included 'LICENSE' document
Author: Thomas Harning Jr <harningt@gmail.com>
]]
local decode = require("json.decode")
local encode = require("json.encode")
local util = require("json.util")

local _G = _G

local _ENV = nil

local json = {
_VERSION = "1.3.4",
_DESCRIPTION = "LuaJSON : customizable JSON decoder/encoder",
_COPYRIGHT = "Copyright (c) 2007-2017 Thomas Harning Jr. <harningt@gmail.com>",
decode = decode,
encode = encode,
util = util
}

_G.json = json

return json
171 changes: 171 additions & 0 deletions packer/common/json/decode.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
--[[
Licensed according to the included 'LICENSE' document
Author: Thomas Harning Jr <harningt@gmail.com>
]]
local lpeg = require("lpeg")

local error = error
local pcall = pcall

local jsonutil = require("json.util")
local merge = jsonutil.merge
local util = require("json.decode.util")

local decode_state = require("json.decode.state")

local setmetatable, getmetatable = setmetatable, getmetatable
local assert = assert
local ipairs, pairs = ipairs, pairs
local string_char = require("string").char

local type = type

local require = require

local _ENV = nil

local modulesToLoad = {
"composite",
"strings",
"number",
"others"
}
local loadedModules = {
}

local json_decode = {}

json_decode.default = {
unicodeWhitespace = true,
initialObject = false,
nothrow = false
}

local modes_defined = { "default", "strict", "simple" }

json_decode.simple = {}

json_decode.strict = {
unicodeWhitespace = true,
initialObject = true,
nothrow = false
}

for _,name in ipairs(modulesToLoad) do
local mod = require("json.decode." .. name)
if mod.mergeOptions then
for _, mode in pairs(modes_defined) do
mod.mergeOptions(json_decode[mode], mode)
end
end
loadedModules[#loadedModules + 1] = mod
end

-- Shift over default into defaultOptions to permit build optimization
local defaultOptions = json_decode.default
json_decode.default = nil

local function generateDecoder(lexer, options)
-- Marker to permit detection of final end
local marker = {}
local parser = lpeg.Ct((options.ignored * lexer)^0 * lpeg.Cc(marker)) * options.ignored * (lpeg.P(-1) + util.unexpected())
local decoder = function(data)
local state = decode_state.create(options)
local parsed = parser:match(data)
assert(parsed, "Invalid JSON data")
local i = 0
while true do
i = i + 1
local item = parsed[i]
if item == marker then break end
if type(item) == 'function' and item ~= jsonutil.undefined and item ~= jsonutil.null then
item(state)
else
state:set_value(item)
end
end
if options.initialObject then
assert(type(state.previous) == 'table', "Initial value not an object or array")
end
-- Make sure stack is empty
assert(state.i == 0, "Unclosed elements present")
return state.previous
end
if options.nothrow then
return function(data)
local status, rv = pcall(decoder, data)
if status then
return rv
else
return nil, rv
end
end
end
return decoder
end

local function buildDecoder(mode)
mode = mode and merge({}, defaultOptions, mode) or defaultOptions
for _, mod in ipairs(loadedModules) do
if mod.mergeOptions then
mod.mergeOptions(mode)
end
end
local ignored = mode.unicodeWhitespace and util.unicode_ignored or util.ascii_ignored
-- Store 'ignored' in the global options table
mode.ignored = ignored

--local grammar = {
-- [1] = mode.initialObject and (ignored * (object_type + array_type)) or value_type
--}
local lexer
for _, mod in ipairs(loadedModules) do
local new_lexer = mod.generateLexer(mode)
lexer = lexer and lexer + new_lexer or new_lexer
end
return generateDecoder(lexer, mode)
end

-- Since 'default' is nil, we cannot take map it
local defaultDecoder = buildDecoder(json_decode.default)
local prebuilt_decoders = {}
for _, mode in pairs(modes_defined) do
if json_decode[mode] ~= nil then
prebuilt_decoders[json_decode[mode]] = buildDecoder(json_decode[mode])
end
end

--[[
Options:
number => number decode options
string => string decode options
array => array decode options
object => object decode options
initialObject => whether or not to require the initial object to be a table/array
allowUndefined => whether or not to allow undefined values
]]
local function getDecoder(mode)
mode = mode == true and json_decode.strict or mode or json_decode.default
local decoder = mode == nil and defaultDecoder or prebuilt_decoders[mode]
if decoder then
return decoder
end
return buildDecoder(mode)
end

local function decode(data, mode)
local decoder = getDecoder(mode)
return decoder(data)
end

local mt = {}
mt.__call = function(self, ...)
return decode(...)
end

json_decode.getDecoder = getDecoder
json_decode.decode = decode
json_decode.util = util
setmetatable(json_decode, mt)

return json_decode

0 comments on commit f366988

Please sign in to comment.