Module: Sitefull::Provider::Azure

Extended by:
ActiveSupport
Includes:
Azure::ARM::Resources::Models, Instance, Networking
Defined in:
lib/sitefull-cloud/provider/azure.rb,
lib/sitefull-cloud/provider/azure/instance.rb,
lib/sitefull-cloud/provider/azure/networking.rb

Defined Under Namespace

Modules: Instance, Networking

Constant Summary collapse

REQUIRED_OPTIONS =
%w(subscription_id).freeze
IMAGES =
{
  debian: { publisher: 'Credativ', offer: 'Debian' },
  ubuntu: { publisher: 'Canonical', offer: 'UbuntuServer' },
  centos: { publisher: 'OpenLogic', offer: 'CentOS' },
  rhel: { publisher: 'RedHat', offer: 'RHEL' },
  suse: { publisher: 'Suse', offer: 'OpenSUSE' },
  windows_server_2008: { publisher: 'MicrosoftWindowsServer', offer: 'WindowsServer', sku_filter: '2008' },
  windows_server_2012: { publisher: 'MicrosoftWindowsServer', offer: 'WindowsServer', sku_filter: '2012' },
  windows_server_2016: { publisher: 'MicrosoftWindowsServer', offer: 'WindowsServer', sku_filter: '2016' }
}.freeze
NETWORK_CIDR_BLOCK =
'172.16.0.0/16'.freeze
SUBNET_CIDR_BLOCK =
'172.16.1.0/24'.freeze
NETWORK_NAME =
'sitefull'.freeze
SUBNET_NAME =
'sitefull'.freeze
RESOURCE_GROUP =
'sitefull'.freeze
SECURITY_GROUP =
'sitefull'.freeze
PUBLIC_IP_NAME =
'sitefull'.freeze
WAIT =
2.freeze
SUCCESS_PROVISIONING_STATE =
'Succeeded'.freeze

Instance Method Summary collapse

Instance Method Details

#connectionObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sitefull-cloud/provider/azure.rb', line 38

def connection
  return @connection unless @connection.nil?

  connections = {
    arm: ::Azure::ARM::Resources::ResourceManagementClient.new(credentials),
    compute: ::Azure::ARM::Compute::ComputeManagementClient.new(credentials),
    network: ::Azure::ARM::Network::NetworkManagementClient.new(credentials),
    storage: ::Azure::ARM::Storage::StorageManagementClient.new(credentials)
  }
  connections.each { |_, v| v.subscription_id = options[:subscription_id] }
  @connection = OpenStruct.new(connections)
end

#create_firewall_rulesObject



74
75
76
77
78
# File 'lib/sitefull-cloud/provider/azure.rb', line 74

def create_firewall_rules
  inbound_firewall_rule 'ssh', '22', 100
  inbound_firewall_rule 'http', '80', 101
  inbound_firewall_rule 'https', '443', 102
end

#create_instance(name, machine_type, image, network_id, key) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sitefull-cloud/provider/azure.rb', line 84

def create_instance(name, machine_type, image, network_id, key)
  subnet = connection.network.subnets.get(resource_group_name, NETWORK_NAME, network_id)
  security_group = connection.network.network_security_groups.get(resource_group_name, SECURITY_GROUP)
  public_ip = public_ip_setup(name).value!.body
  network_interface = network_interface_setup(subnet, security_group, public_ip, name).value!.body

  storage_setup(name)
  sleep WAIT unless (name).properties.provisioning_state == SUCCESS_PROVISIONING_STATE
  storage = (name)

  instance_data = {machine_type: machine_type, image: image, name: name, key: key}
  instance_id = instance_setup(storage, network_interface, instance_data).value!.body.name
  sleep WAIT unless instance(instance_id).properties.provisioning_state == SUCCESS_PROVISIONING_STATE
  instance_id
end

#create_key(_) ⇒ Object



80
81
82
# File 'lib/sitefull-cloud/provider/azure.rb', line 80

def create_key(_)
  OpenStruct.new(key_data)
end

#create_networkObject



67
68
69
70
71
72
# File 'lib/sitefull-cloud/provider/azure.rb', line 67

def create_network
  resource_group = resource_group_setup
  security_group = security_group_setup.value!.body
  network = network_setup(resource_group, security_group).value!.body
  network.properties.subnets.last.name
end

#images(os) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/sitefull-cloud/provider/azure.rb', line 59

def images(os)
  @images unless @images.nil?

  search = IMAGES[os.to_sym]
  image_skus = connection.compute.virtual_machine_images.list_skus(options[:region], search[:publisher], search[:offer])
  @images = image_skus.map { |sku| OpenStruct.new(id: "#{search[:publisher]}:#{search[:offer]}:#{sku.name}", name: "#{search[:offer]} #{sku.name}") }
end

#instance_data(instance_id) ⇒ Object



100
101
102
# File 'lib/sitefull-cloud/provider/azure.rb', line 100

def instance_data(instance_id)
  OpenStruct.new(id: instance_id, public_ip: public_ip(instance_id))
end

#machine_types(region) ⇒ Object



55
56
57
# File 'lib/sitefull-cloud/provider/azure.rb', line 55

def machine_types(region)
  @machine_types ||= connection.compute.virtual_machine_sizes.list(region).value.map { |mt| OpenStruct.new(id: mt.name, name: mt.name) }
end

#regionsObject



51
52
53
# File 'lib/sitefull-cloud/provider/azure.rb', line 51

def regions
  @regions ||= connection.arm.providers.get('Microsoft.Compute').resource_types.find { |rt| rt.resource_type == 'virtualMachines' }.locations.map { |l| OpenStruct.new(id: l.downcase.gsub(/\s/, ''), name: l) }
end

#valid?Boolean

Returns:

  • (Boolean)


104
105
106
107
108
# File 'lib/sitefull-cloud/provider/azure.rb', line 104

def valid?
  !options[:subscription_id].empty? && !connection.empty? && !regions.empty?
rescue MsRestAzure::AzureOperationError => e
  raise StandardError.new JSON.parse(e.response.body)['error']['message']
end