Class: Simp::Cli::Config::Item::YumRepositories

Inherits:
ActionItem show all
Defined in:
lib/simp/cli/config/item/yum_repositories.rb

Instance Attribute Summary collapse

Attributes inherited from Simp::Cli::Config::Item

#allow_user_apply, #config_items, #description, #die_on_apply_fail, #fact, #fail_on_missing_answer, #key, #next_items_tree, #silent, #skip_apply, #skip_query, #skip_yaml, #value

Instance Method Summary collapse

Methods inherited from ActionItem

#query, #to_yaml_s, #validate

Methods included from SafeApplying

#safe_apply

Methods inherited from Simp::Cli::Config::Item

#default_value, #highline_question_type, #next_items, #not_valid_message, #os_value, #print_banner, #print_summary, #puppet_value, #query, #query_ask, #query_extras, #query_status, #recommended_value, #safe_apply, #say_blue, #say_green, #say_red, #say_yellow, #to_yaml_s, #validate

Constructor Details

#initializeYumRepositories

Returns a new instance of YumRepositories.



12
13
14
15
16
17
18
19
# File 'lib/simp/cli/config/item/yum_repositories.rb', line 12

def initialize
  super
  @key         = 'yum::repositories'
  @description = %Q{Sets up the yum repositores for SIMP on apply. (apply-only; noop)}
  @www_yum_dir = File.exists?( '/var/www/yum/') ? '/var/www/yum' : '/srv/www/yum'
  @yum_repos_d = '/etc/yum.repos.d'
  @yaml_file   = '/etc/puppet/environments/simp/hieradata/hosts/puppet.your.domain.yaml'
end

Instance Attribute Details

#www_yum_dirObject

Returns the value of attribute www_yum_dir.



11
12
13
# File 'lib/simp/cli/config/item/yum_repositories.rb', line 11

def www_yum_dir
  @www_yum_dir
end

#yaml_fileObject

Returns the value of attribute yaml_file.



11
12
13
# File 'lib/simp/cli/config/item/yum_repositories.rb', line 11

def yaml_file
  @yaml_file
end

#yum_repos_dObject

Returns the value of attribute yum_repos_d.



11
12
13
# File 'lib/simp/cli/config/item/yum_repositories.rb', line 11

def yum_repos_d
  @yum_repos_d
end

Instance Method Details

#applyObject



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
# File 'lib/simp/cli/config/item/yum_repositories.rb', line 21

def apply
  result = true

  # set up yum repos
  say_green 'Updating YUM Updates Repositories (NOTE: This may take some time)' if !@silent
  yumpath = File.join( @www_yum_dir,
                       Facter.value('operatingsystem'),
                       Facter.value('operatingsystemrelease'),
                       Facter.value('architecture')
                     )
  begin
    Dir.chdir(yumpath) do
      FileUtils.mkdir('Updates') unless File.directory?('Updates')
      Dir.chdir('Updates') do
        system( %q(find .. -type f -name '*.rpm' -exec ln -sf {} \\;) )
        cmd = 'createrepo -qqq -p --update .'
        if @silent
          cmd << ' &> /dev/null'
        else
          puts cmd
        end
        system(cmd)
        raise RuntimeError "'#{cmd}' failed in #{Dir.pwd}" unless ($?.nil? || $?.success?)
      end
    end
    system("chown -R root:apache #{@www_yum_dir}/ #{ '&> /dev/null' if @silent }")
    system("chmod -R u=rwX,g=rX,o-rwx #{@www_yum_dir}/")
    raise RuntimeError, "chmod -R u=rwX,g=rX,o-rwx #{@www_yum_dir}/ failed!"  unless ($?.nil? || $?.success?)
    say_green "Finished configuring Updates repository at #{yumpath}/Updates" if !@silent
  rescue => err
    say_red "ERROR: Something went wrong setting up the Updates repo in #{yumpath}!"
    say_red '       Please make sure your Updates repo is properly configured.'
    say_red "\nError output:\n  #{err.class}\n\n  #{err}"
    result = false
  end

  # disable any CentOS repo spam
  Dir.chdir( @yum_repos_d ) do
    if ! Dir.glob('CentOS*.repo').empty?
      `grep "\\[*\\]" *CentOS*.repo | cut -d "[" -f2 | cut -d "]" -f1 | xargs yum-config-manager --disable`
    end

    # enable 'simp::yum::enable_simp_repos' in hosts/puppet.your.domain.yaml
    if @config_items.fetch('is_master_yum_server').value && !File.exist?('filesystem.repo')
      cmd = %Q{sed -i '/simp::yum::enable_simp_repos : false/ c\\simp::yum::enable_simp_repos : true' #{@yaml_file}}
      puts cmd if !@silent
      %x{#{cmd}}
      result = result && ($?.nil? || $?.success?)
    end
  end

  result
end