Class: Cts::Mpx::Aci::Tasks::Deploy

Inherits:
Object
  • Object
show all
Includes:
Creatable
Defined in:
lib/cts/mpx/aci/tasks/deploy.rb

Overview

Responsible for deploying an image to an account.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#accountString

Returns relative account or nil when untransformed.

Returns:

  • (String)

    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(, *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'] = 

      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'] = 
      entry.save user: user
      block_update_entry entry, *args, &post_block if post_block

      logger.info "deployed #{entry.fields['guid']} to #{} 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

#imageCts::Mpx::Aci::Tasks::Image

Returns Image containing entries to deploy.

Returns:



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(, *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'] = 

      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'] = 
      entry.save user: user
      block_update_entry entry, *args, &post_block if post_block

      logger.info "deployed #{entry.fields['guid']} to #{} 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

#userUser

Returns user to make data service calls with.

Returns:

  • (User)

    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(, *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'] = 

      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'] = 
      entry.save user: user
      block_update_entry entry, *args, &post_block if post_block

      logger.info "deployed #{entry.fields['guid']} to #{} 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

Raises:

  • (ArgumentError)


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

#dependenciesHash

Any dependencies the image may contain

Returns:

  • (Hash)

    dependency hash keyed by entry



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

Parameters:

  • target_account (String)

    to deploy to

Raises:

  • (RuntimeError)

    when image is not deployable



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(, *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'] = 

    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'] = 
    entry.save user: user
    block_update_entry entry, *args, &post_block if post_block

    logger.info "deployed #{entry.fields['guid']} to #{} as #{entry.id || 'new_id'} with #{user.username} via a #{method} call"
  end

  true
end

#deploy_orderArray?

Returns:

  • (Array)

    order to deploy objects in

  • (nil)

    if image state is transformed.

  • (nil)

    if a deploy order could not be generated.



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