Class: Cheftacular::Initializer

Inherits:
Object
  • Object
show all
Defined in:
lib/cheftacular/initializer.rb,
lib/cheftacular/stateless_actions/initialize_data_bag_contents.rb

Instance Method Summary collapse

Constructor Details

#initialize(options, config) ⇒ Initializer

Returns a new instance of Initializer.



4
5
6
7
8
9
10
11
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
# File 'lib/cheftacular/initializer.rb', line 4

def initialize options, config
  @options, @config  = options, config

  initialize_queues

  initialize_yaml_configuration

  initialize_override_yaml_configuration

  initialize_default_cheftacular_options

  initialize_locations

  initialize_directories

  initialize_data_bag_cheftacular_hash if !@config['helper'].is_initialization_command?(ARGV[0]) && !@config['helper'].running_on_chef_node?

  initialize_monkeypatches unless @config['helper'].running_on_chef_node?

  initialize_arguments

  initialize_sub_environment

  initialize_cloud_options unless @config['helper'].is_initialization_command?(ARGV[0])

  initialize_documentation_hash

  initialize_ruby_config

  initialize_ridley unless @config['helper'].is_initialization_command?(ARGV[0])

  initialize_classes

  unless @config['helper'].is_initialization_command?(ARGV[0])
    initialize_cloud_checks

    @config['helper'].completion_rate? 0, 'initializer'

    @config['helper'].completion_rate? 10, 'initializer'

    initialize_ridley_environments

    @config['helper'].completion_rate? 20, 'initializer'

    initialize_ridley_roles_and_nodes

    @config['helper'].completion_rate? 30, 'initializer'

    initialize_data_bags_for_environment @options['env'], true

    @config['helper'].completion_rate? 90, 'initializer'

    initialize_passwords @options['env']

    @config['helper'].completion_rate? 100, 'initializer'

    initialize_version_check        if @config['cheftacular']['strict_version_checks']

    initialize_chef_repo_up_to_date if @config['cheftacular']['keep_chef_repo_cheftacular_yml_up_to_date']
  end
end

Instance Method Details

#initialize_addresses_bag_contents(env, save_on_finish = false) ⇒ Object

User Action Generated: “api1”, “public”: “1.2.3.4”, “address”: “10.208.1.2”, “dn”:“api1.example.com”, “descriptor”: “lb:my-backend-codebase”



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cheftacular/stateless_actions/initialize_data_bag_contents.rb', line 85

def initialize_addresses_bag_contents env, save_on_finish=false
  hash = @config[env]['addresses_bag_hash']

  unless hash.has_key?('addresses')
    hash['addresses'] = []

    save_on_finish = true
  end

  @config['ChefDataBag'].save_addresses_bag(env) if save_on_finish
end

#initialize_argumentsObject

changes to arguments should show up in the documentation methods in their appropriate method file



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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/cheftacular/initializer.rb', line 67

def initialize_arguments
  OptionParser.new do |opts|
    opts.banner = "Usage: cft command [repository] [opts]"

    # Environment arguments

    opts.on('-b', '--datastaging', 'Set the environment to datastaging') do
      @options['env'] = 'datastaging'
    end

    opts.on('-d', '--dev-remote', "Set the environment to devremote") do
      @options['env'] = 'devremote'
    end

    opts.on('--env ENV', 'Set the environment to one you specify') do |env|
      @options['env'] = env
    end

    opts.on('-p', '--prod', "Set the environment to production") do
      @options['env'] = 'production'
    end

    opts.on('-Q','--qa', 'Set the environment to QA') do
      @options['env'] = 'qa'
    end

    opts.on('-s', '--staging', "Set the environment to staging (this is the default)") do
      @options['env'] = 'staging'
    end

    #opts.on('-S', '--split-staging', "Set the sub-environment to split_staging") do
    #  @options['sub_env'] = 'split-staging'
    #end

    opts.on('--split-env SPLIT_ENV_NAME', "Set the sub-environment to the specified split_env") do |sub_env|
      @options['sub_env'] = sub_env
    end

    opts.on('-t', '--test', 'Set the environment to test') do
      @options['env'] = 'test'
    end

    # General arguments

    opts.on('-a', '--address ADDRESS', "Run your command against this address") do |addr|
      @options['address'] = addr
    end

    opts.on('-D', '--debug', "Activate extremely verbose logging") do
      @options['debug'] = true
      @options['verbose'] = true
    end

    opts.on('-E', '--search-env-name ENV_NAME', 'For commands that support searching, return results with this environment in them') do |name|
      @options['search_env_name'] = name
    end

    opts.on('-n', '--node-name NAME', "Run your command against this node_name") do |name|
      @options['node_name'] = name
    end

    opts.on('-q', '--quiet', "Deactivates most forms of output") do
      @options['quiet'] = true
    end

    opts.on('-r', '--role-name NAME', "Run your command against this role_name") do |name|
      @options['role'] = name
    end

    opts.on('-R', '--repository NAME', 'Run your command against this repository / context') do |name|
      @options['repository'] = name
    end

    opts.on('-N', '--search-node-name NODE_NAME', 'For commands that support searching, return results with NODE_NAME in them') do |name|
      @options['search_node_name'] = name
    end

    opts.on('-L', '--search-role-name ROLE_NAME', 'For commands that support searching, return results with ROLE_NAME in them') do |name|
      @options['search_role_name'] = name
    end

    opts.on('-v', '--verbose', "Activates slightly more verbose logging, also causes commands to output to terminal and logs") do
      @options['verbose'] = true
    end

    opts.on('-V', '--version', "Displays the current version of cheftacular") do
      @config['helper'].display_currently_installed_version

      exit
    end

    opts.on('--no-logs', "Do not make logs for any command") do
      @options['no_logs'] = true
    end

    opts.on('-h', '--help', 'Displays the README') do
      @config['helper'].display_readme

      puts "Remember, you can also utilize the cft help command!"

      exit
    end

    # Action Arguments

    opts.on('-e', '--except-role NAME', 'For deployments, will prevent the deploy from triggering on servers with this role') do |name|
      @options['negative_role'] = name
    end

    opts.on('-z', '--unset-github-deploy-args', 'Tells the chef-server that we want to return to using the default revision and organization for a repository') do
      @options['unset_github_deploy_args'] = true
    end

    opts.on('-Z REVISION', '--revision REVISION', "Tells the chef-server what branch / revision it should deploy for a repository") do |revision|
      @options['target_revision'] = revision
    end

    opts.on('-O ORGANIZATION', '--deploy-org ORGANIZATION', "Tells the chef-server to use a certain organization on deploys (git user must have access)") do |org|
      @options['deploy_organization'] = org
    end

    # client-list
    opts.on('-W', '--with-priv', "On cft client_list this will show each server's private addresses") do
      @options['with_private'] = true
    end 

    # cft log options
    opts.on('--nginx', "On cft log pass this argument to fetch nginx logs as well as application logs") do
      @options['get_nginx_logs'] = true
    end

    opts.on('--full', "On cft log pass this argument to fetch the FULL log") do
      @options['get_full_logs'] = true
    end

    opts.on('-l INTEGER', '--lines INTEGER', "On cft log pass this argument to fetch the last X lines of logs") do |num|
      @options['get_log_lines'] = num
    end

    opts.on('--num INTEGER', "On cft log pass this argument to fetch the last X lines of logs") do |num|
      @options['get_log_lines'] = num
    end

    #cft run
    opts.on('--all', "On cft run COMMAND you can pass --all to run the command on multiple nodes") do 
      @options['run_on_all'] = true
    end

    #cft service
    opts.on('--sv', "On cft service calls, utilize the runsv syntax instead of the service syntax (can restart sv programs)") do
      @options['runsv'] = true
    end

    #cloud_bootstrap

    opts.on('--with-dn DOMAIN_NAME', "On hip rax_bootstrap allows you to specify a domain structure other than the default environment one") do |domain|
      @options['with_dn'] = domain
    end

    #cloud

    opts.on('-o', '--cloud CLOUD_NAME', "On cft cloud calls, set the cloud to the one you specify") do |cloud_name|
      @options['preferred_cloud'] = cloud_name
    end

    opts.on('--rax', "On cft cloud calls, set the cloud to Rackspace") do
      @options['preferred_cloud'] = 'rackspace'
    end

    opts.on('--aws', "On cft cloud calls, set the cloud to Amazon Web Services") do
      @options['preferred_cloud'] = 'aws'
    end

    opts.on('--do', "On cft cloud calls, set the cloud to DigitalOcean") do
      @options['preferred_cloud'] = 'digitalocean'
    end

    opts.on('--region REGION', 'On cft cloud calls, set the cloud region to perform operations on to this region') do |region|
      @options['preferred_cloud_region'] = region
    end

    opts.on('--image IMAGE', 'On cft cloud calls, set the default image to this image (can be shorthand like "Ubuntu 14.04"') do |image|
      @options['preferred_cloud_image'] = image
    end

    opts.on('--virtualization-mode MODE', 'On cft cloud calls, set the default virtualization mode to this (On rackspace, only PV or PVHVM are supported)') do |v_mode|
      @options['virtualization_mode'] = v_mode
    end

    opts.on('--route-dns-changes-via SERVICE', 'On cft cloud calls, set the default dns provider to this service') do |service|
      @options['route_dns_changes_via'] = service
    end

    #file | chef_server
    opts.on('--save-to-file FILE_NAME', 'On cft file or chef_server, this option can be used to save the output of the file display methods to your system. Also works in the fetch context') do |path|
      @options['save_to_file'] = path
    end

  end.parse!
end

#initialize_audit_bag_contents(env, save_on_finish = false) ⇒ Object

User Action Generated (see Cheftacular::StatelessAction.compile_audit_log)



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/cheftacular/stateless_actions/initialize_data_bag_contents.rb', line 211

def initialize_audit_bag_contents env, save_on_finish=false
  hash = @config[env]['audit_bag_hash']

  unless hash.has_key?('audit_log')
    hash['audit_log'] = {}

    save_on_finish = true
  end

  @config['ChefDataBag'].save_audit_bag(env) if save_on_finish
end

#initialize_authentication_bag_contents(save_on_finish = false, exit_on_finish = false) ⇒ Object



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
# File 'lib/cheftacular/stateless_actions/initialize_data_bag_contents.rb', line 43

def initialize_authentication_bag_contents save_on_finish=false, exit_on_finish=false
  hash = @config['default']['authentication_bag_hash']

  auth_fix_message = "\n\n knife data bag edit default authentication --secret-file " +
  "#{ @config['locs']['chef'] }/#{ @config['cheftacular']['data_bag_key_file'] } \n\n"

  unless hash.has_key?('authorized_keys')
    hash['authorized_keys'] = []

    puts "Critical! No Authorized Keys for ssh detected in the default authentication bag! " +
    "These must be created. Use cft add_ssh_key_to_bag \"YOUR_SSH_KEY\"."

    save_on_finish, exit_on_finish = true,true
  end

  hash['specific_repository_authorized_keys'] = {} unless hash.has_key?('specific_repository_authorized_keys')

  @config['getter'].get_repo_names_for_repositories.each_key do |repo_name|
    unless hash['specific_repository_authorized_keys'].has_key?(repo_name)
      hash['specific_repository_authorized_keys'][repo_name] = []

      save_on_finish = true
    end 
  end

  if @config['cheftacular']['git_based_deploys']
    if !hash.has_key?('git_private_key') || !hash.has_key?('git_public_key') || !hash.has_key?('git_OAuth')
      puts "Warning! github user credentials in default authentication bag were not found! Please run `cft help create_git_key` and then run that command itself!" unless @options['command'] == 'help'
    end
  end

  @config['ChefDataBag'].save_authentication_bag if save_on_finish

  exit if exit_on_finish && @options['command'] != 'create_git_key'
end

#initialize_bag_for_all_environments(bag_name, total_percent = 100) ⇒ Object



597
598
599
600
601
602
603
604
605
606
607
608
# File 'lib/cheftacular/initializer.rb', line 597

def initialize_bag_for_all_environments bag_name, total_percent=100
  total_bags   = @config['chef_environments'].count
  current_bags = 1

  @config['chef_environments'].each do |env|
    @config['ChefDataBag'].init_bag env, bag_name

    @config['helper'].completion_rate? (percent + (( (current_bags).to_f / total_bags.to_f ) * 100) / ( 100.to_f / total_percent.to_f ) ), __method__

    current_bags += 1
  end
end

#initialize_chef_passwords_bag_contents(env, save_on_finish = false, exit_on_finish = false) ⇒ Object

TODO Reexamine, this might cause issues with the nested hash on encrypted saves



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/cheftacular/stateless_actions/initialize_data_bag_contents.rb', line 103

def initialize_chef_passwords_bag_contents env, save_on_finish=false, exit_on_finish=false
  hash = @config[env]['chef_passwords_bag_hash']

  @config['cheftacular']['global_chef_passwords'].each_pair do |pass_key, pass_length|
    hash[pass_key] = @config['helper'].gen_pass(pass_length) unless hash.has_key?(pass_key)

    save_on_finish = true
  end

  @config['getter'].get_repo_names_for_repositories.each_pair do |repo_name, repo_hash|
    hash[repo_name] = {} unless hash.has_key?(repo_name)

    if repo_hash.has_key?('specific_chef_passwords')
      repo_hash['specific_chef_passwords'].each_pair do |pass_key, pass_length|
        unless hash[repo_name].has_key?(pass_key)
          hash[repo_name][pass_key] = @config['helper'].gen_pass(pass_length)

          save_on_finish = true
        end
      end
    end
    
    save_on_finish = true unless hash[repo_name].has_key?(repo_name)
  end

  @config['ChefDataBag'].save_chef_passwords_bag(env) if save_on_finish
end

#initialize_chef_repo_up_to_dateObject



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/cheftacular/initializer.rb', line 564

def initialize_chef_repo_up_to_date
  if @config['helper'].running_in_mode?('devops')
    @config['cheftacular']['wrapper_cookbooks'].split(',').each do |wrapper_cookbook|
      parsed_hash = if File.exists?( @config['filesystem'].current_chef_repo_cheftacular_file_cache_path ) 
                      File.read( @config['filesystem'].current_chef_repo_cheftacular_file_cache_path )
                    else
                      Digest::SHA2.hexdigest(@config['helper'].compile_chef_repo_cheftacular_yml_as_hash.to_yaml.to_s)
                    end

      wrapper_cookbook_cheftacular_loc = "#{ @config['locs']['cookbooks'] }/#{ wrapper_cookbook }" +
        @config['cheftacular']['location_of_chef_repo_cheftacular_yml'] + '/cheftacular.yml'

      unless File.exist?(wrapper_cookbook_cheftacular_loc)
        puts "Wrapper cookbook \"#{ wrapper_cookbook }\" does not have a cheftacular.yml file in #{ @config['cheftacular']['location_of_chef_repo_cheftacular_yml'] }! Creating..."

        @config['filesystem'].write_chef_repo_cheftacular_yml_file wrapper_cookbook_cheftacular_loc
      end

      if parsed_hash == Digest::SHA2.hexdigest(File.read(wrapper_cookbook_cheftacular_loc))
        next if File.exists?( @config['filesystem'].current_chef_repo_cheftacular_file_cache_path )
      else
        puts "Wrapper cookbook (#{ wrapper_cookbook }) does not have a current cheftacular.yml file in #{ @config['cheftacular']['location_of_chef_repo_cheftacular_yml'] }\"! Overwriting..."

        @config['filesystem'].write_chef_repo_cheftacular_yml_file wrapper_cookbook_cheftacular_loc
      end

      puts "Creating file cache for #{ Time.now.strftime("%Y%m%d") }'s cheftacular.yml."

      @config['filesystem'].write_chef_repo_cheftacular_cache_file parsed_hash
    end
  end
end

#initialize_cheftacular_data_bag_contentsObject



223
224
225
226
227
# File 'lib/cheftacular/stateless_actions/initialize_data_bag_contents.rb', line 223

def initialize_cheftacular_data_bag_contents
  @config['default']['cheftacular_bag_hash'] = @config['cheftacular']
  
  @config['ChefDataBag'].save_cheftacular_bag
end

#initialize_classesObject



499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/cheftacular/initializer.rb', line 499

def initialize_classes
  @config['auditor']                        = Cheftacular::Auditor.new(@options, @config)
  @config['parser']                         = Cheftacular::Parser.new(@options, @config)
  @config['getter']                         = Cheftacular::Getter.new(@options, @config)
  @config['action']                         = Cheftacular::Action.new(@options, @config)
  @config['stateless_action']               = Cheftacular::StatelessAction.new(@options, @config)
  @config['encryptor']                      = Cheftacular::Encryptor.new(@config['data_bag_secret'])
  @config['decryptor']                      = Cheftacular::Decryptor.new(@config['data_bag_secret'])
  @config['action_documentation']           = Cheftacular::ActionDocumentation.new(@options, @config)
  @config['stateless_action_documentation'] = Cheftacular::StatelessActionDocumentation.new(@options, @config)
  @config['error']                          = Cheftacular::Error.new(@options, @config)
  @config['pleasantries']                   = Cheftacular::Pleasantries.new(@options, @config)
  @config['dummy_sshkit']                   = SSHKit::Backend::Netssh.new(SSHKit::Host.new('127.0.0.1'))
  @config['DNS']                            = Cheftacular::DNS.new(@options, @config)
  @config['queue_master']                   = Cheftacular::QueueMaster.new(@options, @config)
  @config['cloud_provider']                 = Cheftacular::CloudProvider.new(@options, @config)
end

#initialize_cloud_checks(exit_on_finish = false) ⇒ Object



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/cheftacular/initializer.rb', line 521

def initialize_cloud_checks exit_on_finish = false
  hash = @config['cheftacular']['cloud_authentication']

  unless hash.has_key?(@options['preferred_cloud'])
    puts "Critical! No Cloud credentials detected for your preferred cloud #{ @options['preferred_cloud'] }, " +
    "Please update the cheftacular.yml cloud_authentication:#{ @options['preferred_cloud'] } key!"

    exit_on_finish = true
  end

  if hash.has_key?('rackspace')
    if !( hash['rackspace'].has_key?('username') || hash['rackspace'].has_key?('api_key') || hash['rackspace'].has_key?('email')) 

      puts "Critical! No cloud credentials detected for the rackspace cloud_authentication hash! There must be both a valid username and an api_key in this hash!"
      puts "Please update the cheftacular.yml cloud_authentication:rackspace key!"

      exit_on_finish = true
    elsif hash['rackspace']['username'].empty? || hash['rackspace']['api_key'].empty? || hash['rackspace']['email'].empty?
      puts "Critical! Cloud credentials detected for the rackspace cloud_authentication hash are blank!"
      puts "Please update the cheftacular.yml cloud_authentication:rackspace key!"

      exit_on_finish = true
    end
  end

  if hash.has_key?('digitalocean')
    if !( hash['digitalocean'].has_key?('client_id') || hash['digitalocean'].has_key?('api_key')) 

      puts "Critical! No cloud credentials detected for the digitalocean cloud_authentication hash! There must be both a valid client_id and an api_key in this hash!"
      puts "Please update the cheftacular.yml cloud_authentication:digitalocean key!"

      exit_on_finish = true
    elsif hash['digitalocean']['client_id'].empty? || hash['digitalocean']['api_key'].empty?
      puts "Critical! Cloud credentials detected for the digitalocean cloud_authentication hash are blank!"
      puts "Please update the cheftacular.yml cloud_authentication:digitalocean key!"

      exit_on_finish = true
    end
  end

  exit if exit_on_finish
end

#initialize_cloud_optionsObject



318
319
320
# File 'lib/cheftacular/initializer.rb', line 318

def initialize_cloud_options
  @config['helper'].set_cloud_options
end

#initialize_config_bag_contents(main_env, save_on_finish = false, exit_on_finish = false) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/cheftacular/stateless_actions/initialize_data_bag_contents.rb', line 131

def initialize_config_bag_contents main_env, save_on_finish=false, exit_on_finish=false
  hash = @config[main_env]['config_bag_hash']

  envs_to_build_in_hash = [main_env]

  config_fix_message = "\n\n knife data bag edit #{ main_env } config\n\n"

  if @config['cheftacular']['run_list_environments'].has_key?(main_env)
    @config['cheftacular']['run_list_environments'][main_env].each_value do |sub_env|
      envs_to_build_in_hash << sub_env
    end
  end

  envs_to_build_in_hash.each do |env|
    hash[env] ||= {}
    
    if !hash[env].has_key?('tld') || ( hash[env].has_key?('tld') && hash[env]['tld'].blank? )
      hash[env]['tld'] = "" unless hash[env].has_key?('tld')

      puts "WARNING! The config bag in environment: #{ main_env }(sub-env: #{ env }) does not have a top level domain set!"
      puts "Please run #{ config_fix_message }And update the tld key!"

      save_on_finish, exit_on_finish = true,true 
    end

    unless hash[env].has_key?('restore_backups')
      hash[env]['restore_backups'] = false

      save_on_finish = true
    end

    unless hash[env].has_key?('fetch_backups')
      hash[env]['fetch_backups'] = false

      save_on_finish = true
    end

    unless hash[env].has_key?('app_revisions')
      hash[env]['app_revisions'] = {}

      save_on_finish = true
    end

    @config['getter'].get_repo_names_for_repositories.each_key do |repo_name|
      #upgrades pre 2.7.2 to 2.8.0+ for config bag formats TODO DELETE ME (remove after 3.0.0)
      if hash[env]['app_revisions'].has_key?(repo_name) && hash[env]['app_revisions'][repo_name].class.to_s == 'String'
        hash[env]['app_revisions'][repo_name] = { "revision" => hash[env]['app_revisions'][repo_name] }
      end

      unless hash[env]['app_revisions'].has_key?(repo_name)
        hash[env]['app_revisions'][repo_name] = {}

        save_on_finish = true
      end 
    end

    unless hash[env].has_key?('cloudflare_activated_domains')
      hash[env]['cloudflare_activated_domains'] = []

      save_on_finish = true
    end
  end

  @config['ChefDataBag'].save_config_bag(main_env) if save_on_finish
end

#initialize_data_bag_cheftacular_hashObject



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/cheftacular/initializer.rb', line 288

def initialize_data_bag_cheftacular_hash
  initialize_ridley

  @config['ChefDataBag'] ||= Cheftacular::ChefDataBag.new(@options, @config)

  @config['ChefDataBag'].init_bag('default', 'cheftacular', false)

  @config['initial_cheftacular_yml'] = @config['cheftacular'].deep_dup

  @config['cheftacular'] = @config['default']['cheftacular_bag_hash'].deep_merge(@config['cheftacular'])

  parsed_cheftacular = Digest::SHA2.hexdigest(@config['helper'].get_cheftacular_yml_as_hash.to_s)

  return true if File.exist?(@config['filesystem'].local_cheftacular_file_cache_path) && File.read(@config['filesystem'].local_cheftacular_file_cache_path) == parsed_cheftacular

  @config['helper'].display_cheftacular_config_diff

  puts "Creating file cache for #{ Time.now.strftime("%Y%m%d") }'s local cheftacular.yml."

  @config['filesystem'].write_local_cheftacular_cache_file parsed_cheftacular
end

#initialize_data_bags_for_environment(env, in_initializer = false, bags_to_load = []) ⇒ Object



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/cheftacular/initializer.rb', line 395

def initialize_data_bags_for_environment env, in_initializer=false, bags_to_load=[]
  @config['ChefDataBag'] ||= Cheftacular::ChefDataBag.new(@options, @config)

  puts("Loading additional data bag data from chef server for environment \"#{ env }\" for bags: #{ bags_to_load.join(', ') }") if !in_initializer && !@options['quiet']

  @config['ChefDataBag'].init_bag('default', 'authentication') if bags_to_load.empty? || bags_to_load.include?('authentication')

  @config['ChefDataBag'].init_bag('default', 'cheftacular', false) if bags_to_load.include?('cheftacular')

  @config['ChefDataBag'].init_bag('default', 'environment_config', false) if bags_to_load.empty? || bags_to_load.include?('environment_config')

  @config['helper'].completion_rate?(38, 'initializer') if in_initializer

  @config['ChefDataBag'].init_bag(env, 'addresses', false) if bags_to_load.empty? || bags_to_load.include?('addresses')

  @config['helper'].completion_rate?(46, 'initializer') if in_initializer

  @config['ChefDataBag'].init_bag(env, 'audit', false) if bags_to_load.empty? || bags_to_load.include?('audit')

  @config['helper'].completion_rate?(54, 'initializer') if in_initializer

  @config['ChefDataBag'].init_bag(env, 'chef_passwords') if bags_to_load.empty? || bags_to_load.include?('chef_passwords')

  @config['helper'].completion_rate?(62, 'initializer') if in_initializer

  @config['ChefDataBag'].init_bag(env, 'config', false) if bags_to_load.empty? || bags_to_load.include?('config')

  @config['helper'].completion_rate?(70, 'initializer') if in_initializer

  @config['ChefDataBag'].init_bag(env, 'logs', false) if bags_to_load.empty? || bags_to_load.include?('logs')

  @config['helper'].completion_rate?(78, 'initializer') if in_initializer

  @config['ChefDataBag'].init_bag(env, 'node_roles', false) if bags_to_load.empty? || bags_to_load.include?('node_roles')

  @config['helper'].completion_rate?(86, 'initializer') if in_initializer

  @config['ChefDataBag'].init_bag(env, 'server_passwords') if bags_to_load.empty? || bags_to_load.include?('server_passwords')
end

#initialize_default_cheftacular_optionsObject



283
284
285
286
# File 'lib/cheftacular/initializer.rb', line 283

def initialize_default_cheftacular_options
  @options['env']        = @config['cheftacular']['default_environment'] if @config['cheftacular'].has_key?('default_environment')
  @options['repository'] = @config['cheftacular']['default_repository']  if @config['cheftacular'].has_key?('default_repository')
end

#initialize_directoriesObject



517
518
519
# File 'lib/cheftacular/initializer.rb', line 517

def initialize_directories
  @config['filesystem'].initialize_log_directories
end

#initialize_documentation_hashObject



327
328
329
330
331
332
333
334
# File 'lib/cheftacular/initializer.rb', line 327

def initialize_documentation_hash
  @config['documentation'] ||= {}
  @config['documentation']['arguments']        ||= []
  @config['documentation']['action']           ||= {}
  @config['documentation']['stateless_action'] ||= {}
  @config['documentation']['application']      ||= {}
  @config['documentation']['devops']           ||= {}
end

#initialize_environment_config_bag_contentsObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/cheftacular/stateless_actions/initialize_data_bag_contents.rb', line 229

def initialize_environment_config_bag_contents
  return true if File.exist?(@config['filesystem'].current_environment_config_cache_file_path)

  hash = @config['default']['environment_config_bag_hash']

  current_env_bag_names = []

  @config[@options['env']].keys.each do |bag_name|
    next if bag_name.include?('_bag_hash')

    current_env_bag_names << bag_name.gsub('_bag','')
  end

  @config['chef_environments'].each do |env|
    next if @config['ridley'].data_bag.find(env).nil?

    hash[env] ||= {}
    bags        = []

    current_env_bag_names.each do |bag_name|
      next if @config['ridley'].data_bag.find(env).item.find(bag_name).nil?

      bags << bag_name
    end

    hash[env]['bags'] = bags
  end

  @config['ChefDataBag'].save_environment_config_bag

  puts "Creating file cache for #{ Time.now.strftime("%Y%m%d") }'s environment_config bag cache."

  @config['filesystem'].write_environment_config_cache
end

#initialize_locationsObject



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/cheftacular/initializer.rb', line 336

def initialize_locations
  locs ||= {}

  if @config['helper'].running_in_mode? 'application'
    locs['root']     = Dir.getwd
    locs['chef-log'] = File.join( locs['root'], 'log')
    locs['app-root'] = locs['root']
  elsif @config['helper'].running_on_chef_node?
    locs['chef-log'] = File.expand_path("/root/sensu_log")
    locs['chef']     = File.expand_path("/etc/chef")
    locs['ssh']      = File.expand_path('/home/deploy/.ssh')
  end

  locs['chef-repo']             = Dir.getwd
  locs['true-root']             = File.expand_path("#{ locs['chef-repo'] }/..")
  locs['roles']                 = File.expand_path("#{ locs['chef-repo'] }/roles")
  locs['nodes']                 = File.expand_path("#{ locs['chef-repo'] }/nodes_dir") #DO NOT RENAME THIS TO NODES
  locs['root']                  = locs['chef-repo']                                    unless locs['root']
  locs['app-root']              = locs['chef-repo']                                    unless locs['app-root']
  locs['chef']                  = File.expand_path("~/.chef")                          unless locs['chef']
  locs['cookbooks']             = File.expand_path("#{ locs['chef-repo'] }/cookbooks")
  locs['berks']                 = File.expand_path('~/.berkshelf/cookbooks')
  locs['wrapper-cookbooks']     = @config['cheftacular']['wrapper_cookbooks']          unless @config['helper'].running_in_mode?('application')
  locs['ssh']                   = File.expand_path('~/.ssh')
  locs['chef-log']              = File.expand_path("#{ locs['root']}/log")             unless locs['chef-log']
  locs['app-tmp']               = File.expand_path("#{ locs['app-root']}/tmp")
  locs['examples']              = File.expand_path("../../../examples", __FILE__)
  locs['doc']                   = File.expand_path("../../../doc", __FILE__)
  locs['cheftacular-lib']       = File.expand_path("../..", __FILE__)
  locs['cheftacular-lib-files'] = locs['cheftacular-lib'] + '/cheftacular/files'

  @config['locs'] = locs
end

#initialize_log_bag_contents(env) ⇒ Object

User Action Generated: {“#{ NODE_NAME }-#{ COMMAND }”: “CONTENT FROM RUN”}



98
99
100
# File 'lib/cheftacular/stateless_actions/initialize_data_bag_contents.rb', line 98

def initialize_log_bag_contents env

end

#initialize_monkeypatchesObject



310
311
312
313
314
315
316
# File 'lib/cheftacular/initializer.rb', line 310

def initialize_monkeypatches
  if File.exists?(File.expand_path("#{ @config['locs']['app-root'] }/config/initializers/cheftacular.rb"))
    puts "Cheftacular Monkeypatch file detected! Preparing to require..."

    require "#{ @config['locs']['app-root'] }/config/initializers/cheftacular"
  end
end

#initialize_node_roles_bag_contents(env) ⇒ Object

User Action Generated (see Cheftacular::StatelessAction.upload_nodes)



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/cheftacular/stateless_actions/initialize_data_bag_contents.rb', line 198

def initialize_node_roles_bag_contents env
  hash = @config[env]['node_roles_bag_hash']

  unless hash.has_key?('node_roles')
    hash['node_roles'] = {}

    save_on_finish = true
  end

  @config['ChefDataBag'].save_node_roles_bag(env) if save_on_finish
end

#initialize_override_yaml_configurationObject



277
278
279
280
281
# File 'lib/cheftacular/initializer.rb', line 277

def initialize_override_yaml_configuration
  @config['cheftacular_overrides'] = @config['helper'].get_cheftacular_yml_override

  @config['cheftacular'] = @config['cheftacular'].merge(@config['cheftacular_overrides'])
end

#initialize_passwords(env, refresh_bag = false) ⇒ Object



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/cheftacular/initializer.rb', line 458

def initialize_passwords env, refresh_bag=false
  @config['server_passwords'] ||= {}

  @config[env]['server_passwords_bag'].reload if refresh_bag

  @config[env]['server_passwords_bag_hash'] = @config[env]['server_passwords_bag'].decrypt.to_hash if refresh_bag

  #data_hash will be { server_name: 'SERVER_NAME', password: 'PASSWORD_STRING' }
  @config[env]['server_passwords_bag_hash'].each_pair do |key, data_hash|
    if key.include?('-deploy-pass')
      addr = key.split('-deploy-pass').first

      @config['server_passwords'][addr] = data_hash
    end
  end
end

#initialize_queuesObject



268
269
270
271
# File 'lib/cheftacular/initializer.rb', line 268

def initialize_queues
  @config['slack_queue']           ||= []
  @config['server_creation_queue'] ||= []
end

#initialize_ridleyObject



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/cheftacular/initializer.rb', line 370

def initialize_ridley
  return unless @config['ridley'].nil?

  @config['data_bag_secret'] = File.read(File.expand_path("#{ @config['locs']['chef'] }/#{ @config['cheftacular']['data_bag_key_file'] }")).chomp

  Ridley::Logging.logger.level = Logger.const_get 'ERROR'

  @config['ridley'] = Ridley.new(
    server_url:                @config['cheftacular']['chef_server_url'],
    client_name:               (@config['helper'].running_on_chef_node? ? @config['helper'].parse_node_name_from_client_file : @config['cheftacular']['cheftacular_chef_user']),
    client_key:                File.expand_path("#{ @config['locs']['chef'] }/#{ @config['helper'].running_on_chef_node? ? 'client' : @config['cheftacular']['cheftacular_chef_user'] }.pem"),
    encrypted_data_bag_secret: @config['data_bag_secret'],
    ssl:                       { verify: @config['cheftacular']['ssl_verify'] }
  )
end

#initialize_ridley_environmentsObject



386
387
388
# File 'lib/cheftacular/initializer.rb', line 386

def initialize_ridley_environments
  @config['chef_environments'] ||= @config['ridley'].environment.all.map { |env| env.name }.delete_if { |env| env == '_default' }
end

#initialize_ridley_roles_and_nodesObject



390
391
392
393
# File 'lib/cheftacular/initializer.rb', line 390

def initialize_ridley_roles_and_nodes
  @config['chef_nodes'] ||= @config['ridley'].node.all
  @config['chef_roles'] ||= @config['ridley'].role.all
end

#initialize_ruby_configObject



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/cheftacular/initializer.rb', line 435

def initialize_ruby_config
  @config['ruby_string'] = @config['cheftacular']['ruby_version']

  begin
    @config['ruby_string'] = File.read(File.expand_path("#{ locs['app-root'] }/.ruby-version")) unless @config['ruby_string']
  rescue StandardError => e
    msg = [
      "Please run this in the root of your application directory,",
      "a ruby string to run commands against was not found in either your cheftacular.yml file or your .ruby-version file."
    ].join(' ')

    @config['error'].exception_output msg, e
  end
  
  @config['ruby_string'] = "ruby-" + @config['ruby_string'] unless @config['ruby_string'].include?('ruby-')

  #TODO Reevaluate for non-rvm setups
  @config['bundle_command']        = "/home/#{ @config['cheftacular']['deploy_user'] }/.rvm/gems/#{ @config['ruby_string'].chomp }@global/bin/bundle"
  @config['ruby_command']          = "/home/#{ @config['cheftacular']['deploy_user'] }/.rvm/rubies/#{ @config['ruby_string'].chomp }/bin/ruby"
  @config['internal_ruby_config']  = File.expand_path(__FILE__)[/(ruby\-[\d\.@\w]+)/]
  #@config['internal_ruby_version'] = @config['internal_ruby_config'][/([\d\.]+)/] #reactivate when needed
end

#initialize_server_passwords_bag_contents(env) ⇒ Object

User Action Generated: “S325DSAGBCVfg5”, “1.2.3.4-root-pass”: “7dfDSFgb5%231”, “1.2.3.4-name”: “test”



80
81
82
# File 'lib/cheftacular/stateless_actions/initialize_data_bag_contents.rb', line 80

def initialize_server_passwords_bag_contents env

end

#initialize_sub_environmentObject

only matters to the config_bag and it’s hash. Used to fetch keys within the bag for certain commands



323
324
325
# File 'lib/cheftacular/initializer.rb', line 323

def initialize_sub_environment
  @options['sub_env'] ||= @options['env']
end

#initialize_version_checkObject



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/cheftacular/initializer.rb', line 475

def initialize_version_check
  current_version = Cheftacular::VERSION

  @config['helper'].set_detected_cheftacular_version

  if @config['helper'].is_higher_version? @config['detected_cheftacular_version'], current_version
    puts "\n Your Cheftacular is out of date. Currently #{ current_version } and remote version is #{ @config['detected_cheftacular_version'] }.\n"

    if @config['internal_ruby_config'].include?('@global')
      puts "Please run rvm #{ @config['internal_ruby_config'] } do gem update cheftacular to update to the latest version"
    else
      @config['stateless_action'].update_cheftacular
    end

    exit
  else
    unless File.exists?( @config['filesystem'].current_version_file_path )
      puts "Creating file cache for #{ Time.now.strftime("%Y%m%d") } (#{ @config['detected_cheftacular_version'] }). No new version detected."

      @config['filesystem'].write_version_file @config['detected_cheftacular_version']
    end
  end
end

#initialize_yaml_configurationObject



273
274
275
# File 'lib/cheftacular/initializer.rb', line 273

def initialize_yaml_configuration
  @config['cheftacular'] = @config['helper'].get_cheftacular_yml_as_hash
end