Class: Zerg::Hive

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/zerg/hive.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hiveObject (readonly)

Returns the value of attribute hive.



35
36
37
# File 'lib/zerg/hive.rb', line 35

def hive
  @hive
end

#load_pathObject (readonly)

Returns the value of attribute load_path.



35
36
37
# File 'lib/zerg/hive.rb', line 35

def load_path
  @load_path
end

Class Method Details

.copy_with_path(src, dst) ⇒ Object



64
65
66
67
# File 'lib/zerg/hive.rb', line 64

def self.copy_with_path(src, dst)
    FileUtils.mkdir_p(File.dirname(dst))
    FileUtils.cp(src, dst)
end

.import(file, force) ⇒ Object



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
# File 'lib/zerg/hive.rb', line 125

def self.import(file, force)
    instance.load
    abort("ERROR: '#{file}' not found!") unless File.exist?(file) 
    abort("ERROR: '#{File.basename(file)}' already exists in hive!") unless !File.directory?(File.join(instance.load_path, File.basename(file))) || force == true

    # check the file against schema.
    begin

        ke_file_hash = JSON.parse( File.open(file, 'r').read )

        # get the tasks schema piece from the driver
        pmgr = ZergGemPlugin::Manager.instance
        pmgr.load
        abort("ERROR: 'drivertype' is missing from #{ke_file}") unless ke_file_hash["vm"]["driver"]["drivertype"] != nil
        driver = pmgr.create("/driver/#{ke_file_hash["vm"]["driver"]["drivertype"]}")
        schema_template = File.open(File.join("#{File.dirname(__FILE__)}", "..", "..", "data", "ke.schema"), 'r').read
        sources = {
            :driver_tasks_schema => driver.task_schema,
            :driver_options_schema => driver.option_schema,
            :driver_folders_schema => driver.folder_schema,
            :driver_ports_schema => driver.port_schema,
            :driver_ssh_schema => driver.ssh_schema
        }
        full_schema = JSON.parse(Erbalize.erbalize_hash(schema_template, sources))

        errors = JSON::Validator.fully_validate(full_schema, ke_file_hash, :errors_as_objects => true)
        abort("ERROR: #{file} failed validation. Errors: #{errors.ai}") unless errors.empty?

        # copy the .ke file
        copy_with_path(file, File.join(instance.load_path, File.basename(file, ".*"), File.basename(file)))

        # copy additonal files
        if ke_file_hash["vm"]["additional_files"] != nil
            ke_file_hash["vm"]["additional_files"].each { |additonal_file|
                additonal_file_path = additonal_file["from"]
                
                # eval possible environment variables
                if additonal_file_path =~ /^ENV\['.+'\]$/
                    additonal_file_path = eval(additonal_file_path)
                end

                # determine if a relative path or a full path is provided. relative path should be relative to 
                # location of the .ke file
                abort("ERROR: #{additonal_file.ai} is missing a file path") unless additonal_file_path != nil
                additonal_file_path = ((Pathname.new additonal_file_path).absolute?) ? additonal_file_path : File.join(File.dirname(file), additonal_file_path)
                abort("ERROR: '#{additonal_file_path}' not found!") unless File.exist?(additonal_file_path) 
                copy_with_path(additonal_file_path, File.join(instance.load_path, File.basename(file, ".*"), additonal_file["to"]))
            }
        end 
    rescue JSON::ParserError => err
        abort("ERROR: Could not parse #{file}. Likely invalid JSON.")
    end
    puts "SUCCESS!"
end

.listObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/zerg/hive.rb', line 69

def self.list
    instance.load

    # iterate over hive configs and print out the names
    puts  "Current hive tasks are:"

    if instance.loaded == false
        puts "No hive loaded!"
        puts "FAILURE!"
        return
    end

    if instance.hive.empty?()
        puts "No tasks defined in hive."
        return
    end

    puts "#{instance.hive.length} tasks in current hive:"
    puts "#{instance.hive.keys.ai}"
end

.remove(taskname, force) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/zerg/hive.rb', line 180

def self.remove(taskname, force)
    instance.load 
    abort("ERROR: '#{taskname}' not found!") unless File.directory?(File.join(instance.load_path, "#{taskname}")) 

    agreed = true
    if force != true
        agreed = agree("Remove task #{taskname}?")
    end

    abort("Cancelled!") unless agreed == true

    FileUtils.rm_rf(File.join(instance.load_path, "driver", taskname))
    FileUtils.rm_rf(File.join(instance.load_path, "#{taskname}"))

    puts "SUCCESS!"
end

.verifyObject



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
# File 'lib/zerg/hive.rb', line 90

def self.verify
    instance.load

    Dir["#{File.join("#{instance.load_path}", "**/*.ke")}"].each { |ke_file|
        begin 
            ke_file_hash = JSON.parse( File.open(ke_file, 'r').read )

            # verify against schema.
            # first get the tasks schema piece from the driver
            pmgr = ZergGemPlugin::Manager.instance
            pmgr.load
            abort("ERROR: 'drivertype' is missing from #{ke_file}") unless ke_file_hash["vm"]["driver"]["drivertype"] != nil
            driver = pmgr.create("/driver/#{ke_file_hash["vm"]["driver"]["drivertype"]}")
            schema_template = File.open(File.join("#{File.dirname(__FILE__)}", "..", "..", "data", "ke.schema"), 'r').read
            sources = {
                :driver_tasks_schema => driver.task_schema,
                :driver_options_schema => driver.option_schema,
                :driver_folders_schema => driver.folder_schema,
                :driver_ports_schema => driver.port_schema,
                :driver_ssh_schema => driver.ssh_schema
            }
            full_schema = JSON.parse(Erbalize.erbalize_hash(schema_template, sources))

            errors = JSON::Validator.fully_validate(full_schema, ke_file_hash, :errors_as_objects => true)
            abort("ERROR: #{ke_file} failed validation. Errors: #{errors.ai}") unless errors.empty?
        rescue JSON::ParserError => err
            abort("ERROR: Could not parse #{ke_file}. Likely invalid JSON.")
        rescue ZergGemPlugin::PluginNotLoaded
            abort("ERROR: driver #{ke_file_hash["vm"]["driver"]["drivertype"]} not found. Did you install the plugin gem?")
        end
    }

    puts "SUCCESS!"
end

Instance Method Details

#loadObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/zerg/hive.rb', line 41

def load
    if loaded
        return
    end

    @load_path = (ENV['HIVE_CWD'] == nil) ? File.join("#{Dir.pwd}", ".hive") : File.join("#{ENV['HIVE_CWD']}", ".hive")
    abort("ERROR: '.hive' not found at #{@load_path}. Run 'zerg init', change HIVE_CWD or run zerg from a different path.") unless File.directory?(@load_path) 

    # load all .ke files into one big hash
    @hive = Hash.new
    Dir["#{File.join(@load_path, "**/*.ke")}"].each { |ke_file|
        # do work on files ending in .rb in the desired directory
        begin 
            ke_file_hash = JSON.parse( IO.read(ke_file) )
            @hive[File.basename(ke_file, ".ke")] = ke_file_hash
        rescue JSON::ParserError
            abort("ERROR: Could not parse #{ke_file}. Likely invalid JSON.")
        end
    }

    @loaded = true
end

#loadedObject



37
38
39
# File 'lib/zerg/hive.rb', line 37

def loaded
    @loaded || false
end