Class: Shiplane::ChefHost

Inherits:
Host
  • Object
show all
Includes:
Airbrussh::Colors
Defined in:
lib/capistrano/chef_host.rb

Constant Summary collapse

REMOTE_CHEF_FOLDER_PATH =
File.join("/var","chef")
LOCAL_CHEF_FOLDER_PATH =
File.expand_path("../../../lib/chef", __FILE__)
LOCAL_CUSTOM_CONFIGURATION_FOLDER_PATH =
File.join(Dir.pwd, '.shiplane', 'bootstrap_config')
REMOTE_CUSTOM_CONFIGURATION_FOLDER_PATH =
File.join(REMOTE_CHEF_FOLDER_PATH, 'cookbooks', 'barebones-docker', 'files', 'default')
COOKBOOKS_FILE_NAME =
"cookbooks.tar.gz"
LOCAL_COOKBOOKS_FOLDER_PATH =
File.expand_path("../../../cookbooks", __FILE__)
LOCAL_SITE_COOKBOOKS_FOLDER_PATH =
File.expand_path("../../../site-cookbooks", __FILE__)
LOCAL_SITE_BERKS_COOKBOOKS_FOLDER_PATH =
File.expand_path("../../../berks-cookbooks", __FILE__)
REMOTE_COOKBOOKS_FOLDER_PATH =
File.join(REMOTE_CHEF_FOLDER_PATH, 'cookbooks')
LOCAL_COOKBOOKS_FILE_PATH =
File.expand_path("../../../#{COOKBOOKS_FILE_NAME}", __FILE__)
REMOTE_COOKBOOKS_FILE_PATH =
File.join(REMOTE_CHEF_FOLDER_PATH, COOKBOOKS_FILE_NAME)
CHEF_PACKAGE_NAME =
config.fetch("bootstrap", {}).fetch("chef-bootstrapper", {}).fetch("package_name")
CHEF_PACKAGE_DOWNLOAD_URL =
config.fetch("bootstrap", {}).fetch("chef-bootstrapper", {}).fetch("package_url")
APT_PACKAGES =
%w(build-essential wget)

Instance Method Summary collapse

Instance Method Details

#configureObject



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
# File 'lib/capistrano/chef_host.rb', line 66

def configure
  with_context do
    errors = {}
    SSHKit.config.default_env["CHEF_LICENSE"] = "accept"
    SSHKit::Coordinator.new(host).each in: :parallel do |h|
      context_variables = fetch(:shiplane_sshkit_values)

      begin
        execute :sudo, 'chef-solo', '-c', "#{Shiplane::ChefHost::REMOTE_CHEF_FOLDER_PATH}/solo.rb", interaction_handler: context_variables[:interaction_handler]
      rescue => e
        errors["#{h}"] = Shiplane::ChefErrorParser.parse(e)
      end
    end

    unless errors.empty?
      write_message(SSHKit::Logger::ERROR, "#{errors.keys.size} Errors encountered:")
      errors.each do |h, trace|
        write_message SSHKit::Logger::INFO, "~" * 80
        write_message SSHKit::Logger::INFO, green("Server: #{h}")
        trace.each do |line|
          write_message SSHKit::Logger::INFO, line
        end
        write_message SSHKit::Logger::INFO, "~" * 80
      end
    end
  end
end

#installObject



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
# File 'lib/capistrano/chef_host.rb', line 23

def install
  with_context do
    SSHKit::Coordinator.new(host).each in: :parallel do
      context_variables = fetch(:shiplane_sshkit_values)

      install_started = test("[ -f #{File.join(Shiplane::ChefHost::REMOTE_CHEF_FOLDER_PATH, '.install-started')} ]")
      install_finished = test("[ -f #{File.join(Shiplane::ChefHost::REMOTE_CHEF_FOLDER_PATH, '.install')} ]")

      if install_started && !install_finished
        execute :sudo, :dpkg, '--configure', '-a', interaction_handler: context_variables[:interaction_handler]
        # execute :sudo, :dpkg, "--remove", "--force-remove-reinstreq", *Shiplane::ChefHost::APT_PACKAGES - %w(wget build-essential), interaction_handler: context_variables[:interaction_handler]
      end

      unless install_finished
        execute :sudo, :sysctl, "-w", "net.ipv6.conf.all.disable_ipv6=1", interaction_handler: context_variables[:interaction_handler]
        execute :sudo, :sysctl, "-w", "net.ipv6.conf.default.disable_ipv6=1", interaction_handler: context_variables[:interaction_handler]
        execute :sudo, :mkdir, '-m', '2777', '-p', Shiplane::ChefHost::REMOTE_CHEF_FOLDER_PATH, interaction_handler: context_variables[:interaction_handler]
        execute :sudo, :touch, File.join(Shiplane::ChefHost::REMOTE_CHEF_FOLDER_PATH, '.install-started'), interaction_handler: context_variables[:interaction_handler]
        execute :sudo, 'apt-get', 'update', interaction_handler: context_variables[:interaction_handler]
        execute :sudo, 'apt-get', 'install', '-y', *Shiplane::ChefHost::APT_PACKAGES, interaction_handler: context_variables[:interaction_handler]
        execute :wget, Shiplane::ChefHost::CHEF_PACKAGE_DOWNLOAD_URL
        execute :sudo, :dpkg, '-i', Shiplane::ChefHost::CHEF_PACKAGE_NAME, interaction_handler: context_variables[:interaction_handler]
        execute :sudo, :ls, '-al', Shiplane::ChefHost::REMOTE_CHEF_FOLDER_PATH, interaction_handler: context_variables[:interaction_handler]
        execute :sudo, :touch, File.join(Shiplane::ChefHost::REMOTE_CHEF_FOLDER_PATH, '.install'), interaction_handler: context_variables[:interaction_handler]
        execute :sudo, :sysctl, "-w", "net.ipv6.conf.all.disable_ipv6=0", interaction_handler: context_variables[:interaction_handler]
        execute :sudo, :sysctl, "-w", "net.ipv6.conf.default.disable_ipv6=0", interaction_handler: context_variables[:interaction_handler]
      end
    end
  end
end

#reinstallObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/capistrano/chef_host.rb', line 54

def reinstall
  with_context do
    SSHKit::Coordinator.new(host).each in: :parallel do
      context_variables = fetch(:shiplane_sshkit_values)

      if(test("[ -f #{File.join(Shiplane::ChefHost::REMOTE_CHEF_FOLDER_PATH, '.install')} ]"))
        execute :sudo, :rm, File.join(Shiplane::ChefHost::REMOTE_CHEF_FOLDER_PATH, '.install'), interaction_handler: context_variables[:interaction_handler]
      end
    end
  end
end