Module: PagerDuty::ChefServer::SyncHelper

Included in:
Sync
Defined in:
lib/pagerduty/chef_server/sync_helper.rb

Instance Method Summary collapse

Instance Method Details

#chef_repo_dirObject



34
35
36
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 34

def chef_repo_dir
  File.expand_path('..', cookbook_dir)
end

#converge_by(msg) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 26

def converge_by(msg)
  if why_run
    ui.info(ui.color("Would ", :cyan)+ msg)
  else
    yield
  end
end

#create_databag(data_bag_name) ⇒ Object



130
131
132
133
134
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 130

def create_databag(data_bag_name)
  converge_by "Create data bag #{data_bag_name}" do
    knife Chef::Knife::DataBagCreate, data_bag_name
  end
end

#data_bag_from_file(name, path) ⇒ Object



51
52
53
54
55
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 51

def data_bag_from_file(name, path)
  converge_by "Create data bag #{name} from #{path}" do
    knife Chef::Knife::DataBagFromFile, name, path
  end
end

#data_bag_from_hash(databag_name, data) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 136

def data_bag_from_hash(databag_name, data)
  tmp = Tempfile.new(['restorechef', '.json'])
  begin
    tmp.write(JSON.dump(data))
    tmp.close
    converge_by "create data bag #{databag_name}" do
      data_bag_from_file(databag_name, tmp.path)
    end
  ensure
    tmp.unlink
  end
end

#databag_dirObject



42
43
44
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 42

def databag_dir
  File.join(chef_repo_dir, 'data_bags')
end

#delete_cookbook(cb) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 101

def delete_cookbook(cb)
  converge_by "Delete cookbook #{cb}" do
    knife Chef::Knife::CookbookDelete, cb do |config|
      config[:yes] = true
      config[:all] = true
    end
  end
end

#environment_dirObject



46
47
48
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 46

def environment_dir
  File.join(chef_repo_dir, 'environments')
end

#ignore_fileObject



157
158
159
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 157

def ignore_file
  File.join(chef_repo_dir, '.pd-ignore')
end

#ignored?(path) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 22

def ignored?(path)
  ignore_patterns && ignore_patterns.any?{|pattern| File.fnmatch?(pattern, path)}
end

#knife(klass, *name_args) {|plugin.config| ... } ⇒ Object

Yields:

  • (plugin.config)


149
150
151
152
153
154
155
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 149

def knife(klass, *name_args)
  klass.load_deps
  plugin = klass.new
  yield plugin.config if Kernel.block_given?
  plugin.name_args = name_args
  plugin.run
end

#role_dirObject



38
39
40
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 38

def role_dir
  File.join(chef_repo_dir, 'roles')
end

#upload_all_cookbooksObject



73
74
75
76
77
78
79
80
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 73

def upload_all_cookbooks
  converge_by 'Upload all cookbooks' do
    knife(Chef::Knife::CookbookUpload) do |config|
      config[:all] = true
      config[:cookbook_path] = cookbook_dir
    end
  end
end

#upload_cookbook(cb) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 82

def upload_cookbook(cb)
  converge_by "Upload cookbook #{cb}" do
    knife(Chef::Knife::CookbookUpload, cb) do |config|
      config[:cookbook_path] = cookbook_dir
      config[:depends] = false
    end
  end
end

#upload_cookbooks(cbcb) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 91

def upload_cookbooks(cbcb)
  sorted_cookbooks = cbcb.sort # definite order + nice printout for why_run
  converge_by "Upload cookbooks #{sorted_cookbooks.join(', ')}" do
    knife Chef::Knife::CookbookUpload, *sorted_cookbooks do |config|
      config[:cookbook_path] = cookbook_dir
      config[:depends] = false
    end
  end
end

#upload_databagsObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 110

def upload_databags
  ui.info(ui.color('updating data bags in batch mode', :yellow))
  existing_databags = Chef::DataBag.list.keys
  Dir[databag_dir+'/*'].each do |db_path|
    if ignored?(db_path)
      ui.info(ui.color("Ignored:", :magenta) + db_path)
      next
    end
    db_name = File.basename(db_path)
    unless existing_databags.include?(db_name)
      create_databag(db_name)
    end
    Dir[db_path+'/*'].each do |path|
      unless ignored?(path)
        data_bag_from_file(File.basename(db_path), path)
      end
    end
  end
end

#upload_environmentsObject



57
58
59
60
61
62
63
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 57

def upload_environments
  Dir[environment_dir+'/*'].reject{|f| File.directory?(f)}.each do |path|
    converge_by "Create environment from #{path}" do
      knife Chef::Knife::EnvironmentFromFile, path
    end
  end
end

#upload_rolesObject



65
66
67
68
69
70
71
# File 'lib/pagerduty/chef_server/sync_helper.rb', line 65

def upload_roles
  Dir[role_dir+'/*'].each do |path|
    converge_by "Create role from #{path}" do
      knife Chef::Knife::RoleFromFile, path
    end
  end
end