Class: Berkflow::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/berkflow/cli.rb

Constant Summary collapse

LATEST =
"latest".freeze

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Cli

Returns a new instance of Cli.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/berkflow/cli.rb', line 9

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



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
# File 'lib/berkflow/cli.rb', line 140

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.map { |node| ridley.node.run(node.public_hostname, 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



61
62
63
64
65
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
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
# File 'lib/berkflow/cli.rb', line 61

def install(url)
  require 'uri'
  require 'open-uri'
  require 'zlib'
  require 'rubygems/package'

  if is_url?(url)
    tempfile = Tempfile.new("berkflow")
    begin
      open(url) { |remote_file| tempfile.write(remote_file.read) }
    rescue OpenURI::HTTPError => ex
      error "Error retrieving remote package: #{ex.message}."
      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.message}"
    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: options[: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



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
# File 'lib/berkflow/cli.rb', line 167

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.map { |node| ridley.node.chef_run(node.public_hostname) }

  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



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
# File 'lib/berkflow/cli.rb', line 203

def upgrade(environment, application, version = LATEST)
  version  = sanitize_version(version)
  env      = find_environment!(environment)
  cookbook = find_cookbook!(application, version)

  unless options[: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)
ensure
  file.close(true) if file
end

#versionObject



238
239
240
# File 'lib/berkflow/cli.rb', line 238

def version
  say Berkflow::VERSION
end