storazzo 0.6.1 โ 0.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +16 -1
- data/README.md +17 -18
- data/VERSION +1 -1
- data/bin/hello-storazzo +5 -1
- data/bin/ricdisk-magic +7 -13
- data/bin/stats-with-md5 +1 -1
- data/bin/storazzo +95 -141
- data/bin/storazzo-symlink.rb +1 -1
- data/bin/storazzo-util +1 -1
- data/lib/storazzo/common.rb +18 -5
- data/lib/storazzo/gcs/client.rb +85 -0
- data/lib/storazzo/main.rb +2 -5
- data/lib/storazzo/media/abstract_ric_disk.rb +162 -167
- data/lib/storazzo/media/gcs_bucket.rb +64 -69
- data/lib/storazzo/media/local_folder.rb +45 -49
- data/lib/storazzo/media/mount_point.rb +11 -15
- data/lib/storazzo/ric_disk.rb +30 -19
- data/lib/storazzo/ric_disk_config.rb +210 -214
- data/lib/storazzo/ric_disk_sample_config.rb +16 -18
- data/lib/storazzo/ric_disk_statsfile.rb +3 -5
- data/lib/storazzo/search_engine.rb +162 -0
- data/lib/storazzo/version.rb +1 -1
- data/lib/storazzo.rb +2 -7
- data/storazzo.gemspec +4 -3
- data/test/gcs/test_client.rb +42 -0
- data/test/media/test_local_folder.rb +2 -10
- data/test/test_ric_disk.rb +3 -1
- data/var/test/disks/ricdisk_stats_v11.rds +19 -11
- metadata +61 -18
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6948ac6219b8540a46f7a2c0a155e097168656ed6b09f0adc9b2601b62bc466d
|
|
4
|
+
data.tar.gz: b6f40f0dd0c025c0a03b59a11ba75b9a6ed0f398f93fb6ce1bf18538ef9ebdbf
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e2133288626b087aa3ec75c95626b7f23cb09b3073589bef4582837b12f7a1559b96afc0a7d42c4422c9fae61372143673170fcfca6ad6bfc9a509b3f1cef404
|
|
7
|
+
data.tar.gz: 1dc88ed96a2d77e5b38f68528315cecc409263bd350acb657ed643eb8e451247db28a23319816f2cfe3a65dc81b43a04fb90b059be36e6e289507a95b23ce49f
|
data/Gemfile
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
source 'https://rubygems.org'
|
|
4
4
|
|
|
5
|
+
gemspec
|
|
6
|
+
|
|
5
7
|
# to troubleshoot/debug things
|
|
6
8
|
gem 'rake'
|
|
7
9
|
gem 'rubocop'
|
|
@@ -9,4 +11,17 @@ gem 'ruby-lsp', '~> 0.0.4', group: :development
|
|
|
9
11
|
|
|
10
12
|
# gem 'pry'
|
|
11
13
|
gem 'pry', group: :development
|
|
12
|
-
|
|
14
|
+
gem 'minitest', group: :development
|
|
15
|
+
|
|
16
|
+
gem 'sqlite3'
|
|
17
|
+
gem 'thor'
|
|
18
|
+
gem 'google-cloud-storage'
|
|
19
|
+
|
|
20
|
+
group :test do
|
|
21
|
+
gem 'minitest'
|
|
22
|
+
gem 'minitest-reporters'
|
|
23
|
+
gem 'mocha'
|
|
24
|
+
gem 'rspec'
|
|
25
|
+
gem 'cucumber'
|
|
26
|
+
gem 'aruba' # For testing CLI applications
|
|
27
|
+
end
|
data/README.md
CHANGED
|
@@ -3,35 +3,34 @@
|
|
|
3
3
|
|
|
4
4
|
๐ฆ Storazzo ๐ gem - a Gem to automatically parse your FS for mounts (๐ฝ ๐พ ๐ฟ ) and compute MD5 (๐ค) of all files therein and then collect in central DB ๐ through ๐ฆ StorazzoApp๐ฆ (TM).
|
|
5
5
|
|
|
6
|
+

|
|
7
|
+
|
|
6
8
|
# INSTALL
|
|
7
9
|
|
|
8
10
|
`gem install storazzo`
|
|
9
11
|
|
|
10
12
|
(Latest version is hosted in https://rubygems.org/gems/storazzo)
|
|
11
13
|
|
|
12
|
-
#
|
|
13
|
-
|
|
14
|
-
I still struggle to enforce the include of LOCAL unchecked code rather than latest required system gem (cmon Ruby!)
|
|
15
|
-
but I found loads of interesting ways to test my code by googling and StoackOverflowing:
|
|
16
|
-
|
|
17
|
-
* `rake test TEST="test/sum_test.rb"`
|
|
18
|
-
* test-gcs-bucket: `ruby -I test test/test_gcs_bucket.rb` (meh - see below)
|
|
19
|
-
* test-media-subfolder: `rake test TEST="test/media/*.rb"`
|
|
14
|
+
# Development & Testing
|
|
20
15
|
|
|
21
|
-
|
|
16
|
+
To run the tools locally without installing the gem, use `just` (recommended) or standard Ruby flags.
|
|
22
17
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
* `
|
|
26
|
-
|
|
18
|
+
### Using `just` (Recommended)
|
|
19
|
+
This repository includes a `justfile` with common development commands:
|
|
20
|
+
* `just setup` - Install dependencies
|
|
21
|
+
* `just test` - Run all tests
|
|
22
|
+
* `just run scan /path` - Run the local `storazzo` CLI
|
|
23
|
+
* `just hello` - Run the local `hello-storazzo` script
|
|
27
24
|
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
### Manual Local Execution
|
|
26
|
+
If you don't have `just` installed, you can use `bundle exec` and the `-Ilib` flag:
|
|
27
|
+
* **Run CLI**: `bundle exec ruby -Ilib bin/storazzo scan /path`
|
|
28
|
+
* **Run Tests**: `bundle exec rake test`
|
|
29
|
+
* **Single Test**: `bundle exec ruby -Ilib:test test/media/test_local_folder.rb`
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
### Why `bundle exec` and `-Ilib`?
|
|
32
|
+
Using `bundle exec` ensures all dependencies from the `Gemfile` are available. The `-Ilib` flag adds the local `lib/` directory to the Ruby load path, ensuring your local changes are used instead of any installed gem version.
|
|
33
33
|
|
|
34
|
-
Now to toggle verbosity I believe I need to go into Rakefile (bummer)
|
|
35
34
|
# Thanks
|
|
36
35
|
|
|
37
36
|
Inspiration from:
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
0.7.3
|
data/bin/hello-storazzo
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
2
|
# frozen_string_literal: true
|
|
3
3
|
|
|
4
|
+
require 'bundler/setup'
|
|
5
|
+
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
|
|
4
6
|
require 'storazzo'
|
|
5
7
|
|
|
6
|
-
|
|
8
|
+
msg = ARGV.empty? ? 'HelloStorazzo' : ARGV.join(' ')
|
|
9
|
+
|
|
10
|
+
Storazzo::Main.say_hi(msg)
|
data/bin/ricdisk-magic
CHANGED
|
@@ -1,21 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
2
|
# frozen_string_literal: true
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
# require 'storazzo'
|
|
8
|
-
# require_relative '../lib/storazzo'
|
|
4
|
+
require 'bundler/setup'
|
|
5
|
+
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
|
|
6
|
+
require 'storazzo'
|
|
9
7
|
require 'fileutils'
|
|
10
8
|
require 'yaml'
|
|
11
9
|
require 'socket'
|
|
12
|
-
require 'optparse'
|
|
10
|
+
require 'optparse'
|
|
13
11
|
|
|
14
|
-
#
|
|
15
|
-
# puts File.expand_path(FileUtils.pwd, "/../")
|
|
16
|
-
# local_gem_path = File.expand_path(FileUtils.pwd, "/../")
|
|
17
|
-
# gem 'storazzo', path: local_gem_path
|
|
18
|
-
require 'storazzo'
|
|
12
|
+
# require 'storazzo'
|
|
19
13
|
# include Storazzo
|
|
20
14
|
# include Storazzo::Colors
|
|
21
15
|
extend Storazzo::Colors
|
|
@@ -32,8 +26,8 @@ include Storazzo::Common # instead
|
|
|
32
26
|
# include 'lib/ric_disk'
|
|
33
27
|
# extend Storazzo::Colors
|
|
34
28
|
|
|
35
|
-
if RUBY_VERSION.split('.')[0]
|
|
36
|
-
puts 'Refusing to launch a script form Ruby 1. Sorry Ric, its 2020 damn it!'
|
|
29
|
+
if RUBY_VERSION.split('.')[0].to_i < 3
|
|
30
|
+
puts 'Refusing to launch a script form Ruby 1 or 2. Sorry Ric, its 2020 damn it!'
|
|
37
31
|
exit 2020
|
|
38
32
|
end
|
|
39
33
|
|
data/bin/stats-with-md5
CHANGED
|
@@ -7,7 +7,7 @@ require 'optparse' # http://ruby.about.com/od/advancedruby/a/optionparser.htm
|
|
|
7
7
|
require 'date' # for DateTime
|
|
8
8
|
require 'tempfile'
|
|
9
9
|
|
|
10
|
-
if RUBY_VERSION.split('.')[0] == 1
|
|
10
|
+
if RUBY_VERSION.split('.')[0].to_i == 1
|
|
11
11
|
puts 'Refusing to launch a script form Ruby 1. Sorry Ric, its 2020 damn it!'
|
|
12
12
|
exit 2020
|
|
13
13
|
end
|
data/bin/storazzo
CHANGED
|
@@ -1,158 +1,112 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
2
|
# frozen_string_literal: true
|
|
3
3
|
|
|
4
|
+
require 'bundler/setup'
|
|
5
|
+
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
|
|
4
6
|
require 'storazzo'
|
|
5
|
-
|
|
7
|
+
require 'thor'
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
extend Storazzo::Colors
|
|
10
|
-
include Storazzo::Common # instead
|
|
11
|
-
|
|
12
|
-
if RUBY_VERSION.split('.')[0] == 1
|
|
13
|
-
puts 'Refusing to launch a script form Ruby 1. Sorry Ric, its 2020 damn it!'
|
|
9
|
+
if RUBY_VERSION.split('.')[0].to_i < 3
|
|
10
|
+
puts 'Refusing to launch a script form Ruby < 3.'
|
|
14
11
|
exit 2020
|
|
15
12
|
end
|
|
16
13
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
14
|
+
module Storazzo
|
|
15
|
+
class CLI < Thor
|
|
16
|
+
include ::Storazzo::Colors
|
|
17
|
+
include ::Storazzo::Common
|
|
18
|
+
|
|
19
|
+
class_option :verbose, type: :boolean, default: false, desc: 'Output more information'
|
|
20
|
+
class_option :debug, type: :boolean, default: false, desc: 'Enable debug mode'
|
|
21
|
+
|
|
22
|
+
desc "scan PATH", "Scan a removable disk or local folder and upload its metadata to GCS"
|
|
23
|
+
def scan(path)
|
|
24
|
+
setup_env
|
|
25
|
+
puts "Scanning path: #{path}"
|
|
26
|
+
|
|
27
|
+
dir = File.expand_path(path)
|
|
28
|
+
unless File.directory?(dir)
|
|
29
|
+
puts "Error: #{dir} is not a valid directory."
|
|
30
|
+
exit 1
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
config = Storazzo::RicDiskConfig.instance
|
|
34
|
+
config.load
|
|
35
|
+
|
|
36
|
+
rd = Storazzo::RicDisk.new(Storazzo::Media::AbstractRicDisk.DirFactory(dir))
|
|
37
|
+
rd.write_config_yaml_to_disk(dir)
|
|
38
|
+
rd.compute_stats_files(upload_to_gcs: true)
|
|
39
|
+
|
|
40
|
+
puts "Scan completed for #{dir}."
|
|
41
|
+
end
|
|
31
42
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
# :mount_types => %w{vfat ntfs},
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
def usage(comment = nil)
|
|
55
|
-
puts white($optparse.banner)
|
|
56
|
-
puts($optparse.summarize)
|
|
57
|
-
# puts("Description: " + gray($myconf[:description]))
|
|
58
|
-
puts red("ERROR: #{comment}") if comment
|
|
59
|
-
# puts "Description: #{ $myconf[:description] }"
|
|
60
|
-
exit 13
|
|
61
|
-
end
|
|
43
|
+
desc "search QUERY", "Search for a keyword across all indexed disks via SQLite DB"
|
|
44
|
+
def search(query)
|
|
45
|
+
setup_env
|
|
46
|
+
require 'storazzo/search_engine'
|
|
47
|
+
|
|
48
|
+
puts "Searching for: #{query}"
|
|
49
|
+
engine = Storazzo::SearchEngine.new
|
|
50
|
+
engine.sync_from_gcs
|
|
51
|
+
results = engine.query(query)
|
|
52
|
+
|
|
53
|
+
if results.empty?
|
|
54
|
+
puts "No results found for '#{query}'."
|
|
55
|
+
else
|
|
56
|
+
puts "Found #{results.size} matches:"
|
|
57
|
+
results.each do |r|
|
|
58
|
+
puts "- [#{r[:disk]}] #{r[:path]} (size: #{r[:size]})"
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
62
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
63
|
+
desc "backup SRC DEST", "Backup a local folder to GCS and update index"
|
|
64
|
+
def backup(src, dest)
|
|
65
|
+
setup_env
|
|
66
|
+
puts "Backing up #{src} to #{dest}"
|
|
67
|
+
|
|
68
|
+
src_dir = File.expand_path(src)
|
|
69
|
+
unless File.directory?(src_dir)
|
|
70
|
+
puts "Error: #{src_dir} is not a valid directory."
|
|
71
|
+
exit 1
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# 1. Run gsutil rsync
|
|
75
|
+
cmd = "gsutil -m rsync -r '#{src_dir}' '#{dest}'"
|
|
76
|
+
puts "Running: #{cmd}"
|
|
77
|
+
system(cmd)
|
|
78
|
+
unless $?.success?
|
|
79
|
+
puts "Failed to backup files to GCS."
|
|
80
|
+
exit 1
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# 2. Compute stats on the destination bucket
|
|
84
|
+
require 'storazzo/search_engine'
|
|
85
|
+
# We could run `stats-with-md5` for GCS here to generate the index
|
|
86
|
+
# and upload to metadata bucket
|
|
87
|
+
puts "Indexing GCS bucket: #{dest}"
|
|
88
|
+
cmd_stats = "stats-with-md5 --autowrite '#{dest}'"
|
|
89
|
+
system(cmd_stats)
|
|
90
|
+
|
|
91
|
+
puts "Backup and indexing completed."
|
|
82
92
|
end
|
|
83
|
-
opts.on('-f', '--force', 'force stuff (DFLT=false)') { $opts[:force] = true }
|
|
84
|
-
opts.on('-h', '--help', 'Display this screen') { usage }
|
|
85
|
-
# opts.on( '-j', '--jabba', 'Activates my Jabber powerful CLI' ) { $opts[:jabba] = true }
|
|
86
|
-
opts.on('-n', '--dryrun', "Don't really execute code") { $opts[:dryrun] = true }
|
|
87
|
-
opts.on('-l', '--logfile FILE', 'Write log to FILE') { |file| $opts[:logfile] = file }
|
|
88
|
-
opts.on('-v', '--verbose', 'Output more information') { $opts[:verbose] = true }
|
|
89
|
-
opts.separator("Description: #{gray($myconf[:description])}")
|
|
90
|
-
# opts.separator " -- "
|
|
91
|
-
end
|
|
92
|
-
$optparse.parse!
|
|
93
|
-
end
|
|
94
93
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
end
|
|
94
|
+
desc "show", "Show options and local folders"
|
|
95
|
+
def show
|
|
96
|
+
setup_env
|
|
97
|
+
puts("1. Mounts/Dirs")
|
|
98
|
+
require 'pp'
|
|
99
|
+
pp Storazzo::Media::GcsBucket.list_all_with_type
|
|
100
|
+
pp Storazzo::Media::AbstractRicDisk.super_duper_list_all_with_type
|
|
101
|
+
end
|
|
104
102
|
|
|
105
|
-
|
|
106
|
-
deb("Hello world from a templated '#{yellow $PROGRAM_NAME}'")
|
|
107
|
-
deb "+ Options are: #{gray $opts}"
|
|
108
|
-
deb "+ Depured args: #{azure ARGV}"
|
|
109
|
-
deb "+ Script-specifig super-cool conf: #{green $prog_conf_d}"
|
|
110
|
-
deb "+ Your configuration: #{purple $myconf.inspect}"
|
|
111
|
-
|
|
112
|
-
# Your code goes here...
|
|
113
|
-
puts white("Hello world from #{$myconf[:app_name]}!")
|
|
114
|
-
puts "Description: '''#{white $myconf[:description]}'''"
|
|
115
|
-
Storazzo::Main.say_hi("Note: if the version of this storazzo is behind local version youre probably using a old library.
|
|
116
|
-
I still need to learn how to call the binary with local/current lib/: bundle exec stiKazzi?")
|
|
117
|
-
|
|
118
|
-
$config = Storazzo::RicDiskConfig.instance
|
|
119
|
-
$config.load
|
|
120
|
-
deb "StorazzoConfig: #{$config}"
|
|
121
|
-
# config.load # auto_sbrodola(ARGV)
|
|
122
|
-
puts yellow("ARGV: #{ARGV}")
|
|
123
|
-
# config.iterate_through_file_list_for_disks(ARGV)
|
|
124
|
-
|
|
125
|
-
if_deb? { Storazzo::Main.say_hi("ARGV: #{ARGV}") }
|
|
126
|
-
|
|
127
|
-
usage('I need at least one argument (storazzo ACTION)') if ARGV == []
|
|
128
|
-
argv_action = ARGV[0]
|
|
129
|
-
case argv_action # first argment - action
|
|
130
|
-
when 'show', 'list'
|
|
131
|
-
show_action(ARGV.tail)
|
|
132
|
-
when 'auto'
|
|
133
|
-
auto_action(ARGV.tail)
|
|
134
|
-
when 'help'
|
|
135
|
-
usage("There you go, here's your HELP :)")
|
|
136
|
-
when String
|
|
137
|
-
usage("Unknown action1: #{argv_action}. Available actions: #{$actions}")
|
|
138
|
-
else
|
|
139
|
-
usage("Unknown action2, really strange!: '#{argv_action}'. Actions: #{$actions}")
|
|
140
|
-
end
|
|
141
|
-
end
|
|
103
|
+
private
|
|
142
104
|
|
|
143
|
-
def
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
puts white("$DEBUG is #{$DEBUG}. Tu turn on, call me with -d") unless $DEBUG
|
|
148
|
-
# warn "[warn] template v#{$TEMPLATE_VER }: proviamo il warn che magari depreca il DEB"
|
|
149
|
-
real_program
|
|
150
|
-
|
|
151
|
-
if_deb? do
|
|
152
|
-
puts 'First I need to figure out how to bring in all the libraries in here..'
|
|
153
|
-
Storazzo::Main.say_hi("ARGV is: #{ARGV.join ', '}")
|
|
154
|
-
puts Storazzo.version
|
|
105
|
+
def setup_env
|
|
106
|
+
$DEBUG = options[:debug]
|
|
107
|
+
$opts = { verbose: options[:verbose], debug: options[:debug] }
|
|
108
|
+
end
|
|
155
109
|
end
|
|
156
110
|
end
|
|
157
111
|
|
|
158
|
-
|
|
112
|
+
Storazzo::CLI.start(ARGV)
|
data/bin/storazzo-symlink.rb
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
storazzo
|
data/bin/storazzo-util
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
storazzo
|
data/lib/storazzo/common.rb
CHANGED
|
@@ -28,7 +28,12 @@
|
|
|
28
28
|
# ๐ถ ๐ง ๐ฆ ๐ง ๐ฉ ๐จ ๐ง ๐ง ๐ง ๐ด ๐ต ๐ค ๐ฅ ๐ช ๐ซ ๐ฌ ๐ญ ๐ฑ ๐ณ ๐ฒ ๐ง ๐ธ ๐คด ๐
๐คถ disabled
|
|
29
29
|
# ๐ง ๐ฆป ๐ฆฎ ๐ฆฏ ๐ฆบ ๐ฆผ ๐ฆฝ ๐ฆพ ๐ฆฟ ๐คต ๐ฎ ๐ท ๐ ๐ ๐ด ๐ต ๐ฆธ ๐ฆน ๐ง ๐ง ๐ง ๐ง ๐ง ๐ง ๐ง ๐ผ ๐ฟ ๐ป ๐น ๐บ ๐ฝ ๐พ ๐ธ ๐ โ ๐ฑ ๐ง ๐ฆด ๐ ๐ ๐ ๐ ๐ ๐ข ๐
๐ฆท ๐ฆต ๐ฆถ ๐ญ ๐ฌ ๐ญ ๐ฌ ๐จ ๐ฉ ๐ช ๐ซ ๐ฐ ๐ฑ ๐ฎ ๐ฏ ๐ฃ ๐ค ๐ฅ ๐ฆ ๐ง ๐ฆ ๐ง ๐ข ๐ซ ๐ค ๐จ ๐ฅ ๐ช ๐ฒ ๐ฅ ๐ก ๐ฉ ๐ฏ ๐ ๐ฐ ๐ฒ
|
|
30
30
|
require_relative 'colors'
|
|
31
|
-
require 'pry'
|
|
31
|
+
# require 'pry'
|
|
32
|
+
begin
|
|
33
|
+
require 'pry'
|
|
34
|
+
rescue LoadError
|
|
35
|
+
# Pry not available, no worries
|
|
36
|
+
end
|
|
32
37
|
|
|
33
38
|
module Storazzo
|
|
34
39
|
module Common
|
|
@@ -52,8 +57,8 @@ module Storazzo
|
|
|
52
57
|
puts "[Wโ ๏ธRN] #{azure(s)}"
|
|
53
58
|
end
|
|
54
59
|
|
|
55
|
-
def err(
|
|
56
|
-
puts "[ERR๐] #{red(
|
|
60
|
+
def err(str)
|
|
61
|
+
puts "[ERR๐] #{red(str)}" # โ
|
|
57
62
|
end
|
|
58
63
|
|
|
59
64
|
def bug(s)
|
|
@@ -68,8 +73,12 @@ module Storazzo
|
|
|
68
73
|
end
|
|
69
74
|
|
|
70
75
|
def ppp(complex_object_to_colorize)
|
|
71
|
-
|
|
72
|
-
|
|
76
|
+
if defined?(Pry::ColorPrinter)
|
|
77
|
+
Pry::ColorPrinter.pp(complex_object_to_colorize)
|
|
78
|
+
else
|
|
79
|
+
require 'pp'
|
|
80
|
+
pp complex_object_to_colorize
|
|
81
|
+
end
|
|
73
82
|
end
|
|
74
83
|
|
|
75
84
|
def fatal(s)
|
|
@@ -84,6 +93,10 @@ module Storazzo
|
|
|
84
93
|
`uname`.chomp == 'Linux'
|
|
85
94
|
end
|
|
86
95
|
|
|
96
|
+
def slugify(string)
|
|
97
|
+
string.to_s.downcase.strip.gsub(/\s+/, '_').gsub(/[^\w-]/, '')
|
|
98
|
+
end
|
|
99
|
+
|
|
87
100
|
private
|
|
88
101
|
|
|
89
102
|
def _debug_true
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'google/cloud/storage'
|
|
4
|
+
require 'storazzo/common'
|
|
5
|
+
|
|
6
|
+
module Storazzo
|
|
7
|
+
module GCS
|
|
8
|
+
class Client
|
|
9
|
+
include Storazzo::Common
|
|
10
|
+
|
|
11
|
+
attr_reader :storage, :project_id, :bucket_name
|
|
12
|
+
|
|
13
|
+
def initialize(project_id = nil, bucket_name = nil)
|
|
14
|
+
@project_id = project_id || autodetect_project_id
|
|
15
|
+
@bucket_name = bucket_name || ENV['GCS_BUCKET'] || "#{@project_id}-storazzo"
|
|
16
|
+
deb "GCS Client initialized for project: #{@project_id}, bucket: #{@bucket_name}"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def storage
|
|
20
|
+
@storage ||= Google::Cloud::Storage.new(project_id: @project_id)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def ensure_bucket_exists
|
|
24
|
+
return true if bucket_exists?(@bucket_name)
|
|
25
|
+
|
|
26
|
+
deb "Bucket #{@bucket_name} not found. Creating it in project #{@project_id}..."
|
|
27
|
+
storage.create_bucket(@bucket_name)
|
|
28
|
+
true
|
|
29
|
+
rescue StandardError => e
|
|
30
|
+
err "Failed to ensure/create bucket #{@bucket_name}: #{e.message}"
|
|
31
|
+
false
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def autodetect_project_id
|
|
35
|
+
# Priority 1: Environment Variable
|
|
36
|
+
return ENV['GOOGLE_CLOUD_PROJECT'] if ENV['GOOGLE_CLOUD_PROJECT']
|
|
37
|
+
|
|
38
|
+
# Priority 2: Config file
|
|
39
|
+
config = Storazzo::RicDiskConfig.instance
|
|
40
|
+
config.load
|
|
41
|
+
return config.project_id if config.project_id && config.project_id != 'YOUR-PROJECT-ID'
|
|
42
|
+
|
|
43
|
+
# Priority 3: GCloud CLI default
|
|
44
|
+
begin
|
|
45
|
+
`gcloud config get-value project 2>/dev/null`.strip
|
|
46
|
+
rescue StandardError
|
|
47
|
+
nil
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def bucket_exists?(bucket_name)
|
|
52
|
+
bucket_name = bucket_name.gsub('gs://', '').split('/').first
|
|
53
|
+
!storage.bucket(bucket_name).nil?
|
|
54
|
+
rescue StandardError => e
|
|
55
|
+
warn "Error checking bucket #{bucket_name}: #{e.message}"
|
|
56
|
+
false
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def list_buckets
|
|
60
|
+
storage.buckets.map(&:name)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def upload_file(local_path, bucket_name, remote_path)
|
|
64
|
+
bucket_name = bucket_name.gsub('gs://', '').split('/').first
|
|
65
|
+
bucket = storage.bucket(bucket_name)
|
|
66
|
+
raise "Bucket #{bucket_name} not found!" unless bucket
|
|
67
|
+
|
|
68
|
+
deb "Uploading #{local_path} to gs://#{bucket_name}/#{remote_path}..."
|
|
69
|
+
bucket.create_file(local_path, remote_path)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def download_file(bucket_name, remote_path, local_path)
|
|
73
|
+
bucket_name = bucket_name.gsub('gs://', '').split('/').first
|
|
74
|
+
bucket = storage.bucket(bucket_name)
|
|
75
|
+
raise "Bucket #{bucket_name} not found!" unless bucket
|
|
76
|
+
|
|
77
|
+
file = bucket.file(remote_path)
|
|
78
|
+
raise "Remote file #{remote_path} not found in bucket #{bucket_name}!" unless file
|
|
79
|
+
|
|
80
|
+
deb "Downloading gs://#{bucket_name}/#{remote_path} to #{local_path}..."
|
|
81
|
+
file.download(local_path)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
data/lib/storazzo/main.rb
CHANGED
|
@@ -15,11 +15,9 @@ module Storazzo
|
|
|
15
15
|
# Arguments:
|
|
16
16
|
# message: (String) - optional
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
module Storazzo
|
|
20
|
-
class Main
|
|
18
|
+
class Main
|
|
21
19
|
require 'storazzo/colors'
|
|
22
|
-
extend Storazzo::Colors
|
|
20
|
+
extend ::Storazzo::Colors
|
|
23
21
|
|
|
24
22
|
# version 1.2
|
|
25
23
|
def self.say_hi(message = nil)
|
|
@@ -78,6 +76,5 @@ module Storazzo
|
|
|
78
76
|
pwhite 'All tests END'
|
|
79
77
|
# puts "All tests END"
|
|
80
78
|
end
|
|
81
|
-
end
|
|
82
79
|
end
|
|
83
80
|
end
|