Skip to content

Commit

Permalink
start, stop, terminate, read_state and auth good
Browse files Browse the repository at this point in the history
  • Loading branch information
devigned committed Mar 27, 2016
1 parent e2b639b commit 9411b2d
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 81 deletions.
2 changes: 1 addition & 1 deletion lib/vagrant-azure/action/is_stopped.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(app, env)
end

def call(env)
env[:result] = env[:machine].state.id != :stopped
env[:result] = env[:machine].state.id == :stopped
@app.call(env)
end
end
Expand Down
40 changes: 7 additions & 33 deletions lib/vagrant-azure/action/read_ssh_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,31 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License in the project root for license information.
require 'log4r'
require 'vagrant-azure/util/machine_id_helper'

module VagrantPlugins
module Azure
module Action
class ReadSSHInfo
include VagrantPlugins::Azure::Util::MachineIdHelper

def initialize(app, env, port = 22)
@app = app
@port = port
@logger = Log4r::Logger.new('vagrant_azure::action::read_ssh_info')
end

def call(env)
env[:ui].detail "Looking for local port #{@port}"

env[:machine_ssh_info] = read_ssh_info(
env[:azure_arm_service],
env
)

env[:ui].detail "Found port mapping #{env[:machine_ssh_info][:port]} --> #{@port}"

env[:machine_ssh_info] = read_ssh_info(env[:azure_arm_service], env)
@app.call(env)
end

def read_ssh_info(azure, env)
return nil if env[:machine].id.nil?
resource_group_name, vm_name = env[:machine].id.split(':')
vm = azure.compute.virtual_machines.get(resource_group_name, vm_name, 'instanceView').value!.body

if vm.nil?
# Machine cannot be found
@logger.info 'Machine not found. Assuming it was destroyed and cleaning up environment'
terminate(env)
return nil
end

# vm.tcp_endpoints.each do |endpoint|
# if endpoint[:local_port] == "#{@port}"
# return { :host => "#{vm.cloud_service_name}.cloudapp.net", :port => endpoint[:public_port] }
# end
# end

return nil
end
parsed = parse_machine_id(env[:machine].id)
public_ip = azure.network.public_ipaddresses.get(parsed[:group], "#{parsed[:name]}-vagrantPublicIP").value!.body

def terminate(env)
destroy_env = env.dup
destroy_env.delete(:interrupted)
destroy_env[:config_validate] = false
destroy_env[:force_confirm_destroy] = true
env[:action_runner].run(Action.action_destroy, destroy_env)
{:host => public_ip.properties.dns_settings.fqdn, :port => 22}
end
end
end
Expand Down
18 changes: 11 additions & 7 deletions lib/vagrant-azure/action/read_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,36 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License in the project root for license information.
require 'log4r'
require 'vagrant-azure/util/vm_status_translator'
require 'vagrant-azure/util/machine_id_helper'

module VagrantPlugins
module Azure
module Action
class ReadState
include VagrantPlugins::Azure::Util::VMStatusTranslator
include VagrantPlugins::Azure::Util::MachineIdHelper

def initialize(app, env)
@app = app
@logger = Log4r::Logger.new('vagrant_azure::action::read_state')
end

def call(env)
env[:machine_state_id] = read_state(env[:azure_arm_service], env)
env[:machine_state_id] = read_state(env[:azure_arm_service], env[:machine])
@app.call(env)
end

def read_state(azure, env)
machine = env[:machine]
def read_state(azure, machine)
return :not_created if machine.id.nil?

# Find the machine
rg_name, vm_name = machine.id.split(':')
parsed = parse_machine_id(machine.id)
vm = nil
begin
vm = azure.compute.virtual_machines.get(rg_name, vm_name, 'instanceView').value!.body
vm = azure.compute.virtual_machines.get(parsed[:group], parsed[:name], 'instanceView').value!.body
rescue MsRestAzure::AzureOperationError => ex
if vm.nil? || [:'shutting-down', :terminated].include?(vm.state.to_sym)
if vm.nil? || tearing_down?(vm.properties.instance_view.statuses)
# The machine can't be found
@logger.info('Machine not found or terminated, assuming it got destroyed.')
machine.id = nil
Expand All @@ -36,7 +40,7 @@ def read_state(azure, env)
end

# Return the state
vm.state.to_sym
power_state(vm.properties.instance_view.statuses)
end

end
Expand Down
12 changes: 7 additions & 5 deletions lib/vagrant-azure/action/restart_vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License in the project root for license information.
require 'log4r'
require 'vagrant-azure/util/machine_id_helper'

module VagrantPlugins
module Azure
module Action
class RestartVM
include VagrantPlugins::Azure::Util::MachineIdHelper

def initialize(app, env)
@app = app
@logger = Log4r::Logger.new('vagrant_azure::action::restart_vm')
end

def call(env)
env[:machine].id =~ /@/

env[:ui].info "Restarting #{$`} in #{$'}"
env[:azure_vm_service].restart_virtual_machine($`, $')

parsed = parse_machine_id(env[:machine].id)
env[:ui].info(I18n.t('vagrant_azure.restarting', parsed))
env[:azure_arm_service].compute.virtual_machines.restart(parsed[:group], parsed[:name])
env[:ui].info(I18n.t('vagrant_azure.restarted', parsed))
@app.call(env)
end
end
Expand Down
24 changes: 19 additions & 5 deletions lib/vagrant-azure/action/run_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
require 'azure_mgmt_resources'
require 'vagrant/util/template_renderer'
require 'vagrant-azure/util/timer'
require 'vagrant-azure/util/machine_id_helper'
require 'haikunator'

module VagrantPlugins
module Azure
module Action
class RunInstance
include Vagrant::Util::Retryable
include VagrantPlugins::Azure::Util::MachineIdHelper

def initialize(app, env)
@app = app
Expand All @@ -23,8 +25,10 @@ def call(env)
# Initialize metrics if they haven't been
env[:metrics] ||= {}

machine = env[:machine]

# Get the configs
config = env[:machine].provider_config
config = machine.provider_config
endpoint = config.endpoint
resource_group_name = config.resource_group_name
location = config.location
Expand Down Expand Up @@ -61,7 +65,6 @@ def call(env)
@logger.info("Time to fetch os image details: #{env[:metrics]['get_image_details']}")

deployment_params = {
sshKeyData: File.read(File.expand_path('~/.ssh/id_rsa.pub')),
dnsLabelPrefix: Haikunator.haikunate(100),
vmSize: vm_size,
vmName: vm_name,
Expand All @@ -73,11 +76,22 @@ def call(env)
virtualNetworkName: virtual_network_name
}

if get_image_os(image_details) != 'Windows'
private_key_paths = machine.config.ssh.private_key_path
if private_key_paths.empty?
raise I18n.t('vagrant_azure.private_key_not_specified')
end

paths_to_pub = private_key_paths.map{ |k| File.expand_path( k + '.pub') }.select{ |p| File.exists?(p) }
raise I18n.t('vagrant_azure.public_key_path_private_key', private_key_paths.join(', ')) if paths_to_pub.empty?
deployment_params.merge!(sshKeyData: File.read(paths_to_pub.first))
end

template_params = {
operating_system: get_image_os(image_details)
operating_system: get_image_os(image_details)
}

env[:ui].info(" -- Putting Resource Group: #{resource_group_name}")
env[:ui].info(" -- Create or Update of Resource Group: #{resource_group_name}")
env[:metrics]['put_resource_group'] = Util::Timer.time do
put_resource_group(azure, resource_group_name, location)
end
Expand All @@ -92,7 +106,7 @@ def call(env)
env[:ui].info('Finished deploying')

# Immediately save the ID since it is created at this point.
env[:machine].id = "#{resource_group_name}:#{vm_name}"
env[:machine].id = serialize_machine_id(resource_group_name, vm_name, location)

@logger.info("Time to deploy: #{env[:metrics]['deployment_time']}")
unless env[:interrupted]
Expand Down
39 changes: 30 additions & 9 deletions lib/vagrant-azure/action/start_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,50 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License in the project root for license information.
require 'log4r'
require 'vagrant-azure/util/machine_id_helper'
require 'vagrant-azure/util/vm_status_translator'
require 'vagrant/util/retryable'
require 'vagrant-azure/util/timer'
require 'vagrant-azure/util/vm_await'

# require 'vagrant/util/retryable'

# Bare bones basic implementation. This a work in progress in very early stages
module VagrantPlugins
module Azure
module Action
# This starts a stopped instance
class StartInstance
include VagrantPlugins::Azure::Util::MachineIdHelper
include VagrantPlugins::Azure::Util::VMStatusTranslator
include VagrantPlugins::Azure::Util::VMAwait

def initialize(app, env)
@app = app
@logger = Log4r::Logger.new('vagrant_azure:action::start_instance')
@logger = Log4r::Logger.new('vagrant_azure::action::start_instance')
end

def call(env)
env[:machine].id = "#{env[:machine].provider_config.vm_name}@#{env[:machine].provider_config.cloud_service_name}" unless env[:machine].id
env[:machine].id =~ /@/
env[:metrics] ||= {}

parsed = parse_machine_id(env[:machine].id)
azure = env[:azure_arm_service]
env[:ui].info(I18n.t('vagrant_azure.starting', parsed))
azure.compute.virtual_machines.start(parsed[:group], parsed[:name])

# Wait for the instance to be ready first
env[:metrics]['instance_ready_time'] = Util::Timer.time do

VagrantPlugins::Azure::CLOUD_SERVICE_SEMAPHORE.synchronize do
env[:ui].info "Attempting to start '#{$`}' in '#{$'}'"
env[:azure_vm_service].start_virtual_machine($`, $')
env[:ui].info(I18n.t('vagrant_azure.waiting_for_ready'))

task = await_true(env) do |vm|
running?(vm.properties.instance_view.statuses)
end

if task.value
env[:ui].info(I18n.t('vagrant_azure.started', parsed))
else
raise I18n.t('vagrant_azure.errors.failed_starting', parsed) unless env[:interrupted]
end
end

@app.call(env)
end
end
Expand Down
47 changes: 32 additions & 15 deletions lib/vagrant-azure/action/stop_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,53 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License in the project root for license information.
require 'log4r'
require 'vagrant-azure/util/machine_id_helper'
require 'vagrant-azure/util/vm_await'
require 'vagrant-azure/util/vm_status_translator'
require 'vagrant-azure/util/timer'

# Bare bones basic implementation. This a work in progress in very early stages
module VagrantPlugins
module Azure
module Action
class StopInstance
include VagrantPlugins::Azure::Util::MachineIdHelper
include VagrantPlugins::Azure::Util::VMAwait
include VagrantPlugins::Azure::Util::VMStatusTranslator

def initialize(app, env)
@app = app
@logger = Log4r::Logger.new('vagrant_azure::action::stop_instance')
end

def call(env)
if env[:machine].state.id == :StoppedDeallocated
env[:ui].info(
I18n.t('vagrant_azure.already_status', :status => 'stopped.')
)
env[:metrics] ||= {}

parsed = parse_machine_id(env[:machine].id)
if env[:machine].state.id == :stopped
env[:ui].info(I18n.t('vagrant_azure.already_status', :status => 'stopped.'))
else
env[:machine].id =~ /@/
VagrantPlugins::Azure::CLOUD_SERVICE_SEMAPHORE.synchronize do
env[:ui].info(
I18n.t(
'vagrant_azure.stopping',
:vm_name => $`,
:cloud_service_name => $'
)
)
env[:azure_vm_service].shutdown_virtual_machine($`, $')
env[:ui].info(I18n.t('vagrant_azure.stopping', parsed))
env[:azure_arm_service].compute.virtual_machines.power_off(parsed[:group], parsed[:name])

# Wait for the instance to be ready first
env[:metrics]['instance_stop_time'] = Util::Timer.time do

env[:ui].info(I18n.t('vagrant_azure.waiting_for_stop'))

task = await_true(env) do |vm|
stopped?(vm.properties.instance_view.statuses)
end

if task.value
env[:ui].info(I18n.t('vagrant_azure.stopped', parsed))
else
raise I18n.t('vagrant_azure.errors.failed_starting', parsed) unless env[:interrupted]
end
end

env[:ui].info(I18n.t('vagrant_azure.stopped', parsed))
end

@app.call(env)
end
end
Expand Down
16 changes: 14 additions & 2 deletions lib/vagrant-azure/action/terminate_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,32 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License in the project root for license information.
require 'log4r'
require 'vagrant-azure/util/machine_id_helper'

module VagrantPlugins
module Azure
module Action
class TerminateInstance
include VagrantPlugins::Azure::Util::MachineIdHelper

def initialize(app, env)
@app = app
@logger = Log4r::Logger.new('vagrant_azure::action::terminate_instance')
end

def call(env)
rg_name, vm_name = env[:machine].id.split(':')
parsed = parse_machine_id(env[:machine].id)

begin
env[:ui].info(I18n.t('vagrant_azure.terminating', parsed))
env[:azure_arm_service].resources.resource_groups.delete(parsed[:group]).value!.body
rescue MsRestAzure::AzureOperationError => ex
unless ex.response.status == 404
raise ex
end
end
env[:ui].info(I18n.t('vagrant_azure.terminated', parsed))

env[:azure_arm_service].compute.virtual_machines.delete(rg_name, vm_name).value!.body
env[:machine].id = nil

@app.call(env)
Expand Down
2 changes: 1 addition & 1 deletion lib/vagrant-azure/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Config < Vagrant.plugin('2', :config)
# @return [String]
attr_accessor :subscription_id

# (Optional) Name of the resource group to use. WARNING: the resource group will be removed upon destroy!!!
# (Optional) Name of the resource group to use.
#
# @return [String]
attr_accessor :resource_group_name
Expand Down
Loading

0 comments on commit 9411b2d

Please sign in to comment.