Class: ChefSync::ChefComponent

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_sync/chef_component.rb

Direct Known Subclasses

Cookbook, DataBagItem, Environment, Role

Constant Summary collapse

CHANGE_LOG_SUMMARIES =
{
  :create => " was created.",
  :update => " was updated."
}
ACTIONABLE_CHANGES =
[:create, :update]
FILE_EXTENSION =
".json"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, local_knife:, remote_knife:, dryrun:) ⇒ ChefComponent

Returns a new instance of ChefComponent.



21
22
23
24
25
26
27
28
29
30
# File 'lib/chef_sync/chef_component.rb', line 21

def initialize(name:, local_knife:, remote_knife:, dryrun:)
  @name = name
  @name_with_extension = name + FILE_EXTENSION

  @local_knife = local_knife
  @remote_knife = remote_knife
  @dryrun = dryrun

  @change = :none
end

Class Attribute Details

.resource_typeObject (readonly)

Returns the value of attribute resource_type.



15
16
17
# File 'lib/chef_sync/chef_component.rb', line 15

def resource_type
  @resource_type
end

.total_resourcesObject

Returns the value of attribute total_resources.



16
17
18
# File 'lib/chef_sync/chef_component.rb', line 16

def total_resources
  @total_resources
end

Instance Attribute Details

#changeObject (readonly)

Returns the value of attribute change.



19
20
21
# File 'lib/chef_sync/chef_component.rb', line 19

def change
  @change
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/chef_sync/chef_component.rb', line 18

def name
  @name
end

Class Method Details

.changes(dryrun) ⇒ Object



49
50
51
# File 'lib/chef_sync/chef_component.rb', line 49

def self.changes(dryrun)
  return self.each(dryrun).select(&:changed?).flat_map(&:summarize_changes)
end

.each(dryrun) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chef_sync/chef_component.rb', line 32

def self.each(dryrun)
  return enum_for(:each, dryrun) unless block_given?

  local_knife = self.make_local_knife
  remote_knife = self.make_remote_knife

  local_resources = self.get_local_resources(local_knife, remote_knife)
  self.total_resources = local_resources.count

  default_args = {local_knife: local_knife, remote_knife: remote_knife, dryrun: dryrun}
  local_resources.tqdm(leave: true, desc: "Checking #{self.resource_type}s").each do |args|
    resource = self.new(args.merge(default_args))
    resource.sync
    yield resource
  end
end

.get_local_resources(local_knife, remote_knife) ⇒ Object



53
54
55
56
# File 'lib/chef_sync/chef_component.rb', line 53

def self.get_local_resources(local_knife, remote_knife)
  local_resources = local_knife.list
  return local_resources.map {|resource_name| {name: resource_name}}
end

.make_local_knifeObject



58
59
60
# File 'lib/chef_sync/chef_component.rb', line 58

def self.make_local_knife
  return ChefSync::Knife.new(self.resource_type, :local)
end

.make_remote_knifeObject



62
63
64
# File 'lib/chef_sync/chef_component.rb', line 62

def self.make_remote_knife
  return ChefSync::Knife.new(self.resource_type, :remote)
end

Instance Method Details

#actionable_change?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/chef_sync/chef_component.rb', line 70

def actionable_change?
  return ACTIONABLE_CHANGES.include?(@change)
end

#changed?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/chef_sync/chef_component.rb', line 66

def changed?
  return @change != :none
end

#compare_local_and_remote_versionsObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef_sync/chef_component.rb', line 94

def compare_local_and_remote_versions
  local_resource = self.get_local_resource
  remote_resource = self.get_remote_resource

  case
  when remote_resource.empty?
    @change = :create
  when local_resource != remote_resource
    @change = :update
  end

  return @change
end

#get_local_resourceObject



82
83
84
# File 'lib/chef_sync/chef_component.rb', line 82

def get_local_resource
  return @local_knife.show(@name)
end

#get_remote_resourceObject



86
87
88
# File 'lib/chef_sync/chef_component.rb', line 86

def get_remote_resource
  return @remote_knife.show(@name)
end

#resource_pathObject



78
79
80
# File 'lib/chef_sync/chef_component.rb', line 78

def resource_path
  return "#{self.class.resource_type}s/#{@name}"
end

#summarize_changesObject



74
75
76
# File 'lib/chef_sync/chef_component.rb', line 74

def summarize_changes
  return self.resource_path + CHANGE_LOG_SUMMARIES[@change]
end

#syncObject



108
109
110
111
112
113
114
# File 'lib/chef_sync/chef_component.rb', line 108

def sync
  action = self.compare_local_and_remote_versions
  if !@dryrun and self.actionable_change?
    self.upload_resource
  end
  return action
end

#upload_resourceObject



90
91
92
# File 'lib/chef_sync/chef_component.rb', line 90

def upload_resource
  return @remote_knife.upload(@name_with_extension)
end