vagrant-lightsail 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.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.rubocop.yml +26 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +117 -0
- data/Rakefile +20 -0
- data/box/lightsail.box +0 -0
- data/box/metadata.json +3 -0
- data/lib/vagrant-lightsail/action/connect_lightsail.rb +33 -0
- data/lib/vagrant-lightsail/action/is_created.rb +18 -0
- data/lib/vagrant-lightsail/action/is_stopped.rb +18 -0
- data/lib/vagrant-lightsail/action/message_already_created.rb +16 -0
- data/lib/vagrant-lightsail/action/message_not_created.rb +16 -0
- data/lib/vagrant-lightsail/action/message_will_not_destroy.rb +16 -0
- data/lib/vagrant-lightsail/action/read_ssh_info.rb +38 -0
- data/lib/vagrant-lightsail/action/read_state.rb +46 -0
- data/lib/vagrant-lightsail/action/run_instance.rb +89 -0
- data/lib/vagrant-lightsail/action/setup_key.rb +55 -0
- data/lib/vagrant-lightsail/action/start_instance.rb +40 -0
- data/lib/vagrant-lightsail/action/stop_instance.rb +28 -0
- data/lib/vagrant-lightsail/action/terminate_instance.rb +31 -0
- data/lib/vagrant-lightsail/action/wait_for_state.rb +40 -0
- data/lib/vagrant-lightsail/action.rb +165 -0
- data/lib/vagrant-lightsail/config.rb +125 -0
- data/lib/vagrant-lightsail/errors.rb +17 -0
- data/lib/vagrant-lightsail/plugin.rb +28 -0
- data/lib/vagrant-lightsail/provider.rb +41 -0
- data/lib/vagrant-lightsail/version.rb +5 -0
- data/lib/vagrant-lightsail.rb +13 -0
- data/locales/en.yml +50 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/vagrant/lightsail_spec.rb +11 -0
- data/test/Vagrantfile +21 -0
- data/test/puppet/lightsail/manifests/default.pp +1 -0
- data/test/scripts/provision.sh +3 -0
- data/test/test.sh +33 -0
- data/vagrant-lightsail.gemspec +27 -0
- metadata +160 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 82b6fc2ea5e0e5fd4c462a2e74516426060ca956
|
4
|
+
data.tar.gz: 3a9418b432a35da4848f966695fa40b0ef2b5b2e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 51b0c4ba1515e68cf9cfa559e51f57708f4576d4a145f4875e3b8d4c9992ff0a1978a98ebf3bfdd49ab678ac02c728dea224021237f044d49dfd05c155c5afd6
|
7
|
+
data.tar.gz: 6b13ed86e7e12095f3dc5ab2961f8f6fbec18be9095ba8f27c65d352d1ca89ede07bb3ef566bf23a5eed1d834da71b71f1cd386b7933732ce7d839c14c62f5f7
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Metrics/LineLength:
|
2
|
+
Enabled: false
|
3
|
+
|
4
|
+
Metrics/AbcSize:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
Metrics/PerceivedComplexity:
|
8
|
+
Enabled: false
|
9
|
+
|
10
|
+
Metrics/MethodLength:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
Style/FileName:
|
14
|
+
Enabled: false
|
15
|
+
|
16
|
+
Metrics/CyclomaticComplexity:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Style/EmptyElse:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Metrics/ModuleLength:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Style/Documentation:
|
26
|
+
Enabled: false
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.6
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
group :development do
|
4
|
+
gem 'vagrant', git: 'https://github.com/mitchellh/vagrant.git', tag: 'v1.8.7'
|
5
|
+
end
|
6
|
+
|
7
|
+
group :plugins do
|
8
|
+
gem 'vagrant-lightsail', path: '.'
|
9
|
+
gem 'vagrant-puppet-install', '~> 4.1'
|
10
|
+
end
|
11
|
+
|
12
|
+
gemspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Alejandro Figueroa
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# Lightsail Vagrant Provider
|
2
|
+
|
3
|
+
`vagrant-lightsail` is a [Vagrant](https://www.vagrantup.com/) 1.2+
|
4
|
+
plugin that support managing [Lightsail](https://amazonlightsail.com/)
|
5
|
+
instances.
|
6
|
+
|
7
|
+
It can:
|
8
|
+
- Create and destroy instances
|
9
|
+
- Power on and off instances
|
10
|
+
- Provision an instance
|
11
|
+
- Setup an SSH public key for authentication
|
12
|
+
|
13
|
+
It is based heavily
|
14
|
+
on [vagrant-aws](https://github.com/mitchellh/vagrant-aws)
|
15
|
+
and
|
16
|
+
[vagrant-digitalocean](https://github.com/devopsgroup-io/vagrant-digitalocean).
|
17
|
+
|
18
|
+
## Install
|
19
|
+
|
20
|
+
Install the provider plugin using the Vagrant command-line interface:
|
21
|
+
|
22
|
+
`vagrant plugin install vagrant-lightsail`
|
23
|
+
|
24
|
+
## Quick Start
|
25
|
+
|
26
|
+
After installing the plugin, specify all the details within the
|
27
|
+
`config.vm.provider` block in your Vagrantfile.
|
28
|
+
|
29
|
+
```
|
30
|
+
Vagrant.configure('2') do |config|
|
31
|
+
config.ssh.private_key_path = 'PATH TO YOUR PRIVATE KEY'
|
32
|
+
config.ssh.username = 'ubuntu'
|
33
|
+
config.vm.box = 'lightsail'
|
34
|
+
config.vm.box_url = 'https://github.com/thejandroman/vagrant-lightsail/raw/master/box/lightsail.box'
|
35
|
+
|
36
|
+
config.vm.provider :lightsail do |provider, override|
|
37
|
+
provider.access_key_id = 'YOUR KEY'
|
38
|
+
provider.secret_access_key = 'YOUR SECRET KEY'
|
39
|
+
provider.keypair_name = 'KEYPAIR NAME'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
And then run `vagrant up`.
|
45
|
+
|
46
|
+
This will start an Ubuntu instance in the us-east-1a availability zone
|
47
|
+
within your account.
|
48
|
+
|
49
|
+
**Note:** if you don't configure `provider.access_key_id` or
|
50
|
+
`provider.secret_access_key` it will attempt to read credentials from
|
51
|
+
environment variables first and then from `$HOME/.aws/`. You can
|
52
|
+
choose your AWS profile and files location by using
|
53
|
+
`provider.aws_profile` and `provider.aws_dir`, however environment
|
54
|
+
variables will always have precedence as defined by
|
55
|
+
the
|
56
|
+
[AWS documentation](http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html).
|
57
|
+
To use profile `vagrantDev` from your AWS files:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
provider.aws_dir = ENV['HOME'] + "/.aws/"
|
61
|
+
provider.aws_profile = "vagrantDev"
|
62
|
+
```
|
63
|
+
|
64
|
+
## Configuration
|
65
|
+
|
66
|
+
The following attributes are available to configure the provider:
|
67
|
+
|
68
|
+
- `access_key_id`
|
69
|
+
* The access key for accessing AWS
|
70
|
+
- `availability_zone`
|
71
|
+
* The availability zone within the region to launch the instance. If
|
72
|
+
nil, it will append `a` to the region.
|
73
|
+
- `aws_dir`
|
74
|
+
* AWS config and credentials location. Defaults to *$HOME/.aws/*.
|
75
|
+
- `aws_profile`
|
76
|
+
* AWS profile in your config files. Defaults to *default*.
|
77
|
+
- `blueprint_id`
|
78
|
+
* The ID for a virtual private server image. Defaults to *ubuntu_16_04*.
|
79
|
+
- `bundle_id`
|
80
|
+
* The bundle of specification information for the instance,
|
81
|
+
including the pricing plan. Defaults to *nano_1_0*.
|
82
|
+
- `endpoint`
|
83
|
+
* A regional endpoint.
|
84
|
+
- `keypair_name`
|
85
|
+
* The name to use when creating an SSH key for
|
86
|
+
authentication. Defaults to *vagrant*.
|
87
|
+
- `region`
|
88
|
+
* The region to start the instance in. Defaults to *us-east-1*.
|
89
|
+
- `secret_access_key`
|
90
|
+
* The secret access key for accessing AWS.
|
91
|
+
- `session_token`
|
92
|
+
* The session token provided by STS.
|
93
|
+
- `user_data`
|
94
|
+
* Plain text user data for the instance being booted.
|
95
|
+
|
96
|
+
## Contributing
|
97
|
+
|
98
|
+
- Fork and clone repo
|
99
|
+
- Install bundler
|
100
|
+
```
|
101
|
+
gem install bundler -v 1.12.5
|
102
|
+
```
|
103
|
+
- Install dependencies
|
104
|
+
```
|
105
|
+
bundle install
|
106
|
+
```
|
107
|
+
- Run tests
|
108
|
+
```
|
109
|
+
bundle exec rake test
|
110
|
+
```
|
111
|
+
- Make code changes
|
112
|
+
- Run tests again
|
113
|
+
- Create a Pull Request!
|
114
|
+
|
115
|
+
## License
|
116
|
+
|
117
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rubocop/rake_task'
|
3
|
+
|
4
|
+
RuboCop::RakeTask.new
|
5
|
+
|
6
|
+
task :shell_test do
|
7
|
+
result = sh 'bash test/test.sh'
|
8
|
+
|
9
|
+
if result
|
10
|
+
puts 'Success!'
|
11
|
+
else
|
12
|
+
puts 'Failure!'
|
13
|
+
exit 1
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Run all tests: rubocop, shell_test'
|
18
|
+
task test: [:rubocop, :shell_test]
|
19
|
+
|
20
|
+
task default: :test
|
data/box/lightsail.box
ADDED
Binary file
|
data/box/metadata.json
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'aws-sdk'
|
2
|
+
require 'log4r'
|
3
|
+
|
4
|
+
module VagrantPlugins
|
5
|
+
module Lightsail
|
6
|
+
module Action
|
7
|
+
# This action connects to Lightsail, verifies credentials work,
|
8
|
+
# and puts the Lightsail connection object into the
|
9
|
+
# `:lightsail_client` key in the environment.
|
10
|
+
class ConnectLightsail
|
11
|
+
def initialize(app, _)
|
12
|
+
@app = app
|
13
|
+
@logger = Log4r::Logger.new('vagrant_lightsail::action::connect_lightsail')
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(env)
|
17
|
+
aws_sdk_config = {
|
18
|
+
access_key_id: env[:machine].provider_config.access_key_id,
|
19
|
+
secret_access_key: env[:machine].provider_config.secret_access_key,
|
20
|
+
session_token: env[:machine].provider_config.session_token,
|
21
|
+
region: env[:machine].provider_config.region
|
22
|
+
}
|
23
|
+
aws_sdk_config[:endpoint] = env[:machine].provider_config.endpoint if env[:machine].provider_config.endpoint
|
24
|
+
|
25
|
+
@logger.info('Connecting to AWS Lightsail...')
|
26
|
+
env[:lightsail_client] = Aws::Lightsail::Client.new(aws_sdk_config)
|
27
|
+
|
28
|
+
@app.call(env)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module Lightsail
|
3
|
+
module Action
|
4
|
+
# This can be used with "Call" built-in to check if the machine
|
5
|
+
# is created and branch in the middleware.
|
6
|
+
class IsCreated
|
7
|
+
def initialize(app, _)
|
8
|
+
@app = app
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(env)
|
12
|
+
env[:result] = env[:machine].state.id != :not_created
|
13
|
+
@app.call(env)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module Lightsail
|
3
|
+
module Action
|
4
|
+
# This can be used with "Call" built-in to check if the machine
|
5
|
+
# is stopped and branch in the middleware.
|
6
|
+
class IsStopped
|
7
|
+
def initialize(app, _)
|
8
|
+
@app = app
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(env)
|
12
|
+
env[:result] = env[:machine].state.id == :stopped
|
13
|
+
@app.call(env)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module Lightsail
|
3
|
+
module Action
|
4
|
+
class MessageAlreadyCreated
|
5
|
+
def initialize(app, _)
|
6
|
+
@app = app
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
env[:ui].info(I18n.t('vagrant_lightsail.already_status', status: 'created'))
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module Lightsail
|
3
|
+
module Action
|
4
|
+
class MessageNotCreated
|
5
|
+
def initialize(app, _)
|
6
|
+
@app = app
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
env[:ui].info(I18n.t('vagrant_lightsail.not_created'))
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module Lightsail
|
3
|
+
module Action
|
4
|
+
class MessageWillNotDestroy
|
5
|
+
def initialize(app, _)
|
6
|
+
@app = app
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
env[:ui].info I18n.t 'vagrant_lightsail.will_not_destroy', name: env[:machine].name
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'log4r'
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module Lightsail
|
5
|
+
module Action
|
6
|
+
# This action reads the SSH info for the machine and puts it into the
|
7
|
+
# `:machine_ssh_info` key in the environment.
|
8
|
+
class ReadSSHInfo
|
9
|
+
def initialize(app, _)
|
10
|
+
@app = app
|
11
|
+
@logger = Log4r::Logger.new('vagrant_lightsail::action::read_ssh_info')
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
env[:machine_ssh_info] = read_ssh_info(env[:lightsail_client],
|
16
|
+
env[:machine])
|
17
|
+
@app.call(env)
|
18
|
+
end
|
19
|
+
|
20
|
+
def read_ssh_info(lightsail, machine)
|
21
|
+
return nil if machine.id.nil?
|
22
|
+
|
23
|
+
begin
|
24
|
+
# Find the machine
|
25
|
+
server = lightsail.get_instance(instance_name: machine.id)
|
26
|
+
rescue Aws::Lightsail::Errors::NotFoundException
|
27
|
+
# The machine can't be found
|
28
|
+
@logger.info('Machine could not be found, assuming it got destroyed.')
|
29
|
+
machine.id = nil
|
30
|
+
return nil
|
31
|
+
end
|
32
|
+
|
33
|
+
{ host: server.instance.public_ip_address, port: 22 }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'log4r'
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module Lightsail
|
5
|
+
module Action
|
6
|
+
# This action reads the state of the machine and puts it in the
|
7
|
+
# `:machine_state_id` key in the environment.
|
8
|
+
class ReadState
|
9
|
+
def initialize(app, _)
|
10
|
+
@app = app
|
11
|
+
@logger = Log4r::Logger.new('vagrant_lightsail::action::read_state')
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
env[:machine_state_id] = read_state(env[:lightsail_client],
|
16
|
+
env[:machine])
|
17
|
+
@app.call(env)
|
18
|
+
end
|
19
|
+
|
20
|
+
def read_state(lightsail, machine)
|
21
|
+
return :not_created if machine.id.nil?
|
22
|
+
|
23
|
+
begin
|
24
|
+
server = lightsail.get_instance_state(instance_name: machine.id)
|
25
|
+
rescue Aws::Lightsail::Errors::NotFoundException
|
26
|
+
return machine_not_found(machine)
|
27
|
+
end
|
28
|
+
|
29
|
+
if [:'shutting-down', :terminated].include? server.state.name.to_sym
|
30
|
+
return machine_not_found(machine)
|
31
|
+
end
|
32
|
+
|
33
|
+
server.state.name.to_sym
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def machine_not_found(machine)
|
39
|
+
@logger.info('Machine could not be found, assuming it got destroyed.')
|
40
|
+
machine.id = nil
|
41
|
+
:not_created
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'log4r'
|
2
|
+
require 'vagrant/util/retryable'
|
3
|
+
|
4
|
+
module VagrantPlugins
|
5
|
+
module Lightsail
|
6
|
+
module Action
|
7
|
+
# This runs the configured instance.
|
8
|
+
class RunInstance
|
9
|
+
include Vagrant::Util::Retryable
|
10
|
+
|
11
|
+
def initialize(app, _)
|
12
|
+
@app = app
|
13
|
+
@logger = Log4r::Logger.new('vagrant_lightsail::action::run_instance')
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(env)
|
17
|
+
# Get the configs
|
18
|
+
availability_zone = env[:machine].provider_config.availability_zone
|
19
|
+
blueprint_id = env[:machine].provider_config.blueprint_id
|
20
|
+
bundle_id = env[:machine].provider_config.bundle_id
|
21
|
+
instance_name = env[:machine].config.vm.hostname || env[:machine].name
|
22
|
+
keypair_name = env[:machine].provider_config.keypair_name
|
23
|
+
region = env[:machine].provider_config.region
|
24
|
+
user_data = env[:machine].provider_config.user_data
|
25
|
+
|
26
|
+
# If there is no keypair then warn the user
|
27
|
+
unless keypair_name
|
28
|
+
env[:ui].warn(I18n.t('vagrant_lightsail.launch_no_keypair'))
|
29
|
+
end
|
30
|
+
|
31
|
+
# Launch!
|
32
|
+
env[:ui].info(I18n.t('vagrant_lightsail.launching_instance'))
|
33
|
+
env[:ui].info(" -- Availability Zone: #{availability_zone}") if availability_zone
|
34
|
+
env[:ui].info(" -- Blueprint ID: #{blueprint_id}")
|
35
|
+
env[:ui].info(" -- Bundle ID: #{bundle_id}")
|
36
|
+
env[:ui].info(" -- Instance Name: #{instance_name}")
|
37
|
+
env[:ui].info(" -- Keypair: #{keypair_name}") if keypair_name
|
38
|
+
env[:ui].info(" -- Region: #{region}")
|
39
|
+
env[:ui].info(" -- User Data: #{user_data}") if user_data
|
40
|
+
env[:ui].info(' -- User Data: yes') if user_data
|
41
|
+
|
42
|
+
options = {
|
43
|
+
availability_zone: availability_zone,
|
44
|
+
blueprint_id: blueprint_id,
|
45
|
+
bundle_id: bundle_id,
|
46
|
+
instance_names: [instance_name],
|
47
|
+
key_pair_name: keypair_name,
|
48
|
+
user_data: user_data
|
49
|
+
}
|
50
|
+
|
51
|
+
begin
|
52
|
+
env[:lightsail_client].create_instances(options).operations[0]
|
53
|
+
rescue Aws::Lightsail::Errors => e
|
54
|
+
raise Errors::LightailError, message: e
|
55
|
+
end
|
56
|
+
|
57
|
+
server = env[:lightsail_client].get_instance(instance_name: instance_name).instance
|
58
|
+
env[:machine].id = server.name
|
59
|
+
|
60
|
+
# wait for ssh to be ready
|
61
|
+
retryable(tries: 120, sleep: 10) do
|
62
|
+
next if env[:interrupted]
|
63
|
+
env[:ui].info(I18n.t('vagrant_lightsail.waiting_for_ssh'))
|
64
|
+
raise 'not ready' unless env[:machine].communicate.ready?
|
65
|
+
end
|
66
|
+
|
67
|
+
env[:ui].info(I18n.t('vagrant_lightsail.ready'))
|
68
|
+
|
69
|
+
terminate(env) if env[:interrupted]
|
70
|
+
|
71
|
+
@app.call(env)
|
72
|
+
end
|
73
|
+
|
74
|
+
def recover(env)
|
75
|
+
return if env['vagrant.error'].is_a?(Vagrant::Errors::VagrantError)
|
76
|
+
terminate(env) if env[:machine].provider.state.id != :not_created
|
77
|
+
end
|
78
|
+
|
79
|
+
def terminate(env)
|
80
|
+
destroy_env = env.dup
|
81
|
+
destroy_env.delete(:interrupted)
|
82
|
+
destroy_env[:config_validate] = false
|
83
|
+
destroy_env[:force_confirm_destroy] = true
|
84
|
+
env[:action_runner].run(Action.destroy, destroy_env)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'log4r'
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module Lightsail
|
5
|
+
module Action
|
6
|
+
# This sets up the configured SSH key
|
7
|
+
class SetupKey
|
8
|
+
def initialize(app, _)
|
9
|
+
@app = app
|
10
|
+
@logger = Log4r::Logger.new('vagrant_lightsail::action::setup_key')
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
keypair_name = env[:machine].provider_config.keypair_name
|
15
|
+
|
16
|
+
begin
|
17
|
+
env[:lightsail_client].get_key_pair(key_pair_name: keypair_name)
|
18
|
+
rescue Aws::Lightsail::Errors::NotFoundException
|
19
|
+
create_ssh_key(keypair_name, env)
|
20
|
+
end
|
21
|
+
|
22
|
+
@app.call(env)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def create_ssh_key(name, env)
|
28
|
+
pub_key = public_key env[:machine].config.ssh.private_key_path
|
29
|
+
|
30
|
+
env[:ui].info I18n.t('vagrant_lightsail.info.creating_key', name: name)
|
31
|
+
|
32
|
+
begin
|
33
|
+
env[:lightsail_client].import_key_pair(
|
34
|
+
key_pair_name: name,
|
35
|
+
public_key_base_64: pub_key
|
36
|
+
)
|
37
|
+
rescue Aws::Lightsail::Errors => e
|
38
|
+
raise Errors::LightailError, message: e
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def public_key(private_key_path)
|
43
|
+
pub_key = if private_key_path.is_a? Array
|
44
|
+
Pathname.new private_key_path[0].to_s + '.pub'
|
45
|
+
else
|
46
|
+
Pathname.new private_key_path.to_s + '.pub'
|
47
|
+
end.expand_path
|
48
|
+
raise Errors::PublicKeyError, path: pub_key.to_s unless pub_key.file?
|
49
|
+
|
50
|
+
pub_key.read
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'log4r'
|
2
|
+
require 'vagrant/util/retryable'
|
3
|
+
|
4
|
+
module VagrantPlugins
|
5
|
+
module Lightsail
|
6
|
+
module Action
|
7
|
+
# This starts a stopped instance.
|
8
|
+
class StartInstance
|
9
|
+
include Vagrant::Util::Retryable
|
10
|
+
|
11
|
+
def initialize(app, _)
|
12
|
+
@app = app
|
13
|
+
@logger = Log4r::Logger.new('vagrant_lightsail::action::start_instance')
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(env)
|
17
|
+
server = env[:lightsail_client].get_instance(instance_name: env[:machine].id).instance
|
18
|
+
|
19
|
+
env[:ui].info I18n.t 'vagrant_lightsail.starting'
|
20
|
+
|
21
|
+
begin
|
22
|
+
env[:lightsail_client].start_instance(instance_name: server.name).operations[0]
|
23
|
+
rescue Aws::Lightsail::Errors => e
|
24
|
+
raise Errors::LightailError, message: e
|
25
|
+
end
|
26
|
+
|
27
|
+
retryable(tries: 120, sleep: 10) do
|
28
|
+
break if env[:interrupted]
|
29
|
+
env[:ui].info I18n.t 'vagrant_lightsail.waiting_for_ssh'
|
30
|
+
raise 'not ready' unless env[:machine].communicate.ready?
|
31
|
+
end
|
32
|
+
|
33
|
+
env[:ui].info I18n.t('vagrant_lightsail.ready')
|
34
|
+
|
35
|
+
@app.call env
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'log4r'
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module Lightsail
|
5
|
+
module Action
|
6
|
+
# This stops the running instance.
|
7
|
+
class StopInstance
|
8
|
+
def initialize(app, _)
|
9
|
+
@app = app
|
10
|
+
@logger = Log4r::Logger.new('vagrant_lightsail::action::stop_instance')
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
server = env[:lightsail_client].get_instance(instance_name: env[:machine].id).instance
|
15
|
+
|
16
|
+
if env[:machine].state.id == :stopped
|
17
|
+
env[:ui].info I18n.t('vagrant_lightsail.already_status', status: env[:machine].state.id)
|
18
|
+
else
|
19
|
+
env[:ui].info(I18n.t('vagrant_lightsail.stopping'))
|
20
|
+
env[:lightsail_client].stop_instance(instance_name: server.name)
|
21
|
+
end
|
22
|
+
|
23
|
+
@app.call(env)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|