From c9fb7f3b438b9e42dab0bf702d02460d8a3f16c3 Mon Sep 17 00:00:00 2001 From: Dan Ryan Date: Tue, 11 Sep 2012 15:09:37 -0400 Subject: [PATCH] old ruby hash styles --- Gemfile | 1 + Gemfile.lock | 1 + app/models/definition.rb | 13 +-- app/models/participant.rb | 12 +-- app/models/provider.rb | 6 +- app/models/provider/cm/chef.rb | 6 +- app/models/provider/notification/email.rb | 6 +- app/models/provider/remote/http.rb | 24 +++++- app/models/provider/server/ec2.rb | 27 +++--- app/models/resource.rb | 8 +- app/models/resource/cm/chef/node.rb | 18 ++-- app/models/resource/mock.rb | 2 +- app/models/resource/notification/campfire.rb | 11 ++- app/models/resource/notification/email.rb | 12 ++- app/models/resource/remote/http.rb | 11 +-- app/models/resource/remote/ssh.rb | 13 +-- app/models/resource/remote/ssh_multi.rb | 13 +-- app/models/resource/server/ec2.rb | 88 ++++++++++---------- config/initializers/inflections.rb | 2 +- db/migrate/20120828020629_create_jobs.rb | 4 +- 20 files changed, 149 insertions(+), 129 deletions(-) diff --git a/Gemfile b/Gemfile index f392dfa..1cccd47 100644 --- a/Gemfile +++ b/Gemfile @@ -40,6 +40,7 @@ gem 'tinder' gem 'net-ssh' gem 'net-ssh-multi' gem 'spice' +gem 'faraday' # Queue processing gem 'sidekiq', '~> 2.1.1' diff --git a/Gemfile.lock b/Gemfile.lock index d65700a..00eca91 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -345,6 +345,7 @@ DEPENDENCIES database_cleaner (>= 0.7.2) email_spec (>= 1.2.1) factory_girl_rails (>= 3.2.0) + faraday fog (~> 1.5.0) foreman forgery (>= 0.5.0) diff --git a/app/models/definition.rb b/app/models/definition.rb index a17e86b..d337685 100644 --- a/app/models/definition.rb +++ b/app/models/definition.rb @@ -6,11 +6,12 @@ class Definition < ActiveRecord::Base has_paper_trail validates :name, - presence: true, - uniqueness: true# , - # format: { with: /[]} + :presence => true, + :uniqueness => true# , + # TODO() + # :format => { with: /[]} validates :content, - presence: true + :presence => true def to_pdef parse @@ -22,8 +23,8 @@ def to_param def to_hash { - name: name, - content: content# , + :name => name, + :content => content# , # pdef: to_pdef } end diff --git a/app/models/participant.rb b/app/models/participant.rb index 11145b1..64b8fc6 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -49,11 +49,11 @@ def to_param def to_hash { - type: type, - regex: regex, - class: self.class.name, - options: options, - actions: self.class.allowed_actions + :type => type, + :regex => regex, + :class => self.class.name, + :options => options, + :actions => self.class.allowed_actions } end @@ -64,7 +64,7 @@ def self.register(type) end def on_workitem - Mastermind.logger.debug provider: type, action: action, params: params, fields: fields + Mastermind.logger.debug :provider => type, :action => action, :params => params, :fields => fields @resource = Mastermind.resources[self.class.type].new(params) diff --git a/app/models/provider.rb b/app/models/provider.rb index 23ecaee..dda85b5 100644 --- a/app/models/provider.rb +++ b/app/models/provider.rb @@ -48,9 +48,9 @@ def to_param def to_hash { - type: type, - class: self.class.name, - options: options + :type => type, + :class => self.class.name, + :options => options # actions: self.class.allowed_actions } end diff --git a/app/models/provider/cm/chef.rb b/app/models/provider/cm/chef.rb index 205f9d6..8155270 100644 --- a/app/models/provider/cm/chef.rb +++ b/app/models/provider/cm/chef.rb @@ -11,9 +11,9 @@ class Chef < Provider def connection Spice::Connection.new( - server_url: options[:server_url], - client_name: options[:client_name], - client_key: options[:client_key] + :server_url => options[:server_url], + :client_name => options[:client_name], + :client_key => options[:client_key] ) end diff --git a/app/models/provider/notification/email.rb b/app/models/provider/notification/email.rb index 3f21716..36f8dc9 100644 --- a/app/models/provider/notification/email.rb +++ b/app/models/provider/notification/email.rb @@ -16,12 +16,8 @@ class Email < Provider # delivery_method :smtp, options # end - action :notify do - requires :subject, :body, :from, :to - + action :notify do # TODO - - {} end end diff --git a/app/models/provider/remote/http.rb b/app/models/provider/remote/http.rb index 27b4705..f672c70 100644 --- a/app/models/provider/remote/http.rb +++ b/app/models/provider/remote/http.rb @@ -2,15 +2,33 @@ module Provider::Remote class HTTP < Provider + + def connection + @connection ||= Faraday.new(:url => resource.url) + end + register :http - action :run do - requires :host, :user, :key_data, :command, :output - + # attribute :payload, :type => Object, :default => {} + # attribute :headers, :type => Object, :default => {} + # attribute :url, :type => String + # attribute :verb, :type => String + # attribute :response, :type => String + + action :get do resource.output = run_ssh(resource.command) {} end + action :post do + end + + action :put do + end + + action :delete do + end + end end \ No newline at end of file diff --git a/app/models/provider/server/ec2.rb b/app/models/provider/server/ec2.rb index a8a2827..f22e262 100644 --- a/app/models/provider/server/ec2.rb +++ b/app/models/provider/server/ec2.rb @@ -7,10 +7,10 @@ class EC2 < Provider def connection(region=nil) Fog::Compute.new( - provider: 'AWS', - aws_access_key_id: options[:aws_access_key_id], - aws_secret_access_key: options[:aws_secret_access_key], - region: (region rescue (params['region'] || 'us-east-1')) + :provider => 'AWS', + :aws_access_key_id => options[:aws_access_key_id], + :aws_secret_access_key => options[:aws_secret_access_key], + :region => (region rescue (params['region'] || 'us-east-1')) ) end @@ -27,20 +27,17 @@ def connection(region=nil) ) server.wait_for { ready? } - resource.attributes = server.attributes - resource.attributes + update_resource_attributes(server.attributes) end action :destroy do - requires :instance_id server = connection(resource.region).servers.get(resource.instance_id) server.destroy server.wait_for { state == 'terminated' } - resource.attributes = server.attributes - resource.attributes + update_resource_attributes(server.attributes) end action :stop do @@ -51,8 +48,7 @@ def connection(region=nil) server.wait_for { state == 'stopped' } - resource.attributes = server.attributes - resource.attributes + update_resource_attributes(server.attributes) end action :start do @@ -63,8 +59,7 @@ def connection(region=nil) server.wait_for { state == 'running' } - resource.attributes = server.attributes - resource.attributes + update_resource_attributes(server.attributes) end action :restart do @@ -78,8 +73,7 @@ def connection(region=nil) server.wait_for { state == 'running' } - resource.attributes = server.attributes - resource.attributes + update_resource_attributes(server.attributes) end action :reboot do @@ -90,8 +84,7 @@ def connection(region=nil) server.wait_for { state == 'running' } - resource.attributes = server.attributes - resource.attributes + update_resource_attributes(server.attributes) end end diff --git a/app/models/resource.rb b/app/models/resource.rb index 02c10be..5b7694f 100644 --- a/app/models/resource.rb +++ b/app/models/resource.rb @@ -2,10 +2,10 @@ class Resource include ActiveAttr::Model include ActiveAttr::TypecastedAttributes - attribute :action, type: String - attribute :name, type: String - attribute :type, type: String, default: lambda { self.class.type } - attribute :provider, type: Object, default: lambda { Mastermind.providers[self.class.type] } + attribute :action, :type => String + attribute :name, :type => String + attribute :type, :type => String, :default => lambda { self.class.type } + attribute :provider, :type => Object, :default => lambda { Mastermind.providers[self.class.type] } validates! :action, :name, :presence => true diff --git a/app/models/resource/cm/chef/node.rb b/app/models/resource/cm/chef/node.rb index 2799d6f..4acdcfe 100644 --- a/app/models/resource/cm/chef/node.rb +++ b/app/models/resource/cm/chef/node.rb @@ -4,15 +4,15 @@ module Resource::CM class Chef::Node < Resource register :chef_node - attribute :name, type: String - attribute :run_list, type: Object, default: [] - attribute :automatic, type: Object, default: {} - attribute :default, type: Object, default: {} - attribute :normal, type: Object, default: {} - attribute :override, type: Object, default: {} - attribute :_rev, type: String - attribute :chef_type, type: String, default: "node" - attribute :json_class, type: String, default: "Chef::Node" + attribute :name, :type => String + attribute :run_list, :type => Object, :default => [] + attribute :automatic, :type => Object, :default => {} + attribute :default, :type => Object, :default => {} + attribute :normal, :type => Object, :default => {} + attribute :override, :type => Object, :default => {} + attribute :_rev, :type => String + attribute :chef_type, :type => String, :default => "node" + attribute :json_class, :type => String, :default => "Chef::Node" end end \ No newline at end of file diff --git a/app/models/resource/mock.rb b/app/models/resource/mock.rb index c56b478..7057134 100644 --- a/app/models/resource/mock.rb +++ b/app/models/resource/mock.rb @@ -1,7 +1,7 @@ class Resource::Mock < Resource register :mock - attribute :message, type: String + attribute :message, :type => String validates! :message, :presence => true, diff --git a/app/models/resource/notification/campfire.rb b/app/models/resource/notification/campfire.rb index c7b9c99..a603ff1 100644 --- a/app/models/resource/notification/campfire.rb +++ b/app/models/resource/notification/campfire.rb @@ -2,10 +2,13 @@ module Resource::Notification class Campfire < Resource register :campfire - attribute :message, type: String - attribute :source, type: String, default: 'mastermind' - attribute :message_id, type: String - attribute :message_type, type: String + attribute :message, :type => String + attribute :source, :type => String, :default => 'mastermind' + attribute :message_id, :type => String + attribute :message_type, :type => String + validates! :message, + :presence => true, + :on => :notify end end diff --git a/app/models/resource/notification/email.rb b/app/models/resource/notification/email.rb index 3c2c230..799d7a3 100644 --- a/app/models/resource/notification/email.rb +++ b/app/models/resource/notification/email.rb @@ -2,10 +2,14 @@ module Resource::Notification class Email < Resource register :email - attribute :subject, type: String - attribute :to, type: String - attribute :from, type: String - attribute :body, type: String + attribute :subject, :type => String + attribute :to, :type => String + attribute :from, :type => String + attribute :body, :type => String + + validates! :subject, :body, :from, :to, + :presence => true, + :on => :notify end end \ No newline at end of file diff --git a/app/models/resource/remote/http.rb b/app/models/resource/remote/http.rb index 2d8565c..021930e 100644 --- a/app/models/resource/remote/http.rb +++ b/app/models/resource/remote/http.rb @@ -2,10 +2,11 @@ module Resource::Remote class HTTP < Resource register :http - attribute :payload, type: String - attribute :headers, type: Object, default: {} - attribute :url, type: String - attribute :verb, type: String - attribute :response, type: String + attribute :payload, :type => String + attribute :headers, :type => Object, :default => {} + attribute :url, :type => String + attribute :verb, :type => String + attribute :response, :type => String + attribute :status, :type => String end end \ No newline at end of file diff --git a/app/models/resource/remote/ssh.rb b/app/models/resource/remote/ssh.rb index 36f2f65..04975ad 100644 --- a/app/models/resource/remote/ssh.rb +++ b/app/models/resource/remote/ssh.rb @@ -2,13 +2,14 @@ module Resource::Remote class SSH < Resource register :ssh - attribute :command, type: String - attribute :host, type: String - attribute :user, type: String - attribute :key_data, type: String - attribute :output, type: String + attribute :command, :type => String + attribute :host, :type => String + attribute :user, :type => String + attribute :key_data, :type => String + attribute :output, :type => String validates! :command, :host, :user, :key_data, - presence: true + :presence => true, + :on => :run end end \ No newline at end of file diff --git a/app/models/resource/remote/ssh_multi.rb b/app/models/resource/remote/ssh_multi.rb index a2011a1..c4454e9 100644 --- a/app/models/resource/remote/ssh_multi.rb +++ b/app/models/resource/remote/ssh_multi.rb @@ -2,13 +2,14 @@ module Resource::Remote class SSHMulti < Resource register :ssh_multi - attribute :command, type: String - attribute :hosts, type: Object - attribute :user, type: String - attribute :key_data, type: String - attribute :output, type: String + attribute :command, :type => String + attribute :hosts, :type => Object + attribute :user, :type => String + attribute :key_data, :type => String + attribute :output, :type => String validates! :command, :hosts, :user, :key_data, - presence: true + :presence => true, + :on => :run end end \ No newline at end of file diff --git a/app/models/resource/server/ec2.rb b/app/models/resource/server/ec2.rb index dbb8b9e..27aece0 100644 --- a/app/models/resource/server/ec2.rb +++ b/app/models/resource/server/ec2.rb @@ -2,45 +2,45 @@ module Resource::Server class EC2 < Resource register :ec2_server - attribute :image_id, type: String - attribute :key_name, type: String - attribute :ami_launch_index, type: Integer - attribute :availability_zone, type: String #, default: 'us-east-1a' - attribute :block_device_mapping, type: Object, default: [] - attribute :client_token, type: String - attribute :dns_name, type: String - attribute :groups, type: Object, default: [ 'default' ] - attribute :flavor_id, type: String - attribute :iam_instance_profile, type: Object, default: {} - attribute :instance_id, type: String - attribute :kernel_id, type: String - attribute :created_at, type: DateTime - attribute :monitoring, type: Boolean, default: false - attribute :network_interface, type: Object, default: [] - attribute :placement_group, type: String - attribute :platform, type: String - attribute :product_codes, type: Object, default: [] - attribute :private_dns_name, type: String - attribute :private_ip_address, type: String - attribute :public_ip_address, type: String - attribute :ramdisk_id, type: String - attribute :reason, type: String - attribute :region, type: String #, default: 'us-east-1' - attribute :root_device_name, type: String - attribute :root_device_type, type: String - attribute :security_group_ids, type: Object, default: [] - attribute :state, type: String - attribute :state_reason, type: Object - attribute :subnet_id, type: String - attribute :tenancy, type: String - attribute :tags, type: Object - attribute :user_data, type: String - attribute :vpc_id, type: String + attribute :image_id, :type => String + attribute :key_name, :type => String + attribute :ami_launch_index, :type => Integer + attribute :availability_zone, :type => String #, :default => 'us-east-1a' + attribute :block_device_mapping, :type => Object, :default => [] + attribute :client_token, :type => String + attribute :dns_name, :type => String + attribute :groups, :type => Object, :default => [ 'default' ] + attribute :flavor_id, :type => String + attribute :iam_instance_profile, :type => Object, :default => {} + attribute :instance_id, :type => String + attribute :kernel_id, :type => String + attribute :created_at, :type => DateTime + attribute :monitoring, :type => Boolean, :default => false + attribute :network_interface, :type => Object, :default => [] + attribute :placement_group, :type => String + attribute :platform, :type => String + attribute :product_codes, :type => Object, :default => [] + attribute :private_dns_name, :type => String + attribute :private_ip_address, :type => String + attribute :public_ip_address, :type => String + attribute :ramdisk_id, :type => String + attribute :reason, :type => String + attribute :region, :type => String #, :default => 'us-east-1' + attribute :root_device_name, :type => String + attribute :root_device_type, :type => String + attribute :security_group_ids, :type => Object, :default => [] + attribute :state, :type => String + attribute :state_reason, :type => Object + attribute :subnet_id, :type => String + attribute :tenancy, :type => String + attribute :tags, :type => Object + attribute :user_data, :type => String + attribute :vpc_id, :type => String alias_method :id, :instance_id alias_method :id=, :instance_id= - validates! :image_id, :flavor_id, :availability_zone, :region, + validates! :image_id, :flavor_id, :availability_zone, :region, :key_name, :presence => true, :on => :create @@ -49,24 +49,24 @@ class EC2 < Resource :on => [ :destroy, :stop, :start, :restart, :reboot ] validates! :image_id, - format: { with: /^ami-[a-f0-9]{8}$/ }, - allow_nil: true + :format => { with: /^ami-[a-f0-9]{8}$/ }, + :allow_blank => true validates! :flavor_id, inclusion: { in: Mastermind::AWS::FLAVORS }, - allow_nil: true + :allow_blank => true validates! :availability_zone, - if: lambda { |s| s.region? && Mastermind::AWS::ZONES[s.region] }, - inclusion: { in: lambda { |s| Mastermind::AWS::ZONES[s.region] } }, - allow_blank: true + :if => lambda { |s| s.region? && Mastermind::AWS::ZONES[s.region] }, + :inclusion => { in: lambda { |s| Mastermind::AWS::ZONES[s.region] } }, + :allow_blank => true validates! :region, - inclusion: { in: Mastermind::AWS::ZONES.keys } + :inclusion => { in: Mastermind::AWS::ZONES.keys } validates! :id, :instance_id, - format: { with: /^i-[a-f0-9]{8}$/ }, - allow_nil: true + :format => { with: /^i-[a-f0-9]{8}$/ }, + :allow_blank => true end end \ No newline at end of file diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index 5d8d9be..7abf8d8 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -9,7 +9,7 @@ # inflect.uncountable %w( fish sheep ) # end # -# These inflection rules are supported but not enabled by default: +# These inflection rules are supported but not enabled by :default => # ActiveSupport::Inflector.inflections do |inflect| # inflect.acronym 'RESTful' # end diff --git a/db/migrate/20120828020629_create_jobs.rb b/db/migrate/20120828020629_create_jobs.rb index fd0942f..9413636 100644 --- a/db/migrate/20120828020629_create_jobs.rb +++ b/db/migrate/20120828020629_create_jobs.rb @@ -3,8 +3,8 @@ def change create_table :jobs do |t| t.string :name t.references :definition - t.text :fields, default: {}.to_json - t.text :last_results, default: {}.to_json + t.text :fields, :default => {}.to_json + t.text :last_results, :default => {}.to_json t.string :last_wfid t.string :state