Skip to content

Commit

Permalink
Merge branch 'master' into custom_button_import
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u authored Aug 1, 2018
2 parents 12083c1 + 3777928 commit 8692d8c
Show file tree
Hide file tree
Showing 26 changed files with 2,628 additions and 72 deletions.
1 change: 1 addition & 0 deletions app/models/chargeable_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def self.cols_on_metric_rollup

def self.col_index(column)
@rate_cols ||= {}
column = VIRTUAL_COL_USES[column] || column
@rate_cols[column] ||= cols_on_metric_rollup.index(column.to_s)
end

Expand Down
1 change: 0 additions & 1 deletion app/models/chargeback/consumption_with_rollups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def tag_list_with_prefix
end

def sum(metric, sub_metric = nil)
metric = ChargeableField::VIRTUAL_COL_USES[metric] || metric
values(metric, sub_metric).sum
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class ManageIQ::Providers::EmbeddedAnsible::AutomationManager < ManageIQ::Provid
require_nested :Refresher
require_nested :RefreshWorker

has_many :configuration_workflows, :dependent => :destroy, :foreign_key => "manager_id", :inverse_of => :manager

def self.ems_type
@ems_type ||= "embedded_ansible_automation".freeze
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ def configuration_script_sources
add_common_default_values
end

def configuration_workflows
default_manager_ref
add_common_default_values
end

def configured_systems
default_manager_ref
add_common_default_values
Expand Down
37 changes: 16 additions & 21 deletions app/models/miq_schedule.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class MiqSchedule < ApplicationRecord
include ReservedMixin
reserve_attribute :resource_id, :big_integer
include DeprecationMixin
deprecate_attribute :towhat, :resource_type

validates :name, :uniqueness => {:scope => [:userid, :towhat]}
validates :name, :description, :towhat, :run_at, :presence => true
validates :name, :uniqueness => {:scope => [:userid, :resource_type]}
validates :name, :description, :resource_type, :run_at, :presence => true
validate :validate_run_at, :validate_file_depot

before_save :set_start_time_and_prod_default
Expand All @@ -14,6 +14,7 @@ class MiqSchedule < ApplicationRecord

belongs_to :file_depot
belongs_to :miq_search
belongs_to :resource, :polymorphic => true
belongs_to :zone

scope :in_zone, lambda { |zone_name|
Expand All @@ -24,11 +25,11 @@ class MiqSchedule < ApplicationRecord
where("updated_at > ?", time)
}

scope :filter_matches_with, ->(exp) { where(:filter => exp) }
scope :with_prod_default_not_in, ->(prod) { where.not(:prod_default => prod).or(where(:prod_default => nil)) }
scope :without_adhoc, -> { where(:adhoc => nil) }
scope :with_towhat, ->(towhat) { where(:towhat => towhat) }
scope :with_userid, ->(userid) { where(:userid => userid) }
scope :filter_matches_with, ->(exp) { where(:filter => exp) }
scope :with_prod_default_not_in, ->(prod) { where.not(:prod_default => prod).or(where(:prod_default => nil)) }
scope :without_adhoc, -> { where(:adhoc => nil) }
scope :with_towhat, ->(resource_type) { where(:resource_type => resource_type) }
scope :with_userid, ->(userid) { where(:userid => userid) }

serialize :sched_action
serialize :filter
Expand All @@ -42,15 +43,9 @@ class MiqSchedule < ApplicationRecord
default_value_for :enabled, true
default_value_for(:zone_id) { MiqServer.my_server.zone_id }

def resource
# HACK: this should be a real relation, but for now it's using a reserve_attribute for backport reasons
return unless resource_id
towhat.safe_constantize.find_by(:id => resource_id)
end

def set_start_time_and_prod_default
run_at # Internally this will correct :start_time to UTC
self.prod_default = "system" if SYSTEM_SCHEDULE_CLASSES.include?(towhat.to_s)
self.prod_default = "system" if SYSTEM_SCHEDULE_CLASSES.include?(resource_type.to_s)
end

def run_at
Expand Down Expand Up @@ -78,7 +73,7 @@ def self.queue_scheduled_work(id, _rufus_job_id, at, _params)
end

method = sched.sched_action[:method] rescue nil
_log.info("Queueing start of schedule id: [#{id}] [#{sched.name}] [#{sched.towhat}] [#{method}]")
_log.info("Queueing start of schedule id: [#{id}] [#{sched.name}] [#{sched.resource_type}] [#{method}]")

action = "action_" + method

Expand All @@ -91,7 +86,7 @@ def self.queue_scheduled_work(id, _rufus_job_id, at, _params)
:msg_timeout => 1200
)

_log.info("Queueing start of schedule id: [#{id}] [#{sched.name}] [#{sched.towhat}] [#{method}]...complete")
_log.info("Queueing start of schedule id: [#{id}] [#{sched.name}] [#{sched.resource_type}] [#{method}]...complete")
msg
elsif sched.resource.respond_to?(method)
sched.resource.send(method, *sched.sched_action[:args])
Expand Down Expand Up @@ -127,20 +122,20 @@ def target_ids
# Let RBAC evaluate the filter's MiqExpression, and return the first value (the target ids)
my_filter = get_filter
return [] if my_filter.nil?
Rbac.filtered(towhat, :filter => my_filter).pluck(:id)
Rbac.filtered(resource_type, :filter => my_filter).pluck(:id)
end

def get_targets
# TODO: Add support to invoke_actions, get_targets, and get_filter to call class methods in addition to the normal instance methods
return [Object.const_get(towhat)] if sched_action.kind_of?(Hash) && ALLOWED_CLASS_METHOD_ACTIONS.include?(sched_action[:method])
return [Object.const_get(resource_type)] if sched_action.kind_of?(Hash) && ALLOWED_CLASS_METHOD_ACTIONS.include?(sched_action[:method])

my_filter = get_filter
if my_filter.nil?
_log.warn("[#{name}] Filter is empty")
return []
end

Rbac.filtered(towhat, :filter => my_filter)
Rbac.filtered(resource_type, :filter => my_filter)
end

def get_filter
Expand Down
12 changes: 6 additions & 6 deletions app/models/miq_widget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,12 @@ def sync_schedule(schedule_info)
end

sched = MiqSchedule.create!(
:name => description,
:description => description,
:sched_action => {:method => "generate_widget"},
:filter => MiqExpression.new("=" => {"field" => "MiqWidget-id", "value" => id}),
:towhat => self.class.name,
:run_at => {
:name => description,
:description => description,
:sched_action => {:method => "generate_widget"},
:filter => MiqExpression.new("=" => {"field" => "MiqWidget-id", "value" => id}),
:resource_type => self.class.name,
:run_at => {
:interval => {:value => value, :unit => unit},
:tz => server_tz,
:start_time => sched_time
Expand Down
1 change: 1 addition & 0 deletions app/models/mixins/supports_feature_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module SupportsFeatureMixin
:external_logging => 'Launch External Logging UI',
:swift_service => 'Swift storage service',
:delete => 'Deletion',
:destroy => 'Destroy',
:delete_aggregate => 'Host Aggregate Deletion',
:delete_floating_ip => 'Floating IP Deletion',
:delete_security_group => 'Security Group Deletion',
Expand Down
9 changes: 2 additions & 7 deletions app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class ServiceTemplate < ApplicationRecord
belongs_to :service_template_catalog

has_many :dialogs, -> { distinct }, :through => :resource_actions
has_many :miq_schedules, :as => :resource, :dependent => :destroy

has_many :miq_requests, :as => :source, :dependent => :nullify
has_many :active_requests, -> { where(:request_state => MiqRequest::ACTIVE_STATES) }, :as => :source, :class_name => "MiqRequest"
Expand Down Expand Up @@ -394,11 +395,6 @@ def queue_order(user_id, options, request_options)
)
end

def miq_schedules
schedule_ids = Reserve.where(:resource_type => "MiqSchedule").collect { |r| r.resource_id if r.reserved == {:resource_id => id} }.compact
MiqSchedule.where(:towhat => "ServiceTemplate", :id => schedule_ids)
end

def order(user_or_id, options = nil, request_options = nil, schedule_time = nil)
user = user_or_id.kind_of?(User) ? user_or_id : User.find(user_or_id)
workflow = provision_workflow(user, options, request_options)
Expand All @@ -413,8 +409,7 @@ def order(user_or_id, options = nil, request_options = nil, schedule_time = nil)
:name => "Order #{self.class.name} #{id} at #{time}",
:description => "Order #{self.class.name} #{id} at #{time}",
:sched_action => {:args => [user.id, options, request_options], :method => "queue_order"},
:resource_id => id,
:towhat => "ServiceTemplate",
:resource => self,
:run_at => {
:interval => {:unit => "once"},
:start_time => time,
Expand Down
1 change: 1 addition & 0 deletions app/models/vm_or_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,7 @@ def self.includes_template?(ids)
end

supports_not :snapshots
supports :destroy

# Stop showing Reconfigure VM task unless the subclass allows
def reconfigurable?
Expand Down
4 changes: 2 additions & 2 deletions db/fixtures/miq_schedules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
:value: 1
:sched_action:
:method: vm_scan
:towhat: Vm
:resource_type: Vm
- :attributes:
:name: "Sample: Hourly VM Analysis"
:userid: system
Expand All @@ -40,4 +40,4 @@
:value: 1
:sched_action:
:method: vm_scan
:towhat: Vm
:resource_type: Vm
4 changes: 2 additions & 2 deletions lib/ansible/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ def run_via_cli(env_vars, extra_vars, playbook_path: nil, role_name: nil, roles_
:roles_path => roles_path,
:role_skip_facts => role_skip_facts))

response(base_dir, ansible_runner_method, result)
res = response(base_dir, ansible_runner_method, result)
ensure
# Clean up the tmp dir for the sync method, for async we will clean it up after the job is finished and we've
# read the output, that will be written into this directory.
FileUtils.remove_entry(base_dir) unless async?(ansible_runner_method)
res.cleanup_filesystem! unless async?(ansible_runner_method)
end
end

Expand Down
22 changes: 22 additions & 0 deletions lib/task_helpers/exports/provision_dialogs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module TaskHelpers
class Exports
class ProvisionDialogs
def export(options = {})
export_dir = options[:directory]

dialogs = options[:all] ? MiqDialog.all : MiqDialog.where(:default => [false, nil])

dialogs.order(:id).to_a.collect! do |dialog|
Exports.exclude_attributes(dialog.to_model_hash, %i(file_mtime created_at updated_at id class))
end

dialogs.each do |dialog|
$log.info("Exporting Provision Dialog: #{dialog[:name]} (#{dialog[:description]})")

fname = Exports.safe_filename("#{dialog[:dialog_type]}-#{dialog[:name]}", options[:keep_spaces])
File.write("#{export_dir}/#{fname}.yaml", dialog.to_yaml)
end
end
end
end
end
20 changes: 20 additions & 0 deletions lib/task_helpers/imports/provision_dialogs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module TaskHelpers
class Imports
class ProvisionDialogs
def import(options = {})
return unless options[:source]

glob = File.file?(options[:source]) ? options[:source] : "#{options[:source]}/*.yaml"
Dir.glob(glob) do |fname|
$log.info("Importing Provision Dialog from: #{fname}")

dialog = YAML.load_file(fname)

miq_dialog = MiqDialog.find_by(:name => dialog[:name], :dialog_type => dialog[:dialog_type])

miq_dialog.nil? ? MiqDialog.create(dialog) : miq_dialog.update(dialog)
end
end
end
end
end
17 changes: 17 additions & 0 deletions lib/tasks/evm_export_import.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# * Roles
# * Tags
# * Service Dialogs
# * Provision Dialogs

namespace :evm do
namespace :export do
Expand Down Expand Up @@ -76,6 +77,14 @@ namespace :evm do

exit # exit so that parameters to the first rake task are not run as rake tasks
end

desc 'Exports all provision dialogs to individual YAML files'
task :provision_dialogs => :environment do
options = TaskHelpers::Exports.parse_options
TaskHelpers::Exports::ProvisionDialogs.new.export(options)

exit # exit so that parameters to the first rake task are not run as rake tasks
end
end

namespace :import do
Expand Down Expand Up @@ -148,5 +157,13 @@ namespace :evm do

exit # exit so that parameters to the first rake task are not run as rake tasks
end

desc 'Imports all provision dialogs from individual YAML files'
task :provision_dialogs => :environment do
options = TaskHelpers::Imports.parse_options
TaskHelpers::Imports::ProvisionDialogs.new.import(options)

exit # exit so that parameters to the first rake task are not run as rake tasks
end
end
end
1 change: 1 addition & 0 deletions lib/workers/miq_worker_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"ManageIQ::Providers::Amazon::CloudManager::EventCatcher" => %i(manageiq_default),
"ManageIQ::Providers::Amazon::CloudManager::MetricsCollectorWorker" => %i(manageiq_default),
"ManageIQ::Providers::Amazon::CloudManager::RefreshWorker" => %i(manageiq_default),
"ManageIQ::Providers::Amazon::StorageManager::S3::RefreshWorker" => %i(manageiq_default),
"ManageIQ::Providers::AnsibleTower::AutomationManager::EventCatcher" => %i(manageiq_default),
"ManageIQ::Providers::AnsibleTower::AutomationManager::RefreshWorker" => %i(manageiq_default),
"ManageIQ::Providers::Azure::CloudManager::EventCatcher" => %i(manageiq_default),
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/chargeable_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

factory :chargeable_field_cpu_cores_used, :parent => :chargeable_field do
description 'Used CPU in Cores'
metric 'cpu_usage_rate_average'
metric 'v_derived_cpu_total_cores_used'
group 'cpu_cores'
source 'used'
end
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/ext_management_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@
trait(:configuration_workflow) do
after(:create) do |x|
type = (x.type.split("::")[0..2] + %w(AutomationManager ConfigurationWorkflow)).join("::")
x.configuration_workflows << FactoryGirl.create(:configuration_workflow, :type => type)
x.configuration_scripts << FactoryGirl.create(:configuration_workflow, :type => type)
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/factories/miq_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
factory :miq_schedule_validation, :class => :MiqSchedule do
sequence(:name) { |n| "schedule_#{seq_padded_for_sorting(n)}" }
description "test"
towhat "MiqReport"
resource_type "MiqReport"
run_at {}
sched_action {}
end
Expand All @@ -12,7 +12,7 @@
sched_action = {:method => "test"}
sequence(:name) { |n| "schedule_#{seq_padded_for_sorting(n)}" }
description "test"
towhat "MiqReport"
resource_type "MiqReport"
run_at run_at
sched_action sched_action
end
Expand All @@ -23,7 +23,7 @@
filter = {:uri_parts => {:instance => 'test', :message => 'create'}, :ui => { :ui_attrs => [], :ui_object => {} }, :parameters => {'request' => 'test_request', 'key1' => 'value1'}}
sequence(:name) { |n| "automate_schedule_#{seq_padded_for_sorting(n)}" }
description "test_automation"
towhat "AutomationRequest"
resource_type "AutomationRequest"
run_at run_at
sched_action sched_action
filter filter
Expand Down
Loading

0 comments on commit 8692d8c

Please sign in to comment.