Class: MinioRunner::McManager

Inherits:
Object
  • Object
show all
Defined in:
lib/minio_runner/mc_manager.rb

Constant Summary collapse

CommandError =
Class.new(StandardError)
DEFAULT_RETRIES =
5
RETRY_DELAY =
1

Class Method Summary collapse

Class Method Details

.bucket_exists?(alias_name, name) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/minio_runner/mc_manager.rb', line 17

def bucket_exists?(alias_name, name)
  # `mc ls` exits non-zero when the bucket is missing, which is fine here.
  _, _, status = run_mc(["ls", "#{alias_name}/#{name}"], allow_failure: true, retries: 0)
  status.success?
end

.commandObject



13
14
15
# File 'lib/minio_runner/mc_manager.rb', line 13

def command
  ["#{MinioRunner::McBinary.binary_file_path}"]
end

.create_bucket(alias_name, name) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/minio_runner/mc_manager.rb', line 23

def create_bucket(alias_name, name)
  MinioRunner.logger.debug("Creating bucket #{alias_name}/#{name}...")
  if !bucket_exists?(alias_name, name)
    run_mc(["mb", "#{alias_name}/#{name}"])
    MinioRunner.logger.debug("Created  #{alias_name}/#{name}.")
  else
    MinioRunner.logger.debug("Bucket #{alias_name}/#{name} already exists, doing nothing.")
  end
end

.run_mc(args, allow_failure: false, retries: DEFAULT_RETRIES) ⇒ Object

Captures stdout/stderr to the minio log file, retries with backoff on non-zero exit codes (covers errors like “Server not initialized yet” and “connection refused” when minio is still starting) and raises ‘CommandError` on actual failures. (unless `allow_failure` is set)

Raises:



60
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
# File 'lib/minio_runner/mc_manager.rb', line 60

def run_mc(args, allow_failure: false, retries: DEFAULT_RETRIES)
  full_command = command + args
  max_attempts = retries + 1
  attempts = 0
  stdout = stderr = status = nil

  while attempts < max_attempts
    stdout, stderr, status = Open3.capture3(*full_command)
    File.open(MinioServerManager.log_file_path, "a") { |f| f.write(stdout, stderr) }

    attempts += 1
    break if status.success? || attempts == max_attempts

    MinioRunner.logger.warn(
      "mc #{args.join(" ")} failed (attempt #{attempts}/#{max_attempts}, " \
        "exit #{status.exitstatus}); retrying in #{RETRY_DELAY}s: #{stderr.strip}",
    )
    sleep(RETRY_DELAY)
  end

  return stdout, stderr, status if status.success? || allow_failure

  raise CommandError,
        "mc #{args.join(" ")} failed after #{attempts} attempts " \
          "(exit #{status.exitstatus}): #{stderr.strip}"
end

.set_alias(name, url) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/minio_runner/mc_manager.rb', line 33

def set_alias(name, url)
  MinioRunner.logger.debug("Setting alias #{name} to #{url}...")
  run_mc(
    [
      "alias",
      "set",
      name,
      url,
      MinioRunner.config.minio_root_user,
      MinioRunner.config.minio_root_password,
    ],
  )
  MinioRunner.logger.debug("Set alias #{name} to #{url}.")
end

.set_anon(alias_name, bucket, policy) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/minio_runner/mc_manager.rb', line 48

def set_anon(alias_name, bucket, policy)
  MinioRunner.logger.debug(
    "Setting anonymous access for #{alias_name}/#{bucket} to policy #{policy}...",
  )
  run_mc(["anonymous", "set", policy, "#{alias_name}/#{bucket}"])
  MinioRunner.logger.debug("Anonymous access set for #{alias_name}/#{bucket}.")
end