Class: EtnaApp::Metis::PutArchive

Inherits:
Etna::Command show all
Includes:
WithEtnaClients
Defined in:
lib/commands.rb

Instance Attribute Summary

Attributes inherited from Etna::Command

#parent

Instance Method Summary collapse

Methods included from WithEtnaClients

#environment, #exit, exit, #janus_client, #magma_client, #metis_client, #polyphemus_client, #token

Methods inherited from Etna::Command

#completions, #fill_in_missing_params, #find_command, #initialize, parent_scope, #setup

Methods included from Etna::CommandOrExecutor

#command_name, #completions_for, #desc, #flag_argspec, #flag_as_parameter, included, #parse_flags, #program_name, #usage

Constructor Details

This class inherits a constructor from Etna::Command

Instance Method Details

#create_upload_workflowObject



159
160
161
162
163
164
165
166
167
# File 'lib/commands.rb', line 159

def create_upload_workflow
  Etna::Clients::Metis::MetisUploadWorkflow.new(
    metis_client: @metis_client,
    metis_uid: SecureRandom.hex,
    project_name: @project_name,
    bucket_name: @bucket_name,
    max_attempts: 3
  )
end

#execute(file, project_name:, bucket_name:, path:) ⇒ Object



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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/commands.rb', line 102

def execute(file, project_name:, bucket_name:, path:)
  @project_name = project_name
  @bucket_name = bucket_name
  basename = ::File.basename(path)
  dir = ::File.dirname(path)

  puts "Creating archive folder"
  metis_client.create_folder(Etna::Clients::Metis::CreateFolderRequest.new(
    project_name: project_name,
    bucket_name: bucket_name,
    folder_path: dir
  ))

  puts "Listing archive folder"
  files = metis_client.list_folder(Etna::Clients::Metis::ListFolderRequest.new(
    project_name: project_name,
    bucket_name: bucket_name,
    folder_path: dir
  )).files.all

  if files.select { |f| f.file_name == basename }.length > 0
    timestr = DateTime.now.strftime("%Y%m%d_%H%M")
    puts "Backing up #{dir}/#{basename} to #{dir}/#{basename}.#{timestr}"
    metis_client.rename_file(Etna::Clients::Metis::RenameFileRequest.new(
      project_name: project_name,
      bucket_name: bucket_name,
      file_path: ::File.join(dir, basename),
      new_bucket_name: bucket_name,
      new_file_path: ::File.join(dir, "#{basename}.#{timestr}"),
    ))
  end

  create_upload_workflow.do_upload(
    ::File.open(file, "r"),
    path
  ) do |progress|
    case progress[0]
    when :error
      puts("Error while uploading: #{progress[1].to_s}")
    else
    end
  end
  puts "Completed upload"

  backups = files.select { |f| f.file_name != basename }.sort_by(&:file_name).reverse
  if backups.length > ROLLING_COUNT
    backups.slice(ROLLING_COUNT..-1).reverse.each do |f|
      puts "Removing rolling back up #{f.file_name}"
      metis_client.delete_file(Etna::Clients::Metis::DeleteFileRequest.new(
        project_name: project_name,
        bucket_name: bucket_name,
        file_path: ::File.join(dir, f.file_name)
      ))
    end
  end
end