Class: Cts::Mpx::Aci::Tasks::Deploy
- Inherits:
-
Object
- Object
- Cts::Mpx::Aci::Tasks::Deploy
- Includes:
- Creatable
- Defined in:
- lib/cts/mpx/aci/tasks/deploy.rb
Overview
Responsible for deploying an image to an account.
Instance Attribute Summary collapse
-
#account ⇒ String
Relative account or nil when untransformed.
-
#image ⇒ Cts::Mpx::Aci::Tasks::Image
Image containing entries to deploy.
-
#user ⇒ User
User to make data service calls with.
Instance Method Summary collapse
-
#block_update_entry(entry, *args, &block) ⇒ Object
rubocop:enable Metrics/AbcSize.
-
#dependencies ⇒ Hash
Any dependencies the image may contain.
-
#deploy(target_account, *args) ⇒ Object
rubocop:disable Metrics/AbcSize reason: these classes is as thin as it can get.
- #deploy_order ⇒ Array?
Instance Attribute Details
#account ⇒ String
Returns relative account or nil when untransformed.
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 |
# File 'lib/cts/mpx/aci/tasks/deploy.rb', line 12 class Deploy include Creatable attribute name: 'account', kind_of: String attribute name: 'image', kind_of: Tasks::Image attribute name: 'user', kind_of: User attribute name: 'pre_block', kind_of: Proc attribute name: 'post_block', kind_of: Proc # Any dependencies the image may contain # @return [Hash] dependency hash keyed by entry def dependencies hash = {} image.entries.each do |e| deps = e.dependencies hash.store e.id, deps if deps.any? end hash end # rubocop:disable Metrics/AbcSize # reason: these classes is as thin as it can get. not splitting it up to satisify rubocop. # deploy a transformed image to an account # @param [String] target_account to deploy to # @raise [RuntimeError] when image is not deployable def deploy(target_account, *args) raise "not a deployable image" unless Validators.image_deployable? image raise "not a deployable image" unless deploy_order deploy_order.each do |ref| entry = image.entries.find { |e| e.id == ref } entry = block_update_entry entry, *args, &pre_block if pre_block query = Query.create service: entry.service, endpoint: entry.endpoint, fields: 'id,guid' query.query['byOwnerId'] = target_account if entry.id.include? 'Field/' query.query['byQualifiedFieldName'] = "#{entry.fields['namespace']}$#{entry.fields['fieldName']}" else query.query['byGuid'] = entry.fields['guid'] end if entry.exists_by? user, query method = 'PUT' response = query.run(user: user) entry.id = response.page.entries.first["id"] else entry.id = nil entry.service = query.service entry.endpoint = query.endpoint method = 'POST' end entry.fields['ownerId'] = target_account entry.save user: user block_update_entry entry, *args, &post_block if post_block logger.info "deployed #{entry.fields['guid']} to #{target_account} as #{entry.id || 'new_id'} with #{user.username} via a #{method} call" end true end # @return [Array] order to deploy objects in # @return [nil] if image state is transformed. # @return [nil] if a deploy order could not be generated. def deploy_order return nil unless image.state == :untransformed hash = dependencies list = image.entries.map(&:id) - dependencies.keys 100.times do |_i| break unless hash.any? hash.delete_if { |k, _v| list.include? k } new_hash = hash.select { |_k, v| (v - list).empty? } list += new_hash.keys if new_hash.any? end return list.uniq if image.entries.map(&:id).count == list.count nil end # rubocop:enable Metrics/AbcSize def block_update_entry(entry, *args, &block) raise ArgumentError, 'block must be provided' unless block raise ArgumentError, 'argument must be an entry' unless entry.is_a? Entry e = entry.dup block.yield e, args e end private def logger Aci.logger end end |
#image ⇒ Cts::Mpx::Aci::Tasks::Image
Returns Image containing entries to deploy.
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 |
# File 'lib/cts/mpx/aci/tasks/deploy.rb', line 12 class Deploy include Creatable attribute name: 'account', kind_of: String attribute name: 'image', kind_of: Tasks::Image attribute name: 'user', kind_of: User attribute name: 'pre_block', kind_of: Proc attribute name: 'post_block', kind_of: Proc # Any dependencies the image may contain # @return [Hash] dependency hash keyed by entry def dependencies hash = {} image.entries.each do |e| deps = e.dependencies hash.store e.id, deps if deps.any? end hash end # rubocop:disable Metrics/AbcSize # reason: these classes is as thin as it can get. not splitting it up to satisify rubocop. # deploy a transformed image to an account # @param [String] target_account to deploy to # @raise [RuntimeError] when image is not deployable def deploy(target_account, *args) raise "not a deployable image" unless Validators.image_deployable? image raise "not a deployable image" unless deploy_order deploy_order.each do |ref| entry = image.entries.find { |e| e.id == ref } entry = block_update_entry entry, *args, &pre_block if pre_block query = Query.create service: entry.service, endpoint: entry.endpoint, fields: 'id,guid' query.query['byOwnerId'] = target_account if entry.id.include? 'Field/' query.query['byQualifiedFieldName'] = "#{entry.fields['namespace']}$#{entry.fields['fieldName']}" else query.query['byGuid'] = entry.fields['guid'] end if entry.exists_by? user, query method = 'PUT' response = query.run(user: user) entry.id = response.page.entries.first["id"] else entry.id = nil entry.service = query.service entry.endpoint = query.endpoint method = 'POST' end entry.fields['ownerId'] = target_account entry.save user: user block_update_entry entry, *args, &post_block if post_block logger.info "deployed #{entry.fields['guid']} to #{target_account} as #{entry.id || 'new_id'} with #{user.username} via a #{method} call" end true end # @return [Array] order to deploy objects in # @return [nil] if image state is transformed. # @return [nil] if a deploy order could not be generated. def deploy_order return nil unless image.state == :untransformed hash = dependencies list = image.entries.map(&:id) - dependencies.keys 100.times do |_i| break unless hash.any? hash.delete_if { |k, _v| list.include? k } new_hash = hash.select { |_k, v| (v - list).empty? } list += new_hash.keys if new_hash.any? end return list.uniq if image.entries.map(&:id).count == list.count nil end # rubocop:enable Metrics/AbcSize def block_update_entry(entry, *args, &block) raise ArgumentError, 'block must be provided' unless block raise ArgumentError, 'argument must be an entry' unless entry.is_a? Entry e = entry.dup block.yield e, args e end private def logger Aci.logger end end |
#user ⇒ User
Returns user to make data service calls with.
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 |
# File 'lib/cts/mpx/aci/tasks/deploy.rb', line 12 class Deploy include Creatable attribute name: 'account', kind_of: String attribute name: 'image', kind_of: Tasks::Image attribute name: 'user', kind_of: User attribute name: 'pre_block', kind_of: Proc attribute name: 'post_block', kind_of: Proc # Any dependencies the image may contain # @return [Hash] dependency hash keyed by entry def dependencies hash = {} image.entries.each do |e| deps = e.dependencies hash.store e.id, deps if deps.any? end hash end # rubocop:disable Metrics/AbcSize # reason: these classes is as thin as it can get. not splitting it up to satisify rubocop. # deploy a transformed image to an account # @param [String] target_account to deploy to # @raise [RuntimeError] when image is not deployable def deploy(target_account, *args) raise "not a deployable image" unless Validators.image_deployable? image raise "not a deployable image" unless deploy_order deploy_order.each do |ref| entry = image.entries.find { |e| e.id == ref } entry = block_update_entry entry, *args, &pre_block if pre_block query = Query.create service: entry.service, endpoint: entry.endpoint, fields: 'id,guid' query.query['byOwnerId'] = target_account if entry.id.include? 'Field/' query.query['byQualifiedFieldName'] = "#{entry.fields['namespace']}$#{entry.fields['fieldName']}" else query.query['byGuid'] = entry.fields['guid'] end if entry.exists_by? user, query method = 'PUT' response = query.run(user: user) entry.id = response.page.entries.first["id"] else entry.id = nil entry.service = query.service entry.endpoint = query.endpoint method = 'POST' end entry.fields['ownerId'] = target_account entry.save user: user block_update_entry entry, *args, &post_block if post_block logger.info "deployed #{entry.fields['guid']} to #{target_account} as #{entry.id || 'new_id'} with #{user.username} via a #{method} call" end true end # @return [Array] order to deploy objects in # @return [nil] if image state is transformed. # @return [nil] if a deploy order could not be generated. def deploy_order return nil unless image.state == :untransformed hash = dependencies list = image.entries.map(&:id) - dependencies.keys 100.times do |_i| break unless hash.any? hash.delete_if { |k, _v| list.include? k } new_hash = hash.select { |_k, v| (v - list).empty? } list += new_hash.keys if new_hash.any? end return list.uniq if image.entries.map(&:id).count == list.count nil end # rubocop:enable Metrics/AbcSize def block_update_entry(entry, *args, &block) raise ArgumentError, 'block must be provided' unless block raise ArgumentError, 'argument must be an entry' unless entry.is_a? Entry e = entry.dup block.yield e, args e end private def logger Aci.logger end end |
Instance Method Details
#block_update_entry(entry, *args, &block) ⇒ Object
rubocop:enable Metrics/AbcSize
99 100 101 102 103 104 105 106 |
# File 'lib/cts/mpx/aci/tasks/deploy.rb', line 99 def block_update_entry(entry, *args, &block) raise ArgumentError, 'block must be provided' unless block raise ArgumentError, 'argument must be an entry' unless entry.is_a? Entry e = entry.dup block.yield e, args e end |
#dependencies ⇒ Hash
Any dependencies the image may contain
23 24 25 26 27 28 29 30 |
# File 'lib/cts/mpx/aci/tasks/deploy.rb', line 23 def dependencies hash = {} image.entries.each do |e| deps = e.dependencies hash.store e.id, deps if deps.any? end hash end |
#deploy(target_account, *args) ⇒ Object
rubocop:disable Metrics/AbcSize reason: these classes is as thin as it can get. not splitting it up to satisify rubocop. deploy a transformed image to an account
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 |
# File 'lib/cts/mpx/aci/tasks/deploy.rb', line 37 def deploy(target_account, *args) raise "not a deployable image" unless Validators.image_deployable? image raise "not a deployable image" unless deploy_order deploy_order.each do |ref| entry = image.entries.find { |e| e.id == ref } entry = block_update_entry entry, *args, &pre_block if pre_block query = Query.create service: entry.service, endpoint: entry.endpoint, fields: 'id,guid' query.query['byOwnerId'] = target_account if entry.id.include? 'Field/' query.query['byQualifiedFieldName'] = "#{entry.fields['namespace']}$#{entry.fields['fieldName']}" else query.query['byGuid'] = entry.fields['guid'] end if entry.exists_by? user, query method = 'PUT' response = query.run(user: user) entry.id = response.page.entries.first["id"] else entry.id = nil entry.service = query.service entry.endpoint = query.endpoint method = 'POST' end entry.fields['ownerId'] = target_account entry.save user: user block_update_entry entry, *args, &post_block if post_block logger.info "deployed #{entry.fields['guid']} to #{target_account} as #{entry.id || 'new_id'} with #{user.username} via a #{method} call" end true end |
#deploy_order ⇒ Array?
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/cts/mpx/aci/tasks/deploy.rb', line 78 def deploy_order return nil unless image.state == :untransformed hash = dependencies list = image.entries.map(&:id) - dependencies.keys 100.times do |_i| break unless hash.any? hash.delete_if { |k, _v| list.include? k } new_hash = hash.select { |_k, v| (v - list).empty? } list += new_hash.keys if new_hash.any? end return list.uniq if image.entries.map(&:id).count == list.count nil end |