vagrant-box-gcs 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1dbf043b2e318db44996f1a9eee12b665146813bb018b16194e70442641d41cc
4
+ data.tar.gz: 264bfe9d4b815c36329ef47b4a50d2f15aaad30b0a9e79c3c709ab56a9d4fc16
5
+ SHA512:
6
+ metadata.gz: c4dd42d502b8628cc68d2436529548c2d814b121291b08c29bb1c511273c724d689769e9030e1c71fdd34ecd2d1e5cb56a6791550f50eba541ac23794d6e30ea
7
+ data.tar.gz: 34d9909d9dd46b39b63503485d5c4ec3031089cad4977ffd544f569b44af63ebf8a346a54bfec8ebe396fc8f2f7c773c57fe2245e97ae8e668f53ffe3889911d
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Arnaud Dezandee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,90 @@
1
+ # Vagrant Box Google Cloud Storage (GCS)
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/vagrant-box-gcs.svg)](https://badge.fury.io/rb/vagrant-box-gcs)
4
+
5
+ This is a [Vagrant](http://www.vagrantup.com) 2.2.0+ plugin that adds the ability to download boxes from
6
+ [Google Compute Storage](http://cloud.google.com/storage/) (GCS).
7
+
8
+ ## Installation
9
+ ```bash
10
+ $ vagrant plugin install vagrant-box-gcs
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Only the `gs://` protocol shorthand is supported
16
+
17
+ The plugin supports fetching compatible metadata file of structured boxes repository
18
+
19
+ #### From metadata box:
20
+
21
+ ```bash
22
+ $ vagrant box add gs://my-bucket/my-org/my-box-metadata.json
23
+ ```
24
+
25
+ The `box update` command line is available when using metadata
26
+ ```bash
27
+ $ vagrant box update --box my-org/my-box
28
+ ```
29
+
30
+ ```ruby
31
+ # Vagrantfile
32
+ Vagrant.configure('2') do |config|
33
+ config.vm.box = 'my-org/my-box'
34
+ config.vm.box_url = 'gs://my-bucket/my-org/my-box-metadata.json'
35
+ end
36
+ ```
37
+
38
+ #### From simple box:
39
+
40
+ ```bash
41
+ $ vagrant box add gs://my-bucket/my-org/my-box/virtualbox.box
42
+ ```
43
+
44
+ ```ruby
45
+ # Vagrantfile
46
+ Vagrant.configure('2') do |config|
47
+ config.vm.box_url = 'gs://my-bucket/my-org/my-box/virtualbox.box'
48
+ end
49
+ ```
50
+
51
+ ## Authentication
52
+
53
+ Authenticating with Google Cloud services requires at most one JSON file.
54
+ Vagrant will look for credentials in the following places, preferring the first location found:
55
+
56
+ 1. A JSON file (Service Account) whose path is specified by the
57
+ `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
58
+
59
+ 2. A JSON file in a location known to the `gcloud` command-line tool.
60
+ (`gcloud auth application-default login` creates it)
61
+
62
+ On Windows, this is:
63
+
64
+ %APPDATA%/gcloud/application_default_credentials.json
65
+
66
+ On other systems:
67
+
68
+ $HOME/.config/gcloud/application_default_credentials.json
69
+
70
+ 3. On Google Compute Engine and Google App Engine Managed VMs, it fetches
71
+ credentials from the metadata server. (Needs a correct VM authentication
72
+ scope configuration)
73
+
74
+
75
+ ## Auto-install
76
+
77
+ Auto-install with some shell in your `Vagrantfile`:
78
+
79
+ ```ruby
80
+ # Vagrantfile
81
+ unless Vagrant.has_plugin?('vagrant-box-gcs')
82
+ system('vagrant plugin install vagrant-box-gcs') || exit!
83
+ exit system('vagrant', *ARGV)
84
+ end
85
+
86
+ Vagrant.configure('2') do |config|
87
+ config.vm.box_url = 'gs://my-bucket/my-org/my-box/virtualbox.box'
88
+ # ...
89
+ end
90
+ ```
@@ -0,0 +1,14 @@
1
+ require 'vagrant-box-gcs/version'
2
+ require 'vagrant-box-gcs/plugin'
3
+ require 'vagrant-box-gcs/errors'
4
+
5
+ module VagrantPlugins
6
+ module BoxGCS
7
+ def self.source_root
8
+ @source_root ||= Pathname.new(File.expand_path('../../', __FILE__))
9
+ end
10
+
11
+ I18n.load_path << File.expand_path('locales/en.yml', source_root)
12
+ I18n.reload!
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ module VagrantPlugins
2
+ module BoxGCS
3
+ module Errors
4
+ class VagrantBoxGCSError < Vagrant::Errors::VagrantError
5
+ error_namespace('vagrant_box_gcs.errors')
6
+ end
7
+
8
+ class CredentialsError < VagrantBoxGCSError
9
+ error_key(:credentials)
10
+ end
11
+
12
+ class MissingFileError < VagrantBoxGCSError
13
+ error_key(:missing_file)
14
+ end
15
+
16
+ class UnauthorizedError < VagrantBoxGCSError
17
+ error_key(:unauthorized)
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require 'tempfile'
2
+ require 'uri'
3
+
4
+ module BoxExtension
5
+ def load_metadata(**download_options)
6
+ uri = URI.parse(@metadata_url)
7
+ if uri.scheme == 'gs'
8
+ tf = Tempfile.new('vagrant-load-metadata')
9
+ tf.close
10
+ VagrantPlugins::BoxGCS::Storage.download(uri, tf.path)
11
+ Vagrant::BoxMetadata.new(File.open(tf.path, 'r'))
12
+ else
13
+ super(**download_options)
14
+ end
15
+ end
16
+ end
17
+
18
+ module Vagrant
19
+ class Box
20
+ prepend BoxExtension
21
+ end
22
+ end
@@ -0,0 +1,37 @@
1
+ require 'digest/sha1'
2
+ require 'uri'
3
+
4
+ module BoxAddExtension
5
+ def metadata_url?(url, env)
6
+ uri = URI.parse(url)
7
+ if uri.scheme == 'gs'
8
+ VagrantPlugins::BoxGCS::Storage.metadata_url?(uri)
9
+ else
10
+ super(url, env)
11
+ end
12
+ end
13
+
14
+ def download(url, env, **opts)
15
+ opts[:ui] = true if !opts.key?(:ui)
16
+ uri = URI.parse(url)
17
+ if uri.scheme == 'gs'
18
+ if (opts[:ui])
19
+ env[:ui].detail(I18n.t('vagrant.box_downloading', url: uri.to_s))
20
+ end
21
+ temp_path = env[:tmp_path].join('box' + Digest::SHA1.hexdigest(uri.to_s))
22
+ VagrantPlugins::BoxGCS::Storage.download(uri, temp_path.to_s)
23
+ else
24
+ super(url, env, **opts)
25
+ end
26
+ end
27
+ end
28
+
29
+ module Vagrant
30
+ module Action
31
+ module Builtin
32
+ class BoxAdd
33
+ prepend BoxAddExtension
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,27 @@
1
+ require "uri"
2
+
3
+ module VagrantPlugins
4
+ module BoxGCS
5
+ class Init
6
+ def initialize(app, _env)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ env[:box_urls].map! do |url|
12
+ begin
13
+ u = URI.parse(url)
14
+ if u.scheme == 'gs'
15
+ BoxGCS::Plugin.init!
16
+ end
17
+ url
18
+ rescue URI::Error
19
+ url
20
+ end
21
+ end
22
+
23
+ @app.call(env)
24
+ end.freeze
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ require 'vagrant'
2
+
3
+ module VagrantPlugins
4
+ module BoxGCS
5
+ class Plugin < Vagrant.plugin('2')
6
+ name 'BoxGCS'
7
+ description 'Vagrant plugin to download boxes from Google GCS.'
8
+
9
+ action_hook(:authenticate_box_gcs, :authenticate_box_url) do |hook|
10
+ require_relative 'middleware/init'
11
+ hook.prepend(Init)
12
+ end
13
+
14
+ def self.init!
15
+ return if defined?(@_init)
16
+
17
+ ENV['GOOGLE_AUTH_SUPPRESS_CREDENTIALS_WARNINGS'] = 'true'
18
+ require_relative 'storage'
19
+ require_relative 'extensions/box_add'
20
+ require_relative 'extensions/box'
21
+ @_init = true
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,50 @@
1
+ require 'pathname'
2
+ require 'json'
3
+ require 'google/cloud/storage'
4
+
5
+ module VagrantPlugins
6
+ module BoxGCS
7
+ class Storage
8
+ def self.init!
9
+ return if defined?(@storage)
10
+
11
+ begin
12
+ @storage = Google::Cloud::Storage.new
13
+ rescue => e
14
+ raise Errors::CredentialsError,
15
+ name: e.class.to_s,
16
+ message: e.message
17
+ end
18
+ end
19
+
20
+ def self.remote_file(uri)
21
+ init!
22
+ begin
23
+ bucket = @storage.bucket uri.host
24
+ bucket.file uri.path[1..-1]
25
+ rescue => e
26
+ body = JSON.parse(e.body)
27
+ raise Errors::UnauthorizedError,
28
+ name: e.message,
29
+ code: e.status_code,
30
+ message: body['error']['message']
31
+ end
32
+ end
33
+
34
+ def self.metadata_url?(uri)
35
+ init!
36
+ file = remote_file(uri)
37
+ file.content_type == 'application/json'
38
+ end
39
+
40
+ def self.download(uri, dest)
41
+ init!
42
+ file = remote_file(uri)
43
+ raise Errors::MissingFileError, message: "No URLs matched: #{uri}" if file.nil?
44
+
45
+ downloaded = file.download dest
46
+ Pathname.new(downloaded.path)
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ module VagrantPlugins
2
+ module BoxGCS
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ en:
2
+ vagrant_box_gcs:
3
+ errors:
4
+ credentials: |-
5
+ BoxGCS: Credentials error
6
+ %{name}: %{message}
7
+ missing_file: |-
8
+ BoxGCS: Missing file error
9
+ %{message}
10
+ unauthorized: |-
11
+ BoxGCS: %{code} %{name}
12
+ %{message}
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-box-gcs
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Arnaud Dezandee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-cloud-storage
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.26.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.26.1
27
+ description:
28
+ email:
29
+ - dezandee.arnaud@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - lib/vagrant-box-gcs.rb
37
+ - lib/vagrant-box-gcs/errors.rb
38
+ - lib/vagrant-box-gcs/extensions/box.rb
39
+ - lib/vagrant-box-gcs/extensions/box_add.rb
40
+ - lib/vagrant-box-gcs/middleware/init.rb
41
+ - lib/vagrant-box-gcs/plugin.rb
42
+ - lib/vagrant-box-gcs/storage.rb
43
+ - lib/vagrant-box-gcs/version.rb
44
+ - locales/en.yml
45
+ homepage: http://www.vagrantup.com
46
+ licenses:
47
+ - MIT
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '2.4'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.1.3
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Vagrant plugin to download boxes from Google GCS.
68
+ test_files: []