summaryrefslogtreecommitdiffstats
path: root/bin/cf2puppet
blob: 8253d83947bc21c65fb58a31075da303fbfad61b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/ruby

#
# = Synopsis
#
# Convert cfengine code to puppet code.
#
# = Usage
#
#   cf2puppet [-h|--help] -o|--out <dir> <cfengine file>
#
# = Description
#
# This script reads in an entire cfengine configuration set, including
# importing necessary files, and converts it to a puppet configuration.
#
# = Options
#
# help::
#   Print this help message
#
# out::
#   Print this help message
#
# = Example
#
#   $ puppetdoc > /tmp/reference.rst
#
# = Author
#
# Luke Kanies
#
# = Copyright
#
# Copyright (c) 2005 Reductive Labs, LLC
# Licensed under the GNU Public License

require 'puppet'
require 'getoptlong'


module Cf2Puppet
    class CfClass < Array
        attr_accessor :name
    end

    class CfAction
        attr_accessor :name, :type

        def []=(param, value)
            @params[param] = value
        end

        def initialize
            @params = {}
        end
    end

    class Parser
        def initialize(file)
            @file = file
            @dir = File.dirname(file)

            unless FileTest.exists?(file)
                $stderr.puts "%s does not exist" % file
                exit(18)
            end
        end

        def parse
            begin
                File.open(@file) { |f|
                    str = f.read

                    # get rid of comments
                    str.gsub(/#.+\n/)
                    str.gsub(/^\s*$/, '') # and blank lines

                    while str do
                        case str
                        when /\A(\w+):[^:]/n:
                            action = $1
                        end
                    end
                    f.foreach { |line|
                        case line.chomp
                        when /(\w+):\s*\n/:
                            $action = $1
                        when /(\w+):\s*\n/:
                            $action = $1
                        end
                    }
                }
            rescue Errno::ENOENT => detail
                $stderr.puts "File %s not found" % file
                return
            rescue Errno::EACCES => detail
                $stderr.puts "Could not open file %s" % file
                return
            end
        end

        module Actions
            def import
            end
        end
    end
end

$haveusage = true

begin
    require 'rdoc/usage'
rescue LoadError
    $haveusage = false
end

result = GetoptLong.new(
	[ "--help",		"-h",			GetoptLong::NO_ARGUMENT ]
)

out = nil

begin
    result.each { |opt,arg|
        case opt
        when "--out"
            out = arg
        when "--help"
            if $haveusage
                RDoc::usage && exit
            else
                puts "No help available unless you have RDoc::usage installed"
                exit
            end
        end
    }
rescue GetoptLong::InvalidOption => detail
    $stderr.puts "Try '#{$0} --help'"
    #if $haveusage
    #    RDoc::usage_no_exit('usage')
    #end
    exit(1)
end

unless out
    puts "You must specify an output directory using '-o'."
    exit(12)
end

if FileTest.exists?(out)
    unless FileTest.directory?(out)
        puts "%s is not a directory" % out
        exit(14)
    end
else
    basedir = File.dirname(out)

    unless FileTest.directory?(basedir)
        puts "Parent directory %s does not exist" % basedir
        exit(16)
    end

    Dir.mkdir(out)
end

files = []
if ARGV.length > 0
    files += ARGV
else
    $stderr.puts "Defaulting to cfagent.conf"
    files << "/var/cfengine/inputs/cfagent.conf"
end

$stderr.puts "****WARNING****
I can absolutely guarantee you that this script will not yet produce
an exact copy of your cfengine configuration.  You _must_not_ just run
the generated configuration; check the entire configuration before
executing.  This is meant as a tool for simplifying migration, not
entirely performing it.
****WARNING****"

files.each { |file|
    handle(file)
}
# $Id$