Class: DreamOps::BaseDeployer

Inherits:
Object
  • Object
show all
Defined in:
lib/dream-ops/deployment/base.rb

Direct Known Subclasses

OpsWorksDeployer, SoloDeployer

Constant Summary collapse

@@spinner =
ThreadedEnum.new do |e|
  loop do
    e << '|'
    e << '/'
    e << '-'
    e << '\\'
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deployer_method(name) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/dream-ops/deployment/base.rb', line 23

def deployer_method(name)
  class_eval <<-EOH, __FILE__, __LINE__ + 1
    def #{name}(*args)
      raise AbstractFunction,
        "##{name} must be implemented on \#{self.class.name}!"
    end
  EOH
end

Instance Method Details

#__bail_with_fatal_error(ex) ⇒ Object



79
80
81
82
# File 'lib/dream-ops/deployment/base.rb', line 79

def __bail_with_fatal_error(ex)
  raise ex
  Thread.exit
end

#__get_cookbook_pathsObject



72
73
74
75
76
77
# File 'lib/dream-ops/deployment/base.rb', line 72

def __get_cookbook_paths()
  # Treat any directory with a Berksfile as a cookbook
  cookbooks = Dir.glob('./**/Berksfile')

  return cookbooks.map { |path| path.gsub(/(.*)(\/Berksfile)(.*)/, '\1') }
end

#analyze(*args) ⇒ Object

Create a deployer method for the declaration



57
# File 'lib/dream-ops/deployment/base.rb', line 57

deployer_method :analyze

#build_cookbook(cookbook) ⇒ Object

Collect cookbook dependencies and compress based on file extension



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dream-ops/deployment/base.rb', line 85

def build_cookbook(cookbook)
  berksfile = Berkshelf::Berksfile.from_file(File.join(cookbook[:path], "Berksfile"))
  berksfile.vendor("berks-cookbooks")

  File.open("berks-cookbooks/Berksfile", 'w') { |file|
    file.write("source \"https://supermarket.chef.io\"\n\n")
    file.write("cookbook \"#{cookbook[:name]}\", path: \"./#{cookbook[:name]}\"")
  }

  if cookbook[:cookbook_filename].end_with? ".zip"
    zf = ZipFileGenerator.new("berks-cookbooks", cookbook[:cookbook_filename])
    zf.write
  elsif cookbook[:cookbook_filename].end_with? ".tar.gz"
    system("tar -czvf #{cookbook[:cookbook_filename]} -C berks-cookbooks . > /dev/null 2>&1")
  end
end

#cleanup_cookbooks(cookbooks) ⇒ Object



102
103
104
105
106
107
# File 'lib/dream-ops/deployment/base.rb', line 102

def cleanup_cookbooks(cookbooks)
  cookbooks.each do |cookbook|
    File.delete(cookbook[:cookbook_filename])
    FileUtils.remove_dir("berks-cookbooks")
  end
end

#deploy(*args) ⇒ Object



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
# File 'lib/dream-ops/deployment/base.rb', line 109

def deploy(*args)
  # Ensure git is installed
  if (!system("which git > /dev/null 2>&1"))
    __bail_with_fatal_error(GitNotInstalledError.new)
  end

  # Find unique cookbooks and deploy targets
  result = analyze(args)

  # Build each unique cookbook once in case it's used by more than one app
  result[:cookbooks].each do |cookbook|
    DreamOps.ui.info "...Building cookbook [#{cookbook[:name]}]"
    build_cookbook(cookbook)

    DreamOps.ui.info "...Deploying cookbook [#{cookbook[:name]}]"
    deploy_cookbook(cookbook)
  end
  cleanup_cookbooks(result[:cookbooks])

  # Update cookbooks if needed and deploy to all targets
  deploy_success = true
  deploy_threads = []
  result[:deploy_targets].each do |target|
    deploy_threads << Thread.new { deploy_target(target, result[:cookbooks]) }
  end
  deploy_threads.each do |t|
    begin
      t.join
    rescue DreamOps::DreamOpsError
      DreamOps.ui.error "#{$!}"
      deploy_success = deploy_success && false
    end
  end

  # If ANY deploy threads failed, exit with failure
  exit(1) if !deploy_success
end

#deploy_cookbook(*args) ⇒ Object

Create a deployer method for the declaration



63
# File 'lib/dream-ops/deployment/base.rb', line 63

deployer_method :deploy_cookbook

#deploy_target(*args) ⇒ Object

Create a deployer method for the declaration



70
# File 'lib/dream-ops/deployment/base.rb', line 70

deployer_method :deploy_target