vagrant-noderings 0.1.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,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "openssl"
5
+ require "securerandom"
6
+ require_relative "api/compute"
7
+ require_relative "version"
8
+
9
+ module VagrantPlugins
10
+ module Noderings
11
+ # The API client and the per-machine files the provider keeps in .vagrant/.
12
+ class Session
13
+ # Tests plug an in-memory API in here.
14
+ class << self
15
+ attr_accessor :transport, :sleeper
16
+ end
17
+
18
+ attr_reader :machine
19
+
20
+ def initialize(machine)
21
+ @machine = machine
22
+ end
23
+
24
+ def config
25
+ machine.provider_config
26
+ end
27
+
28
+ def client
29
+ @client ||= NoderingsVagrant::Client.new(
30
+ token: config.token, api_url: config.api_url, organization_id: config.organization_id,
31
+ user_agent: "vagrant-noderings/#{VERSION} (Vagrant #{Vagrant::VERSION})",
32
+ transport: self.class.transport, sleeper: self.class.sleeper
33
+ )
34
+ end
35
+
36
+ def compute
37
+ NoderingsVagrant::Compute
38
+ end
39
+
40
+ # Runs an API call and turns its failure into a Vagrant error with the API's message.
41
+ def call(what)
42
+ yield
43
+ rescue NoderingsVagrant::APIError, NoderingsVagrant::UsageError, NoderingsVagrant::WaitTimeout,
44
+ *NoderingsVagrant::CONNECTION_ERRORS => e
45
+ raise Errors::APIError, action: what, message: e.message
46
+ end
47
+
48
+ def dir
49
+ path = machine.data_dir.join("state")
50
+ FileUtils.mkdir_p(path)
51
+ path
52
+ end
53
+
54
+ # The hostname to order with: configured, or remembered from the first `up`.
55
+ def hostname
56
+ return config.hostname if config.hostname
57
+
58
+ file = dir.join("hostname")
59
+ return file.read.strip if file.exist?
60
+
61
+ base = "vagrant-#{machine.name}".downcase.gsub(/[^a-z0-9-]+/, "-").squeeze("-")
62
+ name = "#{base[0, 56].chomp('-')}-#{SecureRandom.alphanumeric(6).downcase}"
63
+ file.write(name)
64
+ name
65
+ end
66
+
67
+ def private_key_path
68
+ dir.join("id_rsa")
69
+ end
70
+
71
+ def generated_key?
72
+ private_key_path.exist?
73
+ end
74
+
75
+ # NodeRings SSH key IDs to install: the configured ones, or a key generated for
76
+ # this machine and uploaded as a temporary key.
77
+ def ssh_key_ids
78
+ return call("resolving SSH keys") { compute.resolve_ssh_keys(client, config.ssh_keys) } unless config.ssh_keys.empty?
79
+
80
+ unless generated_key?
81
+ key = OpenSSL::PKey::RSA.generate(3072)
82
+ File.open(private_key_path, "w", 0o600) { |f| f.write(key.to_pem) }
83
+ end
84
+ id_file = dir.join("ssh_key_id")
85
+ return [id_file.read.strip] if id_file.exist?
86
+
87
+ uploaded = call("uploading the SSH key") { client.create_ssh_key("vagrant-#{hostname}", public_key) }
88
+ id_file.write(uploaded["id"])
89
+ [uploaded["id"]]
90
+ end
91
+
92
+ def public_key
93
+ rsa = OpenSSL::PKey::RSA.new(private_key_path.read)
94
+ str = ->(v) { [v.bytesize].pack("N") + v }
95
+ mpint = ->(b) { str.call(b.getbyte(0).nobits?(0x80) ? b : "\0#{b}") }
96
+ blob = str.call("ssh-rsa") + mpint.call(rsa.e.to_s(2)) + mpint.call(rsa.n.to_s(2))
97
+ "ssh-rsa #{[blob].pack('m0')} vagrant-#{hostname}"
98
+ end
99
+
100
+ # Deletes the temporary key and every file the provider kept for this machine.
101
+ def cleanup
102
+ id_file = dir.join("ssh_key_id")
103
+ if id_file.exist?
104
+ call("deleting the temporary SSH key") do
105
+ client.delete_ssh_key(id_file.read.strip)
106
+ rescue NoderingsVagrant::APIError => e
107
+ raise unless e.not_found?
108
+ end
109
+ end
110
+ FileUtils.rm_rf(dir)
111
+ end
112
+
113
+ # The VM this machine points at, or nil when it is gone.
114
+ def vm
115
+ return nil if machine.id.nil?
116
+
117
+ call("reading the server") do
118
+ client.get_virtual_machine(machine.id)
119
+ rescue NoderingsVagrant::APIError => e
120
+ raise unless e.not_found?
121
+
122
+ nil
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VagrantPlugins
4
+ module Noderings
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+ require "vagrant-noderings/plugin"
5
+
6
+ module VagrantPlugins
7
+ # The NodeRings provider for Vagrant.
8
+ module Noderings
9
+ lib_path = Pathname.new(File.expand_path("vagrant-noderings", __dir__))
10
+ autoload :Action, lib_path.join("action")
11
+ autoload :Errors, lib_path.join("errors")
12
+ autoload :Session, lib_path.join("session")
13
+
14
+ # The root of the plugin's source, for the locale file.
15
+ def self.source_root
16
+ @source_root ||= Pathname.new(File.expand_path("..", __dir__))
17
+ end
18
+ end
19
+ end
data/locales/en.yml ADDED
@@ -0,0 +1,47 @@
1
+ en:
2
+ vagrant_noderings:
3
+ already_created: |-
4
+ The server already exists and is running.
5
+ already_stopped: |-
6
+ The server is already stopped.
7
+ not_created: |-
8
+ The server is not created. Run `vagrant up` first.
9
+ will_not_destroy: |-
10
+ The server '%{name}' will not be destroyed, since the confirmation was declined.
11
+ ordering: |-
12
+ Ordering NodeRings server %{hostname} (%{offer}, %{distro})...
13
+ ordered: |-
14
+ Server %{id} ordered. Waiting for it to be provisioned...
15
+ address: |-
16
+ The server has address %{address}.
17
+ waiting_for_ssh: |-
18
+ Waiting for SSH to answer...
19
+ ready: |-
20
+ Machine is booted and ready for use!
21
+ starting: |-
22
+ Starting the server...
23
+ stopping: |-
24
+ Stopping the server. Its disk and IP addresses are kept.
25
+ destroying: |-
26
+ Deleting the server and its temporary SSH key...
27
+
28
+ states:
29
+ short_not_created: |-
30
+ not created
31
+ long_not_created: |-
32
+ The server does not exist. Run `vagrant up` to order it.
33
+ short_running: |-
34
+ running
35
+ long_running: |-
36
+ The server is running. Use `vagrant ssh` to log in, or `vagrant halt` to stop it.
37
+ short_stopped: |-
38
+ stopped
39
+ long_stopped: |-
40
+ The server is stopped. Its disk and IP addresses are kept. Run `vagrant up`
41
+ to start it, or `vagrant destroy` to delete it.
42
+
43
+ errors:
44
+ api_error: |-
45
+ NodeRings: %{action}: %{message}
46
+ no_address: |-
47
+ The server has no IP address yet. Run `vagrant destroy` and try again.
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-noderings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - NodeRings
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: A Vagrant provider that orders NodeRings marketplace virtual machines,
13
+ syncs folders with rsync, provisions them, and deletes them with their temporary
14
+ SSH key on vagrant destroy.
15
+ email:
16
+ - partners@noderings.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - CHANGELOG.md
22
+ - LICENSE
23
+ - README.md
24
+ - lib/vagrant-noderings.rb
25
+ - lib/vagrant-noderings/action.rb
26
+ - lib/vagrant-noderings/action/cleanup_files.rb
27
+ - lib/vagrant-noderings/action/create_server.rb
28
+ - lib/vagrant-noderings/action/destroy_server.rb
29
+ - lib/vagrant-noderings/action/is_created.rb
30
+ - lib/vagrant-noderings/action/is_stopped.rb
31
+ - lib/vagrant-noderings/action/read_ssh_info.rb
32
+ - lib/vagrant-noderings/action/read_state.rb
33
+ - lib/vagrant-noderings/action/start_server.rb
34
+ - lib/vagrant-noderings/action/stop_server.rb
35
+ - lib/vagrant-noderings/action/wait_for_communicator.rb
36
+ - lib/vagrant-noderings/api/client.rb
37
+ - lib/vagrant-noderings/api/compute.rb
38
+ - lib/vagrant-noderings/config.rb
39
+ - lib/vagrant-noderings/errors.rb
40
+ - lib/vagrant-noderings/plugin.rb
41
+ - lib/vagrant-noderings/provider.rb
42
+ - lib/vagrant-noderings/session.rb
43
+ - lib/vagrant-noderings/version.rb
44
+ - locales/en.yml
45
+ homepage: https://github.com/noderings/vagrant-noderings
46
+ licenses:
47
+ - Apache-2.0
48
+ metadata:
49
+ bug_tracker_uri: https://github.com/noderings/vagrant-noderings/issues
50
+ changelog_uri: https://github.com/noderings/vagrant-noderings/blob/main/CHANGELOG.md
51
+ documentation_uri: https://github.com/noderings/vagrant-noderings#readme
52
+ homepage_uri: https://github.com/noderings/vagrant-noderings
53
+ source_code_uri: https://github.com/noderings/vagrant-noderings
54
+ rubygems_mfa_required: 'true'
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '3.0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.6.9
70
+ specification_version: 4
71
+ summary: Vagrant provider for NodeRings virtual machines
72
+ test_files: []