Class: Berkflow::Cli
- Inherits:
-
Thor
- Object
- Thor
- Berkflow::Cli
- Defined in:
- lib/berkflow/cli.rb
Constant Summary collapse
- LATEST =
"latest".freeze
Instance Method Summary collapse
- #exec(environment, command) ⇒ Object
-
#initialize(*args) ⇒ Cli
constructor
A new instance of Cli.
- #install(url) ⇒ Object
- #run_chef(environment) ⇒ Object
- #upgrade(environment, application, version = LATEST) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(*args) ⇒ Cli
Returns a new instance of Cli.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/berkflow/cli.rb', line 17 def initialize(*args) super(*args) if @options[:verbose] Ridley.logger.level = ::Logger::INFO end if @options[:debug] Ridley.logger.level = ::Logger::DEBUG end end |
Instance Method Details
#exec(environment, command) ⇒ Object
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 |
# File 'lib/berkflow/cli.rb', line 155 def exec(environment, command) env = find_environment!(environment) say "Discovering nodes in #{environment}..." nodes = find_nodes(environment) if nodes.empty? say "No nodes in #{environment}. Done." exit(0) end say "Executing command on #{nodes.length} nodes..." success, failures, out = handle_results nodes.pmap { |node| ridley.node.run connect_address(node, ), command } unless success.empty? say "Successfully executed command on #{success.length} nodes" end unless failures.empty? error "Failed to execute command on #{failures.length} nodes" end say "Done. See #{out} for logs." failures.empty? ? exit(0) : exit(1) end |
#install(url) ⇒ Object
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 |
# File 'lib/berkflow/cli.rb', line 69 def install(url) require 'uri' require 'open-uri' require 'zlib' require 'rubygems/package' if is_url?(url) tempfile = Tempfile.new("berkflow") tempfile.binmode begin open(url) { |remote_file| tempfile.write(remote_file.read) } rescue OpenURI::HTTPError => ex error "Error retrieving remote package: #{ex.}." exit(1) end url = tempfile.path end unless File.exist?(url) error "Package not found: #{url}." exit(1) end tmpdir = Dir.mktmpdir("berkflow") begin zlib = Zlib::GzipReader.new(File.open(url, "rb")) io = StringIO.new(zlib.read) zlib.close Gem::Package::TarReader.new io do |tar| tar.each do |tarfile| destination_file = File.join(tmpdir, tarfile.full_name) if tarfile.directory? FileUtils.mkdir_p(destination_file) else destination_directory = File.dirname(destination_file) FileUtils.mkdir_p(destination_directory) unless File.directory?(destination_directory) File.open(destination_file, "wb") { |f| f.print tarfile.read } end end end rescue Zlib::GzipFile::Error => ex error "Error extracting package: #{ex.}" exit(1) end cookbooks_dir = File.join(tmpdir, "cookbooks") unless File.exist?(cookbooks_dir) error "Package did not contain a 'cookbooks' directory." exit(1) end uploaded = Dir.entries(cookbooks_dir).collect do |path| path = File.join(cookbooks_dir, path) next unless File.cookbook?(path) begin cookbook = Ridley::Chef::Cookbook.from_path(path) say "Uploading #{cookbook.cookbook_name} (#{cookbook.version})" ridley.cookbook.upload(path, freeze: true, force: [:force]) true rescue Ridley::Errors::FrozenCookbook true end end.compact say "Uploaded #{uploaded.length} cookbooks." say "Done." ensure tempfile.close(true) if tempfile FileUtils.rm_rf(tmpdir) if tmpdir end |
#run_chef(environment) ⇒ Object
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 |
# File 'lib/berkflow/cli.rb', line 190 def run_chef(environment) env = find_environment!(environment) say "Discovering nodes in #{environment}..." nodes = find_nodes(environment) if nodes.empty? say "No nodes in #{environment}. Done." exit(0) end say "Running Chef Client on #{nodes.length} nodes..." success, failures, out = handle_results nodes.pmap { |node| ridley.node.chef_run connect_address(node, ) } unless success.empty? say "Successfully ran Chef Client on #{success.length} nodes" end unless failures.empty? error "Failed to run Chef Client on #{failures.length} nodes" end say "Done. See #{out} for logs." failures.empty? ? exit(0) : exit(1) end |
#upgrade(environment, application, version = LATEST) ⇒ Object
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 267 268 269 270 271 |
# File 'lib/berkflow/cli.rb', line 239 def upgrade(environment, application, version = LATEST) version = sanitize_version(version) env = find_environment!(environment) cookbook = find_cookbook!(application, version) unless [:force] if locked = env.cookbook_versions[application] if Semverse::Constraint.new(locked).version.to_s == cookbook.version say "Environment already at #{cookbook.version}." say "Done." exit(0) end end end file = Tempfile.new("berkflow") unless contents = cookbook.download_file(:root_file, Berkshelf::Lockfile::DEFAULT_FILENAME, file.path) error "#{application} (#{version}) did not contain a Berksfile.lock" exit(1) end say "Upgrading #{environment} to #{cookbook.version}" say "Applying cookbook locks to #{environment}..." lockfile = Berkshelf::Lockfile.from_file(file.path) unless lockfile.apply(environment) error "Failed to apply Berksfile.lock to #{environment}." exit(1) end run_chef(environment) unless [:skip_chef_run] ensure file.close(true) if file end |