Sha256: 16e1f3003e417c438cadab090a0e4e2f252f2b8a05d525cdf41657b2e4007401

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

module Jendle
  class Job

    def initialize(core)
      @core = core
      @client = core.client
      @logger = core.logger
    end

    def restore(options, source_config)
      source_client = @core.get_client(
        source_config['server_ip'],
        source_config['username'],
        source_config['password']
      )
      get_config_pairs(source_client).each do |job_name, xml|
        apply_proc(job_name, xml, options[:'dry-run'])
      end
    end

    def export(options)
      File.write(options[:output], get_config_pairs.to_yaml)
      @logger.info("exported => #{options[:output]}")
    end

    def apply(options)
      jobs = YAML.load_file(options[:file])
      jobs.each do |job_name, xml|
        apply_proc(job_name, xml, options[:'dry-run'])
      end
    end

    private

    def get_config_pairs(client = @client)
      hash = {}
      client.job.list_all_with_details.each do |job|
        hash[job['name']] = client.job.get_config(job['name'])
      end
      hash
    end

    def apply_proc(job_name, xml, dryrun)
      before_xml = if @client.job.exists?(job_name)
        @client.job.get_config(job_name)
      else
        nil
      end

      if (before_xml == xml)
        @logger.info("no change => #{job_name}")
      else
        puts Diffy::Diff.new(before_xml, xml, :context => 3).to_s(:color)
        unless dryrun
          @client.job.create_or_update(job_name, xml)
        end
        @logger.info("applied => #{job_name}")
      end
      sleep 3
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jendle-0.1.0 lib/jendle/job.rb