vagrant-tiktalik 0.0.1

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,18 @@
1
+ module VagrantPlugins
2
+ module TiktalikVagrant
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, env)
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,16 @@
1
+ module VagrantPlugins
2
+ module TiktalikVagrant
3
+ module Action
4
+ class MessageAlreadyCreated
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env[:ui].info(I18n.t("vagrant_tiktalik.already_created"))
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module VagrantPlugins
2
+ module TiktalikVagrant
3
+ module Action
4
+ class MessageNotCreated
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env[:ui].info(I18n.t("vagrant_tiktalik.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 TiktalikVagrant
3
+ module Action
4
+ class MessageWillNotDestroy
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env[:ui].info(I18n.t("vagrant_tiktalik.will_not_destroy", name: env[:machine].name))
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,31 @@
1
+ require "log4r"
2
+
3
+ require "tiktalik"
4
+
5
+ module VagrantPlugins
6
+ module TiktalikVagrant
7
+ module Action
8
+ class PowerOff
9
+ def initialize(app, env)
10
+ @app = app
11
+ @logger = Log4r::Logger.new("vagrant_tiktalik::action::power_off")
12
+ @config = env[:machine].provider_config
13
+ end
14
+
15
+ def call(env)
16
+ env[:ui].info(I18n.t("vagrant_tiktalik.powering_off"))
17
+
18
+ t = Tiktalik
19
+ t.api_key = @config.api_key
20
+ t.api_secret_key = @config.api_secret
21
+
22
+ i = Tiktalik::Computing::Instance
23
+ instance = i.find env[:machine].id
24
+ instance.stop
25
+
26
+ @app.call(env)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,33 @@
1
+ require "log4r"
2
+
3
+ require "tiktalik"
4
+
5
+ module VagrantPlugins
6
+ module TiktalikVagrant
7
+ module Action
8
+ class PowerOn
9
+ def initialize(app, env)
10
+ @app = app
11
+ @logger = Log4r::Logger.new("vagrant_tiktalik::action::power_on")
12
+ @config = env[:machine].provider_config
13
+ end
14
+
15
+ def call(env)
16
+ env[:ui].info(I18n.t("vagrant_tiktalik.powering_on"))
17
+
18
+ t = Tiktalik
19
+ t.api_key = @config.api_key
20
+ t.api_secret_key = @config.api_secret
21
+
22
+ i = Tiktalik::Computing::Instance
23
+ instance = i.find env[:machine].id
24
+ instance.start
25
+
26
+ env[:machine].id = instance.uuid
27
+
28
+ @app.call(env)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,53 @@
1
+ require "log4r"
2
+
3
+ module VagrantPlugins
4
+ module TiktalikVagrant
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, env)
10
+ @app = app
11
+ @logger = Log4r::Logger.new("vagrant_tiktalik::action::read_ssh_info")
12
+ @config = env[:machine].provider_config
13
+ end
14
+
15
+ def call(env)
16
+ env[:machine_ssh_info] = read_ssh_info(env[:machine])
17
+
18
+ @app.call(env)
19
+ end
20
+
21
+ def read_ssh_info(machine)
22
+ return nil if machine.id.nil?
23
+
24
+ t = Tiktalik
25
+ t.api_key = @config.api_key
26
+ t.api_secret_key = @config.api_secret
27
+
28
+ i = Tiktalik::Computing::Instance
29
+ instance = i.find machine.id
30
+
31
+ if instance.nil?
32
+ # The machine can't be found
33
+ @logger.info("Machine couldn't be found, assuming it got destroyed.")
34
+ machine.id = nil
35
+ return nil
36
+ end
37
+
38
+ ip = nil
39
+ instance.interfaces.each do |iface|
40
+ ip = iface.ip if iface.network.public == true
41
+ end
42
+
43
+ # Read the DNS info
44
+ return {
45
+ :host => ip,
46
+ :port => 22,
47
+ :username => "root"
48
+ }
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,68 @@
1
+ require "log4r"
2
+ require 'time'
3
+
4
+ require "tiktalik"
5
+ #require "tiktalik/computing/instance"
6
+
7
+ module VagrantPlugins
8
+ module TiktalikVagrant
9
+ module Action
10
+ # This action reads the state of the machine and puts it in the
11
+ # `:machine_state_id` key in the environment.
12
+ class ReadState
13
+ def initialize(app, env)
14
+ @app = app
15
+ @logger = Log4r::Logger.new("vagrant_tiktalik::action::read_state")
16
+ @config = env[:machine].provider_config
17
+ end
18
+
19
+ def call(env)
20
+ t = Tiktalik
21
+ t.api_key = @config.api_key
22
+ t.api_secret_key = @config.api_secret
23
+
24
+ env[:machine_state_id] = :not_created
25
+
26
+ hostname = env[:machine].config.vm.hostname || env[:machine].name
27
+
28
+ i = Tiktalik::Computing::Instance
29
+ instances = i.all
30
+ instances.each do |instance|
31
+ if instance.hostname == hostname.to_s
32
+ env[:machine].id = instance.uuid if env[:machine].id.nil?
33
+ case instance.state
34
+ when 12
35
+ if instance.running
36
+ env[:machine_state_id] = :running
37
+ else
38
+ env[:machine_state_id] = :stopped
39
+ end
40
+ else
41
+ env[:machine_state_id] = :pending
42
+ end
43
+ break
44
+ end
45
+ end
46
+
47
+ @app.call(env)
48
+ end
49
+
50
+ #def read_state(env)
51
+ # return :not_created if machine.id.nil?
52
+ #
53
+ # # Find the machine
54
+ # server = aws.servers.get(machine.id)
55
+ # if server.nil? || [:"shutting-down", :terminated].include?(server.state.to_sym)
56
+ # # The machine can't be found
57
+ # @logger.info("Machine not found or terminated, assuming it got destroyed.")
58
+ # machine.id = nil
59
+ # return :not_created
60
+ # end
61
+ #
62
+ # # Return the state
63
+ # return server.state.to_sym
64
+ #end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,133 @@
1
+ require "log4r"
2
+ require 'vagrant/util/retryable'
3
+ require 'vagrant-tiktalik/util/timer'
4
+
5
+ module VagrantPlugins
6
+ module TiktalikVagrant
7
+ module Action
8
+ # This runs the configured instance.
9
+ class RunInstance
10
+ include Vagrant::Util::Retryable
11
+
12
+ def initialize(app, env)
13
+ @app = app
14
+ @logger = Log4r::Logger.new("vagrant_tiktalik::action::run_instance")
15
+ @config = env[:machine].provider_config
16
+ end
17
+
18
+ def call(env)
19
+ image = @config.image
20
+ hostname = env[:machine].config.vm.hostname || env[:machine].name
21
+ size = @config.size
22
+ ssh_key = @config.ssh_key
23
+
24
+ # If there is no keypair then warn the user
25
+ #if !keypair
26
+ # env[:ui].warn(I18n.t("vagrant_tiktalik.launch_no_keypair"))
27
+ #end
28
+
29
+ # If there is a subnet ID then warn the user
30
+ #if subnet_id
31
+ # env[:ui].warn(I18n.t("vagrant_tiktalik.launch_vpc_warning"))
32
+ #end
33
+
34
+ # Launch!
35
+ env[:ui].info(I18n.t("vagrant_tiktalik.launching_instance"))
36
+ env[:ui].info(" -- Image: #{image}")
37
+ env[:ui].info(" -- Hostname: #{hostname}")
38
+ env[:ui].info(" -- Size: #{size}")
39
+ env[:ui].info(" -- SSH key: #{ssh_key}")
40
+
41
+ #env[:ui].info(" -- Security Groups: #{security_groups.inspect}") if !security_groups.empty?
42
+
43
+ t = Tiktalik
44
+ t.api_key = @config.api_key
45
+ t.api_secret_key = @config.api_secret
46
+
47
+ begin
48
+ networks = []
49
+
50
+ n = Tiktalik::Computing::Network
51
+ n.all.each do |network|
52
+ (networks.push network.uuid.to_s) if network.public
53
+ break if network.public
54
+ end
55
+
56
+ env[:ui].info(" -- Networks: #{networks}")
57
+
58
+ options = {
59
+ :image_uuid => image,
60
+ :hostname => hostname,
61
+ :size => size,
62
+ :"networks[]" => networks,
63
+ :ssh_key => ssh_key
64
+ }
65
+
66
+ #if !security_groups.empty?
67
+ # security_group_key = options[:subnet_id].nil? ? :groups : :security_group_ids
68
+ # options[security_group_key] = security_groups
69
+ #end
70
+
71
+ i = Tiktalik::Computing::Instance
72
+ instance = i.create options
73
+ rescue Exception => e
74
+ raise e
75
+ # # Invalid subnet doesn't have its own error so we catch and
76
+ # # check the error message here.
77
+ # if e.message =~ /subnet ID/
78
+ # raise Errors::FogError,
79
+ # :message => "Subnet ID not found: #{subnet_id}"
80
+ # end
81
+ #
82
+ # raise
83
+ #rescue Fog::Compute::TiktalikVagrant::Error => e
84
+ # raise Errors::FogError, :message => e.message
85
+ end
86
+
87
+ ## Immediately save the ID since it is created at this point.
88
+ env[:machine].id = instance.uuid
89
+
90
+ env[:ui].info(I18n.t("vagrant_tiktalik.waiting_for_ready"))
91
+
92
+ ## Wait for the instance to be ready first
93
+ retryable(:tries => 120, :sleep => 10) do
94
+ next if env[:interrupted]
95
+ result = i.find instance.uuid
96
+ yield result if block_given?
97
+ raise 'not ready' if result.state != 12 || !result.running
98
+ end
99
+
100
+ env[:ui].info(I18n.t("vagrant_tiktalik.waiting_for_ssh"))
101
+
102
+ # waiting for ssh to start up and server ssh keys to generate
103
+ retryable(:tries => 120, :sleep => 10) do
104
+ next if env[:interrupted]
105
+ raise 'not ready' if !env[:machine].communicate.ready?
106
+ end
107
+
108
+ # Ready and booted!
109
+ env[:ui].info(I18n.t("vagrant_tiktalik.ready"))
110
+
111
+ @app.call(env)
112
+ end
113
+
114
+ def recover(env)
115
+ return if env["vagrant.error"].is_a?(Vagrant::Errors::VagrantError)
116
+
117
+ if env[:machine].provider.state.id != :not_created
118
+ # Undo the import
119
+ terminate(env)
120
+ end
121
+ end
122
+
123
+ def terminate(env)
124
+ destroy_env = env.dup
125
+ destroy_env.delete(:interrupted)
126
+ destroy_env[:config_validate] = false
127
+ destroy_env[:force_confirm_destroy] = true
128
+ env[:action_runner].run(Action.action_destroy, destroy_env)
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,67 @@
1
+ require "log4r"
2
+
3
+ require "vagrant/util/subprocess"
4
+
5
+ require "vagrant/util/scoped_hash_override"
6
+
7
+ module VagrantPlugins
8
+ module TiktalikVagrant
9
+ module Action
10
+ # This middleware uses `rsync` to sync the folders over to the
11
+ # Tiktalik instance.
12
+ class SyncFolders
13
+ include Vagrant::Util::ScopedHashOverride
14
+
15
+ def initialize(app, env)
16
+ @app = app
17
+ @logger = Log4r::Logger.new("vagrant_tiktalik::action::sync_folders")
18
+ end
19
+
20
+ def call(env)
21
+ @app.call(env)
22
+
23
+ ssh_info = env[:machine].ssh_info
24
+
25
+ env[:machine].config.vm.synced_folders.each do |id, data|
26
+ data = scoped_hash_override(data, :aws)
27
+
28
+ # Ignore disabled shared folders
29
+ next if data[:disabled]
30
+
31
+ hostpath = File.expand_path(data[:hostpath], env[:root_path])
32
+ guestpath = data[:guestpath]
33
+
34
+ # Make sure there is a trailing slash on the host path to
35
+ # avoid creating an additional directory with rsync
36
+ hostpath = "#{hostpath}/" if hostpath !~ /\/$/
37
+
38
+ env[:ui].info(I18n.t("vagrant_tiktalik.rsync_folder",
39
+ :hostpath => hostpath,
40
+ :guestpath => guestpath))
41
+
42
+ # Create the guest path
43
+ env[:machine].communicate.sudo("mkdir -p '#{guestpath}'")
44
+ env[:machine].communicate.sudo(
45
+ "chown #{ssh_info[:username]} '#{guestpath}'")
46
+
47
+ # Rsync over to the guest path using the SSH info
48
+ command = [
49
+ "rsync", "--verbose", "--archive", "-z",
50
+ "--exclude", ".vagrant/",
51
+ "-e", "ssh -p #{ssh_info[:port]} -o StrictHostKeyChecking=no -i '#{ssh_info[:private_key_path]}'",
52
+ hostpath,
53
+ "#{ssh_info[:username]}@#{ssh_info[:host]}:#{guestpath}"]
54
+
55
+ r = Vagrant::Util::Subprocess.execute(*command)
56
+ if r.exit_code != 0
57
+ raise Errors::RsyncError,
58
+ :guestpath => guestpath,
59
+ :hostpath => hostpath,
60
+ :stderr => r.stderr
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end