Skip to content

Instantly share code, notes, and snippets.

@fujiwara
Created August 23, 2016 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujiwara/509622aaa166bcd782968e3321a7b20e to your computer and use it in GitHub Desktop.
Save fujiwara/509622aaa166bcd782968e3321a7b20e to your computer and use it in GitHub Desktop.
chef_recipe_to_stns_toml
require "toml"
class Group
def initialize(name)
@name = name
end
def gid(gid)
@gid = gid
end
def dump
puts "[groups.#{@name}]"
h = {
id: @gid
}
puts TOML::Generator.new(h).body
puts
end
end
class User
def initialize(name)
@name = name
begin
@keys = File.read("files/default/#{@name}.pubkey").split(/\r?\n/)
rescue
@keys = []
end
end
def uid(uid)
@uid = uid
end
def gid(gid)
@gid = gid
end
def comment(comment)
@comment = comment
end
def home(home)
@home = home
end
def shell(shell)
@shell = shell
end
def action(action)
@action = action
end
def available?
@action != :remove
end
def dump
h = {
id: @uid,
group_id: @gid,
directory: @home,
shell: @shell,
}
h[:keys] = @keys unless @keys.empty?
puts "[users.#{@name}]"
puts "# #{@comment}" if @comment != ""
puts TOML::Generator.new(h).body
puts ""
end
end
class Recipe
def initialize
end
def user(name, &block)
u = User.new(name)
u.instance_eval &block
u.dump if u.available?
end
def gem_package(name)
end
def directory(name, &block)
end
def include_recipe(name)
path = "recipes/" + name.split(/::/)[1] + ".rb"
self.instance_eval File.read(path)
end
def group(name, &block)
g = Group.new(name)
g.instance_eval &block
g.dump
end
def cookbook_file(path, &block)
end
end
$KEYS = {}
r = Recipe.new
r.instance_eval File.read(ARGV[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment